package com.ruoyi.hywz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
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.entity.vo.NodeVo;
import com.ruoyi.hywz.mapper.AdsMatHyxtTaskHistoryDMapper;
import com.ruoyi.hywz.service.IAdsMatHyxtInvoiceApplyDService;
import com.ruoyi.hywz.service.IAdsMatHyxtTaskHistoryDService;
import com.ruoyi.hywz.utils.IdGen;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 已办任务表 服务实现类
 * </p>
 *
 * @author mybatisplus-generator
 * @date 2024-04-08 14:25:45
 */
@Service
public class AdsMatHyxtTaskHistoryDServiceImpl extends ServiceImpl<AdsMatHyxtTaskHistoryDMapper, AdsMatHyxtTaskHistoryD> implements IAdsMatHyxtTaskHistoryDService {

    @Autowired
    private IAdsMatHyxtInvoiceApplyDService adsMatHyxtInvoiceApplyDService;

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

    @Override
    public List<NodeVo> getLogList(String businessId) {
        List<NodeVo> result = new ArrayList<>();
        LambdaQueryWrapper<AdsMatHyxtTaskHistoryD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskHistoryD::getBusinessId, businessId);
        queryWrapper.eq(AdsMatHyxtTaskHistoryD::getIsDeleted, 0);
        queryWrapper.orderByDesc(AdsMatHyxtTaskHistoryD::getCreateTime);
        List<AdsMatHyxtTaskHistoryD> list = baseMapper.selectList(queryWrapper);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        AdsMatHyxtInvoiceApplyD applyD = adsMatHyxtInvoiceApplyDService.getById(businessId);
        if (applyD.getCurrentNode().equals(InvoiceNodeEnum.NODE_0.getNode())) {
            NodeVo firstNode = new NodeVo();
            firstNode.setTitle("【未审核】");
            firstNode.setMsg2("手机号码：" + applyD.getCreateUserPhone());
            firstNode.setMsg("审批人：" + applyD.getCreateUserName());
            firstNode.setDate(dateFormat.format(new Date()));
            result.add(firstNode);
        } else if (!applyD.getCurrentNode().equals(InvoiceNodeEnum.END.getNode())) {
            NodeVo firstNode = new NodeVo();
            firstNode.setTitle("【未审核】");
            firstNode.setMsg2("手机号码：" + applyD.getCurrentAuditUserPhone());
            firstNode.setMsg("审批人：" + applyD.getCurrentAuditUserName());
            firstNode.setDate(dateFormat.format(new Date()));
            result.add(firstNode);
        }

        for (AdsMatHyxtTaskHistoryD historyD : list) {
            NodeVo node = new NodeVo();
            node.setTitle("【"+historyD.getNodeLabel()+"】");
            node.setDate(dateFormat.format(historyD.getCreateTime()));
            node.setMsg(historyD.getOwnerUserName()+" "+historyD.getAuditResult()+"于 "+dateFormat2.format(historyD.getCreateTime()));
            if (StringUtils.isNotEmpty(historyD.getAuditMsg())) {
                node.setMsg(node.getMsg() + "("+historyD.getAuditMsg()+")");
            }
            node.setMsg2("手机号码："+historyD.getOwnerUserPhone());
//            node.setIcon(this.getIcon());
            node.setColor(this.getColor());
            result.add(node);
        }
        return result;
    }
    private String getColor() {
        return "#0bbd87";
    }

    @Override
    public void auditOkSave(AdsMatHyxtTaskRunD currentTask) {
        Date date = new Date();
        AdsMatHyxtTaskHistoryD historyD = new AdsMatHyxtTaskHistoryD();
        BeanUtils.copyProperties(currentTask, historyD);
        historyD.setId(IdGen.get32UUID());
        historyD.setAuditTime(date);
        historyD.setAuditResult("确认");
        historyD.setStatus(1);
        historyD.setCreateTime(date);
        historyD.setUpdateTime(date);
        this.save(historyD);
    }

    @Override
    public void auditFailSave(AdsMatHyxtTaskRunD currentTask, String auditMsg) {
        Date date = new Date();
        AdsMatHyxtTaskHistoryD historyD = new AdsMatHyxtTaskHistoryD();
        BeanUtils.copyProperties(currentTask, historyD);
        historyD.setId(IdGen.get32UUID());
        historyD.setAuditTime(date);
        historyD.setAuditResult("退回");
        historyD.setStatus(1);
        historyD.setCreateTime(date);
        historyD.setUpdateTime(date);
        if (StringUtils.isNotEmpty(auditMsg)) {
            historyD.setAuditMsg(auditMsg);
        }
        this.save(historyD);
    }

    @Override
    public AdsMatHyxtTaskHistoryD getOneNode(String businessId, Integer currentNode) {
        LambdaQueryWrapper<AdsMatHyxtTaskHistoryD> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AdsMatHyxtTaskHistoryD::getBusinessId, businessId);
        queryWrapper.eq(AdsMatHyxtTaskHistoryD::getCurrentNode, currentNode);
        queryWrapper.last("limit 1");
        queryWrapper.orderByDesc(AdsMatHyxtTaskHistoryD::getCreateTime);
        return baseMapper.selectOne(queryWrapper);
    }
}
