package com.inkasso.factoring.ledgermanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.inkasso.adapter.util.CodeUtil;
import com.inkasso.adapter.web.LoginUserHolder;
import com.inkasso.auth.exception.ValidatorException;
import com.inkasso.factoring.common.entity.GridPage;
import com.inkasso.factoring.creditmanagement.entity.Enterprise;
import com.inkasso.factoring.creditmanagement.service.IEnterpriseService;
import com.inkasso.factoring.dict.service.DictionaryService;
import com.inkasso.factoring.ledgermanagement.dao.LedgerAccountBasicInfoMapper;
import com.inkasso.factoring.ledgermanagement.entity.LedgerAccountBasicInfo;
import com.inkasso.factoring.ledgermanagement.entity.LedgerFundGoodsAssign;
import com.inkasso.factoring.ledgermanagement.entity.SupplierBankCardInfo;
import com.inkasso.factoring.ledgermanagement.entity.vo.LedgerAccountInfoVo;
import com.inkasso.factoring.ledgermanagement.service.LedgerAccountBasicInfoService;
import com.inkasso.factoring.ledgermanagement.service.LedgerFundGoodsAssignService;
import com.inkasso.factoring.ledgermanagement.service.SupplierBankCardInfoService;
import com.inkasso.factoring.settlement.entity.SearchBusiness;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

/**
 * @author liutao
 * @date 2021.11.11
 */
@Service
public class LedgerAccountBasicInfoServiceImpl extends ServiceImpl<LedgerAccountBasicInfoMapper, LedgerAccountBasicInfo>
        implements LedgerAccountBasicInfoService {

    @Autowired
    private LedgerFundGoodsAssignService ledgerFundGoodsAssignService;
    @Autowired
    private SupplierBankCardInfoService supplierBankCardInfoService;
    @Autowired
    private DictionaryService dictService;
    @Autowired
    private IEnterpriseService enterpriseService;

    @Override
    public GridPage<LedgerAccountBasicInfo> list(SearchBusiness search) {
        //获取当前供应商Id
        search.setUserId(LoginUserHolder.get().getId());
        Page<LedgerAccountBasicInfo> page = PageHelper.startPage(search.getPage(), search.getRows());
        List<LedgerAccountBasicInfo> rows = baseMapper.findAll(search);
        return new GridPage<>(search.getRows(), search.getPage(), page.getTotal(), rows);
    }

    @Override
    public LedgerAccountInfoVo getView(Long id, Long supplierId) {
        LedgerAccountBasicInfo ledgerAccountBasicInfo = getById(id);
        if (ledgerAccountBasicInfo == null || ledgerAccountBasicInfo.getId() == null) {
            throw new ValidatorException(CodeUtil.FAIL, "分账基础信息不能为空");
        }
        if (ledgerAccountBasicInfo.getLedgerType() != null) {
            ledgerAccountBasicInfo.setLedgerTypeDict(dictService.getByCode(ledgerAccountBasicInfo.getLedgerType()));
        }
        if (ledgerAccountBasicInfo.getEnterpriseId() != null) {
            Enterprise enterprise = enterpriseService.getById(ledgerAccountBasicInfo.getEnterpriseId());
            if (enterprise != null) {
                ledgerAccountBasicInfo.setCustomerName(enterprise.getCorporateName());
            }
        }

        LedgerAccountInfoVo ledgerAccountInfoVo = new LedgerAccountInfoVo();

        //货款分配
        List<LedgerFundGoodsAssign> supplierGoodsAssignList = ledgerFundGoodsAssignService.list(new QueryWrapper<LedgerFundGoodsAssign>()
                .eq("ledger_account_basic_info_id", id).eq("supplier_id", supplierId));
        ledgerAccountInfoVo.setGoodsAssignList(supplierGoodsAssignList);

        //关联的供应商账户
        List<SupplierBankCardInfo> supplierBankCardInfoList = supplierBankCardInfoService.list(new QueryWrapper<SupplierBankCardInfo>()
                .eq("ledger_account_basic_info_id", ledgerAccountBasicInfo.getId()).eq("supplier_id", supplierId));
        ledgerAccountInfoVo.setSupplierBankCardInfoList(supplierBankCardInfoList);

        BigDecimal discountCharge = ledgerAccountBasicInfo.getDiscountCharge() == null ? BigDecimal.ZERO : ledgerAccountBasicInfo.getDiscountCharge();

        List<LedgerFundGoodsAssign> goodsAssignList = ledgerFundGoodsAssignService.list(new QueryWrapper<LedgerFundGoodsAssign>()
                .eq("ledger_account_basic_info_id", id));

        BigDecimal totalAmount = goodsAssignList.stream().filter(goods -> goods.getCurrentAssignAmount() != null).map(LedgerFundGoodsAssign::getCurrentAssignAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        BigDecimal amount = supplierGoodsAssignList.stream().filter(goods -> goods.getCurrentAssignAmount() != null).map(LedgerFundGoodsAssign::getCurrentAssignAmount)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        if (discountCharge.compareTo(BigDecimal.ZERO) != 0) {
            discountCharge = discountCharge.multiply(amount).divide(totalAmount, 2, RoundingMode.HALF_UP);
            ledgerAccountBasicInfo.setDiscountCharge(discountCharge);
        }
        ledgerAccountBasicInfo.setLedgerAmount(discountCharge.add(amount));
        ledgerAccountInfoVo.setLedgerAccountBasicInfo(ledgerAccountBasicInfo);
        return ledgerAccountInfoVo;
    }
}
