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.exception.BusinessException;
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.*;
import com.usky.fire.service.util.OnlineMethod;
import com.usky.fire.service.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 * 联网单位信息表 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-08-23
 */
@Service
public class BaseCompanyServiceImpl extends AbstractCrudService<BaseCompanyMapper, BaseCompany> implements BaseCompanyService {

    @Autowired
    private BaseUserCompanyService baseUserCompanyService;

    @Autowired
    private BaseCompanyPersonService baseCompanyPersonService;

    @Autowired
    private BaseCompanyAttach1Service baseCompanyAttach1Service;

    @Autowired
    private DemLawStatisticsService demLawStatisticsService;

    @Autowired
    private DemAtlInspectDeclareService demAtlInspectDeclareService;

    @Autowired
    private DemEnforceReportComplaintService demEnforceReportComplaintService;

    @Autowired
    private BaseCompanyChangeService baseCompanyChangeService;

    @Autowired
    private BaseBuildService baseBuildService;

    @Autowired
    private BaseBuildAttachService baseBuildAttachService;

    @Autowired
    private BaseBuildExtinguishService baseBuildExtinguishService;

    @Override
    public List<BaseCompany> userCompanySelect() {
        List<String> companyIdList = baseUserCompanyService.companyIdList();
        List<BaseCompany> list = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(companyIdList)) {
            list = this.companySelect(companyIdList);
        }
        return list;
    }


    @Override
    public List<BaseCompany> companySelect(List<String> companyIdList) {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getCompanyId, BaseCompany::getCompanyName,BaseCompany::getLinkPerson,BaseCompany::getLinkPhone)
                .in(BaseCompany::getCompanyId, companyIdList)
                .eq(BaseCompany::getEnable, 0)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> list = this.list(queryWrapper);
        return list;
    }

    @Override
    public List<BaseCompany> companyIdList(List<String> streetTownList) {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getCompanyId, BaseCompany::getStreetTown)
                .eq(BaseCompany::getEnable, 0)
                .in(CollectionUtils.isNotEmpty(streetTownList), BaseCompany::getStreetTown, streetTownList);
        List<BaseCompany> list = this.list(queryWrapper);
        return list;
    }


    @Override
    public CommonPage<Object> companyList(String companyName, String organization,String streetTownName,
                                          String companyType, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<BaseCompany> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName, BaseCompany::getOrganization,
                BaseCompany::getAddress, BaseCompany::getCompanyType, BaseCompany::getFireHazard, BaseCompany::getLinkPhone,
                BaseCompany::getFoundTime, BaseCompany::getCompanyNature, BaseCompany::getAdministrativeDivision,
                BaseCompany::getCreateTime,BaseCompany::getStreetTown)
                .eq(BaseCompany::getEnable, 0)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .like(StringUtils.isNotBlank(organization), BaseCompany::getOrganization, organization)
                .eq(StringUtils.isNotBlank(streetTownName), BaseCompany::getStreetTown, streetTownName)
                .eq(StringUtils.isNotBlank(companyType), BaseCompany::getCompanyType, companyType)
                .orderByDesc(BaseCompany::getId);
        page = this.page(page, queryWrapper);
        if (page.getTotal() > 0) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                companyIdList.add(page.getRecords().get(i).getCompanyId());
            }
            List<BaseCompanyPerson> list1 = new ArrayList<>();
            List<BaseCompanyAttach1> list2 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                LambdaQueryWrapper<BaseCompanyPerson> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(BaseCompanyPerson::getId, BaseCompanyPerson::getCompanyId, BaseCompanyPerson::getFireDutyName,
                        BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getFireManagePhone,
                        BaseCompanyPerson::getDelegateName)
                        .in(BaseCompanyPerson::getCompanyId, companyIdList);
                list1 = baseCompanyPersonService.list(queryWrapper1);
                LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getCompanyId, BaseCompanyAttach1::getFixedAssets,
                        BaseCompanyAttach1::getCoverArea, BaseCompanyAttach1::getBuildArea, BaseCompanyAttach1::getEmployeeNum,
                        BaseCompanyAttach1::getMainAttribute, BaseCompanyAttach1::getGovernBody,BaseCompanyAttach1::getBirthCert)
                        .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                list2 = baseCompanyAttach1Service.list(queryWrapper2);
            }

            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("streetTownName", page.getRecords().get(i).getStreetTown());
                map.put("companyId", page.getRecords().get(i).getCompanyId());
                map.put("companyName", page.getRecords().get(i).getCompanyName());
                map.put("organization", page.getRecords().get(i).getOrganization());
                map.put("delegateName", null);
                map.put("address", page.getRecords().get(i).getAddress());
                map.put("companyType", page.getRecords().get(i).getCompanyType());
                map.put("fireHazard", page.getRecords().get(i).getFireHazard());
                map.put("linkPhone", page.getRecords().get(i).getLinkPhone());
                map.put("foundTime", page.getRecords().get(i).getFoundTime());
                map.put("companyNature", page.getRecords().get(i).getCompanyNature());
                map.put("administrativeDivision", page.getRecords().get(i).getAdministrativeDivision());
                map.put("fixedAssets", null);
                map.put("coverArea", null);
                map.put("buildArea", null);
                map.put("employeeNum", null);
                map.put("mainAttribute", null);
                map.put("governBody", null);
                map.put("id2", null);
                map.put("id1", null);
                map.put("fireDutyName", null);
                map.put("fireManageName", null);
                for (int j = 0; j < list1.size(); j++) {
                    if (page.getRecords().get(i).getCompanyId().equals(list1.get(j).getCompanyId())) {
                        map.put("id1", list1.get(j).getId());
                        map.put("fireDutyName", list1.get(j).getFireDutyName());
                        map.put("fireManageName", list1.get(j).getFireManageName());
                        map.put("fireManagePhone", list1.get(j).getFireManagePhone());
                        map.put("delegateName", list1.get(j).getDelegateName());
                    }
                }
                for (int j = 0; j < list2.size(); j++) {
                    if (page.getRecords().get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        map.put("fixedAssets", list2.get(j).getFixedAssets());
                        map.put("coverArea", list2.get(j).getCoverArea());
                        map.put("buildArea", list2.get(j).getBuildArea());
                        map.put("employeeNum", list2.get(j).getEmployeeNum());
                        map.put("mainAttribute", list2.get(j).getMainAttribute());
                        map.put("governBody", list2.get(j).getGovernBody());
                        map.put("id2", list2.get(j).getId());
                        map.put("birthCert", list2.get(j).getBirthCert());
                    }
                }
                map.put("createTime", page.getRecords().get(i).getCreateTime());
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<CompanyExportVO> companyListExport(String companyName, String organization){

        List<CompanyExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName, BaseCompany::getOrganization,
                BaseCompany::getAddress, BaseCompany::getCompanyType, BaseCompany::getFireHazard, BaseCompany::getLinkPhone,
                BaseCompany::getFoundTime, BaseCompany::getCompanyNature, BaseCompany::getAdministrativeDivision, BaseCompany::getCreateTime)
                .eq(BaseCompany::getEnable, 0)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .like(StringUtils.isNotBlank(organization), BaseCompany::getOrganization, organization)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> baseCompanyList = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(baseCompanyList)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < baseCompanyList.size(); i++) {
                companyIdList.add(baseCompanyList.get(i).getCompanyId());
            }
            List<BaseCompanyPerson> list1 = new ArrayList<>();
            List<BaseCompanyAttach1> list2 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                LambdaQueryWrapper<BaseCompanyPerson> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(BaseCompanyPerson::getId, BaseCompanyPerson::getCompanyId, BaseCompanyPerson::getFireDutyName,
                        BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getDelegateName)
                        .in(BaseCompanyPerson::getCompanyId, companyIdList);
                list1 = baseCompanyPersonService.list(queryWrapper1);
                LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getCompanyId, BaseCompanyAttach1::getFixedAssets,
                        BaseCompanyAttach1::getCoverArea, BaseCompanyAttach1::getBuildArea, BaseCompanyAttach1::getEmployeeNum,
                        BaseCompanyAttach1::getMainAttribute, BaseCompanyAttach1::getGovernBody)
                        .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                list2 = baseCompanyAttach1Service.list(queryWrapper2);
            }

            for (int i = 0; i < baseCompanyList.size(); i++) {
                CompanyExportVO companyDataVo = new CompanyExportVO();
                companyDataVo.setCompanyName(baseCompanyList.get(i).getCompanyName());
                companyDataVo.setOrganization(baseCompanyList.get(i).getOrganization());
                companyDataVo.setAddress(baseCompanyList.get(i).getAddress());
                String companyTypeName = null;
                if (StringUtils.isNotBlank(baseCompanyList.get(i).getCompanyType())) {
                    if (baseCompanyList.get(i).getCompanyType().equals("1")) {
                        companyTypeName = "重点单位";
                    } else if (baseCompanyList.get(i).getCompanyType().equals("2")) {
                        companyTypeName = "一般单位";
                    } else if (baseCompanyList.get(i).getCompanyType().equals("3")) {
                        companyTypeName = "九小场所";
                    } else if (baseCompanyList.get(i).getCompanyType().equals("9")) {
                        companyTypeName = "其他单位";
                    }
                }
                companyDataVo.setCompanyType(companyTypeName);
                companyDataVo.setFireHazard(baseCompanyList.get(i).getFireHazard());
                companyDataVo.setLinkPhone(baseCompanyList.get(i).getLinkPhone());
                companyDataVo.setFoundTime(baseCompanyList.get(i).getFoundTime());
                companyDataVo.setCompanyNature(baseCompanyList.get(i).getCompanyNature());
                companyDataVo.setAdministrativeDivision(baseCompanyList.get(i).getAdministrativeDivision());
                for (int j = 0; j < list1.size(); j++) {
                    if (baseCompanyList.get(i).getCompanyId().equals(list1.get(j).getCompanyId())) {
                        companyDataVo.setDelegateName(list1.get(j).getDelegateName());
                        companyDataVo.setFireDutyName(list1.get(j).getFireDutyName());
                        companyDataVo.setFireManageName(list1.get(j).getFireManageName());
                    }
                }
                for (int j = 0; j < list2.size(); j++) {
                    if (baseCompanyList.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        companyDataVo.setGovernBody(list2.get(j).getGovernBody());
                        companyDataVo.setMainAttribute(list2.get(j).getMainAttribute());
                        companyDataVo.setEmployeeNum(list2.get(j).getEmployeeNum());
                        companyDataVo.setFixedAssets(list2.get(j).getFixedAssets());
                        companyDataVo.setCoverArea(list2.get(j).getCoverArea());
                        companyDataVo.setBuildArea(list2.get(j).getBuildArea());
                    }
                }
                companyDataVo.setCreateTime((baseCompanyList.get(i).getCreateTime()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                list.add(companyDataVo);
            }
        }
        return list;
    }

    @Transactional
    @Override
    public void updateCompany(CompanyDataVo companyDataVo) {
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(companyDataVo.getId());
        baseCompany.setCompanyName(companyDataVo.getCompanyName());
        baseCompany.setOrganization(companyDataVo.getOrganization());
        baseCompany.setAddress(companyDataVo.getAddress());
        baseCompany.setCompanyType(companyDataVo.getCompanyType());
        baseCompany.setFireHazard(companyDataVo.getFireHazard());
        baseCompany.setLinkPhone(companyDataVo.getLinkPhone());
        baseCompany.setFoundTime(companyDataVo.getFoundTime());
        baseCompany.setCompanyNature(companyDataVo.getCompanyNature());
        baseCompany.setAdministrativeDivision(companyDataVo.getAdministrativeDivision());
        baseCompany.setUpdatePerson(SecurityUtils.getUsername());
        baseCompany.setUpdateTime(LocalDateTime.now());
        this.updateById(baseCompany);
        BaseCompanyPerson baseCompanyPerson = new BaseCompanyPerson();
        baseCompanyPerson.setId(companyDataVo.getId1());
        baseCompanyPerson.setCompanyName(companyDataVo.getCompanyName());
        baseCompanyPerson.setFireDutyName(companyDataVo.getFireDutyName());
        baseCompanyPerson.setFireManageName(companyDataVo.getFireManageName());
        baseCompanyPerson.setDelegateName(companyDataVo.getDelegateName());
        baseCompanyPerson.setUpdatePerson(SecurityUtils.getUsername());
        baseCompanyPerson.setUpdateTime(LocalDateTime.now());
        baseCompanyPersonService.updateById(baseCompanyPerson);
        BaseCompanyAttach1 baseCompanyAttach1 = new BaseCompanyAttach1();
        baseCompanyAttach1.setId(companyDataVo.getId2());
        baseCompanyAttach1.setFixedAssets(companyDataVo.getFixedAssets());
        baseCompanyAttach1.setCoverArea(companyDataVo.getCoverArea());
        baseCompanyAttach1.setBuildArea(companyDataVo.getBuildArea());
        baseCompanyAttach1.setEmployeeNum(companyDataVo.getEmployeeNum());
        baseCompanyAttach1.setMainAttribute(companyDataVo.getMainAttribute());
        baseCompanyAttach1.setGovernBody(companyDataVo.getGovernBody());
        baseCompanyAttach1.setUpdatePerson(SecurityUtils.getUsername());
        baseCompanyAttach1.setUpdateTime(LocalDateTime.now());
        baseCompanyAttach1Service.updateById(baseCompanyAttach1);
    }

    @Override
    public void delCompany(Integer id) {
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(id);
        baseCompany.setEnable("1");
        baseCompany.setUpdateTime(LocalDateTime.now());
        baseCompany.setUpdatePerson(SecurityUtils.getUsername());
        this.updateById(baseCompany);
    }

    @Override
    public List<Map<String, Object>> lawTrendScatterer(String streetTown, String sattererType) {
        List<Map<String, Object>> list = new ArrayList<>();
        List<String> companyCodeList = new ArrayList<>();
        switch (sattererType) {
            case "双随机执法单位":
                companyCodeList = this.lawStatistics();
                if (CollectionUtils.isNotEmpty(companyCodeList)) {
                    list = this.companyData(streetTown, companyCodeList);
                }
                break;
            case "安全检查单位":
                companyCodeList = this.inspectDeclare();
                if (CollectionUtils.isNotEmpty(companyCodeList)) {
                    list = this.companyData(streetTown, companyCodeList);
                }
                break;
            case "举报投诉":
                list = this.reportComplaint();
                break;
//            case "执法记录仪":
//                break;
            default:
                throw new BusinessException("该类型点位未定义");
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> reportComplaint() {
        IPage<DemEnforceReportComplaint> page = new Page<>(1, 500);
        List<Map<String, Object>> list1 = new ArrayList<>();
        LambdaQueryWrapper<DemEnforceReportComplaint> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemEnforceReportComplaint::getReportAddress, DemEnforceReportComplaint::getLatitude,
                DemEnforceReportComplaint::getLongitude, DemEnforceReportComplaint::getId)
                .eq(DemEnforceReportComplaint::getEnable, 0);
        page = demEnforceReportComplaintService.page(page, queryWrapper);
        List<DemEnforceReportComplaint> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                if (StringUtils.isNotBlank(list.get(i).getLongitude()) && StringUtils.isNotBlank(list.get(i).getLatitude())) {
                    map.put("longitude", list.get(i).getLongitude());
                    map.put("latitude", list.get(i).getLatitude());
                    map.put("name", list.get(i).getReportAddress());
                    map.put("number", list.get(i).getReportPhone());
                } else {
                    List<LocateInfo> lonLat = OnlineMethod.getLonLat(list.get(i).getReportAddress());
                    if (CollectionUtils.isNotEmpty(lonLat)) {
                        map.put("longitude", lonLat.get(0).getLongitude());
                        map.put("latitude", lonLat.get(0).getLatitude());
                        map.put("name", list.get(i).getReportAddress());
                        map.put("number", list.get(i).getReportPhone());
                        DemEnforceReportComplaint demEnforceReportComplaint = new DemEnforceReportComplaint();
                        demEnforceReportComplaint.setId(list.get(i).getId());
                        demEnforceReportComplaint.setLatitude("" + lonLat.get(0).getLatitude());
                        demEnforceReportComplaint.setLongitude("" + lonLat.get(0).getLongitude());
                        demEnforceReportComplaintService.updateById(demEnforceReportComplaint);
                    }
                }
                if (map.get("longitude") != null && map.get("latitude") != null) {
                    if (StringUtils.isNotBlank(map.get("longitude").toString()) && StringUtils.isNotBlank(map.get("latitude").toString())) {
                        list1.add(map);
                    }
                }

            }
        }
        return list1;
    }

    @Override
    public List<String> inspectDeclare() {
        IPage<DemAtlInspectDeclare> page = new Page<>(1, 500);
        List<String> companyCodeList = new ArrayList<>();
        LambdaQueryWrapper<DemAtlInspectDeclare> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemAtlInspectDeclare::getDeclareCompanyCode)
                .eq(DemAtlInspectDeclare::getDeleteFlag, 0)
                .groupBy(DemAtlInspectDeclare::getDeclareCompanyCode);
        page = demAtlInspectDeclareService.page(page, queryWrapper);
        List<DemAtlInspectDeclare> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                companyCodeList.add(list.get(i).getDeclareCompanyCode());
            }
        }
        return companyCodeList;
    }

    @Override
    public List<String> lawStatistics() {
        IPage<DemLawStatistics> page = new Page<>(1, 500);
        List<String> companyCodeList = new ArrayList<>();
        LambdaQueryWrapper<DemLawStatistics> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(DemLawStatistics::getCompanyCode)
                .eq(DemLawStatistics::getDeleteFlag, 0)
                .groupBy(DemLawStatistics::getCompanyCode);
        page = demLawStatisticsService.page(page, queryWrapper);
        List<DemLawStatistics> list = page.getRecords();
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                companyCodeList.add(list.get(i).getCompanyCode());
            }
        }
        return companyCodeList;
    }

    @Override
    public List<Map<String, Object>> companyData(String streetTown, List<String> companyCodeList) {
        List<String> streetTownList = new ArrayList<>();
        streetTownList.add("华漕镇");
        streetTownList.add("新虹街道");
        streetTownList.add("七宝镇");
        streetTownList.add("虹桥镇");
        streetTownList.add("古美路街道");
        streetTownList.add("莘庄镇");
        streetTownList.add("梅陇镇");
        streetTownList.add("颛桥镇");
        streetTownList.add("马桥镇");
        streetTownList.add("江川路街道");
        streetTownList.add("吴泾镇");
        streetTownList.add("浦锦街道");
        streetTownList.add("浦江镇");
        List<Map<String, Object>> list = new ArrayList<>();
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName,
                BaseCompany::getCompanyType, BaseCompany::getLinkPhone, BaseCompany::getAddress, BaseCompany::getStreetTown)
                .eq(BaseCompany::getEnable, 0)
                .eq(StringUtils.isNotBlank(streetTown), BaseCompany::getStreetTown, streetTown)
                .in(CollectionUtils.isNotEmpty(companyCodeList), BaseCompany::getCompanyCode, companyCodeList);
        List<BaseCompany> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                companyIdList.add(list1.get(i).getCompanyId());
            }
            LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getCompanyId, BaseCompanyAttach1::getLongitude,
                    BaseCompanyAttach1::getLatitude)
                    .in(BaseCompanyAttach1::getCompanyId, companyIdList);
            List<BaseCompanyAttach1> list2 = baseCompanyAttach1Service.list(queryWrapper1);
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("number", list1.get(i).getCompanyId());
                map.put("name", list1.get(i).getCompanyName());
                map.put("longitude", null);
                map.put("latitude", null);
                map.put("streetTown", null);
                for (int j = 0; j < list2.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        String longitude = null;
                        String latitude = null;
                        if ((StringUtils.isBlank(list2.get(j).getLongitude()) && StringUtils.isNotBlank(list1.get(i).getAddress()))) {
                            List<LocateInfo> lonLat = OnlineMethod.getLonLat(list1.get(i).getAddress());
                            if (CollectionUtils.isNotEmpty(lonLat)) {
                                if (StringUtils.isBlank(list2.get(j).getLongitude()) && StringUtils.isBlank(list2.get(j).getLatitude())) {
                                    longitude = "" + lonLat.get(0).getLongitude();
                                    latitude = "" + lonLat.get(0).getLatitude();
                                } else {
                                    longitude = list2.get(j).getLongitude();
                                    latitude = list2.get(j).getLatitude();
                                }
                                if (StringUtils.isBlank(list1.get(i).getStreetTown())) {
                                    streetTown = lonLat.get(0).getStreet();
                                } else {
                                    streetTown = list1.get(i).getStreetTown();
                                }
                                this.updateCompany1(list1.get(i).getId(), list2.get(j).getId(), longitude, latitude, streetTown);
                            }
                        } else {
                            longitude = list2.get(j).getLongitude();
                            latitude = list2.get(j).getLatitude();
                            streetTown = list1.get(i).getStreetTown();
                        }
                        map.put("streetTown", streetTown);
                        map.put("longitude", longitude);
                        map.put("latitude", latitude);
                    }
                }
                if (map.get("longitude") != null && map.get("latitude") != null && map.get("streetTown") != null) {
                    String streetTown1 = map.get("streetTown").toString();
                    if (streetTownList.contains(streetTown1)) {
                        if (StringUtils.isNotBlank(streetTown)) {
                            if (streetTown.equals(streetTown1)) {
                                list.add(map);
                            }
                        } else {
                            list.add(map);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Override
    public List<Object> companyScatterer(String streetTown, String companyType) {
//        List<String> companyTypeList = new ArrayList<>();
//        if (StringUtils.isNotBlank(companyType)) {
//            String[] companyTypeList1 = companyType.split(",");
//            for (int i = 0; i < companyTypeList1.length; i++) {
//                companyTypeList.add(companyTypeList1[i]);
//            }
//        }
        List<String> streetTownList = new ArrayList<>();
        streetTownList.add("华漕镇");
        streetTownList.add("新虹街道");
        streetTownList.add("七宝镇");
        streetTownList.add("虹桥镇");
        streetTownList.add("古美路街道");
        streetTownList.add("莘庄镇");
        streetTownList.add("梅陇镇");
        streetTownList.add("颛桥镇");
        streetTownList.add("马桥镇");
        streetTownList.add("江川路街道");
        streetTownList.add("吴泾镇");
        streetTownList.add("浦锦街道");
        streetTownList.add("浦江镇");
        List<Object> list = new ArrayList<>();
        IPage<BaseCompany> page = new Page<>(1, 500);
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName,
                BaseCompany::getCompanyType, BaseCompany::getLinkPhone, BaseCompany::getAddress, BaseCompany::getStreetTown)
                .eq(BaseCompany::getEnable, 0)
                .eq(StringUtils.isNotBlank(streetTown), BaseCompany::getStreetTown, streetTown)
                .eq(StringUtils.isNotBlank(companyType), BaseCompany::getCompanyType, companyType);
        page = this.page(page, queryWrapper);
        List<BaseCompany> list1 = page.getRecords();
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                companyIdList.add(list1.get(i).getCompanyId());
            }
            LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
            queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getCompanyId, BaseCompanyAttach1::getLongitude,
                    BaseCompanyAttach1::getLatitude)
                    .in(BaseCompanyAttach1::getCompanyId, companyIdList);
            List<BaseCompanyAttach1> list2 = baseCompanyAttach1Service.list(queryWrapper1);
            for (int i = 0; i < list1.size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", list1.get(i).getId());
                map.put("companyName", list1.get(i).getCompanyName());
                String companyTypeName = null;
                if (StringUtils.isNotBlank(list1.get(i).getCompanyType())) {
                    if (list1.get(i).getCompanyType().equals("1")) {
                        companyTypeName = "重点单位";
                    } else if (list1.get(i).getCompanyType().equals("2")) {
                        companyTypeName = "一般单位";
                    } else if (list1.get(i).getCompanyType().equals("3")) {
                        companyTypeName = "九小场所";
                    } else if (list1.get(i).getCompanyType().equals("9")) {
                        companyTypeName = "其他单位";
                    }
                }

                map.put("companyTypeName", companyTypeName);
                map.put("companyId", list1.get(i).getCompanyId());
                map.put("linkPhone", list1.get(i).getLinkPhone());
                map.put("address", list1.get(i).getAddress());
                map.put("longitude", null);
                map.put("latitude", null);
                map.put("streetTown", null);
                for (int j = 0; j < list2.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        String longitude = null;
                        String latitude = null;
                        if ((StringUtils.isBlank(list2.get(j).getLongitude()) && StringUtils.isNotBlank(list1.get(i).getAddress()))) {
                            List<LocateInfo> lonLat = OnlineMethod.getLonLat(list1.get(i).getAddress());
                            if (CollectionUtils.isNotEmpty(lonLat)) {
                                if (StringUtils.isBlank(list2.get(j).getLongitude()) && StringUtils.isBlank(list2.get(j).getLatitude())) {
                                    longitude = "" + lonLat.get(0).getLongitude();
                                    latitude = "" + lonLat.get(0).getLatitude();
                                } else {
                                    longitude = list2.get(j).getLongitude();
                                    latitude = list2.get(j).getLatitude();
                                }
                                String location = longitude + "," + latitude;
                                String streetTown2 = OnlineMethod.getStreetTown(location);
                                if (StringUtils.isBlank(list1.get(i).getStreetTown())) {
                                    streetTown = streetTown2;
                                } else {
                                    streetTown = list1.get(i).getStreetTown();
                                }
                                this.updateCompany1(list1.get(i).getId(), list2.get(j).getId(), longitude, latitude, streetTown);
                            }
                        } else {
                            longitude = list2.get(j).getLongitude();
                            latitude = list2.get(j).getLatitude();
                            streetTown = list1.get(i).getStreetTown();
                        }
                        map.put("streetTown", streetTown);
                        map.put("longitude", longitude);
                        map.put("latitude", latitude);
                    }
                }
                if (map.get("longitude") != null && map.get("latitude") != null && map.get("streetTown") != null) {
                    String streetTown1 = map.get("streetTown").toString();
                    if (streetTownList.contains(streetTown1)) {
                        if (StringUtils.isNotBlank(streetTown)) {
                            if (streetTown.equals(map.get("streetTown").toString())) {
                                list.add(map);
                            }
                        } else {
                            list.add(map);
                        }
                    }
                }
            }
        }
        return list;
    }

    @Transactional
    @Override
    public void updateCompany1(Integer id1, Integer id2, String longitude, String latitude, String streetTown) {
        BaseCompany baseCompany = new BaseCompany();
        baseCompany.setId(id1);
        baseCompany.setStreetTown(streetTown);
        baseCompany.setUpdatePerson(SecurityUtils.getUsername());
        baseCompany.setUpdateTime(LocalDateTime.now());
        this.updateById(baseCompany);
        BaseCompanyAttach1 baseCompanyAttach1 = new BaseCompanyAttach1();
        baseCompanyAttach1.setId(id2);
        baseCompanyAttach1.setLongitude(longitude);
        baseCompanyAttach1.setLatitude(latitude);
        baseCompanyAttach1.setUpdatePerson(SecurityUtils.getUsername());
        baseCompanyAttach1.setUpdateTime(LocalDateTime.now());
        baseCompanyAttach1Service.updateById(baseCompanyAttach1);
    }

    @Override
    public int companyCount() {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(BaseCompany::getEnable, 0);
        int count = this.count(queryWrapper);
        return count;
    }

    @Override
    public List<String> companyIdList(String StreetTown) {
        List<String> companyIdList = new ArrayList<>();
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getCompanyId)
                .eq(BaseCompany::getEnable, 0)
                .eq(StringUtils.isNotBlank(StreetTown), BaseCompany::getStreetTown, StreetTown);
        List<BaseCompany> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            for (int i = 0; i < list.size(); i++) {
                companyIdList.add(list.get(i).getCompanyId());
            }
        }
        return companyIdList;
    }

    @Override
    public CommonPage<Map<String, Object>> fireInspectionData(String companyName, String organization, Integer id, Integer pageNum, Integer pageSize) {
        List<Map<String, Object>> list = new ArrayList<>();
        IPage<BaseCompany> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName,
                BaseCompany::getOrganization, BaseCompany::getAddress, BaseCompany::getCompanyType,
                BaseCompany::getLinkPhone)
                .eq(BaseCompany::getEnable, 0)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .like(StringUtils.isNotBlank(organization), BaseCompany::getOrganization, organization)
                .eq(id != 0 && id != null, BaseCompany::getId, id)
                .orderByDesc(BaseCompany::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < page.getRecords().size(); i++) {
                companyIdList.add(page.getRecords().get(i).getCompanyId());
            }
            List<BaseCompanyAttach1> list1 = new ArrayList<>();
            List<BaseCompanyPerson> list2 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getEmployeeNum, BaseCompanyAttach1::getBuildArea,
                        BaseCompanyAttach1::getCompanyId)
                        .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                list1 = baseCompanyAttach1Service.list(queryWrapper1);
                LambdaQueryWrapper<BaseCompanyPerson> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2.select(BaseCompanyPerson::getId, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getFireDutyName,
                        BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getCompanyId)
                        .in(BaseCompanyPerson::getCompanyId, companyIdList);
                list2 = baseCompanyPersonService.list(queryWrapper2);
            }
            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("companyId", page.getRecords().get(i).getCompanyId());
                map.put("companyName", page.getRecords().get(i).getCompanyName());
                map.put("organization", page.getRecords().get(i).getOrganization());
                map.put("address", page.getRecords().get(i).getAddress());
                map.put("companyType", page.getRecords().get(i).getCompanyType());
                map.put("linkPhone", page.getRecords().get(i).getLinkPhone());
                map.put("id1", null);
                map.put("id2", null);
                map.put("employeeNum", null);
                map.put("buildArea", null);
                map.put("delegateName", null);
                map.put("fireDutyName", null);
                map.put("fireManageName", null);
                for (int j = 0; j < list1.size(); j++) {
                    if (page.getRecords().get(i).getCompanyId().equals(list1.get(j).getCompanyId())) {
                        map.put("id1", list1.get(j).getId());
                        map.put("employeeNum", list1.get(j).getEmployeeNum());
                        map.put("buildArea", list1.get(j).getBuildArea());
                    }
                }
                for (int j = 0; j < list2.size(); j++) {
                    if (page.getRecords().get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        map.put("id2", list2.get(j).getId());
                        map.put("delegateName", list2.get(j).getDelegateName());
                        map.put("fireDutyName", list2.get(j).getFireDutyName());
                        map.put("fireManageName", list2.get(j).getFireManageName());
                    }
                }
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<BaseCompanyExportVO> fireInspectionDataExport(String companyName, String organization){
        List<BaseCompanyExportVO> list = new ArrayList<>();
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId, BaseCompany::getCompanyId, BaseCompany::getCompanyName,
                BaseCompany::getOrganization, BaseCompany::getAddress, BaseCompany::getCompanyType,
                BaseCompany::getLinkPhone)
                .eq(BaseCompany::getEnable, 0)
                .like(StringUtils.isNotBlank(companyName), BaseCompany::getCompanyName, companyName)
                .like(StringUtils.isNotBlank(organization), BaseCompany::getOrganization, organization)
                .orderByDesc(BaseCompany::getId);
        List<BaseCompany> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                companyIdList.add(list1.get(i).getCompanyId());
            }
            List<BaseCompanyAttach1> list2 = new ArrayList<>();
            List<BaseCompanyPerson> list3 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getEmployeeNum, BaseCompanyAttach1::getBuildArea,
                        BaseCompanyAttach1::getCompanyId)
                        .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                list2 = baseCompanyAttach1Service.list(queryWrapper1);
                LambdaQueryWrapper<BaseCompanyPerson> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2.select(BaseCompanyPerson::getId, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getFireDutyName,
                        BaseCompanyPerson::getFireManageName, BaseCompanyPerson::getCompanyId)
                        .in(BaseCompanyPerson::getCompanyId, companyIdList);
                list3 = baseCompanyPersonService.list(queryWrapper2);
            }
            for (int i = 0; i < list1.size(); i++) {
                BaseCompanyExportVO baseCompanyExportVO = new BaseCompanyExportVO();
                baseCompanyExportVO.setCompanyName(list1.get(i).getCompanyName());
                baseCompanyExportVO.setOrganization(list1.get(i).getOrganization());
                baseCompanyExportVO.setDelegateName(null);
                baseCompanyExportVO.setAddress(list1.get(i).getAddress());
                //单位类型（1：重点单位、2：一般单位、3：九小场所、9：其他单位）
                String companyTypeName = "";
                if (StringUtils.isNotBlank(list1.get(i).getCompanyType())) {
                    if (list1.get(i).getCompanyType().equals("1")) {
                        companyTypeName = "重点单位";
                    } else if (list1.get(i).getCompanyType().equals("2")) {
                        companyTypeName = "一般单位";
                    } else if (list1.get(i).getCompanyType().equals("3")) {
                        companyTypeName = "九小场所";
                    } else if (list1.get(i).getCompanyType().equals("9")) {
                        companyTypeName = "其他单位";
                    }
                }
                baseCompanyExportVO.setCompanyType(companyTypeName);
                baseCompanyExportVO.setLinkPhone(list1.get(i).getLinkPhone());
                baseCompanyExportVO.setFireDutyName(null);
                baseCompanyExportVO.setFireManageName(null);
                baseCompanyExportVO.setEmployeeNum(null);
                baseCompanyExportVO.setBuildArea(null);
                for (int j = 0; j < list2.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        baseCompanyExportVO.setEmployeeNum(list2.get(j).getEmployeeNum());
                        baseCompanyExportVO.setBuildArea(list2.get(j).getBuildArea());
                    }
                }
                for (int j = 0; j < list3.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list3.get(j).getCompanyId())) {
                        baseCompanyExportVO.setDelegateName(list3.get(j).getDelegateName());
                        baseCompanyExportVO.setFireDutyName(list3.get(j).getFireDutyName());
                        baseCompanyExportVO.setFireManageName(list3.get(j).getFireManageName());
                    }
                }
                list.add(baseCompanyExportVO);
            }
        }

        return list;
    }

    @Transactional
    @Override
    public void addBaseCompany(CompanyAddVO companyAddVO) {
        BaseCompany baseCompany = companyAddVO.getBaseCompany();
        BaseCompanyAttach1 baseCompanyAttach1 = companyAddVO.getBaseCompanyAttach1();
        BaseCompanyPerson baseCompanyPerson = companyAddVO.getBaseCompanyPerson();
        List<BaseBuildAddVO> list;
        list = companyAddVO.getBaseBuildList();
        if (StringUtils.isNotBlank(baseCompany.getCompanyId())) {
            LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.select(BaseCompany::getCompanyId,BaseCompany::getOrganization,BaseCompany::getCompanyName,
                    BaseCompany::getAddress,BaseCompany::getFoundTime,BaseCompany::getCompanyNature, BaseCompany::getStreetTown)
                    .eq(BaseCompany::getCompanyId, baseCompany.getCompanyId())
                    .eq(BaseCompany::getEnable, 0)
                    .orderByDesc(BaseCompany::getId);
            List<BaseCompany> list1 = this.list(queryWrapper);
            if (CollectionUtils.isNotEmpty(list1)) {
                List<String> companyIdList = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    companyIdList.add(list1.get(i).getCompanyId());
                }
                List<BaseCompanyAttach1> list2 = new ArrayList<>();
                List<BaseCompanyPerson> list3 = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(companyIdList)) {
                    LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
                    queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getEmployeeNum,
                            BaseCompanyAttach1::getBuildArea, BaseCompanyAttach1::getCoverArea, BaseCompanyAttach1::getFixedAssets,
                            BaseCompanyAttach1::getCompanyId,BaseCompanyAttach1::getBirthCert)
                            .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                    list2 = baseCompanyAttach1Service.list(queryWrapper1);
                    LambdaQueryWrapper<BaseCompanyPerson> queryWrapper2 = Wrappers.lambdaQuery();
                    queryWrapper2.select(BaseCompanyPerson::getId, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getFireDutyName,
                            BaseCompanyPerson::getFireManageName,BaseCompanyPerson::getFireManagePhone,
                            BaseCompanyPerson::getCompanyId)
                            .in(BaseCompanyPerson::getCompanyId, companyIdList);
                    list3 = baseCompanyPersonService.list(queryWrapper2);
                }
                for (int i = 0; i < list1.size(); i++) {
                    BaseCompanyChange baseCompanyChange = new BaseCompanyChange();
                    baseCompanyChange.setCompanyId(list1.get(i).getCompanyId());
                    baseCompanyChange.setCompanyNature(list1.get(i).getCompanyNature());
                    baseCompanyChange.setCompanyName(list1.get(i).getCompanyName());
                    baseCompanyChange.setOrganization(list1.get(i).getOrganization());
                    baseCompanyChange.setAddress(list1.get(i).getAddress());
                    baseCompanyChange.setFoundTime(list1.get(i).getFoundTime());
                    baseCompanyChange.setStreetTown(list1.get(i).getStreetTown());
                    for (int j = 0; j < list2.size(); j++) {
                        if (list1.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                            baseCompanyChange.setEmployeeNum(list2.get(j).getEmployeeNum());
                            baseCompanyChange.setCoverArea(list2.get(j).getCoverArea());
                            baseCompanyChange.setBuildArea(list2.get(j).getBuildArea());
                            baseCompanyChange.setFixedAssets(list2.get(j).getFixedAssets());
                            baseCompanyChange.setBirthCert(list2.get(j).getBirthCert());
                        }
                    }
                    for (int j = 0; j < list3.size(); j++) {
                        if (list1.get(i).getCompanyId().equals(list3.get(j).getCompanyId())) {
                            baseCompanyChange.setDelegateName(list3.get(j).getDelegateName());
                            baseCompanyChange.setFireDutyName(list3.get(j).getFireDutyName());
                            baseCompanyChange.setFireManageName(list3.get(j).getFireManageName());
                            baseCompanyChange.setFireManagePhone(list3.get(j).getFireManagePhone());
                        }
                    }
                    baseCompanyChangeService.baseCompanyChangeAdd(baseCompanyChange);
                }
            }
            this.updateById(baseCompany);
            baseCompanyAttach1Service.updateById(baseCompanyAttach1);
            baseCompanyPersonService.updateById(baseCompanyPerson);
            if (CollectionUtils.isNotEmpty(list)) {
                for (int j = 0; j < list.size(); j++) {
                    BaseBuild baseBuild = list.get(j).getBaseBuild();
                    BaseBuildAttach baseBuildAttach = list.get(j).getBaseBuildAttach();
                    List<BaseBuildExtinguish> list4 = list.get(j).getBaseBuildExtinguishList();
                    if (null!=baseBuild.getId()) {
                        baseBuild.setUpdateTime(LocalDateTime.now());
                        baseBuild.setDeleteFlag("0");
                        baseBuildService.updateById(baseBuild);
                        if (null!=baseBuildAttach.getId()) {
                            baseBuildAttachService.updateById(baseBuildAttach);
                        }else {
                            baseBuildAttach.setBuildId(baseBuild.getId());
                            baseBuildAttachService.save(baseBuildAttach);
                        }
                        if (CollectionUtils.isNotEmpty(list4)) {
                            for (int k = 0; k < list4.size(); k++) {
                                BaseBuildExtinguish baseBuildExtinguish = list4.get(k);
                                if (null!=baseBuildExtinguish.getId()) {
                                    baseBuildExtinguishService.updateById(baseBuildExtinguish);
                                }else {
                                    baseBuildExtinguish.setBuildId(baseBuild.getId());
                                    baseBuildExtinguishService.save(baseBuildExtinguish);
                                }

                            }
                        }
                    }else {
                        baseBuild.setCreateTime(LocalDateTime.now());
                        baseBuild.setCreator("123");
                        baseBuild.setCompanyId(baseCompany.getCompanyId());
                        baseBuild.setDeleteFlag("0");
                        baseBuildService.save(baseBuild);
                        Integer buildId = baseBuild.getId();
                        if (null!=baseBuildAttach){
                            baseBuildAttach.setBuildId(buildId);
                            baseBuildAttachService.save(baseBuildAttach);
                        }
                        if (CollectionUtils.isNotEmpty(list4)) {
                            for (int k = 0; k < list4.size(); k++) {
                                BaseBuildExtinguish baseBuildExtinguish = list4.get(k);
                                baseBuildExtinguish.setBuildId(buildId);
                                baseBuildExtinguishService.save(baseBuildExtinguish);
                            }
                        }
                    }
                }
            }
        }else {
            UUID uuid = UUID.randomUUID();
            String uuidStr = uuid.toString();
            baseCompany.setCompanyId(uuidStr.replaceAll("-",""));
            baseCompany.setCompanyType("1");
            baseCompany.setEnable("0");
            baseCompany.setCreateTime(LocalDateTime.now());
            baseCompany.setCreator(companyAddVO.getBaseCompanyPerson().getFireManageName());
            this.save(baseCompany);
            baseCompanyAttach1.setCompanyId(uuidStr.replaceAll("-",""));
            baseCompanyAttach1Service.save(baseCompanyAttach1);
            baseCompanyPerson.setCompanyId(uuidStr.replaceAll("-",""));
            baseCompanyPersonService.save(baseCompanyPerson);
            if (CollectionUtils.isNotEmpty(list)) {
                for (int j = 0; j < list.size(); j++) {
                    BaseBuild baseBuild = list.get(j).getBaseBuild();
                    BaseBuildAttach baseBuildAttach = list.get(j).getBaseBuildAttach();
                    List<BaseBuildExtinguish> list4 = list.get(j).getBaseBuildExtinguishList();
                    baseBuild.setCreateTime(LocalDateTime.now());
                    baseBuild.setCreator(companyAddVO.getBaseCompanyPerson().getFireManageName());
                    baseBuild.setCompanyId(uuidStr.replaceAll("-",""));
                    baseBuild.setDeleteFlag("0");
                    baseBuildService.save(baseBuild);
                    Integer buildId = baseBuild.getId();
                    baseBuildAttach.setBuildId(buildId);
                    baseBuildAttachService.save(baseBuildAttach);
                    if (CollectionUtils.isNotEmpty(list4)) {
                        for (int k = 0; k < list4.size(); k++) {
                            BaseBuildExtinguish baseBuildExtinguish = list4.get(k);
                            baseBuildExtinguish.setBuildId(buildId);
                            baseBuildExtinguishService.save(baseBuildExtinguish);
                        }
                    }
                }
            }
        }
    }

    @Override
    public List<CompanyDataVo> companyByNameSelect(String companyName) {
        List<CompanyDataVo> list = new ArrayList<>();
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId,BaseCompany::getCompanyId,BaseCompany::getOrganization,BaseCompany::getCompanyName,
                BaseCompany::getAddress,BaseCompany::getFoundTime,BaseCompany::getStreetTown)
                .like(StringUtils.isNotBlank(companyName),BaseCompany::getCompanyName, companyName)
                .eq(BaseCompany::getEnable, 0)
                .orderByDesc(BaseCompany::getId).last(" limit 10");
        List<BaseCompany> list1 = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list1)) {
            List<String> companyIdList = new ArrayList<>();
            for (int i = 0; i < list1.size(); i++) {
                companyIdList.add(list1.get(i).getCompanyId());
            }
            List<BaseCompanyAttach1> list2 = new ArrayList<>();
            List<BaseCompanyPerson> list3 = new ArrayList<>();
            List<BaseBuild> list4 = new ArrayList<>();
            List<BaseBuildAttach> list5 = new ArrayList<>();
            List<BaseBuildExtinguish> list6 = new ArrayList<>();
            if (CollectionUtils.isNotEmpty(companyIdList)) {
                LambdaQueryWrapper<BaseCompanyAttach1> queryWrapper1 = Wrappers.lambdaQuery();
                queryWrapper1.select(BaseCompanyAttach1::getId, BaseCompanyAttach1::getEmployeeNum,
                        BaseCompanyAttach1::getBuildArea, BaseCompanyAttach1::getCoverArea, BaseCompanyAttach1::getFixedAssets,
                        BaseCompanyAttach1::getCompanyId,BaseCompanyAttach1::getBirthCert)
                        .in(BaseCompanyAttach1::getCompanyId, companyIdList);
                list2 = baseCompanyAttach1Service.list(queryWrapper1);
                LambdaQueryWrapper<BaseCompanyPerson> queryWrapper2 = Wrappers.lambdaQuery();
                queryWrapper2.select(BaseCompanyPerson::getId, BaseCompanyPerson::getDelegateName, BaseCompanyPerson::getFireDutyName,
                        BaseCompanyPerson::getFireManageName,BaseCompanyPerson::getFireManagePhone,
                        BaseCompanyPerson::getCompanyId)
                        .in(BaseCompanyPerson::getCompanyId, companyIdList);
                list3 = baseCompanyPersonService.list(queryWrapper2);
                LambdaQueryWrapper<BaseBuild> queryWrapper3 = Wrappers.lambdaQuery();
                queryWrapper3.select(BaseBuild::getId, BaseBuild::getBuildName, BaseBuild::getAddress,
                        BaseBuild::getBuildStructure,BaseBuild::getAboveFloor,BaseBuild::getUnderFloor,
                        BaseBuild::getBuildArea,BaseBuild::getUnderSpace,BaseBuild::getUseCharacter,
                        BaseBuild::getCompleteYear,BaseBuild::getBuildHigh,BaseBuild::getFireFacilitySystem,
                        BaseBuild::getOtherSystem,BaseBuild::getOtherFacilities,BaseBuild::getFireproofCoat,
                        BaseBuild::getCompanyId)
                        .in(BaseBuild::getCompanyId, companyIdList)
                        .eq(BaseBuild::getDeleteFlag, 0);
                list4 = baseBuildService.list(queryWrapper3);
                List<Integer> buildIdList = new ArrayList<>();
                List<Integer> buildIdList1 = new ArrayList<>();
                if (CollectionUtils.isNotEmpty(list4)) {
                    for (int i = 0; i < list4.size(); i++) {
                        if (19==list4.get(i).getUseCharacter() || 20==list4.get(i).getUseCharacter()){
                            buildIdList.add(list4.get(i).getId());
                        }
                        buildIdList1.add(list4.get(i).getId());
                    }
                }
                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);
                }
                if (CollectionUtils.isNotEmpty(buildIdList1)) {
                    LambdaQueryWrapper<BaseBuildExtinguish> queryWrapper5 = Wrappers.lambdaQuery();
                    queryWrapper5.select(BaseBuildExtinguish::getId, BaseBuildExtinguish::getBuildId,
                            BaseBuildExtinguish::getExtinguishNum,
                            BaseBuildExtinguish::getExtinguishType)
                            .in(BaseBuildExtinguish::getBuildId, buildIdList1)
                            .eq(BaseBuildExtinguish::getDeleteFlag, 0);;
                    list6 = baseBuildExtinguishService.list(queryWrapper5);
                }
            }
            for (int i = 0; i < list1.size(); i++) {
                CompanyDataVo companyDataVo = new CompanyDataVo();
                companyDataVo.setId(list1.get(i).getId());
                companyDataVo.setCompanyId(list1.get(i).getCompanyId());
                companyDataVo.setCompanyName(list1.get(i).getCompanyName());
                companyDataVo.setOrganization(list1.get(i).getOrganization());
                companyDataVo.setAddress(list1.get(i).getAddress());
                companyDataVo.setFoundTime(list1.get(i).getFoundTime());
                companyDataVo.setStreetTown(list1.get(i).getStreetTown());
                for (int j = 0; j < list2.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list2.get(j).getCompanyId())) {
                        companyDataVo.setId1(list2.get(j).getId());
                        companyDataVo.setEmployeeNum(list2.get(j).getEmployeeNum());
                        companyDataVo.setCoverArea(list2.get(j).getCoverArea());
                        companyDataVo.setBuildArea(list2.get(j).getBuildArea());
                        companyDataVo.setFixedAssets(list2.get(j).getFixedAssets());
                        companyDataVo.setBirthCert(list2.get(j).getBirthCert());
                    }
                }
                for (int j = 0; j < list3.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list3.get(j).getCompanyId())) {
                        companyDataVo.setId2(list3.get(j).getId());
                        companyDataVo.setDelegateName(list3.get(j).getDelegateName());
                        companyDataVo.setFireDutyName(list3.get(j).getFireDutyName());
                        companyDataVo.setFireManageName(list3.get(j).getFireManageName());
                        companyDataVo.setFireManagePhone(list3.get(j).getFireManagePhone());
                    }
                }
                List<BaseBuildAddVO> list7 = new ArrayList<>();
                for (int j = 0; j < list4.size(); j++) {
                    if (list1.get(i).getCompanyId().equals(list4.get(j).getCompanyId())) {
                        List<BaseBuildExtinguish> list8 = new ArrayList<>();
                        BaseBuildAddVO baseBuildAddVO = new BaseBuildAddVO();
                        baseBuildAddVO.setBaseBuild(list4.get(j));
                        for (int k = 0; k < list5.size(); k++) {
                            if (list4.get(j).getId().equals(list5.get(k).getBuildId())){
                                baseBuildAddVO.setBaseBuildAttach(list5.get(k));
                            }
                        }
                        for (int l = 0; l < list6.size(); l++) {
                            if (list4.get(j).getId().equals(list6.get(l).getBuildId())){
                                list8.add(list6.get(l));
                                baseBuildAddVO.setBaseBuildExtinguishList(list8);
                            }
                        }
                        list7.add(baseBuildAddVO);
                        companyDataVo.setBaseBuildList(list7);
                    }
                }
                list.add(companyDataVo);
            }
        }
        return list;
    }

    @Override
    public List<BaseCompany> companyIdByTypeList(String companyType) {
        LambdaQueryWrapper<BaseCompany> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(BaseCompany::getId,BaseCompany::getCompanyId, BaseCompany::getCompanyCode,
                BaseCompany::getCompanyName)
                .eq(BaseCompany::getEnable, 0)
                .eq(StringUtils.isNotBlank(companyType), BaseCompany::getCompanyType, companyType);
        List<BaseCompany> list = this.list(queryWrapper);
        return list;
    }
}



