package org.stm.compliane.item.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.jeecg.common.exception.JeecgBootException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.stm.compliane.item.entity.StComplianeRiskRef;
import org.stm.compliane.item.mapper.StComplianeRiskRefMapper;
import org.stm.compliane.item.service.IStComplianeRiskRefService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.stm.compliane.item.vo.StComplianeRiskRefSearchVo;
import org.stm.compliane.item.vo.StComplianeRiskRefVo;
import org.stm.risk.event.entity.StRiskEvent;
import org.stm.risk.event.service.IStRiskEventService;
import org.stm.risk.event.vo.StRiskEventVo;
import org.stm.risk.scenario.service.IStRiskScenarioService;
import org.stm.risk.scenario.vo.StRiskScenarioVo;
import org.stm.utils.StmConstans;
import org.stm.utils.UserUtil;

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

/**
 * @Description: st_compliane_risk_ref
 * @Author: yzh
 * @Date:   2023-03-30
 * @Version: V1.0
 */
@Service
public class StComplianeRiskRefServiceImpl extends ServiceImpl<StComplianeRiskRefMapper, StComplianeRiskRef> implements IStComplianeRiskRefService {

    @Autowired
    private IStRiskEventService iStRiskEventService;

    @Autowired
    private IStRiskScenarioService iStRiskScenarioService;

    @Override
    public <E extends IPage<StComplianeRiskRef>> E page(E page, Wrapper<StComplianeRiskRef> queryWrapper) {
        IPage<StComplianeRiskRef> iPage = super.page(page, queryWrapper);
        List<StComplianeRiskRef> list = iPage.getRecords();
        List<StComplianeRiskRef> records = new ArrayList<>();
        for(StComplianeRiskRef stComplianeRiskRef:list){
            StComplianeRiskRefSearchVo stComplianeRiskRefSearchVo = new StComplianeRiskRefSearchVo();
            BeanUtils.copyProperties(stComplianeRiskRef,stComplianeRiskRefSearchVo);
            StRiskEventVo stRiskEvent = iStRiskEventService.queryVoByRiskNo(stComplianeRiskRef.getRiskNo());
            if(stRiskEvent!=null){
                stComplianeRiskRefSearchVo.setStRiskEvent(stRiskEvent);
                StRiskScenarioVo stRiskScenario = iStRiskScenarioService.queryVoByScenario(stRiskEvent.getScenarioNo());
                if(stRiskScenario!=null){
                    stComplianeRiskRefSearchVo.setStRiskScenario(stRiskScenario);
                }
            }
            records.add(stComplianeRiskRefSearchVo);
        }
        iPage.setRecords(records);
        return (E) iPage;
    }

    @Override
    public boolean save(StComplianeRiskRef entity) {
        StComplianeRiskRefVo stComplianeRiskRefVo = (StComplianeRiskRefVo)entity;
        List<Integer> riskIdList = stComplianeRiskRefVo.getIntRiskIds();
        boolean result = false;
        for(Integer id:riskIdList){
           StRiskEvent stRiskEvent = iStRiskEventService.getById(id);
           if(stRiskEvent==null){
               throw new JeecgBootException("风险事件不存在，请重新查询选择");
           }
            checkRiskRef(entity.getComplianeNo(),stRiskEvent.getRiskEventNo());
            StComplianeRiskRef stComplianeRisk = new StComplianeRiskRef();
            stComplianeRisk.setComplianeNo(entity.getComplianeNo());
            stComplianeRisk.setRiskNo(stRiskEvent.getRiskEventNo());
            stComplianeRisk.setApproveFlag(StmConstans.DEFAULT_APPROVE_Flag);
            stComplianeRisk.setValid(StmConstans.DEFAULT_VALID);
            stComplianeRisk.setCreatedUser(UserUtil.getUserCode());
            result = super.save(stComplianeRisk);
        }
        return result;
    }

    private void checkRiskRef(String complianeNo, String riskEventNo) {
        QueryWrapper<StComplianeRiskRef> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPLIANE_NO",complianeNo);
        queryWrapper.eq("RISK_NO",riskEventNo);
        queryWrapper.last("limit 1");
        StComplianeRiskRef isExit = super.getOne(queryWrapper);
        if(isExit!=null){
            throw new JeecgBootException("当期风险事件关联关系已存在，请重新选择！");
        }
    }
}
