package com.dxhy.sign.util;


import java.util.List;
import java.util.Map;

import com.dxhy.sign.service.qsgz.RulesSenderProvider;
import com.dxhy.sign.service.qsgz.RulesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.dxhy.common.util.InvoiceUtil;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.sign.entity.SignRulesEntity;
import com.dxhy.sign.entity.TDxRecordInvoice;


/**
 * 签收规则
 *
 * @author yangmingue
 */
@Component
@Slf4j
public class SignRulesUtils {

//    @Autowired
//    private Map<String, RulesService> rulesServiceMap;

    @Autowired
    private RulesSenderProvider rulesSenderProvider;

    /**
     * @param userInfo      签收用户信息
     * @param qsType        签收方式
     * @param pramsMap      传入的发票信息
     * @param recordInvoice 底账信息/查验获得信息
     */

    public String checkRules(UserInfo userInfo, String qsType, Map<String, String> pramsMap,
                             TDxRecordInvoice recordInvoice, String invoiceType, List<SignRulesEntity> rules) {

        if (rules.size() > 0) {
            for (SignRulesEntity rule : rules) {
                RulesService rulesService = rulesSenderProvider.getEdiSenderByType(rule.getRuleCode());
                if (rulesService != null) {
                    String validationRules = rulesService.validationRules(userInfo, qsType, pramsMap, recordInvoice, invoiceType);
                    if (StringUtils.isNotBlank(validationRules)) {
                        return validationRules;
                    }
                }
            }
        }
        return "";
    }

    /**
     * 校验扫描基本信息是否正确
     *
     * @param invoiceNo   发票号码
     * @param invoiceCode 发票代码
     * @return 结果
     */
    public String validateInvoiceInfo(String invoiceCode, String invoiceNo) {
        String notes = null;
        InvoiceUtil invoiceUtil = new InvoiceUtil(invoiceCode);
        if (!invoiceUtil.fpdmValid() || invoiceUtil.getFplxdm() == null) {
          //  notes = "发票代码不正确";

        } else {
            invoiceUtil.setFphm(invoiceNo);
            if (!invoiceUtil.fphmValid()) {
            //    notes = "发票号码不正确";
            }
        }
        return notes;
    }
}
