package com.chinaweal.aiccs.aiccs.blacklist.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinaweal.aiccs.aiccs.blacklist.entity.TPyBlacklist;
import com.chinaweal.aiccs.aiccs.blacklist.mapper.TPyBlacklistMapper;
import com.chinaweal.aiccs.aiccs.blacklist.service.ITPyBlacklistService;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.aiccs.org.service.UserBaseService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.exception.custom.BusinessException;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import com.chinaweal.youfool.framework.springboot.rest.ResultCode;
import com.chinaweal.youfool.framework.sso.util.SSOUtil;
import org.springframework.stereotype.Service;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

/**
 * <p>
 * 番禺企业黑名单库 服务实现类
 * </p>
 *
 * @author chinaweal
 * @since 2024-06-27
 */
@Service
public class TPyBlacklistServiceImpl extends BaseServiceImpl<TPyBlacklistMapper, TPyBlacklist> implements ITPyBlacklistService {

    @Resource
    private UserBaseService userBaseService;

    @Override
    public TPyBlacklist saveTPyBlacklist(TPyBlacklist tPyBlacklist) {
        save(tPyBlacklist);
        return tPyBlacklist;
    }

    @Override
    public void updateTPyBlacklist(TPyBlacklist tPyBlacklist) {
        updateById(tPyBlacklist);
    }

    @Override
    public IPage<TPyBlacklist> listTPyBlacklist(BaseQuery<TPyBlacklist> query) {
        TPyBlacklist entity = query.getEntity(TPyBlacklist.class);
        Page<TPyBlacklist> page = query.getPage();
        LambdaQueryWrapper<TPyBlacklist> lambdaQueryWrapper = Wrappers.lambdaQuery();
        return page(page, lambdaQueryWrapper);
    }

    @Override
    public void importBlacklist(MultipartFile file) {
        List<Map<Integer, String>> list;
        try {
            //移除第一个行表头
            list = EasyExcel.read(file.getInputStream()).sheet(0).doReadSync();
        } catch (IOException e) {
            log.error("批量导入有误", e);
            throw new BusinessException(ResultCode.SPECIFIED_QUESTIONED_USER_NOT_EXIST.setMessage("请检查文件内容是否有误"));
        }

        for (Map<Integer, String> item : list) {
            String uniscid = StringUtils.trimToEmpty(item.get(0));
            String entName = StringUtils.trimToEmpty(item.get(1));
            String tagType = StringUtils.trimToEmpty(item.get(2));
            save(new TPyBlacklist()
                    .setUniscid(uniscid)
                    .setEntName(entName)
                    .setTagType(tagType));
        }
    }

    @Override
    public String check(String uniscid, String tagType) {
        AICUser curUser = SSOUtil.getUser();
        if (StringUtils.startsWith(curUser.getRegionID(), "440113")
                || StringUtils.startsWith(curUser.getRegionID(), "440126")) {
            QueryWrapper<TPyBlacklist> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("uniscid", uniscid);
            queryWrapper.eq("tagType", tagType);
            queryWrapper.eq("is_disabled", false);
            int count = this.count(queryWrapper);

            // 1-经济发展的重点企业 2-涉嫌逃避监管的企业 3-有多次被列入经营异常名录记录企业 4-现场核查被投诉企业
            if ("1".equals(tagType) && count > 0) {
                return "该企业为经济发展的重点企业，请审慎处理";
            } else if ("2".equals(tagType) && count > 0) {
                return "该企业为涉嫌逃避监管的企业，请审慎处理";
            } else if ("3".equals(tagType) && count > 0) {
                return "该企业有多次被列入经营异常名录记录，请审慎处理";
            } else if ("4".equals(tagType) && count > 0) {
                StringBuilder sb = new StringBuilder();
                List<TPyBlacklist> list = this.lambdaQuery()
                        .eq(TPyBlacklist::getUniscid, uniscid)
                        .eq(TPyBlacklist::getTagType, tagType)
                        .eq(TPyBlacklist::getIsDisabled, false)
                        .list();
                for (TPyBlacklist item : list) {
                    sb.append(item.getSourceOrgName()).append(item.getSourceName());
                }
                return String.format("该企业已被%s标记为审慎监管企业，请审慎处理。", sb);
            }
        }
        return null;
    }

}
