package org.jeecg.modules.stm.risk.scenario.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.jeecg.modules.stm.base.lable.service.IStLableService;
import org.jeecg.modules.stm.risk.scenario.entity.StRiskScenario;
import org.jeecg.modules.stm.risk.scenario.mapper.StRiskScenarioMapper;
import org.jeecg.modules.stm.risk.scenario.service.IStRiskScenarioService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.risk.scenario.vo.StRiskScenarioVo;
import org.jeecg.modules.stm.utils.StmConstans;
import org.jeecg.modules.stm.utils.UserUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: 风险场景
 * @Author: yzh
 * @Date:   2023-03-05
 * @Version: V1.0
 */
@Service
public class StRiskScenarioServiceImpl extends ServiceImpl<StRiskScenarioMapper, StRiskScenario> implements IStRiskScenarioService {

    private static final String NO_PRE = "CJ";
    private static final  int NO_SIZE = 5;

    @Autowired
    private ISysDictService iSysDictService;

    @Autowired
    private IStLableService iStLableService;

    private String bizNo(){
        QueryWrapper<StRiskScenario> wapper = new QueryWrapper();
        wapper.orderByDesc("SCENARIO_NO");
        wapper.last("limit 1");
        StRiskScenario noUpper = super.baseMapper.selectOne(wapper);
        if(noUpper == null) {
            return NO_PRE+ StringUtils.leftPad("1",NO_SIZE,"0");
        }else{
            // 如果不为空+1
            int no  = Integer.valueOf(noUpper.getScenarioNo().replace(NO_PRE,""))+1;
            String str = ""+no;
            return  NO_PRE+StringUtils.leftPad(str,NO_SIZE,"0");
        }
    }
    private void checkName(StRiskScenario entity, boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StRiskScenario> wapper = new QueryWrapper<>();
        wapper.eq("SCENARIO_NAME",entity.getScenarioName());
        if(!isSave){
            wapper.ne("ID",entity.getId());
        }
        long count = super.count(wapper);
        if(count>0){
            throw new JeecgBootException("风险场景名称已存在");
        }
    }

    @Override
    public boolean save(StRiskScenario entity) {
        checkName(entity,true);
        String no = bizNo();
        StRiskScenarioVo vo = (StRiskScenarioVo) entity;
        StRiskScenario stRiskScenario = new StRiskScenario();
        BeanUtils.copyProperties(entity,stRiskScenario);
        stRiskScenario.setScenarioNo(no);
        stRiskScenario.setCreatedUser(UserUtil.getUserCode());
        stRiskScenario.setValid(StmConstans.DEFAULT_VALID);
        stRiskScenario.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        return super.save(stRiskScenario);
    }

    @Override
    public boolean updateById(StRiskScenario entity) {
        checkName(entity,false);
        StRiskScenarioVo vo = (StRiskScenarioVo) entity;
        StRiskScenario stRiskScenario = new StRiskScenario();
        BeanUtils.copyProperties(entity,stRiskScenario);
        stRiskScenario.setUpdatedUser(UserUtil.getUserCode());
        stRiskScenario.setValid(StmConstans.DEFAULT_VALID);
        stRiskScenario.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        return super.updateById(stRiskScenario);
    }


    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @Override
    public IPage<StRiskScenario> defPage(StRiskScenario stRiskScenario, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {
        QueryWrapper<StRiskScenario> queryWrapper = QueryGenerator.initQueryWrapper(stRiskScenario, parameterMap);
        Page<StRiskScenario> page = new Page<StRiskScenario>(pageNo, pageSize);
        return this.page(page, queryWrapper);
    }

    @Override
    public StRiskScenario queryByScenarioNo(String scenarioNo) {
        QueryWrapper<StRiskScenario> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("SCENARIO_NO",scenarioNo);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public StRiskScenarioVo queryVoByScenario(String scenarioNo) {
        StRiskScenario stRiskScenario = this.queryByScenarioNo(scenarioNo);
        if(stRiskScenario!=null) {
            StRiskScenarioVo stRiskScenarioVo = new StRiskScenarioVo();
            BeanUtils.copyProperties(stRiskScenario,stRiskScenarioVo);
            String text = iSysDictService.queryDictTextByKey("applyobj",stRiskScenario.getApplicableCode());
            stRiskScenarioVo.setApplicableName(text);
            return stRiskScenarioVo;
        }
        return null;
    }

    @Override
    public <E extends IPage<StRiskScenario>> E page(E page, Wrapper<StRiskScenario> queryWrapper) {
        IPage<StRiskScenario> ipage = super.page(page, queryWrapper);
        List<StRiskScenario> data = ipage.getRecords();
        List<StRiskScenario> records = new ArrayList<>();
        for(StRiskScenario stRiskScenario:data){
            StRiskScenarioVo stRiskScenarioVo = new StRiskScenarioVo();
            BeanUtils.copyProperties(stRiskScenario,stRiskScenarioVo);
            String text = iSysDictService.queryDictTextByKey("applyobj",stRiskScenario.getApplicableCode());
            if(StringUtils.isNotEmpty(text)) {
                stRiskScenarioVo.setApplicableName(text);
            }else{
                stRiskScenarioVo.setApplicableName("-");
            }
            records.add(stRiskScenarioVo);
        }
        ipage.setRecords(records);
        return (E) ipage;
    }

}
