package nc.pubitf.payablebill.yx;

import nc.bgy.erm.constants.EstateTypeConstant;
import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.impl.am.db.processor.CountProcessor;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.payablebill.yx.IImagePubService;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
/**
 * 影像采集接口
 * @author ll
 * @date 2021-05-27
 *
 */
public class ImageCommonUtil {
	
	private static IImagePubService iImagePubService = null;
	
	/**
	 * 根据制单人主键查询用户编码
	 * @param cuserid
	 * @return
	 */
	public static String getUserCode(String cuserid) {
		String sql = new StringBuilder("select user_code")
				.append(" from sm_user a")
				.append(" where a.cuserid = '"+ cuserid +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据制单人主键查询用户编码失败,请联系用友顾问!");
		}
		return result;
	}
	
	/**
	 * 根据制单人主键查询用户名称
	 * @param cuserid
	 * @return
	 */
	public static String getUserName(String cuserid) {
		String sql = new StringBuilder("select user_name")
				.append(" from sm_user a")
				.append(" where a.cuserid = '"+ cuserid +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据制单人主键查询用户名称失败,请联系用友顾问!");
		}
		return result;
	}
	
	/**
	 * 根据财务组织主键查询纳税人识别号
	 * @param pk_org 财务组织
	 * @return
	 */
	public static String getTaxPayerCode(String pk_org) {
		String sql = new StringBuilder("select taxpayercode")
				.append(" from org_financeorg ")
				.append(" where islastversion='Y' and pk_financeorg = '"+ pk_org +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据财务组织主键查询纳税人识别号失败,请联系用友顾问!");
		}
		return result;
	}
	
	/**
	 * 根据组织主键查询法人公司名称
	 * @param pk_org 组织主键
	 * @return
	 */
	public static String getCorporationName(String pk_org) {
		String sql = new StringBuilder("select def19")
				.append(" from org_orgs_v a")
				.append(" where a.pk_org = '"+ pk_org +"'").toString();
		String result = "";
		try {
			result = (String) getDao().executeQuery(sql, new ColumnProcessor());
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("根据组织主键查询法人公司名称失败,请联系用友顾问!");
		}
		return result;
	}
	
	/**
	 * 更新影像系统单据状态
	 * @param djlxbm 单据类型
	 * @param billid 单据主键
	 * @param status 需要更新的状态
	 * @throws BusinessException
	 */
	public static void updateImageStatus(String djlxbm, String billid, String status) throws BusinessException {
		if((djlxbm.equals(EstateTypeConstant.WY_YGFYBXD)
				|| djlxbm.equals(EstateTypeConstant.WY_XEBXD)
				|| djlxbm.equals(EstateTypeConstant.WY_DWGWBXD)
				|| djlxbm.equals(EstateTypeConstant.WY_CLFBXD)) 
				&& isSendImage(billid)) {
			getiImagePubService().updateImageStatus(billid, status);
		}
		
	}
	
	/**
	 * 单据是否上传影像待办
	 * @param billid
	 * @return
	 */
	public static boolean isSendImage(String billid){
		String sql = new StringBuilder("SELECT COUNT(*)")
				.append("  FROM IMG_TM_LOG")
				.append(" WHERE BILLID = '"+ billid +"'")
				.append(" AND TM_STATUS = 'S'")
				.append(" AND APICODE = 'IMAGE-NCC-ERM'").toString();
		try {
			Integer count = (Integer) getDao().executeQuery(sql, new CountProcessor());
			if(count != null && count > 0){
				return true;
			}
		} catch (DAOException e) {
			ExceptionUtils.wrappBusinessException("查询单据是否上传影像系统待办失败,请联系用友顾问!");
		}
		return false;
	}
	
	private static IImagePubService getiImagePubService() {
		if(null == iImagePubService) {
			iImagePubService = NCLocator.getInstance().lookup(IImagePubService.class);
		}
		return iImagePubService;
	}
	
	private static BaseDAO getDao() {
		return new BaseDAO();
	}
}
