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

import com.dxhy.api.model.config.UserInfo;
import com.dxhy.common.enums.FplxEnum;
import com.dxhy.common.util.MathUtil;
import com.dxhy.api.model.invoice.TDxRecordInvoice;
import com.dxhy.jxpt.baseService.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 金额校验
        String messge =
                this.validInvoiceAmount(invoiceType, pramsMap, recordInvoice.getInvoiceAmount(),recordInvoice.getTotalAmount(), qsType);
/*        if (!validInvoiceAmount) {
            return "传入的金额与底账信息不一致";

        }
        return "";*/
        return messge;
    }

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

    public String validInvoiceAmount(String invoiceType, Map<String, String> pramsMap, BigDecimal invoiceAmount,
                                      BigDecimal totalAmount, String qsType) {
        String message = "";
        if ("4".equals(qsType)) {
            // 手工签收不校验
            message = "";
        } else {
            //全电的验证和底账的价税合计是否一样
            if(FplxEnum.QDZZP.getFplxDm().equals(invoiceType)
                    || FplxEnum.QDPP.getFplxDm().equals(invoiceType)
                    || FplxEnum.ZZESC.getFplxDm().equals(invoiceType)
                    ||pramsMap.get("invoiceNo").length()==20){
                // 传入的价税合计金额和底账表不一致
                String amount = StringUtils.isNotBlank(pramsMap.get("totalAmount"))?pramsMap.get("totalAmount"):pramsMap.get("invoiceAmount");
                /**
                 * 2023年7月31日17:26:08 判断用数字判断
                 * return amount.equals(MathUtil.round(totalAmount.toPlainString()));
                 **/
                //
                boolean flag =  new BigDecimal(amount).compareTo(totalAmount)==0?true:false;
                if (flag) {
                    message = "";
                } else {
                    message = "传入的价税合计金额和底账信息不一致";
                }
            }else if (FplxEnum.ZP.getFplxDm().equals(invoiceType)
                    || FplxEnum.JDC.getFplxDm().equals(invoiceType)
                    || FplxEnum.DZZP.getFplxDm().equals(invoiceType)
                    || FplxEnum.ZZDZ.getFplxDm().equals(invoiceType)
                    || FplxEnum.ZZJDC.getFplxDm().equals(invoiceType)

            ) {
                // 专票、机动车、通行费校验、电子票

                // 传入的金额和底账表不一致
                boolean flag =  !pramsMap.containsKey("invoiceAmount") || !StringUtils.isNotBlank(pramsMap.get("invoiceAmount"))
                        || (MathUtil.round(pramsMap.get("invoiceAmount")))
                        .equals(MathUtil.round(invoiceAmount.toPlainString()));
                if (flag) {
                    message = "";
                } else {
                    message = "传入的金额与底账信息不一致";
                }
            }
        }
        return message;
    }
}
