package com.chinaweal.aiccs.aiccs.abnormal.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.vo.DissentListVo;
import com.chinaweal.aiccs.aiccs.abnormal.entity.TSAbnList;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.AbnormalTaskCountDto;
import com.chinaweal.aiccs.aiccs.abnormal.entity.dto.ReasonEntTypeDto;
import com.chinaweal.aiccs.aiccs.abnormal.mapper.TSAbnListMapper;
import com.chinaweal.aiccs.aiccs.abnormal.service.TSAbnListService;
import com.chinaweal.aiccs.aiccs.entbase.entity.TREntPriseType;
import com.chinaweal.aiccs.aiccs.entbase.service.TREntPriseTypeService;
import com.chinaweal.aiccs.common.base.entity.dto.task.UniTaskListDto;
import com.chinaweal.aiccs.common.util.StringUtils;
import com.chinaweal.youfool.framework.springboot.mybatis.plus.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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-19
 */
@Service
public class TSAbnListServiceImpl extends BaseServiceImpl<TSAbnListMapper, TSAbnList> implements TSAbnListService {

    @Autowired
    private TREntPriseTypeService entPriseTypeService;

    @Override
    public List<TSAbnList> selectEntList(String pripid) {
        return list(new LambdaQueryWrapper<TSAbnList>().eq(TSAbnList::getPripid, pripid).orderByAsc(TSAbnList::getAbntime));
    }

    @Override
    public IPage<TSAbnList> selectTSAbnByPageList(IPage<TSAbnList> page, TSAbnList tsAbnListModel, String entertype) {
        IPage<TSAbnList> result = null;
        if ("enterprise".equals(entertype) || StringUtils.isBlank(entertype)) {
            result = baseMapper.selectTSAbnByPageList(page, tsAbnListModel);
        } else if ("individual".equals(entertype)) {
            result = baseMapper.selectIndividualByPageList(page, tsAbnListModel);
        } else if ("farmer".equals(entertype)) {
            result = baseMapper.selectFarmerByPageList(page, tsAbnListModel);
        }
        return result;
    }

    @Override
    public IPage<TSAbnList> removeselectByPagebnlist(IPage<TSAbnList> page, TSAbnList tsAbnListModel, String entertype) {
        IPage<TSAbnList> result = null;
        // 工商机构
        String[] decorgArr = null;
        if(StringUtils.isNotBlank(tsAbnListModel.getIntoDecorg())){
            decorgArr = tsAbnListModel.getIntoDecorg().split(",");
        }

        if (StringUtils.isBlank(entertype) || "enterprise".equals(entertype)) {
            result = baseMapper.removeselectByPagebnlist(page, tsAbnListModel, decorgArr);
        } else if ("individual".equals(entertype)) {
            result = baseMapper.removeIndividualByPagebnlist(page, tsAbnListModel, decorgArr);
        } else if ("farmer".equals(entertype)) {
            result = baseMapper.removeFarmerByPagebnlist(page, tsAbnListModel, decorgArr);
        }
        return result;
    }

    @Override
    public IPage<TSAbnList> selectAbnListByPage(int rows, int page, TSAbnList tsAbnListModel) {
        return baseMapper.selectAbnListByPage(new Page<>(page, rows), tsAbnListModel);
    }

    @Override
    public IPage<TSAbnList> selectTSAbnByTimeout(int rows, int page, TSAbnList tsAbnListModel) {
        return baseMapper.selectTSAbnByTimeoutByPage(new Page<>(page, rows), tsAbnListModel, new Date());
    }

