package org.jeecg.modules.stm.best.practices.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.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.domain.vo.StDomainVo;
import org.jeecg.modules.stm.best.practices.entity.StBestPractices;
import org.jeecg.modules.stm.best.practices.mapper.StBestPracticesMapper;
import org.jeecg.modules.stm.best.practices.service.IStBestPracticesService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.best.practices.vo.StBestPracticesVo;
import org.jeecg.modules.stm.utils.QueryCaManParamsUtlis;
import org.jeecg.modules.stm.utils.StmConstans;
import org.jeecg.modules.stm.utils.UserUtil;

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

/**
 * @Description: 行业实践应用
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
@Service
public class StBestPracticesServiceImpl extends ServiceImpl<StBestPracticesMapper, StBestPractices> implements IStBestPracticesService {

    @Autowired
    private IStCaMainService iStCaMainService;
    @Autowired
    private IStDomainService iStDomainService;

    @Autowired
    private ISysDictService iSysDictService;

    @Override
    public <E extends IPage<StBestPractices>> E page(E page, Wrapper<StBestPractices> queryWrapper) {
        IPage<StBestPractices> iPage =  super.page(page, queryWrapper);
        List<StBestPractices> data = iPage.getRecords();
        if(CollectionUtils.isEmpty(data)){
            return (E) iPage;
        }
        List<StBestPractices> records = new ArrayList<>();
        for(StBestPractices stBestPractices:data){
            StBestPracticesVo sBestPracticesVo = new StBestPracticesVo();
            BeanUtils.copyProperties(stBestPractices,sBestPracticesVo);
            StCaMain stCaMain = iStCaMainService.queryByCaNo(stBestPractices.getCaNo());
            if(stCaMain!=null) {
                sBestPracticesVo.setCaName(stCaMain.getCaName());
                StDomainVo stDomain = iStDomainService.queryEntityByCode(stCaMain.getDomainCode());
                if(stDomain!=null) {
                    sBestPracticesVo.setDomainName(stDomain.getPathDomainName());
                }else{
                    sBestPracticesVo.setDomainName("-");
                }
            }else{
                sBestPracticesVo.setCaName("-");
                sBestPracticesVo.setDomainName("-");

            }
            String name = iSysDictService.queryDictTextByKey("applyobj",stBestPractices.getApplicableCode());
            if(StringUtils.hasText(name)) {
                sBestPracticesVo.setApplicableName(name);
            }else {
                sBestPracticesVo.setApplicableName("-");
            }
            records.add(sBestPracticesVo);
        }
        iPage.setRecords(records);
        return (E) iPage;
    }
    private void checkName(StBestPractices entity, boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StBestPractices> wapper = new QueryWrapper<>();
        wapper.eq("PRACTICE_INFO",entity.getPracticeInfo());
        if(!isSave){
            wapper.ne("ID",entity.getId());
        }
        long count = super.count(wapper);
        if(count>0){
            throw new JeecgBootException("行业实践已存在");
        }
    }
    @Override
    public boolean save(StBestPractices entity) {
        checkName(entity,true);
        entity.setCreatedUser(UserUtil.getUserCode());
        entity.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
        entity.setValid(StmConstans.DEFAULT_VALID);
        return super.save(entity);
    }

    @Override
    public boolean updateById(StBestPractices entity) {
        checkName(entity,false);
        entity.setUpdatedUser(entity.getUpdatedUser());
        return super.updateById(entity);
    }

    @Override
    public IPage<StBestPractices> defPage(StBestPractices stBestPractices, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {
        QueryWrapper<StBestPractices> queryWrapper = QueryGenerator.initQueryWrapper(stBestPractices, parameterMap);
        List<String> caNos = QueryCaManParamsUtlis.getCaNos(parameterMap);
        if(!CollectionUtils.isEmpty(caNos)){
            if(caNos.size()==1 && "notExist".equals(caNos.get(0))){
                return null;
            }
            queryWrapper.in("CA_NO",caNos);
        }
        Page<StBestPractices> page = new Page<StBestPractices>(pageNo, pageSize);
        IPage<StBestPractices> pageList = this.page(page, queryWrapper);
        return pageList;
    }
}
