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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.stm.act.main.entity.StCaMain;
import org.stm.act.main.service.IStCaMainService;
import org.stm.compliane.info.entity.StComplianeInfo;
import org.stm.compliane.info.service.IStComplianeInfoService;
import org.stm.compliane.item.entity.StComplianeItem;
import org.stm.compliane.item.mapper.StComplianeItemMapper;
import org.stm.compliane.item.service.IStComplianeItemService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.stm.compliane.item.vo.StCaManComplianeVo;
import org.stm.compliane.item.entity.StComplianeItemCa;

import org.stm.compliane.item.vo.StComplianeItemVo;

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

/**
 * @Description: 合规条目
 * @Author: yzh
 * @Date:   2023-03-05
 * @Version: V1.0
 */
@Service
public class StComplianeItemServiceImpl extends ServiceImpl<StComplianeItemMapper, StComplianeItem> implements IStComplianeItemService {

    @Autowired
    private IStCaMainService iStCaMainService;
    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private StComplianeItemMapper stComplianeItemMapper;

    @Override
    public <E extends IPage<StComplianeItem>> E page(E page, Wrapper<StComplianeItem> queryWrapper) {
        IPage<StComplianeItem> itemIPage = super.page(page, queryWrapper);
        List<StComplianeItem> data = itemIPage.getRecords();
        List<StComplianeItem> records = new ArrayList<>();
        for(StComplianeItem stComplianeItem:data){
            StComplianeItemVo stComplianeItemVo = new StComplianeItemVo();
            BeanUtils.copyProperties(stComplianeItem,stComplianeItemVo);
            StCaMain stCaMain = iStCaMainService.queryByCaNo(stComplianeItem.getCaNo());
            if(stCaMain!=null) {
                stComplianeItemVo.setCaName(stCaMain.getCaName());
            }else{
                stComplianeItemVo.setCaName("-");
            }
            String applyName = iSysDictService.queryDictTextByKey("applyobj",stComplianeItem.getApplicableCode());
            if(StringUtils.isNotEmpty(applyName)) {
                stComplianeItemVo.setApplicableName(applyName);
            }else{
                stComplianeItemVo.setApplicableName("-");
            }
            records.add(stComplianeItemVo);
        }
        itemIPage.setRecords(records);
        return (E) itemIPage;
    }

    @Override
    public IPage<StComplianeItem> caPage(Page<StComplianeItem> page, QueryWrapper<StComplianeItem> queryWrapper) {
        IPage<StComplianeItem> itemIPage = super.page(page, queryWrapper);
        List<StComplianeItem> data = itemIPage.getRecords();
        List<StComplianeItem> caItemList = new ArrayList<StComplianeItem>();
        for(StComplianeItem stComplianeItem:data){
            StCaManComplianeVo stCaManComplianeVo = new StCaManComplianeVo();
            BeanUtils.copyProperties(stComplianeItem,stCaManComplianeVo);
            QueryWrapper<StComplianeInfo> complianeInfoQueryWrapper = new QueryWrapper<>();
            complianeInfoQueryWrapper.eq("COMPLIANE_NO",stComplianeItem.getComplianeNo());
            StComplianeInfo stComplianeInfo = ((IStComplianeInfoService)SpringContextUtils.getBean(IStComplianeInfoService.class)).getOne(complianeInfoQueryWrapper);
            if(stComplianeInfo!=null){
                stCaManComplianeVo.setFileName(stComplianeInfo.getFileName());
                stCaManComplianeVo.setFileUploadPath(stComplianeInfo.getFileUploadPath());
            }
            String applyName = iSysDictService.queryDictTextByKey("applyobj",stComplianeItem.getApplicableCode());
            stCaManComplianeVo.setApplicableName(applyName);
            caItemList.add(stCaManComplianeVo);
        }
        itemIPage.setRecords(caItemList);
        return itemIPage;
    }

    private void checkMoreRef(StComplianeItem entity){
        QueryWrapper<StComplianeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPLIANE_NO",entity.getComplianeNo());
        queryWrapper.eq("CA_NO",entity.getCaNo());
        long count = this.count(queryWrapper);
        if(count>0){
            throw new JeecgBootException("合规条目控制活动关联关系已存在");
        }
    }


    public void checkName(StComplianeItem entity,Boolean isUpdate){
        QueryWrapper<StComplianeItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("COMPLIANE_ITEM",entity.getComplianeItem());
        queryWrapper.eq("COMPLIANE_NO",entity.getComplianeNo());
        if(isUpdate) {
            queryWrapper.ne("ID", entity.getId());
        }
        long count = this.count(queryWrapper);
        if(count>0){
            throw new JeecgBootException("合规条目已存在");
        }
    }

    @Override
    public boolean save(StComplianeItem entity) {
        checkMoreRef(entity);
        checkName(entity,false);
        return super.save(entity);
    }

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

    @Override
    public IPage<StComplianeItem> queryStComplianeItemsWithPage(Page<StComplianeItem> page, StComplianeItem stComplianeItem){
        IPage<StComplianeItem> data = this.baseMapper.queryStComplianeItems(page,stComplianeItem);
        return data;
    }

    @Override
    public void insertItemCa(StComplianeItemCa stComplianeItemCa) {
        this.baseMapper.insertItemCa(stComplianeItemCa);
    }

    @Override
    public void deleteItemCa(StComplianeItemCa stComplianeItemCa) {
        this.baseMapper.deleteItemCa(stComplianeItemCa);
    }

}
