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("1011")
public class Rules1011ServiceImpl 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 phone = "phone";
                String address = "address";
                //String phone = enterpriseConfigEntity.getPhone();
                //String address = enterpriseConfigEntity.getAddress();
                if (StringUtils.isBlank(phone) || StringUtils.isBlank(address)) {
                    return "未配置购方地址电话，请联系管理员！";
                } else {
                    if (StringUtils.isNotBlank(recordInvoice.getGfAddressAndPhone())) {
                        if (!recordInvoice.getGfAddressAndPhone().contains(phone) || !recordInvoice.getGfAddressAndPhone().contains(address)) {
                            return "购方地址或电话错误，签收失败！";
                        }
                    }

                }

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

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