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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zcxk.charge.api.vo.DeviceVo;
import com.zcxk.charge.core.dao.DeviceMapper;
import com.zcxk.charge.api.dto.DeviceDto;
import com.zcxk.charge.core.entity.DeviceEntity;
import com.zcxk.charge.web.service.DeviceService;
import com.zcxk.core.oauth2.pojo.LoginUser;
import com.zcxk.core.oauth2.util.UserUtil;
import com.zcxk.core.utils.util.RedisUtil;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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

    @Resource
    private DeviceMapper deviceMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Override
    public DeviceVo 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) {
        DeviceVo 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<DeviceVo> deviceDtos = deviceMapper.selectByIds(ids);
        for(DeviceVo 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) {
        DeviceVo 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<DeviceVo> 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<DeviceVo> 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<DeviceVo> deviceDtos = deviceMapper.selectList(deviceDto);
        return deviceDtos;
    }

    @Override
    public List<DeviceVo> 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<DeviceVo> deviceDtos = deviceMapper.selectListForReport(deviceDto);
        return deviceDtos;
    }

    @Override
    public List<DeviceVo> 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<DeviceVo> deviceDtos = deviceMapper.selectList(deviceDto);
        Map<Long, DeviceVo> map = new HashMap<>();
        //数据清洗
        if (deviceDto.getSceneId() != null) {
            for (DeviceVo 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<DeviceVo> 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<DeviceVo> 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);
    }
}
