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.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hywz.common.enums.InvoiceNodeEnum;
import com.ruoyi.hywz.entity.AdsMatHyxtInvoiceApplyD;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskHistoryD;
import com.ruoyi.hywz.entity.AdsMatHyxtTaskRunD;
import com.ruoyi.hywz.mapper.AdsMatHyxtTaskRunDMapper;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskHistoryDService;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskRunDService;
import com.ruoyi.hywz.utils.IdGen;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 待办任务表 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-04-08 14:25:45
 */
@Service
public class AdsMatHyxtTaskRunDServiceImpl extends ServiceImpl<AdsMatHyxtTaskRunDMapper, AdsMatHyxtTaskRunD> implements IAdsMatHyxtTaskRunDService {

    @Autowired
    private IAdsMatHyxtTaskHistoryDService adsMatHyxtTaskHistoryDService;

    @Override
    public Map<String, Integer> getTaskNum() {
        Map<String, Integer> result = new HashMap<>(2);
        int todoNum = 0;    // 待办任务
        int finishNum = 0;  // 已办任务

        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (ObjectUtils.isNotEmpty(loginUser) && StringUtils.isNotEmpty(loginUser.getUsername())) {
            LambdaQueryWrapper<AdsMatHyxtTaskRunD> queryWrapper = Wrappers.lambdaQuery();
            queryWrapper.eq(AdsMatHyxtTaskRunD::getOwnerUser, loginUser.getUsername());
            todoNum = baseMapper.selectCount(queryWrapper).intValue();
        }

        result.put("todoNum", todoNum);
        result.put("finishNum", finishNum);
        return result;
    }

    @Override
    public void createInvoiceTask(AdsMatHyxtInvoiceApplyD applyD, SysUser owner) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Date date = new Date();
        // 【开票申请】申请编号：20240409102223742
        String taskName = "【开票申请】申请编号："+applyD.getAppNo();

        AdsMatHyxtTaskRunD taskRunD = new AdsMatHyxtTaskRunD();
        taskRunD.setBusinessId(applyD.getId());
        taskRunD.setTaskName(taskName);
        taskRunD.setTaskType(0);
        taskRunD.setApplyUserId(loginUser.getUser().getUserId());
        taskRunD.setApplyUser(loginUser.getUser().getUserName());
        taskRunD.setApplyUserName(loginUser.getUser().getNickName());
        taskRunD.setApplyUserPhone(loginUser.getUser().getPhonenumber());
        taskRunD.setCurrentNode(InvoiceNodeEnum.NODE_2.getNode());
        taskRunD.setNodeLabel(InvoiceNodeEnum.NODE_2.getDesc());

        taskRunD.setOwnerUser(owner.getUserName());
        taskRunD.setOwnerUserId(owner.getUserId());
        taskRunD.setOwnerUserName(owner.getNickName());
        taskRunD.setOwnerUserPhone(owner.getPhonenumber());
        taskRunD.setCreateTime(date);

        // 保存
        this.save(taskRunD);

