package com.zcxk.charge.web.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.charge.api.dto.*;
import com.zcxk.charge.api.vo.DeviceParmInfoVo;
import com.zcxk.charge.api.vo.ParmTypeCountVo;
import com.zcxk.charge.api.vo.ReportAttributeVo;
import com.zcxk.charge.core.dao.DeviceParmMapper;
import com.zcxk.charge.core.entity.DeviceParmEntity;
import com.zcxk.charge.core.entity.DeviceSceneEntity;
import com.zcxk.charge.core.entity.SceneEntity;
import com.zcxk.charge.web.service.DeviceParmService;
import com.zcxk.charge.web.service.DeviceSceneService;
import com.zcxk.charge.web.service.SceneService;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @description 设备参数
 * @author 青雲   wangli
 * @data 2020-11-17 11:02
 */
@Service
public class DeviceParmServiceImpl  implements DeviceParmService {

    @Resource
    private DeviceParmMapper deviceParmMapper;

    @Autowired
    private SceneService sceneService;

    @Autowired
    private DeviceSceneService deviceSceneService;

    @Override
    public DeviceParmEntity selectById(Integer id) {
        return deviceParmMapper.selectById(id);
    }

    @Override
    public List<DeviceParmEntity> selectByDeviceId(Integer sceneId, Integer id) {
        List<DeviceParmEntity> deviceParmEntities = deviceParmMapper.selectByDeviceId(sceneId, id);
        for (DeviceParmEntity item : deviceParmEntities) {
            item.setDeviceId(id);
        }
        return deviceParmEntities;
    }

    @Override
    public List<DeviceParmEntity> selectByScendId(Integer sceneId) {
        List<DeviceParmEntity> deviceParmEntities = deviceParmMapper.selectBySceneId(sceneId);
        return deviceParmEntities;
    }

    @Override
    public List<DeviceParmEntity> selectByDeviceIdForGis(Integer sceneId, Integer id) {
        List<DeviceParmEntity> deviceParmEntities = deviceParmMapper.selectByDeviceIdForGis(sceneId, id);
        for (DeviceParmEntity item : deviceParmMapper.selectByDeviceIdForGis(sceneId, id)) {
            item.setDeviceId(id);
        }
        return deviceParmEntities;
    }


