package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskSendMsgD;
import com.ruoyi.hywz.mapper.AdsMatHyxtTaskSendMsgDMapper;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskSendMsgDService;
import com.ruoyi.hywz.utils.IdGen;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 定时提醒任务表 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-04-15 11:23:49
 */
@Service
public class AdsMatHyxtTaskSendMsgDServiceImpl extends ServiceImpl<AdsMatHyxtTaskSendMsgDMapper,AdsMatHyxtTaskSendMsgD> implements IAdsMatHyxtTaskSendMsgDService {

    @Override
    public AdsMatHyxtTaskSendMsgD openTask(String applyId, String nodeLabel, String msg, SysUser owner) {
        Date date = new Date();
        // 判断是否已经存在，存在则更新
        AdsMatHyxtTaskSendMsgD msgD = this.checkExist(applyId, nodeLabel, owner);
        if (null != msgD) {
            // 更新
            msgD.setMsgContent(msg);
            msgD.setUpdateTime(date);
            baseMapper.updateById(msgD);
        } else {
            // 新增
            msgD = new AdsMatHyxtTaskSendMsgD();
            msgD.setId(IdGen.get32UUID());
            msgD.setApplyId(applyId);
            msgD.setMsgContent(msg);
            msgD.setNodeLabel(nodeLabel);
            msgD.setStatus(1);
            msgD.setOwnerUserId(owner.getUserId());
            msgD.setOwnerUser(owner.getUserName());
            msgD.setOwnerUserName(owner.getNickName());
            msgD.setOwnerUserPhone(owner.getPhonenumber());
            msgD.setCreateTime(date);
            baseMapper.insert(msgD);
        }
        // 关闭这个单据的其它环节的任务，只保留当前一个任务运行中
        this.closeOtherTask(applyId, nodeLabel);
        return msgD;
    }

    @Override
    public void closeOtherTask(String applyId, String nodeLabel) {
        LambdaUpdateWrapper<AdsMatHyxtTaskSendMsgD> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdsMatHyxtTaskSendMsgD::getApplyId, applyId);
        updateWrapper.ne(AdsMatHyxtTaskSendMsgD::getNodeLabel, nodeLabel);
        updateWrapper.set(AdsMatHyxtTaskSendMsgD::getStatus, 0);
        this.update(updateWrapper);
    }

    @Override
    public List<AdsMatHyxtTaskSendMsgD> getTask(Integer status) {
        LambdaQueryWrapper<AdsMatHyxtTaskSendMsgD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getStatus, status);
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getIsDeleted, 0);
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public void closeTask(String applyId) {
        LambdaUpdateWrapper<AdsMatHyxtTaskSendMsgD> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdsMatHyxtTaskSendMsgD::getApplyId, applyId);
        updateWrapper.set(AdsMatHyxtTaskSendMsgD::getStatus, 0);
        updateWrapper.set(AdsMatHyxtTaskSendMsgD::getUpdateTime, new Date());
        this.update(updateWrapper);
    }

    /**
     * 判断是否已存在
     */
    public AdsMatHyxtTaskSendMsgD checkExist(String applyId, String nodeLabel, SysUser owner) {
        LambdaQueryWrapper<AdsMatHyxtTaskSendMsgD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getApplyId, applyId);
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getStatus, 1);
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getNodeLabel, nodeLabel);
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getOwnerUserId, owner.getUserId());
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getOwnerUser, owner.getUserName());
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getOwnerUserName, owner.getNickName());
        queryWrapper.eq(AdsMatHyxtTaskSendMsgD::getOwnerUserPhone, owner.getPhonenumber());
        List<AdsMatHyxtTaskSendMsgD> list = baseMapper.selectList(queryWrapper);
        if (!list.isEmpty()) {
            return list.get(0);
        }
        return null;
    }
}
