package com.dsk.cscec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
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.SupplierLimitedManageQueryConstants;
import com.dsk.cscec.domain.FSupplierLimited;
import com.dsk.cscec.domain.bo.SupplierLimitedSearchBo;
import com.dsk.cscec.domain.bo.SupplierUnlimitedSearchBo;
import com.dsk.cscec.domain.vo.SupplierLimitedSearchVo;
import com.dsk.cscec.domain.vo.SupplierUnlimitedSearchVo;
import com.dsk.cscec.mapper.FSupplierLimitedMapper;
import com.dsk.cscec.service.FSupplierLimitedService;
import com.dsk.system.utils.DskOpenApiUtil;
import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Service;

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

/**
 * 供应商限制信息表(FSupplierLimited)表服务实现类
 *
 * @author sxk
 * @since 2024-01-05 11:34:23
 */
@Service("fSupplierLimitedService")
public class FSupplierLimitedServiceImpl extends ServiceImpl<FSupplierLimitedMapper, FSupplierLimited> implements FSupplierLimitedService {
    @Resource
    private FSupplierLimitedMapper baseMapper;
    @Resource
    private DskOpenApiUtil dskOpenApiUtil;

    /**
     * 获取涉诉限用禁用名单
     *
     * @param searchBo  查询实体
     * @param pageQuery 分页对象
     * @return 所有数据
     */
    @Override
    public TableDataInfo<SupplierLimitedSearchVo> querySupplierLimitedList(SupplierLimitedSearchBo searchBo, PageQuery pageQuery) {
        QueryWrapper<FSupplierLimited> wrapper = Wrappers.query();
        wrapper
                //供应商名称
                .like(StringUtils.isNotBlank(searchBo.getSupplierName()), "fsl.fsuppliername", searchBo.getSupplierName())
                //是否为局名义被诉案件
                .eq(StringUtils.isNotBlank(searchBo.getIsGroupCase()), "fsl.fisgroupcase", searchBo.getIsGroupCase())
                //限用/禁用时间
                .between(ObjectUtils.isNotNull(searchBo.getLimitedStartTime()) && ObjectUtils.isNotNull(searchBo.getLimitedEndTime()),
                        "fsl.fts",
                        searchBo.getLimitedStartTime(),
                        searchBo.getLimitedEndTime())
                //筛选涉诉限用禁用名单数据
                .eq("fsl.fdatatype", SupplierLimitedManageQueryConstants.LIMITED_DATA_TYPE)
                //根据限用/禁用时间倒序排序
                .orderByDesc("fsl.fts");

        Page<SupplierLimitedSearchVo> page = baseMapper.selectPageSupplierLimitedList(pageQuery.build(), wrapper);

        for (SupplierLimitedSearchVo searchVo : page.getRecords()) {
            Map<String, Object> params = new HashMap<>();
            String supplierName = searchVo.getFsuppliername();
            params.put("keyword", supplierName);
            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 (supplierName.equals(companyName)) {
                        searchVo.setSupplierCid(Math.toIntExact(MapUtils.getLong(companyData, "jskEid")));
                    }
                }
                //查不到则抛异常
                //throw new ServiceException("未查询到该供应商Cid");
            }
            //关键字标红
            if (StringUtils.isNotBlank(searchBo.getSupplierName())) {
                searchVo.setFsuppliername(StringUtils.markInRed(searchVo.getFsuppliername(), searchBo.getSupplierName()));
            }
        }
        return TableDataInfo.build(page);
    }

    /**
     * 获取解除限用禁用名单
     *
     * @param searchBo  查询实体
     * @param pageQuery 分页对象
     * @return 所有数据
     */
    @Override
    public TableDataInfo<SupplierUnlimitedSearchVo> querySupplierUnlimitedList(SupplierUnlimitedSearchBo searchBo, PageQuery pageQuery) {
        QueryWrapper<FSupplierLimited> wrapper = Wrappers.query();
        wrapper
                //申请单位
                .like(StringUtils.isNotBlank(searchBo.getApplyUnit()), "fsl.fjobunitname", searchBo.getApplyUnit())
                //解除限用/禁用时间
                .between(ObjectUtils.isNotNull(searchBo.getUnlimitedStartTime()) && ObjectUtils.isNotNull(searchBo.getUnlimitedEndTime()),
                        "fsl.fts",
                        searchBo.getUnlimitedStartTime(),
                        searchBo.getUnlimitedEndTime())
                //筛选解除限用禁用名单数据
                .eq("fsl.fdatatype", SupplierLimitedManageQueryConstants.UNLIMITED_DATA_TYPE)
                //根据解除限用/禁用时间倒序排序
                .orderByDesc("fsl.fts");

        Page<SupplierUnlimitedSearchVo> page = baseMapper.selectPageSupplierUnlimitedList(pageQuery.build(), wrapper);

        for (SupplierUnlimitedSearchVo searchVo : page.getRecords()) {
            Map<String, Object> params = new HashMap<>();
            String supplierName = searchVo.getFsuppliername();
            params.put("keyword", supplierName);
            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 (supplierName.equals(companyName)) {
                        searchVo.setSupplierCid(Math.toIntExact(MapUtils.getLong(companyData, "jskEid")));
                    }
                }
                //查不到则抛异常
                //throw new ServiceException("未查询到该供应商Cid");
            }
        }
        return TableDataInfo.build(page);
    }
}