    @Override
    public Integer insert(DeviceParmEntity deviceParmEntity) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceParmEntity.setDateUpdate(new Date());
        deviceParmEntity.setUpdateBy(loginUser.getUsername());
        deviceParmEntity.setDateCreate(new Date());
        deviceParmEntity.setCreateBy(loginUser.getUsername());
        deviceParmEntity.setStatus(1);
        deviceParmEntity.setTenantId(loginUser.getTenantId());
        return deviceParmMapper.insert(deviceParmEntity);
    }

    @Override
    public Integer batchInsert(List<DeviceParmEntity> deviceParmEntities) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        for (DeviceParmEntity deviceParmEntity : deviceParmEntities) {
            deviceParmEntity.setDateUpdate(new Date());
            deviceParmEntity.setUpdateBy(loginUser.getUsername());
            deviceParmEntity.setDateCreate(new Date());
            deviceParmEntity.setCreateBy(loginUser.getUsername());
            deviceParmEntity.setStatus(1);
            deviceParmEntity.setTenantId(loginUser.getTenantId());
        }
        return deviceParmMapper.batchInsert(deviceParmEntities);
    }

    @Override
    public Integer delete(Integer id) {
        return deviceParmMapper.deleteById(id);
    }

    @Override
    public Integer update(DeviceParmEntity deviceParmEntity) {
        return deviceParmMapper.update(deviceParmEntity);
    }

    @Override
    public void batchUpdate(List<DeviceParmEntity> deviceParmEntities) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        for (DeviceParmEntity deviceParmEntity : deviceParmEntities) {
            deviceParmEntity.setDateUpdate(new Date());
            deviceParmEntity.setUpdateBy(loginUser.getUsername());
            deviceParmMapper.update(deviceParmEntity);
        }
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<DeviceParmEntity> entityList) {
        for (DeviceParmEntity item : entityList) {
            if (item.getId() == null) {
                this.insert(item);
            } else {
                this.update(item);
            }
        }
        return true;
    }

    @Override
    public List<DeviceParmEntity> selectList(DeviceParmEntity deviceParmEntity) {
        return deviceParmMapper.selectList(deviceParmEntity);
    }

    @Override
    public IPage<DeviceParmEntity> selectPage(IPage<DeviceParmEntity> page, DeviceParmEntity deviceParmEntity) {
        return deviceParmMapper.selectPage(page, deviceParmEntity);
    }

    @Override
    public List<ReportAttributeVo> findAttributeNameList(ReportDto reportDto) {
        return deviceParmMapper.findAttributeNameList(reportDto);
    }

    @Override
    public List<ReportAttributeVo> findHistoryDataAttributeNameList(ReportDto reportDto) {
        return deviceParmMapper.findHistoryDataAttributeNameList(reportDto);
    }

    @Override
    public List<DeviceParmInfoVo> selectBySceneAndDeviceId(Integer parentSceneId, Integer sceneId, Integer deviceId) {
        DeviceParmInfoDto deviceParmInfo = new DeviceParmInfoDto();
        deviceParmInfo.setParentSceneId(parentSceneId.longValue());
        deviceParmInfo.setSceneId(sceneId.longValue());
        deviceParmInfo.setDeviceId(deviceId);
        List<Long> ids = sceneService.findByParentIdsLike(parentSceneId.longValue());
        if (ids.contains(sceneId.longValue())) {
            ids.remove(sceneId.longValue());
        }
        deviceParmInfo.setSceneIds(ids);
        List<DeviceParmInfoVo> deviceParmInfos = deviceParmMapper.findBySceneDeviceId(deviceParmInfo);
        for (DeviceParmInfoVo item : deviceParmInfos) {
            item.setSceneId(sceneId.longValue());
            item.setParentSceneId(parentSceneId.longValue());
        }
        return deviceParmInfos;
    }

    @Override
    public void batchSaveEdit(Long sceneId, Long deviceId, List<DeviceParmInfoDto> deviceParmInfos) {
        //判断此设备是否已经被绑定
        LoginUser loginUser = UserUtil.getCurrentUser();
        DeviceSceneEntity deviceSceneEntity = new DeviceSceneEntity();
        deviceSceneEntity.setSceneId(sceneId);
        deviceSceneEntity.setDeviceId(deviceId);
        SceneEntity sceneEntity = sceneService.findParentSceneById(sceneId);
        List<DeviceSceneEntity> deviceScenes = deviceSceneService.findList(deviceSceneEntity);
        if (deviceParmInfos.size() == 0 && deviceScenes.size() > 0) {
            //如果所有的参数未被选择，则取消掉设备与场景的关联关系
            deviceSceneService.removeById(deviceScenes.get(0).getId());
            //去掉参数的对应关系
            List<DeviceParmInfoVo> deviceParmInfoList = deviceParmMapper.selectBindByDeviceId(sceneId, deviceId);
            List<Long> ids = new ArrayList<>();
            for (DeviceParmInfoVo info : deviceParmInfoList) {
                deviceParmMapper.deleteById(info.getId().intValue());
            }

        } else if (deviceParmInfos.size() > 0) {
            //没有的时候创建关联关系
            if (deviceScenes.size() == 0) {
                deviceSceneEntity.setParentSceneId(sceneEntity.getParentSceneId());
                deviceSceneEntity.setTenantId(loginUser.getTenantId());
                deviceSceneEntity.setCreateBy(loginUser.getName());
                deviceSceneEntity.setDateCreate(new Date());
                deviceSceneEntity.setUpdateBy(loginUser.getName());
                deviceSceneEntity.setDateUpdate(new Date());
                deviceSceneEntity.setStatus(1);
                deviceSceneService.save(deviceSceneEntity);
            }
            //查询已经绑定的所有参数
            List<DeviceParmInfoVo> deviceParmInfoList = deviceParmMapper.selectBindByDeviceId(sceneId, deviceId);
            Map<Long,Long> idMaps= new HashMap<>();
            for (DeviceParmInfoVo info : deviceParmInfoList) {
                idMaps.put(Long.valueOf(info.getAttributeId()),info.getId());
            }
            for (DeviceParmInfoDto item : deviceParmInfos) {
                if (idMaps.containsKey(item.getAttributeId().longValue())) {
                    idMaps.remove(item.getAttributeId().longValue());
                } else if (!idMaps.containsKey(item.getAttributeId().longValue())) {
                    //新增
                    DeviceParmEntity deviceParmEntity = new DeviceParmEntity();
                    deviceParmEntity.setSceneId(sceneId);
                    deviceParmEntity.setParentSceneId(sceneEntity.getParentSceneId());
                    deviceParmEntity.setDeviceId(deviceId.intValue());
                    deviceParmEntity.setStatus(1);
                    deviceParmEntity.setAttributeId(item.getAttributeId());
                    deviceParmEntity.setAttributeName(item.getAttributeName());
                    deviceParmEntity.setCreateBy(loginUser.getName());
                    deviceParmEntity.setDateCreate(new Date());
                    deviceParmEntity.setUpdateBy(loginUser.getName());
                    deviceParmEntity.setDateUpdate(new Date());
                    this.insert(deviceParmEntity);
                }
            }
            //将ids 移除
            for(Long id : idMaps.values()) {
                deviceParmMapper.deleteById(id.intValue());
            }
        }
    }

    @Override
    public List<DeviceParmEntity> selectDeviceBySceneIdAndType(DeviceParmEntity deviceParmEntity) {
        return deviceParmMapper.selectDeviceBySceneIdAndType(deviceParmEntity);
    }

    @Override
    public List<ParmTypeCountVo> findAlarmCount(DeviceParmEntity deviceParmEntity) {
        return deviceParmMapper.findAlarmCount(deviceParmEntity);
    }
    @Override
    public List<ParmTypeCountVo> findAlarmTotalCount(DeviceParmEntity deviceParmEntity){
        return deviceParmMapper.findAlarmTotalCount(deviceParmEntity);
    }

    @Override
    public List<ParmTypeCountVo> findAlarmCountTotalCount(DeviceParmEntity deviceParmEntity){
        return deviceParmMapper.findAlarmCountTotalCount(deviceParmEntity);
    }

    @Override
    public void batchUpdateStatisticalDimension(List<ReportStatisticalDimensionDto> statisticalDimensionDtos) {
        for (ReportStatisticalDimensionDto deviceParmEntity : statisticalDimensionDtos) {
            deviceParmMapper.updateStatisticalDimension(deviceParmEntity);
        }
    }


}
