package com.huimv.environ.eco.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.environ.eco.entity.EcoDevice;
import com.huimv.environ.eco.mapper.EcoDeviceMapper;
import com.huimv.environ.eco.service.EcoDeviceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.PrimitiveIterator;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zn
 * @since 2022-11-09
 */
@Service
public class EcoDeviceServiceImpl extends ServiceImpl<EcoDeviceMapper, EcoDevice> implements EcoDeviceService {

    @Resource
    private EcoDeviceMapper ecoDeviceMapper;
    @Autowired
    private static final String globalFarmCode = "331023001";
    @Override
    public Result add(EcoDevice ecoDevice) {

        QueryWrapper<EcoDevice> wrapper = new QueryWrapper<>();
        String deviceCode = ecoDevice.getDeviceCode();
        wrapper.eq(StringUtils.isNotBlank(deviceCode),"device_code",deviceCode);
        if (ecoDeviceMapper.selectOne(wrapper) == null) {

            Integer code = DeviceType.SUPERVISE.code;
            Integer code1 = DeviceType.DRYING.code;
            Integer farmId = ecoDevice.getFarmId();
            if (farmId == null) {
                farmId = Integer.parseInt(globalFarmCode);
            }
            EcoDevice device = new EcoDevice();

            device.setFarmId(ecoDevice.getFarmId());
            device.setDeviceCode(ecoDevice.getDeviceCode());
            device.setUnitId(ecoDevice.getUnitId());
            device.setPigpenId(ecoDevice.getPigpenId());
            device.setLocation(ecoDevice.getLocation());
            if (ecoDevice.getDeviceType().equals(code)) {
                device.setDeviceType(code);
            }
            if (ecoDevice.getDeviceType().equals(code1)) {
                device.setDeviceType(code1);
            }
            ecoDeviceMapper.insert(device);
            return new Result(ResultCode.SUCCESS, "添加成功");
        } else {
            return new Result(ResultCode.FAIL, "添加失败,设备已存在");
        }
    }

    @Override
    public Result edit(EcoDevice ecoDevice) {
        QueryWrapper<EcoDevice> wrapper = new QueryWrapper<>();
        String deviceCode = ecoDevice.getDeviceCode();
        wrapper.eq(StringUtils.isNotBlank(deviceCode),"device_code",deviceCode);
        EcoDevice ecoDevice1 = ecoDeviceMapper.selectOne(wrapper);
        if (ecoDevice1==null) {
            EcoDevice ecoDevice2 = new EcoDevice();
            ecoDevice2.setFarmId(ecoDevice.getFarmId());
            ecoDevice2.setDeviceCode(ecoDevice.getDeviceCode());
            ecoDevice2.setDeviceType(ecoDevice.getDeviceType());
            ecoDevice2.setUnitId(ecoDevice.getUnitId());
            ecoDevice2.setLocation(ecoDevice.getLocation());
            ecoDevice2.setPigpenId(ecoDevice.getPigpenId());
            baseMapper.insert(ecoDevice2);
        } else {
            ecoDevice1.setFarmId(ecoDevice.getFarmId());
            ecoDevice1.setDeviceCode(ecoDevice.getDeviceCode());
            ecoDevice1.setDeviceType(ecoDevice.getDeviceType());
            ecoDevice1.setUnitId(ecoDevice.getUnitId());
            ecoDevice1.setLocation(ecoDevice.getLocation());
            ecoDevice1.setPigpenId(ecoDevice.getPigpenId());
            baseMapper.updateById(ecoDevice1);
        }
        return new Result(ResultCode.SUCCESS,"编辑成功");
    }

    @Override
    public Result remove(Map<String, String> paramsMap) {
        String ids = paramsMap.get("ids");
        String[] split = ids.split(",");
        List<String> list = Arrays.asList(split);

        return new Result(ResultCode.SUCCESS,ecoDeviceMapper.deleteBatchIds(list));
    }

    @Override
    public Result list(Map<String,String> paramsMap) {
        //pageNo
        String pageNo = paramsMap.get("pageNo");
        if (StringUtils.isBlank(pageNo)) {
            pageNo = "1";
        }
        //pageSize
        String pageSize = paramsMap.get("pageSize");
        if (StringUtils.isBlank(pageSize)) {
            pageSize = "10";
        }
        //farmId
        String farmId = paramsMap.get("farmId");
        if (farmId == null) {
            farmId = globalFarmCode;
        }

        String deviceCode = paramsMap.get("deviceCode");
        String deviceType = paramsMap.get("deviceType");
        QueryWrapper<EcoDevice> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(farmId),"farm_id", farmId)
                    .like("device_code",deviceCode)
                    .like("device_type",deviceType);
        Page<EcoDevice> page = new Page<>(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        return new Result(ResultCode.SUCCESS,ecoDeviceMapper.selectPage(page,queryWrapper));
    }

    @Override
    public List<Map> getDeviceList2() {
        return ecoDeviceMapper.selectList4();
    }

    @Override
    public List<Map> getDryDeviceByDeviceType() {
        int dryDeivceType = 2;
        return ecoDeviceMapper.getDryDevice(dryDeivceType);
    }


    //内部关键字枚举
    public enum DeviceType{
        SUPERVISE( 1,"监测设备"),
        DRYING(2,"烘干设备")
        ;

        private Integer code;
        private String type;

        DeviceType(Integer code,String type) {
            this.code = code;
            this.type = type;
        }

        public Integer getCode() {
            return code;
        }

        private String getType() {
            return type;
        }

        @Override
        public String toString() {
            return "DeviceType{" +
                    "code=" + code +
                    ", type='" + type + '\'' +
                    '}';
        }
    }

}
