package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.page.TableDataInfo;
import com.dsk.common.utils.StringUtils;
import com.dsk.cscec.constant.QueryConstants;
import com.dsk.cscec.domain.DProject;
import com.dsk.cscec.domain.SysAdvisoryBody;
import com.dsk.cscec.domain.bo.AdvisoryBodySearchBo;
import com.dsk.cscec.domain.bo.ProjectSearchBo;
import com.dsk.cscec.domain.vo.AdvisoryBodySearchVo;
import com.dsk.cscec.domain.vo.ProjectSearchVo;
import com.dsk.cscec.mapper.DProjectMapper;
import com.dsk.cscec.mapper.SysAdvisoryBodyMapper;
import com.dsk.cscec.service.IDProjectService;
import com.dsk.jsk.domain.EnterpriseInfoHeaderBody;
import com.dsk.system.utils.DskOpenApiUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 咨询机构管理
 *
 * @author makejava
 * @since 2023-12-10 15:34:51
 */
@Slf4j
@Service("dProjectService")
public class IDProjectServiceImpl extends ServiceImpl<DProjectMapper, DProject> implements IDProjectService {
    @Resource
    private DProjectMapper baseMapper;
    @Resource
    private SysAdvisoryBodyMapper advisoryBodyMapper;
    @Resource
    private DskOpenApiUtil dskOpenApiUtil;

    /**
     * 获取所有项目列表数据
     *
     * @param projectSearchBo 查询实体
     * @param pageQuery       分页对象
     * @return 所有数据
     */
    @Override
    public TableDataInfo<ProjectSearchVo> queryProjectList(ProjectSearchBo projectSearchBo, PageQuery pageQuery) {
        //根据咨询机构名称查询对应的项目IDs
        String advisoryBodyName = projectSearchBo.getAdvisoryBodyName();
        List<Long> projectIds = new ArrayList<>();
        if (StringUtils.isNotBlank(advisoryBodyName)) {
            for (SysAdvisoryBody sysAdvisoryBody : advisoryBodyMapper.selectList(new LambdaQueryWrapper<SysAdvisoryBody>()
                    .like(SysAdvisoryBody::getAdvisoryBodyName, advisoryBodyName))) {
                if (ObjectUtil.isNotNull(sysAdvisoryBody.getProjectKey())) {
                    projectIds.add(sysAdvisoryBody.getProjectKey());
                }
            }
        }

        Page<ProjectSearchVo> page = baseMapper.selectPageProjectList(pageQuery.build(), this.buildProjectQueryWrapper(projectSearchBo, projectIds));

        //补充咨询机构信息
        for (ProjectSearchVo projectSearchVo : page.getRecords()) {
            SysAdvisoryBody advisoryBody = this.getAdvisoryBodyByProjectKey(projectSearchVo.getProjectKey());
            if (ObjectUtil.isNotNull(advisoryBody)) {
                projectSearchVo.setAdvisoryBody(advisoryBody);
            }
        }
        return TableDataInfo.build(page);
    }

    private Wrapper<DProject> buildProjectQueryWrapper(ProjectSearchBo projectSearchBo, List<Long> projectIds) {
        QueryWrapper<DProject> wrapper = Wrappers.query();
        //咨询机构的项目数据取中建一局集团第二建筑有限公司和他的下级组织
        wrapper
                //承包单位ID
                .eq("p.contract_org_id", QueryConstants.LEVEL1_COMPANY_ID)
                //承包单位主键(字段叫主键,但并不是主键)
                .eq("p.contract_org_name", QueryConstants.LEVEL1_COMPANY_NAME)
                //项目名称
                .like(StringUtils.isNotBlank(projectSearchBo.getProjectName()), "p.project_name", projectSearchBo.getProjectName())
                //根据咨询机构名称查出来对应的项目ID
                .in(!projectIds.isEmpty(), "p.project_id", projectIds)
                //业主单位
                .like(StringUtils.isNotBlank(projectSearchBo.getOwnerUnit()), "p.owner_name", projectSearchBo.getOwnerUnit())
                //项目创建时间(字段去的合同签约日期)
                .between(ObjectUtil.isNotNull(projectSearchBo.getProjectStartTime()) && ObjectUtil.isNotNull(projectSearchBo.getProjectEndTime()),
                        "p.sign_date",
                        projectSearchBo.getProjectStartTime(),
                        projectSearchBo.getProjectEndTime())
                //项目承接类型下拉选项
                .eq(StringUtils.isNotBlank(projectSearchBo.getProjectUndertakeType()), "p.isinvestproject", projectSearchBo.getProjectUndertakeType())
                //工程基础大类下拉选项
                .eq(StringUtils.isNotBlank(projectSearchBo.getEngineeringFoundationCategory()), "p.project_type1", projectSearchBo.getEngineeringFoundationCategory())
                //工程类别明细
                .eq(StringUtils.isNotBlank(projectSearchBo.getEngineeringCategoryDetail()), "p.project_type", projectSearchBo.getEngineeringCategoryDetail())
                //合同金额
                .between(ObjectUtil.isNotNull(projectSearchBo.getMinContractAmount()) && ObjectUtil.isNotNull(projectSearchBo.getMaxContractAmount()),
                        "p.contract_orig_value",
                        projectSearchBo.getMinContractAmount(),
                        projectSearchBo.getMaxContractAmount())
                .orderByDesc("p.load_time");
        return wrapper;
    }

