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.mybatis.core.AbstractCrudService;
import com.usky.fire.domain.DemAtlInspectCertificate;
import com.usky.fire.domain.DemAtlInspectDeclare;
import com.usky.fire.mapper.DemAtlInspectDeclareMapper;
import com.usky.fire.service.DemAtlInspectCertificateService;
import com.usky.fire.service.DemAtlInspectDeclareService;
import com.usky.fire.service.vo.DemAtlInspectDeclareExportVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消防安检申报总表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-10-08
 */
@Service
public class DemAtlInspectDeclareServiceImpl extends AbstractCrudService<DemAtlInspectDeclareMapper, DemAtlInspectDeclare> implements DemAtlInspectDeclareService {

    @Autowired
    private DemAtlInspectCertificateService demAtlInspectCertificateService;

    @Override
    public CommonPage<Object> atlInspectDeclareList(String projectCode, String declareCompanyName, Integer id, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<DemAtlInspectDeclare> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<DemAtlInspectDeclare> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemAtlInspectDeclare::getId, DemAtlInspectDeclare::getProjectCode, DemAtlInspectDeclare::getDeclareCompanyName,
                DemAtlInspectDeclare::getDeclarePerson, DemAtlInspectDeclare::getFillTime, DemAtlInspectDeclare::getSiteName,
                DemAtlInspectDeclare::getLegalRepresentative, DemAtlInspectDeclare::getSiteArea, DemAtlInspectDeclare::getLinkPerson,
                DemAtlInspectDeclare::getLinkPhone, DemAtlInspectDeclare::getSiteNature, DemAtlInspectDeclare::getBuildNature,
                DemAtlInspectDeclare::getAcceptStatus, DemAtlInspectDeclare::getFireFacilities, DemAtlInspectDeclare::getAcceptName,
                DemAtlInspectDeclare::getAcceptTime)
                .eq(DemAtlInspectDeclare::getDeleteFlag, 0)
                .eq(id != null & id != 0, DemAtlInspectDeclare::getId, id)
                .like(StringUtils.isNotBlank(projectCode), DemAtlInspectDeclare::getProjectCode, projectCode)
                .like(StringUtils.isNotBlank(declareCompanyName), DemAtlInspectDeclare::getDeclareCompanyName, declareCompanyName)
                .orderByDesc(DemAtlInspectDeclare::getId);
        page = this.page(page, queryWrapper);
        if (page.getTotal() > 0) {
            List<String> projectCodeList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                projectCodeList.add(page.getRecords().get(i).getProjectCode());
            }
            List<DemAtlInspectCertificate> list1 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(projectCodeList)) {
                LambdaQueryWrapper<DemAtlInspectCertificate> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(DemAtlInspectCertificate::getId, DemAtlInspectCertificate::getProjectCode,
                        DemAtlInspectCertificate::getSafetyPerson, DemAtlInspectCertificate::getUseNature,
                        DemAtlInspectCertificate::getSiteAddress)
                        .in(DemAtlInspectCertificate::getProjectCode, projectCodeList)
                        .eq(DemAtlInspectCertificate::getDeleteFlag, 0);
                list1 = demAtlInspectCertificateService.list(queryWrapper1);
            }
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", page.getRecords().get(i).getId());
                map.put("projectCode", page.getRecords().get(i).getProjectCode());
                map.put("declareCompanyName", page.getRecords().get(i).getDeclareCompanyName());
                map.put("declarePerson", page.getRecords().get(i).getDeclarePerson());
                map.put("fillTime", page.getRecords().get(i).getFillTime());
                map.put("siteName", page.getRecords().get(i).getSiteName());
                map.put("legalRepresentative", page.getRecords().get(i).getLegalRepresentative());
                map.put("siteArea", page.getRecords().get(i).getSiteArea());
                map.put("linkPerson", page.getRecords().get(i).getLinkPerson());
                map.put("linkPhone", page.getRecords().get(i).getLinkPhone());
                map.put("siteNature", page.getRecords().get(i).getSiteNature());
                map.put("buildNature", page.getRecords().get(i).getBuildNature());
                map.put("acceptStatus", page.getRecords().get(i).getAcceptStatus());
                map.put("fireFacilities", page.getRecords().get(i).getFireFacilities());
                map.put("acceptName", page.getRecords().get(i).getAcceptName());
                map.put("acceptTime", page.getRecords().get(i).getAcceptTime());
                map.put("id1", null);
                map.put("safetyPerson", null);
                map.put("useNature", null);
                map.put("siteAddress", null);
                for (int j = 0; j < list1.size(); j++) {
                    if (page.getRecords().get(i).getProjectCode().equals(list1.get(j).getProjectCode())) {
                        map.put("id1", list1.get(j).getId());
                        map.put("safetyPerson", list1.get(j).getSafetyPerson());
                        map.put("useNature", list1.get(j).getUseNature());
                        map.put("siteAddress", list1.get(j).getSiteAddress());
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DemAtlInspectDeclareExportVO> atlInspectDeclareListExport(String projectCode, String declareCompanyName, Integer id){
        List<DemAtlInspectDeclareExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<DemAtlInspectDeclare> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemAtlInspectDeclare::getId, DemAtlInspectDeclare::getProjectCode, DemAtlInspectDeclare::getDeclareCompanyName,
                DemAtlInspectDeclare::getDeclarePerson, DemAtlInspectDeclare::getFillTime, DemAtlInspectDeclare::getSiteName,
                DemAtlInspectDeclare::getLegalRepresentative, DemAtlInspectDeclare::getSiteArea, DemAtlInspectDeclare::getLinkPerson,
                DemAtlInspectDeclare::getLinkPhone, DemAtlInspectDeclare::getSiteNature, DemAtlInspectDeclare::getBuildNature,
                DemAtlInspectDeclare::getAcceptStatus, DemAtlInspectDeclare::getFireFacilities, DemAtlInspectDeclare::getAcceptName,
                DemAtlInspectDeclare::getAcceptTime)
                .eq(DemAtlInspectDeclare::getDeleteFlag, 0)
                .eq(id != null & id != 0, DemAtlInspectDeclare::getId, id)
                .like(StringUtils.isNotBlank(projectCode), DemAtlInspectDeclare::getProjectCode, projectCode)
                .like(StringUtils.isNotBlank(declareCompanyName), DemAtlInspectDeclare::getDeclareCompanyName, declareCompanyName)
                .orderByDesc(DemAtlInspectDeclare::getId);
        List<DemAtlInspectDeclare> list1= this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> projectCodeList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                projectCodeList.add(list1.get(i).getProjectCode());
            }
            List<DemAtlInspectCertificate> list2 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(projectCodeList)) {
                LambdaQueryWrapper<DemAtlInspectCertificate> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(DemAtlInspectCertificate::getId, DemAtlInspectCertificate::getProjectCode,
                        DemAtlInspectCertificate::getSafetyPerson, DemAtlInspectCertificate::getUseNature,
                        DemAtlInspectCertificate::getSiteAddress)
                        .in(DemAtlInspectCertificate::getProjectCode, projectCodeList)
                        .eq(DemAtlInspectCertificate::getDeleteFlag, 0);
                list2 = demAtlInspectCertificateService.list(queryWrapper1);
            }
            for (int i = 0; i < list1.size(); i++) {
                DemAtlInspectDeclareExportVO demAtlInspectDeclareExportVO = new DemAtlInspectDeclareExportVO();
                demAtlInspectDeclareExportVO.setProjectCode(list1.get(i).getProjectCode());
                demAtlInspectDeclareExportVO.setDeclareCompanyName(list1.get(i).getDeclareCompanyName());
                demAtlInspectDeclareExportVO.setDeclarePerson(list1.get(i).getDeclarePerson());
                demAtlInspectDeclareExportVO.setFillTime((list1.get(i).getFillTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                demAtlInspectDeclareExportVO.setSiteName(list1.get(i).getSiteName());
                demAtlInspectDeclareExportVO.setLegalRepresentative(list1.get(i).getLegalRepresentative());
                demAtlInspectDeclareExportVO.setSiteArea(list1.get(i).getSiteArea());
                demAtlInspectDeclareExportVO.setLinkPerson(list1.get(i).getLinkPerson());
                demAtlInspectDeclareExportVO.setLinkPhone(list1.get(i).getLinkPhone());
                demAtlInspectDeclareExportVO.setSiteNature(list1.get(i).getSiteNature());
                demAtlInspectDeclareExportVO.setBuildNature(list1.get(i).getBuildNature());
                demAtlInspectDeclareExportVO.setSiteAddress(null);
                demAtlInspectDeclareExportVO.setUseNature(null);
                demAtlInspectDeclareExportVO.setSafetyPerson(null);
                demAtlInspectDeclareExportVO.setFireFacilities(list1.get(i).getFireFacilities());
                demAtlInspectDeclareExportVO.setAcceptTime((list1.get(i).getAcceptTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                demAtlInspectDeclareExportVO.setAcceptStatus(list1.get(i).getAcceptStatus());
                demAtlInspectDeclareExportVO.setAcceptName(list1.get(i).getAcceptName());
                for (int j = 0; j < list2.size(); j++) {
                    if (list1.get(i).getProjectCode().equals(list2.get(j).getProjectCode())) {
                        demAtlInspectDeclareExportVO.setSiteAddress(list2.get(j).getSiteAddress());
                        demAtlInspectDeclareExportVO.setUseNature(list2.get(j).getUseNature());
                        demAtlInspectDeclareExportVO.setSafetyPerson(list2.get(j).getSafetyPerson());
                    }
                }
                list.add(demAtlInspectDeclareExportVO);
            }
        }

        return list;
    }

    @Transactional
    @Override
    public void updateAtlInspectCertificate(DemAtlInspectDeclare demAtlInspectDeclare) {
        this.updateById(demAtlInspectDeclare);
        if (demAtlInspectDeclare.getId1() != null && demAtlInspectDeclare.getId1() != 0) {
            DemAtlInspectCertificate demAtlInspectCertificate = new DemAtlInspectCertificate();
            demAtlInspectCertificate.setId(demAtlInspectDeclare.getId1());
            demAtlInspectCertificate.setSafetyPerson(demAtlInspectDeclare.getSafetyPerson());
            demAtlInspectCertificate.setUseNature(demAtlInspectDeclare.getUseNature());
            demAtlInspectCertificate.setSiteAddress(demAtlInspectDeclare.getSiteAddress());
            demAtlInspectCertificateService.updateById(demAtlInspectCertificate);
        }
    }

    @Transactional
    @Override
    public void delAtlInspectCertificate(Integer id, Integer zid) {
        DemAtlInspectDeclare demAtlInspectDeclare = new DemAtlInspectDeclare();
        demAtlInspectDeclare.setId(id);
        demAtlInspectDeclare.setDeleteFlag("1");
        this.updateById(demAtlInspectDeclare);
        if (zid != null && zid != 0) {
            DemAtlInspectCertificate demAtlInspectCertificate = new DemAtlInspectCertificate();
            demAtlInspectCertificate.setId(zid);
            demAtlInspectCertificate.setDeleteFlag("1");
            demAtlInspectCertificateService.updateById(demAtlInspectCertificate);
        }
    }

}