    /**
     * 获取异常主体列表分页
     * @param page 分页参数
     * @param customParamMap 查询条件
     * @return
     */
    @Override
    public IPage<DissentListVo> getDissentListPage(Page<DissentListVo> page, Map<String,Object> customParamMap) {

        Object searchDate = customParamMap.get("searchDate");
        if(searchDate instanceof List){
            List<String> list = (List<String>) searchDate;
            customParamMap.put("startDate",list.get(0));
            customParamMap.put("endDate",list.get(1));
        }

        IPage<DissentListVo> listPage = baseMapper.getDissentListPage(page, customParamMap);
        //封装主体类型名称
        listPage.getRecords().forEach(each -> {
            TREntPriseType type = entPriseTypeService.getEntPriseTypeFromCache(each.getEnttype());
            if(type != null){
                each.setEnttypeName(type.getDisplayvalue());
            }
        });
        return listPage;
    }

    @Override
    public IPage<DissentListVo> getRemDissentListPage(Page<DissentListVo> page, Map<String, Object> customParamMap) {
        Object searchDate = customParamMap.get("searchDate");
        if(searchDate instanceof List){
            List<String> list = (List<String>) searchDate;
            customParamMap.put("startDate",list.get(0));
            customParamMap.put("endDate",list.get(1));
        }

        IPage<DissentListVo> listPage = baseMapper.getRemDissentListPage(page, customParamMap);
        //封装主体类型名称
        listPage.getRecords().forEach(each -> {
            TREntPriseType type = entPriseTypeService.getEntPriseTypeFromCache(each.getEnttype());
            if(type != null){
                each.setEnttypeName(type.getDisplayvalue());
            }
        });
        return listPage;
    }

    @Override
    public List<String> listRemovedPripid(String beginDate) {
        return baseMapper.listRemovedPripid(beginDate);
    }

    @Override
    public TSAbnList getFirstAbnormalList(Wrapper<TSAbnList> wrapper) {
        return baseMapper.selectFirstAbnormalList(wrapper);
    }

    @Override
    public List<ReasonEntTypeDto> statisticSpeCauseAndEntType(Map<String, Object> params) {
        return baseMapper.statisticSpeCauseAndEntType(params);
    }

    @Override
    public List<AbnormalTaskCountDto> statisticAbnormalTaskCount(Map<String, Object> params) {
        return baseMapper.statisticAbnormalTaskCount(params);
    }

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

    @Override
    public List<TSAbnList> queryTSAbnByREMDATE(String pripid) {
        return baseMapper.queryTSAbnByREMDATE(pripid);
    }

    @Override
    public TSAbnList queryOneByPripid(String pripid) {
        return baseMapper.queryOneByPripid(pripid);
    }

    @Override
    public List<TSAbnList> getTSAbnListByMap(Map<String, Object> parm) {
        return baseMapper.getTSAbnListByMap(parm);
    }

    @Override
    public TSAbnList selectTsAbnbyMap(Map<String, Object> paramAbn) {
        return baseMapper.selectTsAbnbyMap(paramAbn);
    }

    @Override
    public TSAbnList selectTsAbnById(String abnlistid) {
        return baseMapper.selectById(abnlistid);
    }

    @Override
    public TSAbnList queryByAbnlistid(String abnlistid) {
        return baseMapper.queryByAbnlistid(abnlistid);
    }

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

    @Override
    public List<TSAbnList> selectAbnListByPripid(String pripid,String newAbnTime,String abnTime ) {
        return baseMapper.selectAbnListByPripid(pripid,newAbnTime,abnTime);
    }

    @Override
    public List<TSAbnList> getDeregEntList() {
        return baseMapper.selectDeregEntList();
    }

    @Override
    public List<TSAbnList> getCancelEntList() {
        return baseMapper.selectCancelEntList();
    }

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

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

    @Override
    public Boolean saveAbnBatch(List<TSAbnList> list) {
        return baseMapper.saveAbnBatch(list);
    }

    @Override
    public int checknb(String entityno, String reportYear) {
        return baseMapper.checknb(entityno, reportYear);
    }

    @Override
    public Boolean saveByOne(TSAbnList tsAbnList){
        TSAbnList byId = this.getById(tsAbnList.getAbnlistid());
        if(byId == null){
            this.save(tsAbnList);
        }
        return true;
    }
}
