package com.qmth.sop.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qmth.sop.business.bean.result.FlowTaskSmsResult;
import com.qmth.sop.business.bean.result.WorkTaskResult;
import com.qmth.sop.business.entity.TSJobRemind;
import com.qmth.sop.business.mapper.TSJobRemindMapper;
import com.qmth.sop.business.service.TSJobRemindService;
import com.qmth.sop.common.enums.JobQuartzTypeEnum;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;

/**
 * <p>
 * 任务提醒表 服务实现类
 * </p>
 *
 * @author wangliang
 * @since 2023-08-16
 */
@Service
public class TSJobRemindServiceImpl extends ServiceImpl<TSJobRemindMapper, TSJobRemind> implements TSJobRemindService {

    /**
     * 保存任务提醒
     *
     * @param tsJobRemindList
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean saveJobRemind(List<TSJobRemind> tsJobRemindList) {
        return this.saveBatch(tsJobRemindList);
    }

    /**
     * 根据taskId更新任务提醒
     *
     * @param taskId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean updateJobRemind(Long taskId) {
        return this.update(new UpdateWrapper<TSJobRemind>().lambda()
                .eq(TSJobRemind::getObjId, taskId)
                .set(TSJobRemind::getEnable, false));
    }

    /**
     * 根据crmNo更新任务提醒
     *
     * @param crmNo
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public Boolean updateJobRemind(String crmNo) {
        return this.update(new UpdateWrapper<TSJobRemind>().lambda()
                .eq(TSJobRemind::getCrmNo, crmNo).eq(TSJobRemind::getType, JobQuartzTypeEnum.FLOW)
                .set(TSJobRemind::getEnable, false));
    }

    /**
     * 获取流程任务发送短信内容
     *
     * @param id
     * @return
     */
    @Override
    public FlowTaskSmsResult getFlowTaskRemindSmsInfo(Long id) {
        return this.baseMapper.getFlowTaskRemindSmsInfo(id);
    }

    /**
     * 根据用户id和sop单号集合查找处理时限
     *
     * @param userId
     * @param sopNoSet
     * @return
     */
    @Override
    public List<WorkTaskResult> getJobRemindProcessHour(Long userId, Set<String> sopNoSet) {
        return this.baseMapper.getJobRemindProcessHour(userId, sopNoSet);
    }
}
