package org.jeecg.modules.stm.inner.order.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.common.util.SpringContextUtils;
import org.jeecg.modules.stm.act.main.entity.StCaMain;
import org.jeecg.modules.stm.act.main.service.IStCaMainService;

import org.jeecg.modules.stm.common.service.IStCommonService;
import org.jeecg.modules.stm.common.vo.DutyVo;
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.jeecg.modules.stm.base.domain.service.IStDomainService;
import org.jeecg.modules.stm.base.domain.vo.StDomainVo;
import org.jeecg.modules.stm.inner.control.entity.StInnerControl;
import org.jeecg.modules.stm.inner.control.service.IStInnerControlService;
import org.jeecg.modules.stm.inner.order.entity.StInnerControlOrder;
import org.jeecg.modules.stm.inner.order.mapper.StInnerControlOrderMapper;
import org.jeecg.modules.stm.inner.order.service.IStInnerControlOrderService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.stm.inner.order.vo.StInnerControlOrderVo;

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

/**
 * @Description: 内控制度表
 * @Author: yzh
 * @Date:   2023-03-05
 * @Version: V1.0
 */
@Service
public class StInnerControlOrderServiceImpl extends ServiceImpl<StInnerControlOrderMapper, StInnerControlOrder> implements IStInnerControlOrderService {

    @Autowired
    private IStCaMainService iStCaMainService;

    @Autowired
    private IStInnerControlService iStInnerControlService;

    @Autowired
    private IStDomainService iStDomainService;

    @Autowired
    private ISysDictService iSysDictService;


    private void checkName(StInnerControlOrder entity, boolean isSave){
        if(entity==null){
            return;
        }
        QueryWrapper<StInnerControlOrder> wapper = new QueryWrapper<>();
        wapper.eq("MANAGE_ORDER",entity.getManageOrder());
        if(!isSave){
            wapper.ne("ID",entity.getId());
        }
        long count = super.count(wapper);
        if(count>0){
            throw new JeecgBootException("内控制度已存在");
        }
    }

    @Override
    public boolean save(StInnerControlOrder entity) {
        checkName(entity,true);
        return super.save(entity);
    }

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

    @Override
    public <E extends IPage<StInnerControlOrder>> E page(E page, Wrapper<StInnerControlOrder> queryWrapper) {
        IPage<StInnerControlOrder> iPage =  super.page(page, queryWrapper);
        List<StInnerControlOrder> data = iPage.getRecords();
        if(CollectionUtils.isEmpty(data)){
            return (E) iPage;
        }
        List<StInnerControlOrder> records = new ArrayList<>();
        for(StInnerControlOrder stInnerControlOrder:data){
            StInnerControlOrderVo stInnerControlOrderVo = new StInnerControlOrderVo();
            BeanUtils.copyProperties(stInnerControlOrder,stInnerControlOrderVo);
            StInnerControl stInnerControl = iStInnerControlService.queryByStrategyNo(stInnerControlOrder.getStrategyNo());
            if(stInnerControlOrderVo!=null) {
                stInnerControlOrderVo.setInnerControlStrategy(stInnerControl.getInnerControlStrategy());
                stInnerControlOrderVo.setInnerControlClaim(stInnerControl.getInnerControlClaim());
                StCaMain stCaMain = iStCaMainService.queryByCaNo(stInnerControl.getCaNo());
                if (stCaMain != null) {
                    StDomainVo stDomain = iStDomainService.queryEntityByCode(stCaMain.getDomainCode());
                    if (stDomain != null) {
                        stInnerControlOrderVo.setDomainName(stDomain.getPathDomainName());
                    }else{
                        stInnerControlOrderVo.setDomainName("-");
                    }
                }else{
                    stInnerControlOrderVo.setDomainName("-");
                }
            }else{
                stInnerControlOrderVo.setInnerControlStrategy("-");
                stInnerControlOrderVo.setInnerControlClaim("-");
                stInnerControlOrderVo.setDomainName("-");
            }
            String name = iSysDictService.queryDictTextByKey("applyobj",stInnerControlOrder.getApplicableCode());
            if(StringUtils.isNotEmpty(name)) {
                stInnerControlOrderVo.setApplicableName(name);
            }else{
                stInnerControlOrderVo.setApplicableName("-");
            }
            DutyVo dutyVo = SpringContextUtils.getBean(IStCommonService.class).queryDutyByCode(stInnerControlOrderVo.getDivisionDuty());
            if(dutyVo!=null){
                stInnerControlOrderVo.setDivisionDutyName(dutyVo.initDutyNames());
            }else{
                stInnerControlOrderVo.setDivisionDutyName("-");
            }
            records.add(stInnerControlOrderVo);
        }
        iPage.setRecords(records);
        return (E) iPage;
    }

    @Override
    public IPage<StInnerControlOrder> defPage(StInnerControlOrder stInnerControlOrder, Integer pageNo, Integer pageSize, Map<String, String[]> parameterMap) {
        QueryWrapper<StInnerControlOrder> queryWrapper = QueryGenerator.initQueryWrapper(stInnerControlOrder, parameterMap);
		String [] domCode = parameterMap.get("domainCode");
        String [] innerControlStrategy = parameterMap.get("innerControlStrategy");
        String [] caNo = parameterMap.get("caNo");
        boolean queryCaMainFlag = false;
        QueryWrapper<StCaMain> stCaMainQueryWrapper = new QueryWrapper<>();
        if(domCode!=null && domCode.length == 1 && StringUtils.isNotEmpty(domCode[0])){
            stCaMainQueryWrapper.like("domain_code",domCode[0]);
            queryCaMainFlag = true;
        }
        boolean queryInnerControl = false;
        QueryWrapper<StInnerControl> stInnerControlQueryWrapper = new QueryWrapper<>();
        if(queryCaMainFlag){
            List<StCaMain> caMains = iStCaMainService.list(stCaMainQueryWrapper);
            if(CollectionUtils.isEmpty(caMains)){
                stInnerControlQueryWrapper.eq("CA_NO","notexist");
                queryInnerControl = true;
            }else{
                List<String> caNos = caMains.stream().map(StCaMain::getCaNo).collect(Collectors.toList());
                stInnerControlQueryWrapper.in("CA_NO",caNos);
                queryInnerControl = true;
            }
        }
        if(innerControlStrategy!=null && innerControlStrategy.length == 1
                && StringUtils.isNotEmpty(innerControlStrategy[0])){
            stInnerControlQueryWrapper.like("INNER_CONTROL_STRATEGY",innerControlStrategy[0]);
            queryInnerControl = true;
        }
        if(caNo!=null && caNo.length==1 && StringUtils.isNotEmpty(caNo[0])){
            stInnerControlQueryWrapper.eq("CA_NO",caNo[0]);
            queryInnerControl = true;
        }
        if(queryInnerControl){
            List<StInnerControl> innerControls = iStInnerControlService.list(stInnerControlQueryWrapper);
            if(!CollectionUtils.isEmpty(innerControls)){
                List<String> strategyNos = innerControls.stream().map(StInnerControl::getStrategyNo).collect(Collectors.toList());
                queryWrapper.in("STRATEGY_NO",strategyNos);
            }else{
                queryWrapper.in("STRATEGY_NO","notexist");
            }
        }
        Page<StInnerControlOrder> page = new Page<StInnerControlOrder>(pageNo, pageSize);
		return this.page(page,queryWrapper);
    }
}
