package com.cf.imes.module.system.api.funds;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.Assert.AssertUtils;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.module.system.api.funds.dto.InvoiceTitleInfoDTO;
import com.cf.imes.module.system.api.funds.dto.OrganAmountDTO;
import com.cf.imes.module.system.dal.dataobject.funds.invoice.InvoiceTitleInfoDO;
import com.cf.imes.module.system.dal.dataobject.funds.organamount.OrganAmountDO;
import com.cf.imes.module.system.dal.mysql.funds.invoice.InvoiceTitleInfoMapper;
import com.cf.imes.module.system.dal.mysql.funds.organamount.OrganAmountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.math.BigDecimal;

import static com.cf.imes.module.system.enums.ErrorCodeConstants.INVOICE_TITLE_NO_EXIST;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.ORGAN_AMOUNT_NO_EXIST;


/**
 * @author 资金管理feign实现类
 */
@RestController
@Validated
@Slf4j
public class SystemFundsApiImpl implements SystemFundsApi {

    @Resource
    private OrganAmountMapper organAmountMapper;


    @Resource
    private InvoiceTitleInfoMapper invoiceTitleInfoMapper;


    @Override
    public CommonResult<Boolean> reduceOrganAmount(Long organId, BigDecimal invoiceAmount) {

        OrganAmountDO organAmountDO = validateOrganAmountExists(organId);

        BigDecimal invoiceableAmount = organAmountDO.getInvoiceableAmount().subtract(invoiceAmount);

        if(invoiceableAmount.signum() < 0){
            invoiceableAmount = BigDecimal.valueOf(0);
        }


        organAmountDO.setInvoiceableAmount(invoiceableAmount);
        organAmountMapper.updateById(organAmountDO);

        return CommonResult.success(Boolean.TRUE);

    }




    @Override
    public CommonResult<Boolean> addOrganAmount(Long organId, BigDecimal invoiceAmount) {

        OrganAmountDO organAmountDO = validateOrganAmountExists(organId);

        BigDecimal invoiceableAmount = organAmountDO.getInvoiceableAmount().add(invoiceAmount);


        organAmountDO.setInvoiceableAmount(invoiceableAmount);
        organAmountMapper.updateById(organAmountDO);

        return CommonResult.success(Boolean.TRUE);

    }

    @Override
    public CommonResult<OrganAmountDTO> getOrganAmount(Long organId) {

        return CommonResult.success(BeanUtils.toBean(validateOrganAmountExists(organId),OrganAmountDTO.class));

    }


    @Override
    public CommonResult<InvoiceTitleInfoDTO> getInvoiceTitle(Long invoiceTitleId) {

        InvoiceTitleInfoDO invoiceTitleInfoDO = invoiceTitleInfoMapper.selectById(invoiceTitleId);

        AssertUtils.notEmpty(invoiceTitleInfoDO,INVOICE_TITLE_NO_EXIST);

        return CommonResult.success(BeanUtils.toBean(invoiceTitleInfoDO,InvoiceTitleInfoDTO.class));

    }





    private OrganAmountDO validateOrganAmountExists(Long organId) {

        OrganAmountDO organAmountDO = organAmountMapper.selectOne(new LambdaUpdateWrapper<OrganAmountDO>()
                .eq(OrganAmountDO::getOrganId, organId)
                .eq(OrganAmountDO::getDeleted, false));

        AssertUtils.notEmpty(organAmountDO,ORGAN_AMOUNT_NO_EXIST);

        return organAmountDO;

    }




}
