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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.jeecg.modules.stm.audit.entity.AuditLabelRelLib;
import org.jeecg.modules.stm.audit.mapper.AuditLabelRelLibMapper;
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.lable.entity.StLable;
import org.jeecg.modules.stm.base.lable.service.IStLableService;

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

/**
 * @Description: 标准库与标签关系表
 * @Author: jeecg-boot
 * @Date:   2024-08-01
 * @Version: V1.0
 */
@Service
public class AuditLabelRelLibServiceImpl extends ServiceImpl<AuditLabelRelLibMapper, AuditLabelRelLib> implements IAuditLabelRelLibService {

    @Autowired
    private IStLableService stLableService;
    @Override
    public Map<Integer,StringBuffer> getLablesByTids(List<Integer> tids) {
        Map<Integer,StringBuffer> retMap = new HashMap<>();
        QueryWrapper<AuditLabelRelLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("tid",tids);
        List<AuditLabelRelLib> list = this.list(queryWrapper);
        for(AuditLabelRelLib label:list){
            Integer tid = label.getTid();
            String labelName = label.getLabelName();
            String labelCode = label.getLabelCode();
            StringBuffer tmpSb = retMap.get(tid);
            if(tmpSb==null){
                tmpSb = new StringBuffer();
                tmpSb.append(labelName).append("|").append(labelCode);
                retMap.put(tid,tmpSb);
            }  else {
                int p = tmpSb.indexOf("|");
                StringBuffer tmpSb2 = new StringBuffer();
                String strName = tmpSb.substring(0,p) + ","+labelName;
                String strCode = tmpSb.substring(p+1)+"," + labelCode;
                tmpSb2.append(strName).append("|").append(strCode);
                retMap.put(tid,tmpSb2);
            }
        }
        return retMap;
    }

    @Override
    public void saveLabelRel(String labelCodes,Integer tid,Integer tp) {
        QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("lable_code",labelCodes.split(","));
        List<StLable> list = getLableByCodes(labelCodes);
        List<AuditLabelRelLib> saveList = new ArrayList<>();
        for(int i=0;i<list.size();i++){
            StLable stLable =list.get(i);
            AuditLabelRelLib labelRel = new AuditLabelRelLib();
            labelRel.setLabelCode(stLable.getLableCode());
            labelRel.setLabelName(stLable.getLableName());
            labelRel.setTp(tp);
            labelRel.setTid(tid);
            saveList.add(labelRel);
        }
        this.saveBatch(saveList);
    }

    private List<StLable> getLableByCodes(String codes) {
        QueryWrapper<StLable> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("lable_code",codes.split(","));
        List<StLable> list = stLableService.list(queryWrapper);
        return list;
    }

    @Override
    public void deleteLabelRel(Integer tid,Integer tp) {
        QueryWrapper<AuditLabelRelLib> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tid",tid).eq("tp",tp);
        this.remove(queryWrapper);
    }

}
