package com.ruoyi.approvalFlow.service.impl;

import java.util.*;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ruoyi.approvalFlow.domain.*;
import com.ruoyi.approvalFlow.mapper.*;
import com.ruoyi.approvalFlow.service.IAuditPathsService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.finance.domain.TFee;
import com.ruoyi.finance.mapper.TFeeMapper;
import com.ruoyi.finance.service.impl.TFeeServiceImpl;
import com.ruoyi.reportManagement.domain.TWhgenleg;
import com.ruoyi.reportManagement.mapper.TWhgenlegMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.warehouseBusiness.domain.TWarehouseAgreement;
import com.ruoyi.warehouseBusiness.domain.TWarehouseBills;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsfees;
import com.ruoyi.warehouseBusiness.domain.TWarehousebillsitems;
import com.ruoyi.warehouseBusiness.mapper.TWarehouseAgreementMapper;
import com.ruoyi.warehouseBusiness.mapper.TWarehouseBillsMapper;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsfeesMapper;
import com.ruoyi.warehouseBusiness.mapper.TWarehousebillsitemsMapper;
import com.ruoyi.warehouseBusiness.service.impl.TWarehouseAgreementServiceImpl;
import com.ruoyi.warehouseBusiness.service.impl.TWarehouseBillsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 审批流配置主Service业务层处理
 *
 * @author ruoyi
 * @date 2021-01-21
 */
