package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.BaseBuild;
import com.usky.fire.domain.BaseDevice;
import com.usky.fire.domain.BaseDeviceStatus;
import com.usky.fire.mapper.BaseBuildMapper;
import com.usky.fire.mapper.BaseDeviceMapper;
import com.usky.fire.mapper.BaseDeviceStatusMapper;
import com.usky.fire.service.BaseDeviceService;
import com.usky.fire.service.BaseDeviceStatusService;
import com.usky.fire.service.vo.BaseDeviceTypeVO;
import com.usky.fire.service.vo.BaseDeviceVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-15
 */
@Service
public class BaseDeviceServiceImpl extends AbstractCrudService<BaseDeviceMapper, BaseDevice> implements BaseDeviceService {
    @Autowired
    private BaseDeviceStatusMapper baseDeviceStatusMapper;

    @Autowired
    private BaseBuildMapper baseBuildMapper;

    @Override
    public List<BaseDevice> deviceData(String deviceCode) {
        LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseDevice::getDeviceCode, BaseDevice::getDeviceCode, BaseDevice::getInstallAddress,
                BaseDevice::getDeviceType, BaseDevice::getDeviceName, BaseDevice::getCompanyId)
                .eq(BaseDevice::getEnable, 1)
                .eq(BaseDevice::getDeviceCode, deviceCode);
        List<BaseDevice> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public Integer deviceCount(String companyId, Integer deviceType) {
        LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseDevice::getEnable, 1)
                .eq(BaseDevice::getCompanyId, companyId)
                .eq(deviceType != 0, BaseDevice::getDeviceType, deviceType);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public BaseDeviceVO deviceTypeList(){

        BaseDeviceVO baseDeviceVO = new BaseDeviceVO();
        LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseDevice::getEnable,1)
            .in(BaseDevice::getDeviceType,"45","332","358","359","376","395","401","420","425","426","427","428","429","433","434");
        Integer totalCount = this.count(queryWrapper);
        baseDeviceVO.setDeviceCount(totalCount);

        QueryWrapper<BaseDevice> queryWrapper1 = Wrappers.query();
        queryWrapper1.select("device_type as deviceTypeCode","device_name as deviceTypeName","count(*) as deviceCount")
                .eq("enable",1)
                .in("device_type","45","332","358","359","376","395","401","420","425","426","427","428","429","433","434")
                .groupBy("device_type");
        List<Map<String,Object>> map = this.listMaps(queryWrapper1);
        if(map.size() > 0){
            List<BaseDeviceTypeVO> list1 = new ArrayList<>();
            QueryWrapper<BaseDeviceStatus> queryWrapper2 = Wrappers.query();
            queryWrapper2.select("company_id as deviceTypeCode","count(*) as lineCount")
                    .ne("device_status",0)
                    .groupBy("company_id");
            List<Map<String,Object>> map1 = baseDeviceStatusMapper.selectMaps(queryWrapper2);

            for (int i = 0; i < map.size(); i++) {
                BaseDeviceTypeVO baseDeviceTypeVO = new BaseDeviceTypeVO();
                baseDeviceTypeVO.setDeviceTypeCode(Integer.parseInt(map.get(i).get("deviceTypeCode").toString()));
                baseDeviceTypeVO.setDeviceTypeName(map.get(i).get("deviceTypeName").toString());
                baseDeviceTypeVO.setDeviceCount(Integer.parseInt(map.get(i).get("deviceCount").toString()));

                for (int j = 0; j < map1.size(); j++) {
                    int typeCode = Integer.parseInt(map.get(i).get("deviceTypeCode").toString());
                    int typeCode1 = Integer.parseInt(map1.get(j).get("deviceTypeCode").toString());
                    if(typeCode1 == typeCode){
                        int lineCount = Integer.parseInt(map1.get(j).get("lineCount").toString());
                        int deviceCount = Integer.parseInt(map.get(i).get("deviceCount").toString());
                        baseDeviceTypeVO.setLineCount(lineCount);
                        double rate = (double)lineCount/deviceCount*100;
                        baseDeviceTypeVO.setLineRate(rate);
                        list1.add(baseDeviceTypeVO);
                    }
                }
            }
            baseDeviceVO.setTypeList(list1);
        }


        return baseDeviceVO;
    }

    @Override
    public List<BaseBuild> deviceTypeScatter(Integer deviceTypeCode){
        List<BaseBuild> list = new ArrayList<>();

        LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseDevice::getCompanyId)
                .eq(BaseDevice::getEnable,1)
            .eq(BaseDevice::getDeviceType,deviceTypeCode);
        List<BaseDevice> deviceList = this.list(queryWrapper);
        if(CollectionUtils.isNotEmpty(deviceList)){
            List<String> buildNumList = new ArrayList<>();
            for (int i = 0; i < deviceList.size(); i++) {
                if(buildNumList.contains(deviceList.get(i).getCompanyId())){

                }else {
                    buildNumList.add(deviceList.get(i).getCompanyId());
                }

            }
            if(buildNumList.size() > 0){
                LambdaQueryWrapper<BaseBuild> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.in(BaseBuild::getBuildNum,buildNumList)
                        .eq(BaseBuild::getDeleteFlag,0);
                list = baseBuildMapper.selectList(queryWrapper1);
            }
        }

        return list;
    }

}
