package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.util.Arith;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.BaseCompany;
import com.usky.fire.domain.BaseDevice;
import com.usky.fire.domain.BaseDeviceStatus;
import com.usky.fire.domain.DemCase;
import com.usky.fire.mapper.DemCaseMapper;
import com.usky.fire.service.BaseCompanyService;
import com.usky.fire.service.BaseDeviceService;
import com.usky.fire.service.BaseDeviceStatusService;
import com.usky.fire.service.DemCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 设备案件 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-12
 */
@Service
public class DemCaseServiceImpl extends AbstractCrudService<DemCaseMapper, DemCase> implements DemCaseService {

    @Autowired
    private BaseDeviceService baseDeviceService;

    @Autowired
    private BaseCompanyService baseCompanyService;

    @Autowired
    private BaseDeviceStatusService baseDeviceStatusService;

    @Override
    public CommonPage<DemCase> caseList(String streetTown, String companyName, String deviceType, Integer id, Integer pageNum, Integer pageSize) {
        IPage<DemCase> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemCase> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(deviceType), DemCase::getDeviceType, deviceType)
                .eq(StringUtils.isNotBlank(streetTown), DemCase::getStreet, streetTown)
                .eq(StringUtils.isNotBlank(companyName), DemCase::getDutyGroup, companyName)
                .eq(id != 0 && id != null, DemCase::getId, id)
                .orderByDesc(DemCase::getId);
        page = this.page(page, queryWrapper);
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public Map<String, Object> caseStatistics(String streetTown, String companyId) {
        int deviceCount = 0;
        int onLineDeviceCount = 0;
        int caseCount = 0;
//        int handleCaseCount = 0;
        int closureCaseCount = 0;
        List<Integer> deviceTypeList = new ArrayList<>();
        deviceTypeList.add(2);
        deviceTypeList.add(5);
        List<String> deviceCodeList = this.deviceList(streetTown, companyId, deviceTypeList);
        if (CollectionUtils.isNotEmpty(deviceCodeList)) {
            onLineDeviceCount = this.deviceStatusCount(deviceCodeList, 1);
        }
        deviceCount = deviceCodeList.size();
        caseCount = this.caseCount1(streetTown, companyId, null);
//        handleCaseCount = caseCount(streetTown, companyId, 2);
        closureCaseCount = this.caseCount1(streetTown, companyId, 3);

        Map<String, Object> map = new HashMap<>();
        map.put("deviceCount", deviceCount);
        if (onLineDeviceCount != 0 && deviceCount != 0) {
            map.put("onLineDeviceRatio", Arith.div(onLineDeviceCount, deviceCount, 2) * 100 + "%");
        } else {
            map.put("onLineDeviceRatio", "0%");
        }
        map.put("handleCaseRatio", "100%");
//        if (handleCaseCount != 0 && caseCount != 0) {
//            map.put("handleCaseRatio", handleCaseCount / caseCount + "%");
//        } else {
//            map.put("handleCaseRatio", "0%");
//        }

        if (closureCaseCount != 0 && caseCount != 0) {
            double Ratio = Arith.div(closureCaseCount, caseCount, 2) * 100;
            map.put("closureCaseRatio", Ratio + "%");
        } else {
            map.put("closureCaseRatio", "0%");
        }
        return map;
    }

    @Override
    public Integer caseCount(String streetTown, String companyId, Integer caseFlag) {
        List<String> companyNameList = new ArrayList<>();
        if (StringUtils.isNotBlank(streetTown) || StringUtils.isNotBlank(companyId)) {
            List<BaseCompany> companyList = this.companyList(streetTown, companyId);
            if (CollectionUtils.isNotEmpty(companyList)) {
                for (int i = 0; i < companyList.size(); i++) {
                    companyNameList.add(companyList.get(i).getCompanyName());
                }
            }
        }

        LambdaQueryWrapper<DemCase> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(streetTown), DemCase::getStreet, streetTown)
                .in(CollectionUtils.isNotEmpty(companyNameList), DemCase::getDutyGroup, companyNameList)
                .eq(caseFlag != null, DemCase::getCaseFlag, caseFlag);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public Integer caseCount1(String streetTown, String companyId, Integer caseFlag) {
        LambdaQueryWrapper<DemCase> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(StringUtils.isNotBlank(streetTown), DemCase::getStreet, streetTown)
                .eq(caseFlag != null, DemCase::getCaseFlag, caseFlag);
        int count = this.count(queryWrapper);
        return count;
    }


    @Override
    public int deviceStatusCount(List<String> deviceCodeList, Integer deviceStatus) {
        LambdaQueryWrapper<BaseDeviceStatus> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.in(CollectionUtils.isNotEmpty(deviceCodeList), BaseDeviceStatus::getDeviceCode, deviceCodeList)
                .eq(deviceStatus != null, BaseDeviceStatus::getDeviceStatus, deviceStatus);
        int count = baseDeviceStatusService.count(queryWrapper);
        return count;
    }

    @Override
    public List<BaseCompany> companyList(String streetTown, String companyId) {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getCompanyId, BaseCompany::getCompanyName)
                .eq(StringUtils.isNotBlank(streetTown), BaseCompany::getStreetTown, streetTown)
                .eq(StringUtils.isNotBlank(companyId), BaseCompany::getCompanyId, companyId)
                .eq(BaseCompany::getEnable, 0)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> companyList = baseCompanyService.list(queryWrapper);
        return companyList;
    }


    @Override
    public List<String> deviceList(String streetTown, String companyId, List<Integer> deviceTypeList) {
        List<String> companyIdList = new ArrayList<>();
        if (StringUtils.isNotBlank(streetTown)) {
            List<BaseCompany> companyList = this.companyList(streetTown, null);
            if (CollectionUtils.isNotEmpty(companyList)) {
                for (int i = 0; i < companyList.size(); i++) {
                    companyIdList.add(companyList.get(i).getCompanyId());
                }
            }
        } else if (StringUtils.isNotBlank(companyId)) {
            companyIdList.add(companyId);
        }

        LambdaQueryWrapper<BaseDevice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseDevice::getDeviceType, BaseDevice::getDeviceCode)
                .eq(BaseDevice::getEnable, 1)
                .in(CollectionUtils.isNotEmpty(deviceTypeList), BaseDevice::getDeviceType, deviceTypeList)
                .in(CollectionUtils.isNotEmpty(companyIdList), BaseDevice::getCompanyId, companyIdList);
        List<BaseDevice> deviceList = baseDeviceService.list(queryWrapper);
        List<String> deviceCodeList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(deviceList)) {
            for (int i = 0; i < deviceList.size(); i++) {
                deviceCodeList.add(deviceList.get(i).getDeviceCode());
            }
        }
        return deviceCodeList;
    }

}
