package com.jianshui.platform.service.impl;

import com.jianshui.common.core.domain.AjaxResult;
import com.jianshui.common.utils.ValidateUtils;
import com.jianshui.platform.dto.TaxCalculateDTO;
import com.jianshui.platform.service.TaxCalculateService;
import com.jianshui.platform.vo.TaxCalculateVO;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;

/**
 * @Author: kane
 * @Description: 金额税额计算业务层
 * @CreateTime: 2023-06-07  17:43
 * @Version: 1.0
 **/
@Service
public class TaxCalculateServiceImpl implements TaxCalculateService {
    /**
     * 功能描述: 金额税额计算
     * @param taxCalculateDTO
     * @return : com.jianshui.common.core.domain.AjaxResult
     */
    @Override
    public AjaxResult taxCalculate(TaxCalculateDTO taxCalculateDTO) {
        //校验
        ValidateUtils.validate(taxCalculateDTO);
        //含税金额
        BigDecimal money = taxCalculateDTO.getNumber().multiply(taxCalculateDTO.getUnitPrice());
        BigDecimal moneyResult = money.setScale(2,BigDecimal.ROUND_HALF_UP);
        //税率计算
        BigDecimal taxRate = taxCalculateDTO.getTaxRate().divide(new BigDecimal("100"));
        BigDecimal taxRateResult = taxRate.add(new BigDecimal("1"));
        //税额
        BigDecimal taxMoney = money.multiply(taxRate).divide(taxRateResult,2,BigDecimal.ROUND_HALF_UP);
        //封装实体类
        TaxCalculateVO taxCalculateVO = new TaxCalculateVO();
        taxCalculateVO.setId(taxCalculateDTO.getId());
        taxCalculateVO.setMoney(moneyResult);
        taxCalculateVO.setTaxMoney(taxMoney);
        return AjaxResult.success(taxCalculateVO);
    }
}
