package org.stm.best.cases.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 org.apache.catalina.User;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.stm.act.main.entity.StCaMain;
import org.stm.base.classify.entity.StClassify;
import org.stm.best.cases.entity.StBestCase;
import org.stm.best.cases.mapper.StBestCaseMapper;
import org.stm.best.cases.service.IStBestCaseService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.stm.best.cases.vo.StBestCaseVo;
import org.stm.compliane.info.constant.ComplianeClass;
import org.stm.utils.StmConstans;
import org.stm.utils.UserUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: st_best_case
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
@Service
public class StBestCaseServiceImpl extends ServiceImpl<StBestCaseMapper, StBestCase> implements IStBestCaseService {
    private static final String NO_PRE = "AL";
    private static final  int NO_SIZE = 5;

    @Autowired
    private ISysDictService iSysDictService;

    private String bizNo(){
        QueryWrapper<StBestCase> wapper = new QueryWrapper();
        wapper.orderByDesc("CASE_NO");
        wapper.last("limit 1");
        StBestCase noUpper = super.baseMapper.selectOne(wapper);
        if(noUpper == null) {
            return NO_PRE+ StringUtils.leftPad("1",NO_SIZE,"0");
        }else{
            // 如果不为空+1
            int no  = Integer.valueOf(noUpper.getCaseNo().replace(NO_PRE,""))+1;
            String str = ""+no;
            return  NO_PRE+StringUtils.leftPad(str,NO_SIZE,"0");
        }
    }
    private void checkName(StBestCase entity, boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StBestCase> wapper = new QueryWrapper<>();
        wapper.eq("FILE_NAME",entity.getFileName());
        if(!isSave){
            wapper.ne("CASE_NO",entity.getCaseNo());
        }
        long count = super.count(wapper);
        if(count>0){
            throw new JeecgBootException("文件名称已存在");
        }
    }
    @Override
    public boolean save(StBestCase entity) {
        checkName(entity,true);
        String no = bizNo();
        entity.setCaseNo(no);
        entity.setCreatedUser(UserUtil.getUserCode());
        // 设置默认文件状态
        entity.setStatus(ComplianeClass.FILE_STATYS);
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        entity.setValid(StmConstans.DEFAULT_VALID);
        return super.save(entity);
    }

    @Override
    public boolean updateById(StBestCase entity) {
        checkName(entity,false);
        entity.setUpdatedUser(UserUtil.getUserCode());
        return super.updateById(entity);
    }

    @Override
    public <E extends IPage<StBestCase>> E page(E page, Wrapper<StBestCase> queryWrapper) {
        IPage<StBestCase> iPage = super.page(page, queryWrapper);
        List<StBestCase> data = iPage.getRecords();
        List<StBestCase> records = new ArrayList<>();
        for(StBestCase stBestCase:data){
            StBestCaseVo stBestCaseVo = new StBestCaseVo();
            BeanUtils.copyProperties(stBestCase,stBestCaseVo);
            String text = iSysDictService.queryDictTextByKey("applyrange",stBestCase.getApplyRangeCode());
            if(StringUtils.isNotEmpty(text)) {
                stBestCaseVo.setApplyRangeName(text);
            }else {
                stBestCaseVo.setApplyRangeName("-");
            }
            String stausName = iSysDictService.queryDictTextByKey("filestatus",stBestCase.getStatus());
            if(StringUtils.isNotEmpty(stausName)) {
                stBestCaseVo.setStatusName(stausName);
            }else{
                stBestCaseVo.setStatusName("-");
            }
            records.add(stBestCaseVo);
        }
        iPage.setRecords(records);
        return (E) iPage;
    }
}
