package com.usky.issue.service.impl;

import com.alibaba.fastjson.JSONObject;
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.issue.domain.SpHj2017;
import com.usky.issue.domain.SpOwner;
import com.usky.issue.mapper.SpHj2017Mapper;
import com.usky.issue.service.SpHj2017Service;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.issue.service.SpOwnerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author fu
 * @since 2023-12-13
 */
@Service
public class SpHj2017ServiceImpl extends AbstractCrudService<SpHj2017Mapper, SpHj2017> implements SpHj2017Service {
    @Autowired
    private SpOwnerService spOwnerService;

    @Override
    public List<Map<String, Object>> statusCount(String requestBody) {
        JSONObject requestVO = JSONObject.parseObject(requestBody);
        String companyCode = requestVO.get("companyCode").toString();
        Integer currentPage = (Integer) requestVO.get("current");
        Integer pageSize = (Integer) requestVO.get("size");
        int startIndex = (currentPage - 1) * pageSize;
        QueryWrapper<SpHj2017> queryWrapper = Wrappers.query();
        queryWrapper.select("DATE_FORMAT(time, '%Y-%m-%d %H:%i:%s') AS alarm_time", "data2 AS alarm_type", "data4 AS alarm_point", "device_code")
                .inSql("device_code", "SELECT owner_code FROM sp_owner WHERE company = '" + companyCode + "' AND dwtype IN (1,3)");
        queryWrapper.last("LIMIT " + pageSize + " OFFSET " + startIndex)
                .orderByDesc("id");
        List<Map<String, Object>> deviceStatusList = this.listMaps(queryWrapper);
        QueryWrapper<SpOwner> queryWrapper1 = Wrappers.query();
        queryWrapper1.select("owner_code","unitinfo")
                .eq("company",companyCode)
                .in("dwtype",1,3);
        List<Map<String, Object>> deviceInfoList = spOwnerService.listMaps(queryWrapper1);
        if(CollectionUtils.isNotEmpty(deviceStatusList)){
            for (int i=0;i<deviceStatusList.size();i++){
                if(CollectionUtils.isNotEmpty(deviceInfoList)){
                    for (int j=0;j<deviceInfoList.size();j++){
                        if(deviceStatusList.get(i).get("device_code").equals(deviceInfoList.get(j).get("owner_code"))){
                            deviceStatusList.get(i).put("device_address",deviceInfoList.get(j).get("unitinfo"));
                            break;
                        }
                    }
                }
            }
        }
        return deviceStatusList;
    }

    public List<Map<String, Object>> typeList(String requestBody) {
        JSONObject requestVO = JSONObject.parseObject(requestBody);
        String companyCode = requestVO.get("companyCode").toString();
        QueryWrapper<SpHj2017> queryWrapper1 = Wrappers.query();
        queryWrapper1.select("COUNT(CASE WHEN clzt = 0 THEN 1 ELSE NULL END) AS untreated", "COUNT(CASE WHEN clzt = 1 THEN 1 ELSE NULL END) AS processed")
                .inSql("device_code", "SELECT owner_code FROM sp_owner WHERE company = '" + companyCode + "' AND " +
                        "dwtype = 1");
        List<Map<String, Object>> deviceStatusList1 = this.listMaps(queryWrapper1);
        return deviceStatusList1;
    }

    public Integer countDevice(String requestBody) {
        JSONObject requestVO = JSONObject.parseObject(requestBody);
        String companyCode = requestVO.get("companyCode").toString();

        QueryWrapper<SpHj2017> queryWrapper1 = Wrappers.query();
        queryWrapper1.select("DISTINCT device_code")
                .inSql("device_code", "SELECT owner_code FROM sp_owner WHERE company = '" + companyCode + "'");
        List<SpHj2017> SpHj2017 = this.list(queryWrapper1);

        Integer deviceCount = SpHj2017.size();

        return deviceCount;
    }

}
