package com.mini.capi.api.biz;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.mini.capi.biz.domain.*;
import com.mini.capi.biz.service.*;
import com.mini.capi.model.ApiResult;
import com.mini.capi.utils.BigDecimalUtils;
import com.mini.capi.utils.vId;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/erpApi")
public class erpController {


    @Resource
    private ErpTransactionFlowService erpTransactionFlowService;

    @Resource
    private ErpAccountService erpAccountService;

    @Resource
    private ErpCategoryService erpCategoryService;


    @Resource
    private ErpExpenseService erpExpenseService;

    @Resource
    private ErpIncomeService erpIncomeService;

    /**
     * 流水交易记账
     */
    @GetMapping("getTranFlow")
    public ApiResult<?> getErpTranFlow(String flowId) {
        UpdateWrapper<ErpTransactionFlow> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("flow_id", flowId);
        ErpTransactionFlow erpTransactionFlow = erpTransactionFlowService.getById(flowId);
        ErpAccount account = erpAccountService.getById(erpTransactionFlow.getAccountId());
        ErpCategory erpCategory = erpCategoryService.getById(erpTransactionFlow.getCategoryId());
        switch (erpTransactionFlow.getTransactionType()) {
            case "1":
                getExpense(erpTransactionFlow, account, erpCategory);
                updateAccount(account, erpTransactionFlow.getTransactionType(), erpTransactionFlow.getAmount());
                break;
            case "2":
                getIncome(erpTransactionFlow, account, erpCategory);
                updateAccount(account, erpTransactionFlow.getTransactionType(), erpTransactionFlow.getAmount());
                break;
        }
        erpTransactionFlow.setIsFinish("1");
        erpTransactionFlow.setUpdateTime(LocalDateTime.now());
        erpTransactionFlowService.update(erpTransactionFlow, updateWrapper);
        return ApiResult.success();
    }


    /**
     * 收入
     */
    private void getIncome(ErpTransactionFlow erpTransactionFlow, ErpAccount account, ErpCategory erpCategory) {
        ErpIncome erpIncome = new ErpIncome();
        erpIncome.setIncomeId(vId.getUid());
        erpIncome.setSflowId(erpTransactionFlow.getFlowId());
        erpIncome.setAccountId(account.getAccountId());
        erpIncome.setCategoryId(erpCategory.getCategoryId());
        erpIncome.setAmount(erpTransactionFlow.getAmount());
        erpIncomeService.save(erpIncome);
    }

    /**
     * 支出
     */
    private void getExpense(ErpTransactionFlow erpTransactionFlow, ErpAccount account, ErpCategory erpCategory) {
        ErpExpense erpExpense = new ErpExpense();
        erpExpense.setExpenseId(vId.getUid());
        erpExpense.setZflowId(erpTransactionFlow.getFlowId());
        erpExpense.setAccountId(account.getAccountId());
        erpExpense.setCategoryId(erpCategory.getCategoryId());
        erpExpense.setAmount(erpTransactionFlow.getAmount());
        erpExpenseService.save(erpExpense);
    }

    /**
     * 账户更新
     */
    private void updateAccount(ErpAccount account, String getTransactionType, BigDecimal amount) {
        UpdateWrapper<ErpAccount> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("account_id", account.getAccountId());
        switch (getTransactionType) {
            case "1":
                account.setCurrentBalance(BigDecimalUtils.subtract(account.getCurrentBalance(), amount));
                break;
            case "2":
                account.setCurrentBalance(BigDecimalUtils.add(account.getCurrentBalance(), amount));
                break;
        }
        account.setUpdateTime(LocalDateTime.now());
        erpAccountService.update(account, updateWrapper);
    }

}
