package com.usky.fire.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.StringUtils;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.BaseCompany;
import com.usky.fire.domain.BaseCompanyPerson;
import com.usky.fire.mapper.BaseCompanyMapper;
import com.usky.fire.mapper.BaseCompanyPersonMapper;
import com.usky.fire.mapper.MhPostInspectMapper;
import com.usky.fire.service.BaseCompanyPersonService;
import com.usky.fire.service.BaseUserCompanyService;
import com.usky.fire.service.vo.BaseCompanyPersonVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 联网单位消防人员信息 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-24
 */
@Service
public class BaseCompanyPersonServiceImpl extends AbstractCrudService<BaseCompanyPersonMapper, BaseCompanyPerson> implements BaseCompanyPersonService {

    @Autowired
    private MhPostInspectMapper mhPostInspectMapper;

    @Autowired
    private BaseCompanyMapper baseCompanyMapper;

    @Override
    public CommonPage<BaseCompanyPerson> baseCompanyPersonList(String companyCode, String companyName, Integer pageNum, Integer pageSize) {
//        IPage<BaseCompany> page1 = new Page<>(pageNum, pageSize);
        IPage<BaseCompanyPerson> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BaseCompany> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(BaseCompany::getCompanyId)
                .eq(BaseCompany::getEnable,0)
                .eq(BaseCompany::getCompanyType,"1")
                .like(StringUtils.isNotBlank(companyCode), BaseCompany::getCompanyCode, companyCode)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> baseCompanies = baseCompanyMapper.selectList(queryWrapper1);
        if (CollectionUtils.isNotEmpty(baseCompanies)){
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < baseCompanies.size(); i++) {
                companyIdList.add(baseCompanies.get(i).getCompanyId());
            }
            LambdaQueryWrapper<BaseCompanyPerson> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(BaseCompanyPerson::getCompanyId, companyIdList)
                    .orderByDesc(BaseCompanyPerson::getId);
            page = this.page(page, queryWrapper);
            if (page.getTotal() > 0) {
                List<BaseCompanyPerson> baseCompanyPeople = mhPostInspectMapper.inspectStatistics(null);
                for (int i = 0; i < page.getRecords().size(); i++) {
                    for (int j = 0; j < baseCompanyPeople.size(); j++) {
                        if (page.getRecords().get(i).getCompanyId().equals(baseCompanyPeople.get(j).getCompanyId())) {
                            page.getRecords().get(i).setInspectCount(baseCompanyPeople.get(j).getInspectCount());
                            page.getRecords().get(i).setAnswerCount(baseCompanyPeople.get(j).getAnswerCount());
                            page.getRecords().get(i).setInspectTime(baseCompanyPeople.get(j).getInspectTime());
                        }
                    }
                }
            }

        }
        return new CommonPage<>(page.getRecords(), page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<BaseCompanyPersonVO> baseCompanyPersonListExport(String companyCode,String companyName){
        LambdaQueryWrapper<BaseCompany> queryWrapper1 = Wrappers.lambdaQuery();
        queryWrapper1.select(BaseCompany::getCompanyId)
                .eq(BaseCompany::getEnable,0)
                .like(StringUtils.isNotBlank(companyCode), BaseCompany::getCompanyCode, companyCode)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> baseCompanies = baseCompanyMapper.selectList(queryWrapper1);
        List<BaseCompanyPersonVO> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(baseCompanies)){
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < baseCompanies.size(); i++) {
                companyIdList.add(baseCompanies.get(i).getCompanyId());
            }
            LambdaQueryWrapper<BaseCompanyPerson> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.in(BaseCompanyPerson::getCompanyId, companyIdList)
                    .orderByDesc(BaseCompanyPerson::getId);
            List<BaseCompanyPerson> list1 = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list1)) {
                List<BaseCompanyPerson> baseCompanyPeople = mhPostInspectMapper.inspectStatistics(null);
                for (int i = 0; i < list1.size(); i++) {
                    BaseCompanyPersonVO baseCompanyPersonVO = new BaseCompanyPersonVO();
                    baseCompanyPersonVO.setCompanyName(list1.get(i).getCompanyName());
                    baseCompanyPersonVO.setCompanyCode(list1.get(i).getCompanyCode());
                    baseCompanyPersonVO.setFireDutyName(list1.get(i).getFireDutyName());
                    baseCompanyPersonVO.setFireDutyPhone(list1.get(i).getFireDutyPhone());
                    baseCompanyPersonVO.setFireManageName(list1.get(i).getFireManageName());
                    baseCompanyPersonVO.setFireManagePhone(list1.get(i).getFireManagePhone());
                    baseCompanyPersonVO.setFireControlPhone(list1.get(i).getFireControlPhone());
                    for (int j = 0; j < baseCompanyPeople.size(); j++) {
                        if (list1.get(i).getCompanyId().equals(baseCompanyPeople.get(j).getCompanyId())) {
                            baseCompanyPersonVO.setInspectCount(baseCompanyPeople.get(j).getInspectCount());
                            baseCompanyPersonVO.setAnswerCount(baseCompanyPeople.get(j).getAnswerCount());
                            baseCompanyPersonVO.setInspectTime((baseCompanyPeople.get(j).getInspectTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        }
                    }
                    list.add(baseCompanyPersonVO);
                }
            }

        }
        return list;
    }

    @Override
    public Map<String, Object> postInspectStatistics() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        String format = simpleDateFormat.format(date);
        String startTime = format + " 00:00:00";
        String endTime = format + " 23:59:59";
//        List<String> companyIdList = baseUserCompanyService.companyIdList();
        String spentTime = "--:--";
        Integer answeredCount = 0;
        Integer notAnsweredCount = 0;
        Integer count = 0;
//        if (CollectionUtils.isNotEmpty(companyIdList)) {
        Integer spentTimeAvg = mhPostInspectMapper.mhPostInspectStatistics(" AVG(spent_time) ", 1, startTime, endTime, null, null);
        if (spentTimeAvg == null) {
            spentTimeAvg = 0;
        }
        answeredCount = mhPostInspectMapper.mhPostInspectStatistics(" count(*) ", 1, startTime, endTime, null, null);
        notAnsweredCount = mhPostInspectMapper.mhPostInspectStatistics(" count(*) ", 0, startTime, endTime, null, null);
        count = answeredCount + notAnsweredCount;
        spentTime = String.format("%0" + 2 + "d", spentTimeAvg / 60) + ":" + String.format("%0" + 2 + "d", spentTimeAvg % 60);
//        spentTime = spentTimeAvg / 60 + ":" + spentTimeAvg % 60;
//        }
        Map<String, Object> map = new HashMap<>();
        map.put("spentTimeAvg", spentTime);
        map.put("count", count);
        map.put("answeredCount", answeredCount);
        map.put("notAnsweredCount", notAnsweredCount);
        return map;
    }
    @Override
    public List<BaseCompanyPerson> baseCompanyPersonByCompanyId(String companyId){
        LambdaQueryWrapper<BaseCompanyPerson> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompanyPerson::getFireManageName)
                .eq(BaseCompanyPerson::getCompanyId, companyId);
        List<BaseCompanyPerson> list = this.list(queryWrapper);
        return list;
    }
}
