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

import jakarta.servlet.http.HttpServletRequest;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.baosong.entity.BaosongTask;
import org.jeecg.modules.stm.baosong.entity.BaosongTaskAlloc;
import org.jeecg.modules.stm.baosong.entity.BaosongTpl;
import org.jeecg.modules.stm.baosong.mapper.BaosongTaskMapper;
import org.jeecg.modules.stm.baosong.service.IBaosongTaskAllocService;
import org.jeecg.modules.stm.baosong.service.IBaosongTaskReviewService;
import org.jeecg.modules.stm.baosong.service.IBaosongTaskService;
import org.jeecg.modules.stm.baosong.service.IBaosongTplService;
import org.jeecg.modules.stm.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description: 任务表
 * @Author: jeecg-boot
 * @Date:   2024-12-17
 * @Version: V1.0
 */
@Service
public class BaosongTaskServiceImpl extends ServiceImpl<BaosongTaskMapper, BaosongTask> implements IBaosongTaskService {

    @Autowired
    private IBaosongTaskAllocService baosongTaskAllocService;
    @Autowired
    private IBaosongTplService baosongTplService;

    @Override
    public void updateTaskSta(Integer sta,Integer taskId) {
        BaosongTask baosongTask = new BaosongTask();
        baosongTask.setSta(sta);
        baosongTask.setId(taskId);
        this.updateById(baosongTask);
    }

    @Override
    public List<BaosongTpl> findUserRightForTpl(Integer taskId) {
        List<BaosongTpl> retTplList = new ArrayList<>();

        try {
            List<BaosongTaskAlloc> allocTplList = baosongTaskAllocService.lambdaQuery()
                    .eq(BaosongTaskAlloc::getFillUser,  UserUtil.getUserId())
                    .eq(BaosongTaskAlloc::getTaskid,taskId)
                    .list();
            Set<Integer> tplIdSet = new HashSet<>();
            for (BaosongTaskAlloc alloc : allocTplList) {
                Integer tplId = alloc.getTplid();
                if (tplId != null) {
                    tplIdSet.add(tplId);
                }
            }

            if (!tplIdSet.isEmpty()) {
                retTplList = baosongTplService.lambdaQuery()
                        .in(BaosongTpl::getId, tplIdSet)
                        .list();
            }

        } catch (Exception e) {
            log.error("查询用户模板权限失败", e);
        }

        return retTplList;
    }

}
