package com.xtd.project.hkmanager.service.impl;

import com.xtd.common.exception.CustomException;
import com.xtd.common.utils.IdUtils;
import com.xtd.project.hkmanager.entity.Device;
import com.xtd.project.hkmanager.entity.DeviceForm;
import com.xtd.project.hkmanager.mapper.DeviceMapper;
import com.xtd.project.hkmanager.service.DeviceManagerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author lym
 * @date 2020/4/8 17:35
 */
@Service
public class DeviceManagerServiceImpl implements DeviceManagerService {

    @Resource
    DeviceMapper deviceMapper;

    @Override
    public int delByPrimaryKey(String s) {
        return deviceMapper.delete(s);
    }

    @Override
    public int save(Device device) {
        return 0;
    }

    @Override
    public int saveSelective(Device device) {
        int isExist = deviceMapper.isExistDevice(device);
        if (isExist > 0){
            throw new CustomException("监控点名称已存在",1);
        }
        device.setGuid(IdUtils.simpleUUID());
        return deviceMapper.insertSelective(device);
    }

    @Override
    public Device queryByPrimaryKey(String s) {
        return deviceMapper.selectByPrimaryKey(s);
    }

    @Override
    public int modifyByPrimaryKeySelective(Device device) {
        return deviceMapper.updateByPrimaryKeySelective(device);
    }

    @Override
    public int modifyByPrimaryKeyWithBLOBs(Device device) {
        return 0;
    }

    @Override
    public int modifyByPrimaryKey(Device device) {
        return 0;
    }

    @Override
    public int batchSave(Map<String, Device> map) {
        List<String> ids = deviceMapper.findIds();
        for (String id:ids){
            map.remove(id);
        }

        int i = 0;
        if (map.size() > 0){
            Collection<Device> values = map.values();
            List<Device> list = new ArrayList<>(values);
            i = deviceMapper.batchSave(list);
        }

        return i;
    }

    @Override
    public List<Device> selectList(DeviceForm device) {
        return deviceMapper.selectList(device);
    }
}
