package com.dxhy.sign.service.qsgz.impl;

import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.util.MathUtil;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.sign.entity.TDxRecordInvoice;
import com.dxhy.sign.service.qsgz.RulesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Slf4j
@Service("1004")
public class Rules1004ServiceImpl implements RulesService {

    @Override
    public String validationRules(UserInfo userInfo, String qsType, Map<String, String> pramsMap, TDxRecordInvoice recordInvoice, String invoiceType) {
        // 1004 金额校验
        boolean validInvoiceAmount =
                this.validInvoiceAmount(invoiceType, pramsMap, recordInvoice.getInvoiceAmount(), qsType);
        if (!validInvoiceAmount) {
            return "传入的金额与底账信息不一致";

        }
        return "";
    }

    @Override
    public String getEdiType() {
        return "1004";
    }

    public boolean validInvoiceAmount(String invoiceType, Map<String, String> pramsMap, BigDecimal invoiceAmount,
                                      String qsType) {
        if ("4".equals(qsType)) {
            // 手工签收不校验
            return true;
        } else {
            if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.JDC.getFplxDm().equals(invoiceType)
                    || FplxEnum.DZZP.getFplxDm().equals(invoiceType)) {
                // 专票、机动车、通行费校验、电子票

                // 传入的金额和底账表不一致
                return !pramsMap.containsKey("invoiceAmount") || !StringUtils.isNotBlank(pramsMap.get("invoiceAmount"))
                        || (MathUtil.round(pramsMap.get("invoiceAmount")))
                        .equals(MathUtil.round(invoiceAmount.toPlainString()));
            }
            return true;
        }
    }
}
