package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.domain.AdvisoryBody;
import com.dsk.cscec.domain.AdvisoryBodyProject;
import com.dsk.cscec.domain.bo.AdvisoryBodySearchBo;
import com.dsk.cscec.domain.vo.AdvisoryBodySearchVo;
import com.dsk.cscec.mapper.AdvisoryBodyMapper;
import com.dsk.cscec.mapper.AdvisoryBodyProjectMapper;
import com.dsk.cscec.service.AdvisoryBodyService;
import com.dsk.jsk.domain.EnterpriseInfoHeaderBody;
import com.dsk.system.utils.DskOpenApiUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * 咨询机构(SysAdvisoryBody)表服务实现类
 *
 * @author sxk
 * @since 2023-12-12 10:12:09
 */
@Service("advisoryBodyService")
public class AdvisoryBodyServiceImpl extends ServiceImpl<AdvisoryBodyMapper, AdvisoryBody> implements AdvisoryBodyService {
    @Resource
    private AdvisoryBodyMapper baseMapper;
    @Resource
    private DskOpenApiUtil dskOpenApiUtil;
    @Resource
    private AdvisoryBodyProjectMapper advisoryBodyProjectMapper;

    /**
     * 获取所有企业列表(咨询机构)数据
     *
     * @param advisoryBodySearchBo 查询实体
     * @param pageQuery            分页对象
     * @return 所有数据
     */
    @Override
    public TableDataInfo<AdvisoryBodySearchVo> queryAdvisoryBodyList(AdvisoryBodySearchBo advisoryBodySearchBo, PageQuery pageQuery) {
        //先按照分页要求从咨询机构表中查出分页数据
        QueryWrapper<AdvisoryBody> wrapper = Wrappers.query();
        wrapper.like(StringUtils.isNotBlank(advisoryBodySearchBo.getAdvisoryBodyName()), "advisory_body_name", advisoryBodySearchBo.getAdvisoryBodyName())
                .between(ObjectUtil.isNotNull(advisoryBodySearchBo.getMinLastCooperateDate()) && ObjectUtil.isNotNull(advisoryBodySearchBo.getMaxLastCooperateDate()),
                        "create_time",
                        advisoryBodySearchBo.getMinLastCooperateDate(),
                        advisoryBodySearchBo.getMaxLastCooperateDate())
                .like(StringUtils.isNotBlank(advisoryBodySearchBo.getBusinessScope()), "business_scope", advisoryBodySearchBo.getBusinessScope());
        Page<AdvisoryBodySearchVo> page = baseMapper.selectPageAdvisoryBodyList(pageQuery.build(), wrapper);

        //根据咨询机构cid查询对应的企业信息
        for (AdvisoryBodySearchVo advisoryBodySearchVo : page.getRecords()) {
            EnterpriseInfoHeaderBody infoHeaderBody = new EnterpriseInfoHeaderBody();
            //有Cid的才去建设库查数据
            if (ObjectUtil.isNotNull(advisoryBodySearchVo.getAdvisoryBodyCid())) {
                infoHeaderBody.setCompanyId(Math.toIntExact(advisoryBodySearchVo.getAdvisoryBodyCid()));
            } else {
                continue;
            }
            Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(infoHeaderBody, false, false));
            Map companyData = MapUtils.getMap(companyMap, "data", null);
            //最近一次合作时间
            List<AdvisoryBodyProject> advisoryBodyProjectList = advisoryBodyProjectMapper.selectList(new LambdaQueryWrapper<AdvisoryBodyProject>()
                    .select(AdvisoryBodyProject::getCreateTime)
                    .eq(AdvisoryBodyProject::getAdvisoryBodyCid, advisoryBodySearchVo.getAdvisoryBodyCid())
                    .orderByDesc(AdvisoryBodyProject::getCreateTime));
            if (ObjectUtil.isNotNull(advisoryBodyProjectList) && !advisoryBodyProjectList.isEmpty()) {
                advisoryBodySearchVo.setLastCooperateTime(advisoryBodyProjectList.get(0).getCreateTime());
            }
            //经营范围
            advisoryBodySearchVo.setBusinessStatus(MapUtils.getString(companyData, "businessStatus", ""));
            //法人
            advisoryBodySearchVo.setCorporatePerson(MapUtils.getString(companyData, "corporatePerson", ""));
            //注册资本
            advisoryBodySearchVo.setRegCapital(MapUtils.getString(companyData, "regCapital", ""));
            //拼接地区
            String provinceName = MapUtils.getString(companyData, "provinceName", "");
            String cityName = MapUtils.getString(companyData, "cityName", "");
            String districtName = MapUtils.getString(companyData, "districtName", "");
            advisoryBodySearchVo.setRegArea(provinceName + "-" + cityName + "-" + districtName);
            //成立日期
            advisoryBodySearchVo.setRegisteredDate(MapUtils.getString(companyData, "registeredDate", ""));
            //经营范围
            advisoryBodySearchVo.setBusinessScope(MapUtils.getString(companyData, "businessScope", ""));
            //合作项目数量
            advisoryBodySearchVo.setCooperateProjectCount(advisoryBodyProjectMapper.selectCount(new LambdaQueryWrapper<AdvisoryBodyProject>()
                    .eq(AdvisoryBodyProject::getAdvisoryBodyCid, advisoryBodySearchVo.getAdvisoryBodyCid())));
        }
        return TableDataInfo.build(page);
    }
}

