TPurchaseServiceImpl.java 5.18 KB
Newer Older
lizhichao's avatar
lizhichao committed
1 2
package com.zehong.system.service.impl;

耿迪迪's avatar
耿迪迪 committed
3 4
import java.math.BigDecimal;
import java.util.Date;
lizhichao's avatar
lizhichao committed
5 6 7
import java.util.List;

import com.zehong.common.core.domain.entity.SysUser;
耿迪迪's avatar
耿迪迪 committed
8
import com.zehong.common.core.exception.BusinessException;
lizhichao's avatar
lizhichao committed
9 10
import com.zehong.common.utils.DateUtils;
import com.zehong.common.utils.SecurityUtils;
耿迪迪's avatar
耿迪迪 committed
11 12 13 14
import com.zehong.system.domain.TAccount;
import com.zehong.system.domain.TCashOperate;
import com.zehong.system.mapper.TAccountMapper;
import com.zehong.system.mapper.TCashOperateMapper;
lizhichao's avatar
lizhichao committed
15 16 17 18 19
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zehong.system.mapper.TPurchaseMapper;
import com.zehong.system.domain.TPurchase;
import com.zehong.system.service.ITPurchaseService;
耿迪迪's avatar
耿迪迪 committed
20 21 22 23
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
lizhichao's avatar
lizhichao committed
24 25 26 27 28 29 30 31 32 33 34 35 36

/**
 * 外部采购(报销)申请Service业务层处理
 * 
 * @author zehong
 * @date 2023-06-16
 */
@Service
public class TPurchaseServiceImpl implements ITPurchaseService 
{
    @Autowired
    private TPurchaseMapper tPurchaseMapper;

耿迪迪's avatar
耿迪迪 committed
37 38 39 40 41 42
    @Resource
    private TAccountMapper tAccountMapper;

    @Resource
    private TCashOperateMapper tCashOperateMapper;

lizhichao's avatar
lizhichao committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    /**
     * 查询外部采购(报销)申请
     * 
     * @param purchaseId 外部采购(报销)申请ID
     * @return 外部采购(报销)申请
     */
    @Override
    public TPurchase selectTPurchaseById(Long purchaseId)
    {
        return tPurchaseMapper.selectTPurchaseById(purchaseId);
    }

    /**
     * 查询外部采购(报销)申请列表
     * 
     * @param tPurchase 外部采购(报销)申请
     * @return 外部采购(报销)申请
     */
    @Override
    public List<TPurchase> selectTPurchaseList(TPurchase tPurchase)
    {
        return tPurchaseMapper.selectTPurchaseList(tPurchase);
    }

    /**
     * 新增外部采购(报销)申请
     * 
     * @param tPurchase 外部采购(报销)申请
     * @return 结果
     */
    @Override
    public int insertTPurchase(TPurchase tPurchase)
    {
        tPurchase.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        tPurchase.setPurchaseDeptId(user.getDeptId());
耿迪迪's avatar
耿迪迪 committed
79
        tPurchase.setHandledByUserId(user.getUserId());//经办人
lizhichao's avatar
lizhichao committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
        return tPurchaseMapper.insertTPurchase(tPurchase);
    }

    /**
     * 修改外部采购(报销)申请
     * 
     * @param tPurchase 外部采购(报销)申请
     * @return 结果
     */
    @Override
    public int updateTPurchase(TPurchase tPurchase)
    {
        tPurchase.setUpdateTime(DateUtils.getNowDate());
        return tPurchaseMapper.updateTPurchase(tPurchase);
    }

    /**
     * 批量删除外部采购(报销)申请
     * 
     * @param purchaseIds 需要删除的外部采购(报销)申请ID
     * @return 结果
     */
    @Override
    public int deleteTPurchaseByIds(Long[] purchaseIds)
    {
        return tPurchaseMapper.deleteTPurchaseByIds(purchaseIds);
    }

    /**
     * 删除外部采购(报销)申请信息
     * 
     * @param purchaseId 外部采购(报销)申请ID
     * @return 结果
     */
    @Override
    public int deleteTPurchaseById(Long purchaseId)
    {
        return tPurchaseMapper.deleteTPurchaseById(purchaseId);
    }
耿迪迪's avatar
耿迪迪 committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int approvalPurchase(TPurchase tPurchase){
        if ("2".equals(tPurchase.getPurchaseStatus())){
            //校验及更新可用金额
            checkAccount(tPurchase);
            //更新操作记录
            insertOperateLog(tPurchase);
        }
        tPurchase.setUpdateTime(DateUtils.getNowDate());
        return tPurchaseMapper.updateTPurchase(tPurchase);
    }

    /**
     * 校验部门可用金额
     * @param tPurchase 采购信息
     * @return
     */
    private boolean checkAccount(TPurchase tPurchase){
        //更新账户可用金额
        TAccount tAccount=new TAccount();
        tAccount.setDeptId(tPurchase.getPurchaseDeptId());
        List<TAccount> rst= tAccountMapper.selectTAccountList(tAccount);
        if(!CollectionUtils.isEmpty(rst)){
            TAccount accountInfo = rst.get(0);
            if(accountInfo.getAbleAmount().compareTo(tPurchase.getTotal()) == -1){
                throw new BusinessException("账户可用金额不足!");
            }
            accountInfo.setAbleAmount(accountInfo.getAbleAmount().subtract(tPurchase.getTotal()));
            tAccountMapper.updateTAccount(accountInfo);
        }
        return true;
    }

    /**
     * 记录资金变动
     * @param tPurchase 采购信息
     */
    private void insertOperateLog(TPurchase tPurchase){
        TCashOperate operate = new TCashOperate();
        operate.setOperateDeptId(tPurchase.getPurchaseDeptId());
        operate.setOperateAmount(tPurchase.getTotal());
        operate.setOperateTime(new Date());
        operate.setRelationDoc(String.valueOf(tPurchase.getPurchaseId()));
        operate.setOperateType("4");
165
        operate.setDocumentType("3");//1.交易单 2.借支单 3.外部采购单 4.借贷单
耿迪迪's avatar
耿迪迪 committed
166 167
        tCashOperateMapper.insertTCashOperate(operate);
    }
lizhichao's avatar
lizhichao committed
168
}