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.common.security.utils.SecurityUtils;
import com.usky.fire.domain.*;
import com.usky.fire.mapper.BaseCompanyMapper;
import com.usky.fire.service.BaseBuildAttachService;
import com.usky.fire.service.BaseBuildExtinguishService;
import com.usky.fire.service.BaseBuildService;
import com.usky.fire.mapper.BaseBuildMapper;
import com.usky.fire.service.BaseCompanyService;
import com.usky.fire.service.vo.BaseBuildAddVO;
import com.usky.fire.service.vo.BaseBuildExportVO;
import com.usky.fire.service.vo.CompanyDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.swing.text.DateFormatter;
import java.time.LocalDateTime;
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-11-22
 */
@Service
public class BaseBuildServiceImpl extends AbstractCrudService<BaseBuildMapper, BaseBuild> implements BaseBuildService {

    @Autowired
    private BaseBuildAttachService baseBuildAttachService;

    @Autowired
    private BaseBuildExtinguishService baseBuildExtinguishService;

    @Autowired
    private BaseCompanyMapper baseCompanyMapper;

    @Override
    public CommonPage<Object> baseBuildList(String buildNum, String buildName, String companyId, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<BaseBuild> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BaseBuild> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseBuild::getId, BaseBuild::getBuildNum, BaseBuild::getBuildName, BaseBuild::getAddress, BaseBuild::getBuildArea,
                BaseBuild::getFireRating, BaseBuild::getUseCharacter, BaseBuild::getBuildStructure, BaseBuild::getBuildHigh,
                BaseBuild::getCompleteYear, BaseBuild::getSafePerson, BaseBuild::getManagePerson,
                BaseBuild::getFireRisk, BaseBuild::getBuildPlan, BaseBuild::getCompanyId)
                .eq(BaseBuild::getDeleteFlag, 0)
                .like(StringUtils.isNotBlank(buildNum), BaseBuild::getBuildNum, buildNum)
                .like(StringUtils.isNotBlank(buildName), BaseBuild::getBuildName, buildName)
                .eq(StringUtils.isNotBlank(companyId), BaseBuild::getCompanyId, companyId)
                .orderByDesc(BaseBuild::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<String> companyIdList = new ArrayList<>();
            for (int j = 0; j < page.getRecords().size(); j++) {
                companyIdList.add(page.getRecords().get(j).getCompanyId());
            }
            List<BaseCompany> list1 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                String[] a2 = companyIdList.toArray(new String[companyIdList.size()]);
                list1 = baseCompanyMapper.companyNameList(a2);
            }
            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("buildNum", page.getRecords().get(i).getBuildNum());
                map.put("buildName", page.getRecords().get(i).getBuildName());
                map.put("address", page.getRecords().get(i).getAddress());
                map.put("buildArea", page.getRecords().get(i).getBuildArea());
                map.put("fireRating", page.getRecords().get(i).getFireRating());
                map.put("useCharacter", page.getRecords().get(i).getUseCharacter());
                map.put("buildStructure", page.getRecords().get(i).getBuildStructure());
                map.put("buildHigh", page.getRecords().get(i).getBuildHigh());
                map.put("completeYear", page.getRecords().get(i).getCompleteYear());
                map.put("safePerson", page.getRecords().get(i).getSafePerson());
                map.put("managePerson", page.getRecords().get(i).getManagePerson());
                map.put("fireRisk", page.getRecords().get(i).getFireRisk());
                map.put("buildPlan", page.getRecords().get(i).getBuildPlan());
                if (CollectionUtils.isNotEmpty(list1)) {
                    for (int j = 0; j < list1.size(); j++) {
                        if (page.getRecords().get(i).getCompanyId().equals(list1.get(j).getCompanyId())) {
                            map.put("companyName", list1.get(j).getCompanyName());
                        }
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<BaseBuildExportVO> baseBuildListExport(String buildNum, String buildName){
        List<BaseBuildExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseBuild> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseBuild::getId, BaseBuild::getBuildNum, BaseBuild::getBuildName, BaseBuild::getAddress, BaseBuild::getBuildArea,
                BaseBuild::getFireRating, BaseBuild::getUseCharacter, BaseBuild::getBuildStructure, BaseBuild::getBuildHigh,
                BaseBuild::getCompleteYear, BaseBuild::getSafePerson, BaseBuild::getManagePerson, BaseBuild::getFireRisk, BaseBuild::getBuildPlan)
                .eq(BaseBuild::getDeleteFlag, 0)
                .like(StringUtils.isNotBlank(buildNum), BaseBuild::getBuildNum, buildNum)
                .like(StringUtils.isNotBlank(buildName), BaseBuild::getBuildName, buildName)
                .orderByDesc(BaseBuild::getId);
        List<BaseBuild> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                BaseBuildExportVO baseBuildExportVO = new BaseBuildExportVO();
                baseBuildExportVO.setBuildNum(list1.get(i).getBuildNum());
                baseBuildExportVO.setBuildName(list1.get(i).getBuildName());
                baseBuildExportVO.setAddress(list1.get(i).getAddress());
                baseBuildExportVO.setBuildArea(list1.get(i).getBuildArea());
                baseBuildExportVO.setFireRating(list1.get(i).getFireRating());
                baseBuildExportVO.setUseCharacter(list1.get(i).getUseCharacter());
                baseBuildExportVO.setBuildStructure(list1.get(i).getBuildStructure());
                baseBuildExportVO.setBuildHigh(list1.get(i).getBuildHigh());
                baseBuildExportVO.setCompleteYear((list1.get(i).getCompleteYear()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
                baseBuildExportVO.setSafePerson(list1.get(i).getSafePerson());
                baseBuildExportVO.setManagePerson(list1.get(i).getManagePerson());
                baseBuildExportVO.setFireRisk(list1.get(i).getFireRisk());
                baseBuildExportVO.setBuildPlan(list1.get(i).getBuildPlan());
                list.add(baseBuildExportVO);
            }
        }

        return list;
    }


    @Override
    public void updateBaseBuild(BaseBuild baseBuild) {
        baseBuild.setUpdatePerson(SecurityUtils.getLoginUser().getSysPerson().getFullName());
        baseBuild.setUpdateTime(LocalDateTime.now());
        this.updateById(baseBuild);
    }

    @Override
    public void delBaseBuild(Integer id) {
        BaseBuild baseBuild = new BaseBuild();
        baseBuild.setId(id);
        baseBuild.setUpdatePerson(SecurityUtils.getLoginUser().getSysPerson().getFullName());
        baseBuild.setUpdateTime(LocalDateTime.now());
        baseBuild.setDeleteFlag("1");
        this.updateById(baseBuild);
    }

    @Override
    public List<BaseBuildAddVO> baseBuildByIdList(String companyId, String buildName){
        List<BaseBuildAddVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseBuild> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseBuild::getDeleteFlag, 0)
                .eq(BaseBuild::getCompanyId, companyId)
                .orderByDesc(BaseBuild::getId);
        List<BaseBuild> list1 = this.list(queryWrapper);
        List<Integer> buildIdList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list1)) {
            for (int i = 0; i < list1.size(); i++) {
                buildIdList.add(list1.get(i).getId());
            }
            List<BaseBuildAttach> list5 = new ArrayList<>();
            List<BaseBuildExtinguish> list6 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(buildIdList)) {
                LambdaQueryWrapper<BaseBuildAttach> queryWrapper4 = Wrappers.lambdaQuery();
                queryWrapper4.select(BaseBuildAttach::getId, BaseBuildAttach::getBuildId,
                        BaseBuildAttach::getHazardousProduceA,
                        BaseBuildAttach::getHazardousProduceB,BaseBuildAttach::getHazardousProduceC,
                        BaseBuildAttach::getHazardousProduceD,BaseBuildAttach::getHazardousStorageA,
                        BaseBuildAttach::getHazardousStorageB,BaseBuildAttach::getHazardousStorageC,BaseBuildAttach::getHazardousStorageD)
                        .in(BaseBuildAttach::getBuildId, buildIdList);
                list5 = baseBuildAttachService.list(queryWrapper4);
                LambdaQueryWrapper<BaseBuildExtinguish> queryWrapper5 = Wrappers.lambdaQuery();
                queryWrapper5.select(BaseBuildExtinguish::getId, BaseBuildExtinguish::getBuildId,
                        BaseBuildExtinguish::getExtinguishNum,
                        BaseBuildExtinguish::getExtinguishType)
                        .in(BaseBuildExtinguish::getBuildId, buildIdList)
                        .eq(BaseBuildExtinguish::getDeleteFlag, 0);;
                list6 = baseBuildExtinguishService.list(queryWrapper5);
            }
            for (int i = 0; i < list1.size(); i++) {
                List<BaseBuildExtinguish> list8 = new ArrayList<>();
                BaseBuildAddVO baseBuildAddVO = new BaseBuildAddVO();
                baseBuildAddVO.setBaseBuild(list1.get(i));
                for (int k = 0; k < list5.size(); k++) {
                    if (list1.get(i).getId()==list5.get(k).getBuildId()){
                        baseBuildAddVO.setBaseBuildAttach(list5.get(k));
                    }
                }
                for (int l = 0; l < list6.size(); l++) {
                    if (list1.get(i).getId()==list6.get(l).getBuildId()){
                        list8.add(list6.get(l));
                        baseBuildAddVO.setBaseBuildExtinguishList(list8);
                    }
                }
                list.add(baseBuildAddVO);
            }
        }
        return list;
    }

}
