package nc.bs.arap.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.jdbc.framework.processor.BeanListProcessor;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.arap.payable.AggPayableBillVO;
import nc.vo.arap.receivable.AggReceivableBillVO;
import nc.vo.arap.receivable.ReceivableBillItemVO;
import nc.vo.bd.defdoc.DefdocVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;


/**
 * 费用分摊应收单工具
 * @author chenjm
 *
 */
public class RecBillShareCheckUtil {
	
	/**
	 * 根据客户主键查找纳税人识别号
	 * @author chenjm
	 * @param customerId
	 * @return
	 */
	public static String getTaxpayercodeByCustomerId(String customerId){
		String sql = new StringBuilder("select a.taxpayercode")
				.append(" from org_financeorg a")
				.append(" left join bd_customer b")
				.append(" on a.pk_financeorg = b.pk_financeorg")
				.append(" where b.pk_customer = '"+ customerId +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据客户主键查找纳税人识别号失败!");
		}
		return result;
	}
	
	/**
	 * 根据业务单元版本主键查找纳税人识别号
	 * @author chenjm
	 * @param customerId
	 * @return
	 */
	public static String getTaxpayercodeByOrgId(String OrgId){
		String sql = new StringBuilder("select a.taxpayercode")
				.append(" from org_financeorg a")
				.append(" where a.pk_vid = '"+ OrgId +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据业务单元主键查找纳税人识别号失败!");
		}
		return result;
	}
	
	/**
	 * 获取跨营业执照不控制的费用类型
	 * @return
	 */
	public static List<String> getNotCtrlFYType(){
		String sql = new StringBuilder("select b.code")
				.append(" from bd_defdoclist a")
				.append(" left join bd_defdoc b")
				.append(" on a.pk_defdoclist = b.pk_defdoclist")
				.append(" where b.enablestate = 2")
				.append(" and a.code = 'WYFW153'").toString();
		List<String> fylxList = new ArrayList<>();
		try {
			fylxList = (List<String>) getDao().executeQuery(sql, new ColumnListProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("获取跨营业执照不控制的费用类型失败!");
		}
		return fylxList;
	}
	
	/**
	 * 查询费用类型信息
	 * @param fylxId
	 * @return
	 */
	public static List<DefdocVO> getFYLXVO(List<String> fylxId){
		List<DefdocVO> lvos = null;
		try {
			String sql = "select code,name from bd_defdoc where " + SqlUtils.getInStr("pk_defdoc", fylxId.toArray(new String[0]));
			lvos = (List<DefdocVO>) getDao().executeQuery(sql, new BeanListProcessor(DefdocVO.class));
		} catch (BusinessException e) {
			ExceptionUtils.wrappBusinessException("查询费用类型信息失败!");
		}
		return lvos;
	}
	
	
	private static BaseDAO getDao() {
		return new BaseDAO();
	}

	public static void checkFYType(AggReceivableBillVO recbill) {
		// 费用分摊单位
		String pk_org_v = recbill.getHeadVO().getPk_org_v();
		// 费用承担单位
		String customer = null;
		if(null != recbill.getBodyVOs() && 0 < recbill.getBodyVOs().length) {
			customer = recbill.getBodyVOs()[0].getCustomer();
		}
//		if(true) {
//			ExceptionUtils.wrappBusinessException("测试");
//		}
		String Ft_taxpayercode = getTaxpayercodeByOrgId(pk_org_v);
		String Cd_taxpayercode = getTaxpayercodeByCustomerId(customer);
		if(StringUtils.isBlank(Ft_taxpayercode) || StringUtils.isBlank(Cd_taxpayercode) || !Ft_taxpayercode.equals(Cd_taxpayercode)) {
			List<String> list = new ArrayList<String>();
			ReceivableBillItemVO[] bvos = recbill.getBodyVOs();
			for(ReceivableBillItemVO bvo : bvos) {
				list.add(bvo.getDef52());
			}
			if(null != list && 0 < list.size()) {
				List<DefdocVO> lvos = getFYLXVO(list);
				if(null != lvos && 0 < lvos.size()) {
					List<String> fylxids = getNotCtrlFYType();
					for(DefdocVO dvo : lvos) {
						if(!fylxids.contains(dvo.getCode())) {
							ExceptionUtils.wrappBusinessException("【"+ dvo.getName() +"】分摊不可以跨营业执照进行分摊，请修改！");
						}
					}
				}
			}
		}
	}

	/**
	 * 校验上游费用分摊应收单是否生成凭证
	 * @param paybill
	 */
	public static void checkVoucher(AggPayableBillVO paybill) {
		if(null != paybill) {
			String pk_recbill = paybill.getBodyVOs()[0].getTop_billid();
			if(!StringUtils.isBlank(pk_recbill)) {
				String sql = new StringBuilder("select a.des_relationid")
						.append(" from fip_relation a")
						.append(" where a.des_system = 'GL'")
						.append(" and a.src_relationid = '"+ pk_recbill +"'").toString();
				String result = "";
				try {
					result = (String) getDao().executeQuery(sql, new ColumnProcessor());
				} catch (DAOException e) {
					ExceptionUtils.wrappBusinessException("校验上游费用分摊应收单是否生成凭证失败!");
				}
				if(!StringUtils.isBlank(result)) {
					ExceptionUtils.wrappBusinessException("上游分摊应收单存在正式凭证不允许取消审批，如需取消请联系应收方删除正式凭证!");
				}
			}
		}
	}

}
