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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.stm.audit.entity.AuditActRelOthers;
import org.jeecg.modules.stm.audit.mapper.AuditActRelOthersMapper;
import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
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 jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 控制活动关系表
 * @Author: jeecg-boot
 * @Date:   2024-06-17
 * @Version: V1.0
 */
@Service
public class AuditActRelOthersServiceImpl extends ServiceImpl<AuditActRelOthersMapper, AuditActRelOthers> implements IAuditActRelOthersService {
    @Autowired
    private IStDomainService stDomainService;
    @Autowired
    private IAuditLabelRelLibService stLabelRelLibService;
    @Autowired
    private IAuditActService auditActService;
    @Override
    public  List<Map<String,Object>> queryActAndComplianeItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndComplianeItems(idList);
        return list;
    }

    @Override
    public  List<Map<String,Object>> queryActAndInnerControlItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndInnerControlItems(idList);
        return list;
    }

    @Override
    public  List<Map<String,Object>> queryActAndInnerStrategyItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndInnerStrategyItems(idList);
        return list;
    }
    @Override
    public  List<Map<String,Object>> queryActAndProblemItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndProblemItems(idList);
        return list;
    }
    @Override
    public  List<Map<String,Object>> queryActAndRiskItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndRiskItems(idList);
        return list;
    }
    @Override
    public  List<Map<String,Object>> queryActAndCheckItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndCheckItems(idList);
        return list;
    }

    @Override
    public  List<Map<String,Object>> queryActAndPracticeItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndPracticeItems(idList);
        return list;
    }

    @Override
    public  List<Map<String,Object>> queryActAndRiskScenarioItems(List<Integer> idList){
        List<Map<String,Object>> list = this.baseMapper.queryActAndRiskScenarioItems(idList);
        return list;
    }

    @Override
    public  List<Map<String,Object>> queryAllAct(){
        List<Map<String,Object>> actList = this.baseMapper.queryAllAct();
        List<String> domainCodeList = new ArrayList<>();
        List<Integer> actIds = new ArrayList<>();
        for (Map<String,Object> act : actList) {
            actIds.add(Integer.parseInt(act.get("id").toString()));
            if(act.get("domainCode")==null)continue;
            String domainCode = act.get("domainCode").toString();
            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 (Map<String,Object> act : actList) {
                Integer tid = new Integer(act.get("id").toString());
                if(act.get("domainCode")==null)continue;
                if(domainCodeNameMap==null) continue;
                String domainCode = act.get("domainCode").toString();
                act.put("domainName",domainCodeNameMap.get(domainCode));
            }
        }

        return actList;
    }

    @Override
    public  List<Map<String,Object>> queryAllLabels(){
        List<Map<String,Object>> list = this.baseMapper.queryAllLabels();
        return list;
    }

    @Override
    public void deleteActRel(String tids,Integer tp) {
        String[] tidAry = tids.split(",");
        QueryWrapper<AuditActRelOthers> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("tid",tidAry).eq("tp",tp);
        this.remove(queryWrapper);
    }

    @Override
    public Map<Integer,String> queryAllActByTids(List<Integer> tidList,Integer tp){
        List<Map<String,Object>> dataList = this.baseMapper.queryAllActByTids(tidList,tp);
        Map<Integer,String> retMap = new HashMap<>();
        for (Integer tId : tidList) {
            StringBuffer sb = new StringBuffer();
            StringBuffer sbId = new StringBuffer();
            for (Map<String, Object> map : dataList) {
                Integer actId = Integer.valueOf(map.get("actId").toString());
                Integer tId1 = Integer.valueOf(map.get("tid").toString());
                if(tId.intValue()==tId1.intValue()) {
                    sb.append(",").append(map.get("caName").toString());
                    sbId.append(",").append(actId);
                }
            }
            if(sb.length()>0) {
                String strTmp  = sb.substring(1) + "|"+sbId.substring(1);
                retMap.put(tId,strTmp);
            }
        }

        return retMap;
    }

    public void setActRelations(List<AuditActRelOthers> actRelList,String actIds,Integer tid,Integer tp) {
        if(actIds==null||tid==null) return;
        String[] actIdAry = actIds.split(",");
        for(String actId : actIdAry) {
            if(actId==null||actId.equals("")) continue;
            AuditActRelOthers actRel = new AuditActRelOthers();
            actRel.setTp(tp);
            actRel.setActId(Integer.valueOf(actId));
            actRel.setTid(tid);
            actRelList.add(actRel);
        }

    }

}
