package org.jeecg.modules.stm.audit.service.impl;

import org.jeecg.modules.stm.audit.entity.AuditAct;
import org.jeecg.modules.stm.audit.mapper.AuditActMapper;
import org.jeecg.modules.stm.audit.service.IAuditActService;
import org.jeecg.modules.stm.audit.service.IAuditLabelRelLibService;
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.base.domain.service.IStDomainService;

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

/**
 * @Description: 控制项表
 * @Author: jeecg-boot
 * @Date:   2024-08-31
 * @Version: V1.0
 */
@Service
public class AuditActServiceImpl extends ServiceImpl<AuditActMapper, AuditAct> implements IAuditActService {
    @Autowired
    private IStDomainService stDomainService;
    @Autowired
    private IAuditLabelRelLibService stLabelRelLibService;

    public List<AuditAct> queryAllAct(){
        List<AuditAct> actList = this.list();
        List<String> domainCodeList = new ArrayList<>();
        List<Integer> actIds = new ArrayList<>();
        for (AuditAct act : actList) {
            actIds.add(act.getId());
            String domainCode = act.getDomainCode();
            if(domainCode!=null &&!domainCode.equals("")) {
                domainCodeList.add(domainCode);
            }
        }

        Map<String, String> domainCodeNameMap = new HashMap<>();
        if(domainCodeList.size()>0) {
            domainCodeNameMap = stDomainService.getAllParentCode(domainCodeList);
        }

        if(actIds.size()>0) {
            Map<Integer, StringBuffer> idLabelMap = stLabelRelLibService.getLablesByTids(actIds);
            for (AuditAct act : actList) {
                Integer tid = act.getId();
                if(idLabelMap!=null && idLabelMap.get(tid)!=null) {
                    StringBuffer sbTmp  =  idLabelMap.get(tid);
                    int p = sbTmp.indexOf("|");
                    String labelNames = sbTmp.substring(0,p);
                    String labelCodes= sbTmp.substring(p+1);
                    act.setLabels(labelNames);
                    act.setLabelCodes(labelCodes);
                }

                if(act.getDomainCode()!=null && domainCodeNameMap!=null) {
                    act.setDomainName(domainCodeNameMap.get(act.getDomainCode()));
                }
            }
        }

        return actList;
    }
}
