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

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaweal.aiccs.aiccs.abnormal.entity.EBaseinfo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.EntWritsDto;
import com.chinaweal.aiccs.aiccs.query.entity.TSEntBaseQuery;
import com.chinaweal.aiccs.aiccs.query.mapper.TSEntBaseQueryMapper;
import com.chinaweal.aiccs.aiccs.seriousillegal.entity.TSSerIllegalList;
import com.chinaweal.aiccs.aiccs.system.entity.TRBaseCode;
import com.chinaweal.aiccs.aiccs.system.service.TRBaseCodeService;
import com.chinaweal.aiccs.common.base.entity.dto.task.UniTaskListDto;
import com.chinaweal.aiccs.aiccs.seriousillegal.mapper.TSSerIllegalListMapper;
import com.chinaweal.aiccs.aiccs.seriousillegal.service.TSSerIllegalListService;
import com.chinaweal.aicorg.model.AICUser;
import com.chinaweal.youfool.framework.springboot.base.query.BaseQuery;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 严重违法企业名单 服务实现类
 * </p>
 *
 * @author <a href="https://blog.lroyia.top">lroyia</a>
 * @since 2021-01-26
 */
@Service
public class TSSerIllegalListServiceImpl extends BaseServiceImpl<TSSerIllegalListMapper, TSSerIllegalList> implements TSSerIllegalListService {

    @Resource
    private TSEntBaseQueryMapper tsEntBaseQueryMapper;
    @Autowired
    private TRBaseCodeService baseCodeService;

    @Override
    public List<TSSerIllegalList> selectEntList(TSSerIllegalList serIllegalListModel) {
        return baseMapper.selectEntList(serIllegalListModel);
    }

    @Override
    public IPage<TSEntBaseQuery> serIllegalListByPage(int rows, int page, TSEntBaseQuery entBaseQueryModel) {
        return tsEntBaseQueryMapper.selectByPageExptList(new Page<>(page, rows), entBaseQueryModel);
    }

    @Override
    public IPage<TSSerIllegalList> selectTSSerIllegalList(int rows, int page, TSSerIllegalList tsSerIllegalListModel, String type) {
        if("apply".equals(type)){//依申请移出查询【列入原因是异常名录期限届满，且满5年未再发生相关情形】
            return baseMapper.selectTSSerIllegalApplyByPage(new Page<>(page, rows), tsSerIllegalListModel, new Date());

        }else if("timeout".equals(type)){//期限届满移出查询【存在严重违法失信行为，满5年未再发生相关情形】
            return baseMapper.selectTSSerIllegalTimeoutByPage(new Page<>(page, rows), tsSerIllegalListModel, new Date());

        }else if("dissent".equals(type)){ //严重违法失信 异议查询
            return baseMapper.selectTSSerIllegalDissentByPage(new Page<>(page, rows), tsSerIllegalListModel);

        }else if("credit".equals(type)){ //严重违法失信 信用修复移出查询
            return baseMapper.selectCreditTSSerIllegalByPage(new Page<>(page, rows), tsSerIllegalListModel);

        }else{//普通查询
            return baseMapper.selectTSSerIllegalListByPage(new Page<>(page, rows), tsSerIllegalListModel);
        }
    }

    @Override
    public IPage<TSSerIllegalList> selectTSSerIllegalListCountByPage(int rows, int page, TSSerIllegalList tsSerIllegalListModel, String[] regorgArr, String[] territorialArr) {
        return baseMapper.selectTSSerIllegalListCountByPage(new Page<>(page, rows), tsSerIllegalListModel, regorgArr, territorialArr);
    }

    @Override
    public IPage<TSSerIllegalList> findLostCreditEntByPage(int rows, int page, TSSerIllegalList tsSerIllegalListModel) {
        IPage<TSSerIllegalList> pageResult = new Page<>(page, rows);
        pageResult.setRecords(new ArrayList<>());
        return pageResult;
    }

    @Override
    public TSSerIllegalList getFirstIllegalListRecord(Wrapper<TSSerIllegalList> wrapper) {
        return baseMapper.selectFirstIllegalListRecord(wrapper);
    }

    @Override
    public IPage<UniTaskListDto> getUniIllegalTaskListPage(IPage<UniTaskListDto> page, Map<String, Object> params) {
        return baseMapper.selectUniIllegalTaskListPage(page, params);
    }

