package org.jeecg.modules.stm.base.lable.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.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.jeecg.modules.stm.base.classify.service.IStClassifyService;
import org.jeecg.modules.stm.base.lable.entity.StLable;
import org.jeecg.modules.stm.base.lable.mapper.StLableMapper;
import org.jeecg.modules.stm.base.lable.service.IStLableService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.base.lable.vo.StLableVo;
import org.jeecg.modules.stm.utils.StmConstans;
import org.jeecg.modules.stm.utils.UserUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: st_lable
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
@Service
public class StLableServiceImpl extends ServiceImpl<StLableMapper, StLable> implements IStLableService {

    @Autowired
    private IStClassifyService iStClassifyService;

    private String lableCode(){
        QueryWrapper<StLable> wapper = new QueryWrapper();
        wapper.orderByDesc("lable_code");
        wapper.last("limit 1");
        StLable noUpper = super.baseMapper.selectOne(wapper);
        if(noUpper == null) {
            return StringUtils.leftPad("1",4,"0");
        }else{
            // 如果不为空+1
            int no  = Integer.valueOf(noUpper.getLableCode())+1;
            String str = ""+no;
            return StringUtils.leftPad(str,4,"0");
        }
    }
    private void checkLableName(String name,Integer id){
        if(StringUtils.isEmpty(name)){
            throw new JeecgBootException("标签名称不允许为空");
        }
        QueryWrapper<StLable> wapper = new QueryWrapper();
        wapper.eq("lable_name",name);
        if(id!=null) {
            wapper.ne("id", id);
        }
        wapper.last("limit 1");
        StLable noUpper = super.baseMapper.selectOne(wapper);
        if(noUpper != null) {
            throw new JeecgBootException("标签名称已经存在");
        }
    }
    @Override
    public boolean save(StLable entity) {
        checkLableName(entity.getLableName(),null);
        String lableCode = lableCode();
        entity.setLableCode(lableCode);
        entity.setCreatedUser(UserUtil.getUserCode());
        entity.setValid(StmConstans.DEFAULT_VALID);
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        return super.save(entity);
    }

    @Override
    public boolean updateById(StLable entity){
        checkLableName(entity.getLableName(), entity.getId());
        entity.setUpdatedUser(UserUtil.getUserCode());
        return super.updateById(entity);
    }

    @Override
    public List<StLable> listByCods(List<String> lableCodeList) {
        if(CollectionUtils.isEmpty(lableCodeList)){
            return null;
        }
        QueryWrapper<StLable> wapper = new QueryWrapper();
        wapper.in("lable_code",lableCodeList);
        return super.list(wapper);
    }

    @Override
    public List<String>  getCodes(String labelName, List<String> lableCodes,List<String> sClassifyCodes) {
        if(StringUtils.isEmpty(labelName) && (CollectionUtils.isEmpty(sClassifyCodes) ||
                sClassifyCodes.contains("noChecked"))
                && CollectionUtils.isEmpty(lableCodes)){
            return null;
        }
        List<StLable> lableList = null;
        if(StringUtils.isEmpty(labelName) && CollectionUtils.isEmpty(sClassifyCodes)
                && !CollectionUtils.isEmpty(lableCodes)){
            return lableCodes;
        }else if(StringUtils.isNotEmpty(labelName) && CollectionUtils.isEmpty(sClassifyCodes)){
            QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("LABLE_NAME","%"+labelName+"%");
            lableList =  this.baseMapper.selectList(queryWrapper);
        }else if(StringUtils.isEmpty(labelName) && !CollectionUtils.isEmpty(sClassifyCodes)){
            QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("SCLASSIFY_CODE",sClassifyCodes);
            lableList =  this.baseMapper.selectList(queryWrapper);
        }else if(StringUtils.isNotEmpty(labelName) && !CollectionUtils.isEmpty(sClassifyCodes)){
            QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
            queryWrapper.like("LABLE_NAME","%"+labelName+"%");
            queryWrapper.in("SCLASSIFY_CODE",sClassifyCodes);
            lableList =  this.baseMapper.selectList(queryWrapper);
        }
        if(!CollectionUtils.isEmpty(lableList)){
            List<String> codes = lableList.stream().map(StLable::getLableCode).collect(Collectors.toList());
            if(CollectionUtils.isEmpty(lableCodes)){
                return codes;
            }else{
                return codes.stream().filter(code->lableCodes.contains(code)).collect(Collectors.toList());
            }
        }
        return Arrays.asList("notExist");
    }

    @Override
    public <E extends IPage<StLable>> E page(E page, Wrapper<StLable> queryWrapper) {
        IPage<StLable> stLableIPage = super.page(page, queryWrapper);
        List<StLable> stLables = stLableIPage.getRecords();
        List<StLable> records = new ArrayList<>();
        for(StLable stLable:stLables){
            StLableVo stLableVo = new StLableVo();
            BeanUtils.copyProperties(stLable,stLableVo);
            String name = iStClassifyService.getNameByCode(stLable.getPclassifyCode());
            if(StringUtils.isNotEmpty(name)){
                stLableVo.setPclassifyName(name);
            }else{
                stLableVo.setPclassifyName("-");
            }
            String sName = iStClassifyService.getNameByCode(stLable.getSclassifyCode());
            if(StringUtils.isNotEmpty(sName)){
                stLableVo.setSclassifyName(sName);
            }else{
                stLableVo.setSclassifyName("-");
            }
            records.add(stLableVo);
        }
        stLableIPage.setRecords(records);
        return (E) stLableIPage;
    }
}
