package com.dsk.cscec.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
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.exception.ServiceException;
import com.dsk.common.utils.StringUtils;
import com.dsk.cscec.domain.AdvisoryBody;
import com.dsk.cscec.domain.AdvisoryBodyProject;
import com.dsk.cscec.domain.bo.AddAdvisoryBodyBo;
import com.dsk.cscec.domain.bo.AdvisoryBodySearchBo;
import com.dsk.cscec.domain.vo.AdvisoryBodyExistVo;
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 org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
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);
    }

    /**
     * 校验咨询机构是否存在
     *
     * @param advisoryBodyName 咨询机构名称
     * @return 校验结果
     */
    @Override
    public AdvisoryBodyExistVo checkAdvisoryBodyExist(String advisoryBodyName) {
        //先从咨询机构表查询是否存在,存在则直接返回
        AdvisoryBody advisoryBody = baseMapper.selectOne(new LambdaQueryWrapper<AdvisoryBody>()
                .eq(AdvisoryBody::getAdvisoryBodyName, advisoryBodyName));
        if (ObjectUtil.isNotNull(advisoryBody)) {
            //咨询机构表中有记录,则设置为非新咨询机构
            AdvisoryBodyExistVo advisoryBodyExistVo = BeanUtil.toBean(advisoryBody, AdvisoryBodyExistVo.class);
            advisoryBodyExistVo.setIsNewAdvisoryBody(false);
            return advisoryBodyExistVo;
        } else {
            Map<String, Object> params = new HashMap<>();
            params.put("keyword", advisoryBodyName);
            Map jskData = MapUtils.getMap(dskOpenApiUtil.requestBody("/nationzj/enterprice/index", params), "data", null);
            //防止没有数据而导致强转错误,所以先判断下total
            if (MapUtils.getInteger(jskData, "total", 0) > 0) {
                List<Map<String, Object>> data = (List<Map<String, Object>>) jskData.get("list");
                for (Map<String, Object> companyData : data) {
                    //企业名称完全匹配上,则直接返回给前端
                    String companyName = StringUtils.removeRed(MapUtils.getString(companyData, "name","NotExist"));
                    if (advisoryBodyName.equals(companyName)) {
                        AdvisoryBodyExistVo advisoryBodyExistVo = new AdvisoryBodyExistVo();
                        advisoryBodyExistVo.setAdvisoryBodyCid(Math.toIntExact(MapUtils.getLong(companyData, "jskEid")));
                        advisoryBodyExistVo.setAdvisoryBodyName(companyName);
                        advisoryBodyExistVo.setBusinessScope(MapUtils.getString(companyData, "businessScope"));
                        advisoryBodyExistVo.setIsNewAdvisoryBody(true);
                        return advisoryBodyExistVo;
                    }
                }
                //查不到则抛异常
                throw new ServiceException("咨询机构不存在或结果不唯一,请输入完整咨询机构企业名称");
            }
        }
        return null;
    }

    /**
     * 新增咨询机构
     *
     * @param addAdvisoryBodyBo 新增信息
     * @return 添加结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addAdvisoryBody(AddAdvisoryBodyBo addAdvisoryBodyBo) {
        long advisoryBodyId = IdUtil.getSnowflakeNextId();;
        //如果是新增咨询机构,则需要新增记录到咨询机构表
        if (addAdvisoryBodyBo.getIsNewAdvisoryBody()) {
            AdvisoryBody newAdvisoryBody = BeanUtil.toBean(addAdvisoryBodyBo, AdvisoryBody.class);
            newAdvisoryBody.setAdvisoryBodyId(advisoryBodyId);
            baseMapper.insert(newAdvisoryBody);
        }

        AdvisoryBodyProject advisoryBodyProject = BeanUtil.toBean(addAdvisoryBodyBo, AdvisoryBodyProject.class);
        advisoryBodyProject.setAdvisoryBodyId(advisoryBodyId);
        return advisoryBodyProjectMapper.insert(advisoryBodyProject);
    }

    /**
     * 每小时更新一次咨询机构经营范围
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAdvisoryBodyBusinessScope() {
        EnterpriseInfoHeaderBody infoHeaderBody = new EnterpriseInfoHeaderBody();
        for (AdvisoryBody advisoryBody : baseMapper.selectList(null)) {
            infoHeaderBody.setCompanyId(Math.toIntExact(advisoryBody.getAdvisoryBodyCid()));
            Map<String, Object> companyMap = dskOpenApiUtil.requestBody("/api/jsk/enterprise/infoHeader", BeanUtil.beanToMap(infoHeaderBody, false, false));
            Map companyData = MapUtils.getMap(companyMap, "data", null);
            String businessScope = MapUtils.getString(companyData, "businessScope", "empty");
            if (!"empty".equals(businessScope)) {
                advisoryBody.setBusinessScope(businessScope);
                baseMapper.updateById(advisoryBody);
            }
        }
    }
}

