package com.yuchen.saas.park.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuchen.saas.park.api.dto.irrigate.BindDTO;
import com.yuchen.saas.park.api.dto.irrigate.ControlIrrigateDTO;
import com.yuchen.saas.park.api.entity.device.PmgBatSmartEquipment;
import com.yuchen.saas.park.api.entity.park.PmgBatIrrigateBind;
import com.yuchen.saas.park.api.vo.park.irrigate.AllDeviceInfoVO;
import com.yuchen.saas.park.service.mapper.PmgBatIrrigateBindMapper;
import com.yuchen.saas.park.service.service.PmgBatSmartEquipmentService;
import com.yuchen.saas.park.service.service.PmgBatIrrigateBindService;
import org.nest.springwrap.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author 颜艺波
 * @date 2022/6/30 15:35
 * @Description
 */
@Service
public class PmgBatIrrigateBindServiceImpl extends ServiceImpl<PmgBatIrrigateBindMapper, PmgBatIrrigateBind>
        implements PmgBatIrrigateBindService {

    @Autowired
    PmgBatSmartEquipmentService equipmentService;

    @Override
    public R bind(BindDTO bindDTO) {

        if (!deviceIsExits(bindDTO.getParkId(), bindDTO.getSoilProductKey(), bindDTO.getSoilDeviceName())) {
            return R.error("土壤温湿设备不存在");
        }
        if (!deviceIsExits(bindDTO.getParkId(), bindDTO.getImpulseProductKey(), bindDTO.getImpulseDeviceName())) {
            return R.error("脉冲设备不存在");
        }
        deviceIsExits(bindDTO.getParkId(), bindDTO.getImpulseProductKey(), bindDTO.getImpulseDeviceName());
        if (this.count(Wrappers.<PmgBatIrrigateBind>lambdaQuery()
                .eq(PmgBatIrrigateBind::getParkId, bindDTO.getParkId())
                .eq(PmgBatIrrigateBind::getSoilProductKey, bindDTO.getSoilProductKey())
                .eq(PmgBatIrrigateBind::getSoilDeviceName, bindDTO.getSoilDeviceName())
                .eq(PmgBatIrrigateBind::getIsDeleted, 0)) > 0) {
            return R.error("该土壤温湿设备已和脉冲设备绑定，不能再进行绑定");
        }
        if (this.count(Wrappers.<PmgBatIrrigateBind>lambdaQuery()
                .eq(PmgBatIrrigateBind::getImpulseProductKey, bindDTO.getImpulseProductKey())
                .eq(PmgBatIrrigateBind::getImpulseDeviceName, bindDTO.getImpulseDeviceName())
                .eq(PmgBatIrrigateBind::getIsDeleted, 0)) > 0) {
            return R.error("该脉冲设备已和土壤温湿设备绑定，不能再进行绑定");
        }
        PmgBatIrrigateBind irrigateBind = new PmgBatIrrigateBind();
        irrigateBind.setParkId(bindDTO.getParkId());
        irrigateBind.setSoilProductKey(bindDTO.getSoilProductKey());
        irrigateBind.setSoilDeviceName(bindDTO.getSoilDeviceName());
        irrigateBind.setImpulseProductKey(bindDTO.getImpulseProductKey());
        irrigateBind.setImpulseDeviceName(bindDTO.getImpulseDeviceName());
        irrigateBind.setCreateTime(new Date());
        irrigateBind.setUpdateTime(new Date());
        irrigateBind.setIsDeleted(0);
        return R.status(this.save(irrigateBind));
    }

    @Override
    public R unBind(BindDTO bindDTO) {
        PmgBatIrrigateBind irrigateBind = this.getOne(Wrappers.<PmgBatIrrigateBind>lambdaQuery()
                .eq(PmgBatIrrigateBind::getParkId, bindDTO.getParkId())
                .eq(PmgBatIrrigateBind::getImpulseProductKey, bindDTO.getImpulseProductKey())
                .eq(PmgBatIrrigateBind::getImpulseDeviceName, bindDTO.getImpulseDeviceName())
                .eq(PmgBatIrrigateBind::getSoilProductKey, bindDTO.getSoilProductKey())
                .eq(PmgBatIrrigateBind::getSoilDeviceName, bindDTO.getSoilDeviceName())
                .eq(PmgBatIrrigateBind::getIsDeleted, 0));
        if (irrigateBind == null) {
            return R.error("绑定关系不存在，解除绑定失败");
        }
        // 解除绑定
        return R.status(this.removeById(irrigateBind.getId()));
    }

    @Override
    public List<AllDeviceInfoVO> allDeviceInfo(Long parkId) {
        List<AllDeviceInfoVO> allDeviceInfoVOList = new LinkedList<>();
        // 查询浇灌（脉冲、土壤温湿度）设备
        List<PmgBatSmartEquipment> list = equipmentService.list(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                .eq(PmgBatSmartEquipment::getParkId, parkId)
                .eq(PmgBatSmartEquipment::getCategoryCode, 8));
        for (PmgBatSmartEquipment e : list) {
            AllDeviceInfoVO allDeviceInfoVO = new AllDeviceInfoVO();
            allDeviceInfoVO.setProductKey(e.getProductKey());
            allDeviceInfoVO.setDeviceName(e.getDeviceName());
            allDeviceInfoVO.setDeviceAliasName(e.getName());
            allDeviceInfoVO.setIsOnline(e.getIsOnline());
            allDeviceInfoVO.setPlanePosition(e.getPlanePosition());
            // 查询该土壤温湿设备是否绑定脉冲设备
            List<PmgBatIrrigateBind> bind =
                    this.list(Wrappers.<PmgBatIrrigateBind>lambdaQuery()
                            .eq(PmgBatIrrigateBind::getSoilProductKey, e.getProductKey())
                            .eq(PmgBatIrrigateBind::getSoilDeviceName, e.getDeviceName())
                            .eq(PmgBatIrrigateBind::getIsDeleted, 0));
            if (!bind.isEmpty()) {
                allDeviceInfoVO.setImpulseDeviceName(bind.get(0).getImpulseDeviceName());
                allDeviceInfoVO.setImpulseProductKey(bind.get(0).getImpulseProductKey());
            }
            allDeviceInfoVOList.add(allDeviceInfoVO);
        }
        return allDeviceInfoVOList;
    }

    @Override
    public Boolean start(ControlIrrigateDTO controlIrrigateDTO) {
        //todo 开启浇灌业务操作
        return true;
    }

    @Override
    public Boolean stop(ControlIrrigateDTO controlIrrigateDTO) {
        //todo 关闭浇灌业务操作
        return true;
    }

    @Override
    public PmgBatIrrigateBind getByDeviceName(String productKey, String deviceName) {
        LambdaQueryWrapper<PmgBatIrrigateBind> queryWrapper = Wrappers.lambdaQuery();
        if (productKey.equals("clFhXa9kJ0GKeZqU")){
            queryWrapper.eq(PmgBatIrrigateBind::getSoilProductKey,productKey);
            queryWrapper.eq(PmgBatIrrigateBind::getSoilDeviceName,deviceName);
        }else{
            queryWrapper.eq(PmgBatIrrigateBind::getImpulseProductKey,productKey);
            queryWrapper.eq(PmgBatIrrigateBind::getImpulseDeviceName,deviceName);
        }
        return getOne(queryWrapper);
    }

    private Boolean deviceIsExits(Long parkId, String productKey, String deviceName) {
        if (equipmentService.count(Wrappers.<PmgBatSmartEquipment>lambdaQuery()
                .eq(PmgBatSmartEquipment::getParkId, parkId)
                .eq(PmgBatSmartEquipment::getProductKey, productKey)
                .eq(PmgBatSmartEquipment::getDeviceName, deviceName)) < 1) {
            return false;
        }
        return true;
    }
}
