package com.zehong.system.service.impl.tradeSettlement;

import com.zehong.common.utils.SecurityUtils;
import com.zehong.system.domain.TAccount;
import com.zehong.system.domain.TCashOperate;
import com.zehong.system.domain.TTradeProject;
import com.zehong.system.mapper.TAccountMapper;
import com.zehong.system.mapper.TCashOperateMapper;
import com.zehong.system.mapper.TTradeProjectMapper;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author geng
 * 自动结算交易尾款
 */
@Component
public class AutoSettlePendingPayment {

    @Resource
    private TTradeProjectMapper tTradeProjectMapper;

    @Resource
    private TAccountMapper tAccountMapper;

    @Resource
    private TCashOperateMapper tCashOperateMapper;


    /**
     * 自动结算本部尾款
     * @param incomeAmount 进账金额
     * @param account 账户信息
     * @param deptId 部门id
     */
    public void autoSettlePendingPayment(BigDecimal incomeAmount, TAccount account,Long deptId){
        //判断进账金额及本部自动划拨开关是否开启
        if(incomeAmount.compareTo(BigDecimal.ZERO) == 1 && "0".equals(account.getTransferSwitch())){
            //结算本部尾款
            List<TTradeProject> pendingPaymentInfo = tTradeProjectMapper.getTradePendingPaymentInfo(deptId);
            for(TTradeProject project : pendingPaymentInfo){
                if(incomeAmount.compareTo(BigDecimal.ZERO) == 0){
                    return;
                }
                //本部账户结算
                BigDecimal amount = settleSelfAccountInfo(incomeAmount,project,account);
                //卖方部门账户结算
                settleSaleAccountInfo(amount,project);
                //重新计算进账金额
                incomeAmount = incomeAmount.subtract(amount);
            }
        }
    }

    /**
     * 本部门账户信息变动
     * @param incomeAmount 进账金额
     * @param project 交易项目
     * @param account 账户信息
     * @return
     */
    private BigDecimal settleSelfAccountInfo(BigDecimal incomeAmount,TTradeProject project,TAccount account) {
        //更新项目尾款信息
        BigDecimal amount = incomeAmount.compareTo(project.getPendingPayment()) == 1 ? project.getPendingPayment() : incomeAmount;
        //尾款
        BigDecimal pendingPayment = incomeAmount.compareTo(project.getPendingPayment()) == -1 ? project.getPendingPayment().subtract(incomeAmount) : new BigDecimal("0");
        project.setPendingPayment(pendingPayment);
        tTradeProjectMapper.updateTTradeProject(project);
        //更新账户信息
        account.setAbleAmount(account.getAbleAmount().subtract(amount));
        account.setUpdateTime(new Date());
        tAccountMapper.updateTAccount(account);
        //添加操作记录
        TCashOperate operate = new TCashOperate();
        operate.setOperateDeptId(project.getTradeDeptId());
        operate.setOperateAmount(amount.compareTo(BigDecimal.ZERO) == 1?amount:amount.abs());
        operate.setOperateTime(new Date());
        operate.setRelationDoc(project.getTradeId().toString());
        operate.setOperateType("3");
        //1.交易单 2.借支单 3.外部采购单 4.借贷单
        operate.setDocumentType("1");
        operate.setOperatePersonId(SecurityUtils.getLoginUser().getUser().getUserId());
        tCashOperateMapper.insertTCashOperate(operate);
        return amount;
    }

    /**
     * 卖方部门结算
     * @param amount 进账金额
     * @param project 交易项目信息
     */
    private void settleSaleAccountInfo(BigDecimal amount,TTradeProject project){
        //更新账户信息
        TAccount tAccount = new TAccount();
        tAccount.setDeptId(project.getApplyDeptId());
        List<TAccount> accounts =  tAccountMapper.selectTAccountList(tAccount);
        TAccount accountInfo = accounts.get(0);
        accountInfo.setAbleAmount(accountInfo.getAbleAmount().add(amount));
        accountInfo.setUpdateTime(new Date());
        tAccountMapper.updateTAccount(accountInfo);
        //添加操作记录
        TCashOperate operate = new TCashOperate();
        operate.setOperateDeptId(project.getApplyDeptId());
        operate.setOperateAmount(amount.compareTo(BigDecimal.ZERO) == 1?amount:amount.abs());
        operate.setOperateTime(new Date());
        operate.setRelationDoc(project.getTradeId().toString());
        operate.setOperateType("10");
        //1.交易单 2.借支单 3.外部采购单 4.借贷单
        operate.setDocumentType("1");
        operate.setOperatePersonId(SecurityUtils.getLoginUser().getUser().getUserId());
        tCashOperateMapper.insertTCashOperate(operate);
    }

}