@Service
public class AuditPathsServiceImpl implements IAuditPathsService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private AuditPathsMapper auditPathsMapper;

    @Autowired
    private AuditItemsMapper auditItemsMapper;

    @Autowired
    private TWhgenlegMapper tWhgenlegMapper;

    @Autowired
    private AuditItemsUsersMapper auditItemsUsersMapper;

    @Autowired
    private AuditPathsActsMapper auditPathsActsMapper;

    @Autowired
    private AuditPathsLevelsMapper auditPathsLevelsMapper;

    @Autowired
    private TWarehouseBillsMapper tWarehouseBillsMapper;

    @Autowired
    private TWarehouseBillsServiceImpl tWarehouseBillsService;

    @Autowired
    private TWarehousebillsfeesMapper tWarehousebillsfeesMapper;

    @Autowired
    private TWarehousebillsitemsMapper tWarehousebillsitemsMapper;

    @Autowired
    private TFeeServiceImpl tFeeServiceImpl;

    @Autowired
    private TWarehouseAgreementServiceImpl tWarehouseAgreementService;

    @Autowired
    private TFeeMapper tFeeMapper;

    @Autowired
    private TWarehouseAgreementMapper tWarehouseAgreementMapper;

    /**
     * 查询审批流配置主
     *
     * @param id 审批流配置主ID
     * @return 审批流配置主
     */
    @Override
    public AjaxResult selectAuditPathsById(Long id) {
        Map<String, Object> map = new HashMap<>();
        AuditPaths auditPaths = auditPathsMapper.selectAuditPathsById(id);
        if (StringUtils.isNull(auditPaths)) {
            return AjaxResult.error("该审批流不存在，请确认");
        }
        map.put("auditPaths", auditPaths);
        AuditPathsLevels auditPathsLevels = new AuditPathsLevels();
        auditPathsLevels.setPathId(id);
        List<AuditPathsLevels> pathsLevelsList = auditPathsLevelsMapper.selectAuditPathsLevelsList(auditPathsLevels);
        if (StringUtils.isNotNull(pathsLevelsList)) {
            List<List<Long>> longs = new ArrayList<>();
            for (AuditPathsLevels ap : pathsLevelsList) {
                List<Long> longList = new ArrayList<>();
                if (ap.getAuditUserId() != null) {
                    JSONArray jsonArray = JSONArray.parseArray(ap.getAuditUserId());
                    longList = JSONObject.parseArray(jsonArray.toJSONString(), Long.class);
                }
                longs.add(longList);
            }
            map.put("auditUserIds", longs);
            map.put("auditPathsLevels", pathsLevelsList);
        }
        return AjaxResult.success(map);
    }

    /**
     * 查询审批流配置主列表
     *
     * @param auditPaths 审批流配置主
     * @return 审批流配置主
     */
    @Override
    public List<AuditPaths> selectAuditPathsList(AuditPaths auditPaths) {
        return auditPathsMapper.selectAuditPathsList(auditPaths);
    }

    /**
     * 新增审批流配置主
     *
     * @param auditPaths 审批流配置主
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult insertAuditPaths(String auditPaths, String auditPathsLevels, LoginUser loginUser) {
        AuditPaths apath = JSONArray.parseObject(auditPaths, AuditPaths.class);
        if ("A".equals(apath.getStatus()) && (StringUtils.isNull(auditPathsLevels) || "[]".equals(auditPathsLevels))) {
            return AjaxResult.error("未找到级次信息无法提交，请确认");
        }
        long fPid = 0L;
        if (StringUtils.isNotNull(apath.getId())) {
            if ("S".equals(apath.getStatus())) {
                AuditPathsActs auditPathsActs = new AuditPathsActs();
                auditPathsActs.setPathId(apath.getId());
                int sum = auditPathsActsMapper.selectCountAuditPathsActs(auditPathsActs);
                if (sum > 0) {
                    return AjaxResult.error("该审批配置已被使用，请取消使用后提交");
                }
            }
            auditPathsLevelsMapper.deleteAuditPathsLevelsBypathId(apath.getId());
            auditPathsMapper.updateAuditPaths(apath);
            fPid = apath.getId();
        } else {
            AuditPaths paths = new AuditPaths();
            paths.setPathName(apath.getPathName());
            List<AuditPaths> audiList = auditPathsMapper.selectAuditPathsList(paths);
            if (audiList.size() > 0) {
                return AjaxResult.error("审核路径名已存在，请确认");
            }
            apath.setOpUserId(loginUser.getUser().getUserId());
            apath.setOpDate(new Date());
            apath.setOpUserName(loginUser.getUsername());
            auditPathsMapper.insertAuditPaths(apath);
            fPid = apath.getId();
        }
        if (StringUtils.isNotNull(auditPathsLevels) && !"[]".equals(auditPathsLevels)) {
            JSONArray warehouseJSON = JSONArray.parseArray(auditPathsLevels);
            List<AuditPathsLevels> auditPathsLevelsList = JSONObject.parseArray(warehouseJSON.toJSONString(), AuditPathsLevels.class);
            if ("F".equals(auditPathsLevelsList.get(auditPathsLevelsList.size() - 1).getIffinalItem())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("操作失败：最后一级状态有误，请确认");
            }
            Long level = 0L;
            boolean last = false;
            for (AuditPathsLevels lev : auditPathsLevelsList) {
                if (StringUtils.isNull(lev.getAuditUserId())) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("操作失败：请维护审核人");
                }
                if ("T".equals(lev.getIffinalItem()) && !last) {
                    last = true;
                } else if ("T".equals(lev.getIffinalItem()) && last) {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return AjaxResult.error("操作失败：最后一级只能存在一个");
                }
                level++;
                lev.setPathId(fPid);
                lev.setLevelId(level);
                auditPathsLevelsMapper.insertAuditPathsLevels(lev);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 修改审批流配置主
     *
     * @param auditPaths 审批流配置主
     * @return 结果
     */
    @Override
    public int updateAuditPaths(AuditPaths auditPaths) {
        return auditPathsMapper.updateAuditPaths(auditPaths);
    }

    /**
     * 批量删除审批流配置主
     *
     * @param ids 需要删除的审批流配置主ID
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult deleteAuditPathsByIds(Long[] ids) {
        int line = 0;
        for (Long lo : ids) {
            line++;
            AuditPathsActs auditPathsActs = new AuditPathsActs();
            auditPathsActs.setPathId(lo);
            int sum = auditPathsActsMapper.selectCountAuditPathsActs(auditPathsActs);
            if (sum > 0) {
                return AjaxResult.error("第" + line + "行，审批已被使用无法删除");
            }
        }
        auditPathsMapper.deleteAuditPathsByIds(ids);
        return AjaxResult.success();
    }

    /**
     * 删除审批流配置主信息
     *
     * @param id 审批流配置主ID
     * @return 结果
     */
    @Override
    public int deleteAuditPathsById(Long id) {
        return auditPathsMapper.deleteAuditPathsById(id);
    }

    /**
     * 单据点击查看审批流
     *
     * @param auditItems 条件
     * @return 结果
     */
    @Override
    public AjaxResult projectEndQueryPendingVal(AuditItems auditItems) {
        if (auditItems.getActId() >= 110 && auditItems.getActId() <= 150) {
            List<Map<String, Object>> mapList = auditItemsMapper.selectWarehouseApprover(auditItems);
            return AjaxResult.success(mapList);
        } else if (auditItems.getActId() >= 210 && auditItems.getActId() <= 230) {
            // 财务审批
            List<Map<String, Object>> mapList = auditItemsMapper.selectWarehouseApprover(auditItems);
            return AjaxResult.success(mapList);
        } else if (auditItems.getActId() >= 310 && auditItems.getActId() <= 320) {
            // 协议审批
            List<Map<String, Object>> mapList = auditItemsMapper.selectWarehouseApprover(auditItems);
            return AjaxResult.success(mapList);
        }
        return AjaxResult.success();
    }

    /**
     * 首页点击查看审批流 获取对应数据
     *
     * @param auditItems 查询条件
     * @return 结果
     */
    @Override
    public AjaxResult selectAllAuditItems(AuditItems auditItems) {
        return AjaxResult.success(auditItemsMapper.selectAuditItems(auditItems));
    }

    /**
     * 审批通过
     *
     * @param auditItems 审核人、单据信息
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult approved(AuditItems auditItems) {
        // 审核通过时间
        Date auditItemtem = auditItems.getAuditItem();
        long fettle = 0L;
        int number = 0;
        AuditItems item = new AuditItems();
        item.setBillId(auditItems.getId());
        item.setActId(auditItems.getActId());
        List<AuditItems> auditItemsList = auditItemsMapper.selectAuditItemsList(item);
        if (auditItemsList.size() <= 0) {
            return AjaxResult.error("未找到审批信息，请确认");
        }
        for (AuditItems at : auditItemsList) {
            if (!"S".equals(at.getAuditStatus())) {
                continue;
            }
            AuditItemsUsers itemsUsers = new AuditItemsUsers();
            itemsUsers.setPid(at.getId());
            List<AuditItemsUsers> auditItemsUsers = auditItemsUsersMapper.selectAuditItemsUsersList(itemsUsers);
            for (AuditItemsUsers au : auditItemsUsers) {
                if (au.getUserId().equals(auditItems.getSendUserId()) && "S".equals(au.getAuditStatus())) {
                    number++;
                    if (StringUtils.isEmpty(auditItems.getAuditMsg())) {
                        at.setAuditMsg("同意");
                    } else {
                        at.setAuditMsg(auditItems.getAuditMsg());
                    }
                    at.setAuditStatus("A");
                    //at.setAuditItem(new Date());
                    at.setAuditOpTime(new Date());
                    at.setAuditUserId(au.getUserId());
                    at.setAuditItem(auditItemtem);
                    int updateAuditItems = auditItemsMapper.updateAuditItems(at);
                    if (updateAuditItems <= 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("审批通过失败: 更新审批状态失败");
                    }
                    au.setAuditStatus("A");
                    int updateAuditItemsUsers = auditItemsUsersMapper.updateAuditItemsUsers(au);
                    if (updateAuditItemsUsers <= 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("审批通过失败: 更新审批状态失败");
                    }
                    if (auditItems.getActId() >= 110 && auditItems.getActId() <= 150) {
                        if ("T".equals(at.getIffinalItem())) {
                            fettle = 6L;
                        } else if ("F".equals(at.getIffinalItem())) {
                            fettle = 5L;
                        }
                        // 仓库模块
                        tWarehouseBillsMapper.warehouseFollowUpdate(auditItems.getBillId(), fettle, at.getAuditItem());
                        tWarehousebillsfeesMapper.warehouseFeesFollowUpdate(auditItems.getBillId(), fettle, at.getAuditItem());
                        if (Objects.equals(auditItems.getActId(), 150)) {
                            List<TWarehousebillsfees> feesList = tWarehousebillsfeesMapper.selectWarehousebillsfeesByPId(auditItems.getBillId());
                            if (StringUtils.isNull(feesList) || Objects.equals(feesList.size(), 0)) {
                                for (TWarehousebillsfees f : feesList) {
                                    if ("KCZZ".equals(f.getfBilltype())) {
                                        TWhgenleg whgenleg = new TWhgenleg();
                                        whgenleg.setfId(f.getSrcId());
                                        whgenleg.setfChargedate(DateUtils.dateAdd(f.getfBillingDeadline(), 1));
                                        tWhgenlegMapper.updateTWhgenleg(whgenleg);
                                    } else {
                                        TWarehousebillsitems billsItem = new TWarehousebillsitems();
                                        billsItem.setfId(f.getSrcId());
                                        billsItem.setfChargedate(DateUtils.dateAdd(f.getfBillingDeadline(), 1));
                                        if ("SJCK".equals(f.getfBilltype())) {
                                            billsItem.setfStorageFeeDeadline(f.getfBillingDeadline());
                                        }
                                        tWarehousebillsitemsMapper.updateTWarehousebillsitems(billsItem);
                                    }
                                }
                            }
                        }
                    } else if (auditItems.getActId() >= 210 && auditItems.getActId() <= 230) {
                        // 财务模块
                        if ("T".equals(at.getIffinalItem())) {
                            fettle = 6L;
                            // 更新费用明细
                            AjaxResult ajaxResult = tFeeServiceImpl.billsfeesFollow(auditItems.getBillId());
                            String code = ajaxResult.get("code").toString();
                            if ("500".equals(code)) {
                                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                                return ajaxResult;
                            }
                        } else if ("F".equals(at.getIffinalItem())) {
                            fettle = 5L;
                        }
                        // 变更财务状态
                        AjaxResult ajaxResult = tFeeServiceImpl.tfeeFollow(auditItems.getBillId(), fettle);
                        String code = ajaxResult.get("code").toString();
                        if ("500".equals(code)) {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            return ajaxResult;
                        }
                    } else if (auditItems.getActId() >= 310 && auditItems.getActId() <= 320) {
                        // 协议模块
                        if ("T".equals(at.getIffinalItem())) {
                            fettle = 6L;
                        } else if ("F".equals(at.getIffinalItem())) {
                            fettle = 5L;
                        }
                        // 变更财务状态
                        AjaxResult ajaxResult = tWarehouseAgreementService.agreementFollow(auditItems.getBillId(), fettle);
                        String code = ajaxResult.get("code").toString();
                        if ("500".equals(code)) {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            return ajaxResult;
                        }
                    }
                    if ("F".equals(at.getIffinalItem())) {
                        at.setLevelId(at.getLevelId() + 1);
                        int updateNum = auditItemsMapper.updateAuditStatus(at);
                        if (updateNum <= 0) {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            return AjaxResult.error("审批通过失败: 更新审批状态失败");
                        }
                    }
                    break;
                }
            }
        }
        if (number <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("该审批已被操作");
        }
        return AjaxResult.success();
    }

    /**
     * 审批驳回
     *
     * @param auditItems 审核人、单据信息
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult approvalRejected(AuditItems auditItems) {
        long fettle = 0L;
        if (auditItems.getActId() >= 110 && auditItems.getActId() <= 150) {
            fettle = 3L;
            tWarehouseBillsMapper.warehouseFollowUpdate(auditItems.getBillId(), fettle, auditItems.getAuditItem());
            tWarehousebillsfeesMapper.warehouseFeesFollowUpdate(auditItems.getBillId(), fettle, auditItems.getAuditItem());
            if (Objects.equals(auditItems.getActId(), 150)) {
                List<TWarehousebillsfees> feesList = tWarehousebillsfeesMapper.selectWarehousebillsfeesByPId(auditItems.getBillId());
                for (TWarehousebillsfees wareItem : feesList) {
                    if (StringUtils.isNull(wareItem.getSrcId())) {
                        continue;
                    }
                    if ("KCZZ".equals(wareItem.getfBilltype())) {
                        TWhgenleg whgenleg = new TWhgenleg();
                        whgenleg.setfId(wareItem.getSrcId());
                        whgenleg.setfChargedate(wareItem.getfChargedate());
                        int i = tWhgenlegMapper.updateTWhgenleg(whgenleg);
                        if (i <= 0) {
                            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                            return AjaxResult.error("更新库存费用状态失败，请联系管理员");
                        }
                    } else {
                        TWarehousebillsitems billsItem = new TWarehousebillsitems();
                        billsItem.setfId(wareItem.getSrcId());
                        billsItem.setfBilltype(wareItem.getfBilltype());
                        if (!"SJCK".equals(wareItem.getfBilltype())) {
                            billsItem.setfChargedate(wareItem.getfBillingDeadline());
                            tWarehousebillsitemsMapper.updateTWarehousebillsitems(billsItem);
                        } else {
                            billsItem.setfChargedate(wareItem.getfChargedate());
                        }
                        tWarehousebillsitemsMapper.agreementApprovalRejected(billsItem);
                    }
                }
            }
        } else if (auditItems.getActId() >= 210 && auditItems.getActId() <= 230) {
            fettle = 3L;
            // 财务模块
            AjaxResult ajaxResult = tFeeServiceImpl.tfeeFollow(auditItems.getBillId(), fettle);
            String code = ajaxResult.get("code").toString();
            if ("500".equals(code)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ajaxResult;
            }
        } else if (auditItems.getActId() >= 310 && auditItems.getActId() <= 320) {
            fettle = 3L;
            // 协议模块
            AjaxResult ajaxResult = tWarehouseAgreementService.agreementFollow(auditItems.getBillId(), fettle);
            String code = ajaxResult.get("code").toString();
            if ("500".equals(code)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return ajaxResult;
            }
        }
        AuditItems item = new AuditItems();
        item.setBillId(auditItems.getId());
        item.setActId(auditItems.getActId());
        List<AuditItems> auditItem = auditItemsMapper.selectAuditItemsList(item);
        int number = 0;
        for (AuditItems at : auditItem) {
            if (!"S".equals(at.getAuditStatus())) {
                continue;
            }
            AuditItemsUsers itemsUsers = new AuditItemsUsers();
            itemsUsers.setPid(at.getId());
            List<AuditItemsUsers> auditItemsUsersList = auditItemsUsersMapper.selectAuditItemsUsersList(itemsUsers);
            for (AuditItemsUsers au : auditItemsUsersList) {
                if (Objects.equals(au.getUserId(), auditItems.getSendUserId())) {
                    number++;
                    at.setAuditStatus("B");
                    if (auditItems.getAuditMsg() == null || "".equals(auditItems.getAuditMsg())) {
                        at.setAuditMsg("不同意");
                    } else {
                        at.setAuditMsg(auditItems.getAuditMsg());
                    }
                    //at.setAuditItem(new Date());
                    at.setAuditOpTime(new Date());
                    at.setAuditUserId(au.getUserId());
                    int updateAuditItems = auditItemsMapper.updateAuditItems(at);
                    if (updateAuditItems <= 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("审批驳回失败: 更新审批状态失败");
                    }
                    au.setAuditStatus("B");
                    int updateAuditItemUser = auditItemsUsersMapper.updateAuditItemsUsers(au);
                    if (updateAuditItemUser <= 0) {
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return AjaxResult.error("审批驳回失败: 更新审批状态失败");
                    }
                    if ("F".equals(at.getIffinalItem())) {
                        int deleteNum = auditItemsMapper.deleteUpLevelId(at);
                        if (deleteNum <= 0) {
                            return AjaxResult.error("未找到后续除审批信息");
                        }
                    }
                }
            }
        }
        if (number <= 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("该审批已被操作");
        }
        return AjaxResult.success();
    }

    /**
     * 审批撤销（我提交的 没人审核）
     *
     * @param auditItems 审核人、单据信息
     * @return
     */
    @Override
    @Transactional
    public AjaxResult revoke(AuditItems auditItems) {
        long fettle = 0L;
        if (auditItems.getActId() >= 110 && auditItems.getActId() <= 150) {
            fettle = 2L;
            // 查询仓库状态
            TWarehouseBills tWarehouseBills = tWarehouseBillsMapper.selectTWarehousebillsById(auditItems.getBillId());
            if (!tWarehouseBills.getfBillstatus().equals(4L)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("审批撤销失败: 已经过审批");
            }
            tWarehouseBillsMapper.warehouseFollowUpdate(auditItems.getBillId(), fettle, auditItems.getAuditItem());
            tWarehousebillsfeesMapper.warehouseFeesFollowUpdate(auditItems.getBillId(), fettle, auditItems.getAuditItem());
        } else if (auditItems.getActId() >= 210 && auditItems.getActId() <= 230) {
            fettle = 2L;
            // 查询财务状态
            TFee tFee = tFeeMapper.selectTFeeById(auditItems.getBillId());
            if (!"4".equals(tFee.getfBillstatus())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("审批撤销失败: 已经过审批");
            }
            // 财务模块
            AjaxResult ajaxResult = tFeeServiceImpl.tfeeFollow(auditItems.getBillId(), fettle);
            String code = ajaxResult.get("code").toString();
            if ("500".equals(code)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("审批撤销失败: 更新审批状态失败");
            }
        } else if (auditItems.getActId() >= 310 && auditItems.getActId() <= 320) {
            fettle = 2L;
            // 查询协议状态
            TWarehouseAgreement tWarehouseAgreement = tWarehouseAgreementMapper.selectTWarehouseAgreementById(auditItems.getBillId());
            if (!"4".equals(tWarehouseAgreement.getfBillstatus())) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("审批撤销失败: 已经过审批");
            }
            // 协议模块
            AjaxResult ajaxResult = tWarehouseAgreementService.agreementFollow(auditItems.getBillId(), fettle);
            String code = ajaxResult.get("code").toString();
            if ("500".equals(code)) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return AjaxResult.error("审批撤销失败: 更新审批状态失败");
            }
        }
        AuditItems item = new AuditItems();
        item.setBillId(auditItems.getId());
        item.setActId(auditItems.getActId());
        int deleteNum = auditItemsMapper.deletePurchaseApproval(item);
        if (deleteNum == 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("审批撤销失败: 更新审批状态失败");
        }
        return AjaxResult.success();
    }

}
