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

import com.dxhy.common.enums.FplxEnum;
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.joda.time.DateTime;
import org.springframework.stereotype.Service;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Slf4j
@Service("1005")
public class Rules1005ServiceImpl implements RulesService {
    @Override
    public String validationRules(UserInfo userInfo, String qsType, Map<String, String> pramsMap, TDxRecordInvoice recordInvoice, String invoiceType) {
        // 1005 开票日期校验
        boolean validInvoiceDate =
                this.validInvoiceDate(qsType, invoiceType, pramsMap, recordInvoice.getInvoiceDate());
        if (!validInvoiceDate) {
            return "传入的开票日期与底账信息不一致";
        }
        return "";
    }

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

    public boolean validInvoiceDate(String qsType, String invoiceType, Map<String, String> pramsMap,
                                    Date invoiceDates) {
        if ("4".equals(qsType)) {
            // 手工签收不校验
            return true;
        } else {
            if (FplxEnum.ZP.getFplxDm().equals(invoiceType) || FplxEnum.TXF.getFplxDm().equals(invoiceType)
                    || FplxEnum.JDC.getFplxDm().equals(invoiceType) || FplxEnum.DZZP.getFplxDm().equals(invoiceType)
                    || FplxEnum.QDZZP.getFplxDm().equals(invoiceType)) {
                // 专票、机动车、通行费校验
                String invoiceDate = pramsMap.get("invoiceDate").replace("-", "");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
                ParsePosition pos = new ParsePosition(0);
                Date parse = formatter.parse(invoiceDate, pos);
                final String scanInvoiceDate = formatter.format(parse);
                final String recordInvoiceDate = new DateTime(invoiceDates).toString("yyyyMMdd");
                log.info("底账库中的日期为：" + recordInvoiceDate + "传入的日期为" + scanInvoiceDate);
                return scanInvoiceDate.equals(recordInvoiceDate);
            }
        }
        return true;

    }
}