    /**
     * 根据项目ID查询咨询机构
     *
     * @param projectKey 项目主键
     * @return 咨询机构
     */
    private SysAdvisoryBody getAdvisoryBodyByProjectKey(Long projectKey) {
        return advisoryBodyMapper.selectOne(new LambdaQueryWrapper<SysAdvisoryBody>()
                .eq(SysAdvisoryBody::getProjectKey, projectKey));
    }

    /**
     * 获取所有企业列表(咨询机构)数据
     *
     * @param advisoryBodySearchBo 查询实体
     * @param pageQuery            分页对象
     * @return 所有数据
     */
    @Override
    public TableDataInfo<AdvisoryBodySearchVo> queryAdvisoryBodyList(AdvisoryBodySearchBo advisoryBodySearchBo, PageQuery pageQuery) {
        //先按照分页要求从咨询机构表中查出分页数据
        QueryWrapper<SysAdvisoryBody> wrapper = Wrappers.query();
        Page<AdvisoryBodySearchVo> page = advisoryBodyMapper.selectPageAdvisoryBodyList(pageQuery.build(), wrapper);
        //根据咨询机构cid查询对应的企业信息
        for (AdvisoryBodySearchVo advisoryBodySearchVo : page.getRecords()) {
            EnterpriseInfoHeaderBody infoHeaderBody = new EnterpriseInfoHeaderBody();
            infoHeaderBody.setCompanyId(Math.toIntExact(advisoryBodySearchVo.getAdvisoryBodyCid()));
            Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(infoHeaderBody, false, false));
            Map companyData = MapUtils.getMap(companyMap, "data", null);
            //最近一次合作时间
            advisoryBodySearchVo.setLastCooperateTime(advisoryBodyMapper.selectLastCooperateTime(advisoryBodySearchVo.getAdvisoryBodyId()));
            advisoryBodySearchVo.setBusinessStatus(MapUtils.getString(companyData, "businessStatus", ""));
            advisoryBodySearchVo.setCorporatePerson(MapUtils.getString(companyData, "corporatePerson", ""));
            String provinceName = MapUtils.getString(companyData, "provinceName", "");
            String cityName = MapUtils.getString(companyData, "cityName", "");
            String districtName = MapUtils.getString(companyData, "districtName", "");
            advisoryBodySearchVo.setRegCapital(provinceName + cityName + districtName);
            advisoryBodySearchVo.setRegArea(MapUtils.getString(companyData, "regArea", ""));
            advisoryBodySearchVo.setRegisteredDate(MapUtils.getString(companyData, "registeredDate", ""));
            advisoryBodySearchVo.setBusinessScope(MapUtils.getString(companyData, "businessScope", ""));
            //合作项目数量
            advisoryBodySearchVo.setCooperateProjectCount(advisoryBodyMapper.selectCount(new LambdaQueryWrapper<SysAdvisoryBody>()
                    .eq(SysAdvisoryBody::getAdvisoryBodyId, advisoryBodySearchVo.getAdvisoryBodyId())));
        }
        return TableDataInfo.build(page);
    }
}

