package org.jeecg.modules.stm.act.collection.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.jeecg.modules.stm.act.collection.entity.StCaMainCollection;
import org.jeecg.modules.stm.act.collection.entity.StCaMainRelCollection;
import org.jeecg.modules.stm.act.collection.mapper.StCaMainCollectionMapper;
import org.jeecg.modules.stm.act.collection.service.IStCaMainCollectionService;
import org.jeecg.modules.stm.act.collection.service.IStCaMainRelCollectionService;
import org.jeecg.modules.stm.act.collection.vo.StCaMainCollectionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
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;

/**
 * @Description: 活动集合
 * @Author: yzh
 * @Date:   2023-03-06
 * @Version: V1.0
 */
@Service
public class StCaMainCollectionServiceImpl extends ServiceImpl<StCaMainCollectionMapper, StCaMainCollection> implements IStCaMainCollectionService {
    private static final String COLLECT_NO_PRE = "JH";
    private static final  int NO_SIZE = 5;

    @Autowired
    private IStCaMainRelCollectionService iStCaMainRelCollectionService;
    private String collectNo(){
        QueryWrapper<StCaMainCollection> wapper = new QueryWrapper();
        wapper.orderByDesc("collection_no");
        wapper.last("limit 1");
        StCaMainCollection noUpper = super.baseMapper.selectOne(wapper);
        if(noUpper == null) {
            return COLLECT_NO_PRE+ StringUtils.leftPad("1",NO_SIZE,"0");
        }else{
            // 如果不为空+1
            int no  = Integer.valueOf(noUpper.getCollectionNo().replace(COLLECT_NO_PRE,""))+1;
            String str = ""+no;
            return  COLLECT_NO_PRE+StringUtils.leftPad(str,NO_SIZE,"0");
        }
    }

    private void checkName(StCaMainCollection entity,boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StCaMainCollection> stCaMainCollectionQueryWrapper = new QueryWrapper<>();
        stCaMainCollectionQueryWrapper.eq("COLLECTION_NAME",entity.getCollectionName());
        if(!isSave){
            stCaMainCollectionQueryWrapper.ne("COLLECTION_NO",entity.getCollectionNo());
        }
        long count = super.count(stCaMainCollectionQueryWrapper);
        if(count>0){
            throw new JeecgBootException("活动集合名称已存在");
        }
    }

    @Override
    public boolean save(StCaMainCollection entity) {
        checkName(entity,true);
        entity.setCollectionNo(collectNo());
        entity.setCreatedUser(UserUtil.getUserCode());
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        entity.setVaild(StmConstans.DEFAULT_VALID);
        return super.save(entity);
    }

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


    private Integer getCollectCount(StCaMainCollection stCaMainCollection){
        QueryWrapper<StCaMainRelCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("collection_no",stCaMainCollection.getCollectionNo());
        long count = iStCaMainRelCollectionService.count(wrapper);
        return Math.toIntExact(count);
    }

    @Override
    public boolean removeById(Serializable id) {
        if(id==null){
            return true;
        }
        StCaMainCollection stCaMainCollection = this.getById(id);
        QueryWrapper<StCaMainRelCollection> wrapper = new QueryWrapper<>();
        wrapper.eq("collection_no",stCaMainCollection.getCollectionNo());
        iStCaMainRelCollectionService.remove(wrapper);
        return super.removeById(id);
    }

    @Override
    public <E extends IPage<StCaMainCollection>> E page(E page, Wrapper<StCaMainCollection> queryWrapper) {
        IPage<StCaMainCollection> data = super.page(page,queryWrapper);
        List<StCaMainCollection> list = data.getRecords();
        List<StCaMainCollection> records = new ArrayList<>();
        for(StCaMainCollection stCaMainCollection:list){
            StCaMainCollectionVo stCaMainCollectionVo = new StCaMainCollectionVo();
            BeanUtils.copyProperties(stCaMainCollection,stCaMainCollectionVo);
            int count =  getCollectCount(stCaMainCollection);
            stCaMainCollectionVo.setCaCount(count);
            records.add(stCaMainCollectionVo);
        }
        data.setRecords(records);
        return (E) data;
    }
}