        AdsMatHyxtTaskHistoryD historyD = new AdsMatHyxtTaskHistoryD();
        historyD.setId(IdGen.get32UUID());
        historyD.setBusinessId(applyD.getId());
        historyD.setTaskName(taskName);
        historyD.setTaskType(0);
        historyD.setApplyUserId(loginUser.getUser().getUserId());
        historyD.setApplyUser(loginUser.getUser().getUserName());
        historyD.setApplyUserName(loginUser.getUser().getNickName());
        historyD.setCurrentNode(InvoiceNodeEnum.NODE_0.getNode());
        historyD.setNodeLabel(InvoiceNodeEnum.NODE_0.getDesc());
        historyD.setOwnerUserId(loginUser.getUser().getUserId());
        historyD.setOwnerUser(loginUser.getUser().getUserName());
        historyD.setOwnerUserName(loginUser.getUser().getNickName());
        historyD.setOwnerUserPhone(loginUser.getUser().getPhonenumber());
        historyD.setAuditTime(date);
        historyD.setAuditResult("提交");
        historyD.setCreateTime(date);
        // 加入历史
        adsMatHyxtTaskHistoryDService.save(historyD);
    }

    @Override
    public void delete(String businessId) {
        LambdaQueryWrapper<AdsMatHyxtTaskRunD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskRunD::getBusinessId, businessId);
        baseMapper.delete(queryWrapper);
    }

    @Override
    public void delete(String businessId, InvoiceNodeEnum node) {
        LambdaQueryWrapper<AdsMatHyxtTaskRunD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskRunD::getBusinessId, businessId);
        queryWrapper.eq(AdsMatHyxtTaskRunD::getCurrentNode, node.getNode());
        baseMapper.delete(queryWrapper);
    }

    @Override
    public AdsMatHyxtTaskRunD getOneByBusinessId(String businessId) {
        LambdaQueryWrapper<AdsMatHyxtTaskRunD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskRunD::getBusinessId, businessId);
        queryWrapper.eq(AdsMatHyxtTaskRunD::getIsDeleted, 0);
        queryWrapper.last("limit 1");
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateTask(String id, Integer currentNode, String nodeLabel, SysUser applyUser, SysUser ownerUser) {
        Date date = new Date();
        LambdaUpdateWrapper<AdsMatHyxtTaskRunD> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(AdsMatHyxtTaskRunD::getId, id);
        updateWrapper.set(AdsMatHyxtTaskRunD::getCurrentNode, currentNode);
        updateWrapper.set(AdsMatHyxtTaskRunD::getNodeLabel, nodeLabel);
        updateWrapper.set(AdsMatHyxtTaskRunD::getApplyUserId, applyUser.getUserId());
        updateWrapper.set(AdsMatHyxtTaskRunD::getApplyUser, applyUser.getUserName());
        updateWrapper.set(AdsMatHyxtTaskRunD::getApplyUserName, applyUser.getNickName());
        updateWrapper.set(AdsMatHyxtTaskRunD::getApplyUserPhone, applyUser.getPhonenumber());
        updateWrapper.set(AdsMatHyxtTaskRunD::getOwnerUserId, ownerUser.getUserId());
        updateWrapper.set(AdsMatHyxtTaskRunD::getOwnerUser, ownerUser.getUserName());
        updateWrapper.set(AdsMatHyxtTaskRunD::getOwnerUserName, ownerUser.getNickName());
        updateWrapper.set(AdsMatHyxtTaskRunD::getOwnerUserPhone, ownerUser.getPhonenumber());
        updateWrapper.set(AdsMatHyxtTaskRunD::getCreateTime, date);
        updateWrapper.set(AdsMatHyxtTaskRunD::getUpdateTime, date);
        this.update(updateWrapper);
    }

    @Override
    public void auditRollback(AdsMatHyxtTaskHistoryD historyD, AdsMatHyxtTaskRunD taskRunD) {
        Date date = new Date();
        taskRunD.setApplyUserId(historyD.getApplyUserId());
        taskRunD.setApplyUser(historyD.getApplyUser());
        taskRunD.setApplyUserName(historyD.getApplyUserName());
        taskRunD.setNodeLabel(historyD.getNodeLabel());
        taskRunD.setOwnerUserId(historyD.getOwnerUserId());
        taskRunD.setOwnerUser(historyD.getOwnerUser());
        taskRunD.setOwnerUserName(historyD.getOwnerUserName());
        taskRunD.setStatus(0);
        taskRunD.setCreateTime(date);
        taskRunD.setCurrentNode(historyD.getCurrentNode());
        baseMapper.updateById(taskRunD);
    }

    @Override
    public void createTask(String taskId, SysUser ownerUser) {
        AdsMatHyxtTaskRunD taskRunD = baseMapper.selectById(taskId);
        if (null != taskRunD) {
            AdsMatHyxtTaskRunD newTask = new AdsMatHyxtTaskRunD();
            BeanUtils.copyProperties(taskRunD, newTask);
            newTask.setId(IdGen.get32UUID());
            newTask.setBranchType(1);
            newTask.setOwnerUserId(ownerUser.getUserId());
            newTask.setOwnerUser(ownerUser.getUserName());
            newTask.setOwnerUserName(ownerUser.getNickName());
            baseMapper.insert(newTask);
        }
    }

}
