package com.zcxk.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.common.StringUtils;
import com.zcxk.dao.DeviceMapper;
import com.zcxk.dto.DeviceDto;
import com.zcxk.entity.DeviceEntity;
import com.zcxk.model.LoginUser;
import com.zcxk.service.DeviceService;
import com.zcxk.util.RedisUtil;
import com.zcxk.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @description
 * @auto wangli
 * @data 2020-11-16 11:26
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Resource
    private DeviceMapper deviceMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public DeviceDto selectById(Long id) {
        return deviceMapper.selectById(id);
    }

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

    @Override
    public Integer delete(Long id) {
        DeviceDto deviceDto = deviceMapper.selectById(id);
        if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
            redisUtil.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
        }
        return deviceMapper.deleteById(id);
    }

    @Override
    public Integer batchDelete(List<Long> ids) {
        List<DeviceDto> deviceDtos = deviceMapper.selectByIds(ids);
        for(DeviceDto deviceDto : deviceDtos){
            if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
                redisUtil.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
            }
        }
        return deviceMapper.batchDelete(ids);
    }
    @Override
    public Integer update(DeviceEntity deviceEntity) {
        DeviceDto deviceDto = deviceMapper.selectById(deviceEntity.getId());
        if(deviceDto != null && StringUtils.isNotBlank(deviceDto.getDeviceCode())){
            redisUtil.del(("sms_water_"+deviceDto.getDeviceCode()).getBytes());
        }
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceEntity.setUpdateBy(loginUser.getUsername());
        deviceEntity.setDateUpdate(new Date());
        return deviceMapper.update(deviceEntity);
    }

    @Override
    public IPage<DeviceDto> selectPage(IPage<DeviceDto> page,  DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());

        return deviceMapper.selectPage(page,deviceDto);
    }

    @Override
    public List<DeviceDto> selectList(DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());
        List<DeviceDto> deviceDtos = deviceMapper.selectList(deviceDto);
        return deviceDtos;
    }

    @Override
    public List<DeviceDto> selectListForReport(DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());
        deviceDto.setUserId(loginUser.getId());
        List<DeviceDto> deviceDtos = deviceMapper.selectListForReport(deviceDto);
        return deviceDtos;
    }

    @Override
    public List<DeviceDto> selectListByType(DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());
        List<DeviceDto> deviceDtos = deviceMapper.selectList(deviceDto);
        Map<Long, DeviceDto> map = new HashMap<>();
        //数据清洗
        if (deviceDto.getSceneId() != null) {
            for (DeviceDto item : deviceDtos) {
                if (!map.containsKey(item.getId())) {
                    map.put(item.getId(), item);
                } else {
                    map.get(item.getId()).setSceneId(deviceDto.getSceneId());
                }
            }
        }
        return map.values().stream().collect(Collectors.toList());
    }

    @Override
    public List<DeviceDto> selectListByScene(DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());
        return deviceMapper.selectListByScene(deviceDto);
    }


    @Override
    public List<DeviceDto> selectListBySceneId(DeviceDto deviceDto) {
        LoginUser loginUser = UserUtil.getCurrentUser();
        deviceDto.setTenantId(loginUser.getTenantId());
        deviceDto.setProgramItems(loginUser.getProgramItemList());
        deviceDto.setUserType(loginUser.getType());
        //1是公司，2是公司及以下，3部门，4部门及以下，5自定义
        deviceDto.setPermissonType(loginUser.getPermissonType());
        return deviceMapper.selectListBySceneId(deviceDto);
    }
}
