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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dxhy.api.model.config.UserInfo;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigEntity;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.jxpt.baseService.sign.dao.SysEnterpriseConfigSignDao;
import com.dxhy.api.model.invoice.TDxRecordInvoice;
import com.dxhy.jxpt.baseService.sign.service.qsgz.RulesService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

@Service("1012")
public class Rules1012ServiceImpl implements RulesService {

    @Resource
    SysEnterpriseConfigSignDao sysEnterpriseConfigSignDao;

    @Override
    public String validationRules(UserInfo userInfo, String qsType, Map<String, String> pramsMap, TDxRecordInvoice recordInvoice, String invoiceType) {
        // 购方银行账号校验
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        if (StringUtils.isNotBlank(recordInvoice.getGfTaxNo())) {
            SysEnterpriseConfigEntity enterpriseConfigEntity =
                    sysEnterpriseConfigSignDao.selectOne(new LambdaQueryWrapper<SysEnterpriseConfigEntity>()
                            .eq(SysEnterpriseConfigEntity::getNsrsbh, recordInvoice.getGfTaxNo()));
            if (enterpriseConfigEntity != null) {
                String bank = "bank";
                String account = "account";
                //String bank = enterpriseConfigEntity.getBank();
                //String account = enterpriseConfigEntity.getAccount();
                if (StringUtils.isBlank(bank) || StringUtils.isBlank(account)) {
                    return "未配置购方银行账号，请联系管理员！";
                } else {
                    if (StringUtils.isNotBlank(recordInvoice.getGfBankAndNo())) {
                        if (!recordInvoice.getGfBankAndNo().contains(bank) || !recordInvoice.getGfBankAndNo().contains(account)) {
                            return "购方开户行或账号错误，签收失败！";
                        }
                    }

                }

            } else {
                return "未配置购方信息，请联系管理员！";
            }
        }
        return "";
    }

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