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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.vo.UserInfo;
import com.dxhy.sign.dao.SysDeptDao;
import com.dxhy.sign.entity.SysDeptEntity;
import com.dxhy.sign.entity.TDxRecordInvoice;
import com.dxhy.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
    SysDeptDao sysDeptDao;

    @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())) {
            SysDeptEntity sysDeptEntity =
                    sysDeptDao.selectOne(new QueryWrapper<SysDeptEntity>().eq("taxno", recordInvoice.getGfTaxNo()).eq("org_type", "5"));
            if (sysDeptEntity != null) {
                String bank = sysDeptEntity.getBank();
                String account = sysDeptEntity.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";
    }
}