    @Override
    public int getIllegalTaskListCount(Map<String, Object> params) {
        return baseMapper.selectIllegalTaskListCount(params);
    }

    @Override
    public IPage<TSSerIllegalList> selectSerillegalList(IPage<TSSerIllegalList> page, Map<String, Object> params) {
        return baseMapper.selectSerillegalList(page, params);
    }

    @Override
    public TSSerIllegalList selecttsSerIllegalAppByMap(Map<String, Object> params) {
        return baseMapper.selecttsSerIllegalAppByMap(params);
    }

    @Override
    public TSSerIllegalList selecttsSerIllegalAppByid(String illegalListID) {
        return baseMapper.selecttsSerIllegalAppByid(illegalListID);
    }

    @Override
    public List<TSSerIllegalList> listOutTimeIllegalList(String date) {
        return baseMapper.listOutTimeIllegalList(date);
    }

    @Override
    public List<TSSerIllegalList> listCancelEnts() {
        return baseMapper.listCancelEnts();
    }

    @Override
    public IPage<TSSerIllegalList> queryAbnormalLrList(IPage<TSSerIllegalList> iPage) {
        return baseMapper.queryAbnormalLrList(iPage);
    }

    @Override
    public IPage<TSSerIllegalList> queryAbnormalYcList(IPage<TSSerIllegalList> page) {
        return baseMapper.queryAbnormalYcList(page);
    }
    @Override
    public IPage<EntWritsDto> queryAbnormalLrByNameList(IPage<EntWritsDto> page, Map<String, Object> pram) {
        return baseMapper.queryAbnormalLrByNameList(page,pram);
    }

    @Override
    public IPage<EntWritsDto> queryAbnormalYcByNameList(IPage<EntWritsDto> page, Map<String, Object> pram) {
        return baseMapper.queryAbnormalYcByNameList(page,pram);
    }

    @Override
    public IPage<EntWritsDto> queryToIllegalByNameList(IPage<EntWritsDto> page, Map<String, Object> pram) {
        return baseMapper.queryToIllegalByNameList(page,pram);
    }

    @Override
    public boolean saveByOne(TSSerIllegalList tsSerIllegalList){
        TSSerIllegalList byId = this.getById(tsSerIllegalList.getIllegallistid());
        if(byId == null){
            this.save(tsSerIllegalList);
        }
        return true;
    }

    @Override
    public IPage<TSSerIllegalList> listXrSeriousIllegalListEnt(BaseQuery<EBaseinfo> query, AICUser user) {
        EBaseinfo entity = query.getEntity(EBaseinfo.class);
        Page<EBaseinfo> page = query.getPage();
        //设置登记机关条件
        entity.setRegorg(user.getRegionID().substring(0, 6));
        IPage<EBaseinfo> eBaseinfoIPage = baseMapper.queryEBaseinfoInSeriousIllegal(page, entity);

        IPage<TSSerIllegalList> xrSeriousIllegalListIPage = new Page<>();
        xrSeriousIllegalListIPage.setCurrent(eBaseinfoIPage.getCurrent())
                .setSize(eBaseinfoIPage.getSize())
                .setTotal(eBaseinfoIPage.getTotal());
        List<TSSerIllegalList> records = new ArrayList<>();
        //显示出每个主体的第一条严重违法信息
        eBaseinfoIPage.getRecords().forEach(eBaseinfo -> {
            List<TSSerIllegalList> illegals = this.list(new LambdaQueryWrapper<TSSerIllegalList>()
                    .eq(TSSerIllegalList::getPripid, eBaseinfo.getPripid())
                    .eq(TSSerIllegalList::getOperationStatus, "0")
                    .isNull(TSSerIllegalList::getRemdate));
            //取企业的第一条严重违法信息
            TSSerIllegalList tsSerIllegalList = illegals.get(0);
            // 列入原因
            Map<String, TRBaseCode> serCodeMap = baseCodeService.getBaseCodeMapFormCache("CD73");
            TRBaseCode reasonCode = serCodeMap.get(tsSerIllegalList.getSerillrea());
            tsSerIllegalList.setSerillreaString(reasonCode.getName());
            records.add(tsSerIllegalList);
        });

        xrSeriousIllegalListIPage.setRecords(records);
        return xrSeriousIllegalListIPage;
    }
}
