package nc.bs.pu.wgpt.process;

import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.net.servlet.IReturnStatus;
import nc.util.uap.wgpt.WuYeWgptUtil;
import nc.vo.esb.ResultEsbVO;
import nc.vo.esb.ResultVO;
import nc.vo.pf.pub.util.SQLUtil;
import nc.vo.pub.BusinessException;
import nc.vo.wgpt.log.WuYeWgptLogVO;
import nccloud.commons.collections.CollectionUtils;
import uap.json.JSONObject;

/**
 * @Description: 【网购平台->NC】项目档案查询接口
 * 
 * @author 周蓝萍 
 * @date 2020-11-05
 * @version with NCC
 */
public class QueryProjectInfoProcess {

	@SuppressWarnings("unchecked")
	public String queryProject(JSONObject jsonobj) throws BusinessException {
		String rsdata = null;
		String nc_id = null;
		Object orderno = null;
		ResultEsbVO<ResultVO> esb = new ResultEsbVO<ResultVO>();
		ResultVO result = new ResultVO();
		result.setResult("");
		esb.setResultInfo(result);
		try {
			if (jsonobj == null || "".equals(jsonobj)) {// 无Post提交值
				result.setCode(IReturnStatus.FAILING);// 失败
				result.setMessage("Post所提交请求Json格式错误或者无值!");
				WuYeWgptUtil.createResultEsbVO(esb);
				esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_FAILING);
				esb.getEsbInfo().setReturnMsg("失败");
				esb.setResultInfo(result);
			} else {
				BaseDAO baseDAO = new BaseDAO();
				JSONObject obj = jsonobj.getJSONObject("requestInfo");
				uap.json.JSONArray orgcodes = obj.getJSONArray("orgcode");
				List<String> strList = orgcodes.getArrayList();
				String ts = obj.getString("ts");
				String wheresql = "";
				if(CollectionUtils.isNotEmpty(strList)) {
					wheresql = SQLUtil.buildSqlForIn("t2.code", strList.toArray(new String[0]));
                }else {
                	wheresql = " t1.ts >='"+ts+"'";
                }
//				String sql=
//						"select distinct t2.code,\n" +
//								"    t1.project_code,\n" + 
//								"    t1.project_name,\n" + 
//								"    t1.creationtime,\n" + 
//								"    t1.modifiedtime,\n" + 
//								"    case when t4.code='030'\n" + 
//								"      then 'Y'\n" + 
//								"        else 'N'\n" + 
//								"          end ischoujin\n" + 
//								"from bd_project \n" + 
//								"left join org_orgs t2 on t1.pk_org = t2.pk_org and t2.dr=0 and isbusinessunit='Y' \n" + 
//								"left join org_accountingbook t3 on t2.pk_org = t3.pk_relorg and t3.dr=0\n" + 
//								"left join bd_accchart t4 on t4.pk_accchart = t3.pk_curraccchart and t4.dr=0\n" + 
//								"where nvl(t1.dr,0)=0 and "+wheresql;
				StringBuffer sqlstr=new StringBuffer("");
				sqlstr.append("select distinct t2.code,").append("\r\n");
				sqlstr.append("               t1.project_code,").append("\r\n");
				sqlstr.append("               t1.project_name,").append("\r\n");
				sqlstr.append("               t1.creationtime,").append("\r\n");
				sqlstr.append("               t1.modifiedtime,").append("\r\n");
				sqlstr.append("               case").append("\r\n");
				sqlstr.append("                 when t4.code = '030' then").append("\r\n");
				sqlstr.append("                  'Y'").append("\r\n");
				sqlstr.append("                 else").append("\r\n");
				sqlstr.append("                  'N'").append("\r\n");
				sqlstr.append("               end ischoujin").append("\r\n");
				sqlstr.append(" from bd_project t1").append("\r\n");
				sqlstr.append(" left join org_orgs t2").append("\r\n");
				sqlstr.append("   on t1.pk_org = t2.pk_org").append("\r\n");
				sqlstr.append("  and t2.dr = 0").append("\r\n");
				sqlstr.append("  and isbusinessunit = 'Y'").append("\r\n");
				sqlstr.append(" left join org_accountingbook t3").append("\r\n");
				sqlstr.append("   on t2.pk_org = t3.pk_relorg").append("\r\n");
				sqlstr.append("  and t3.dr = 0 left").append("\r\n");
				sqlstr.append(" join bd_accchart t4").append("\r\n");
				sqlstr.append("   on t4.pk_accchart = t3.pk_curraccchart").append("\r\n");
				sqlstr.append("  and t4.dr = 0").append("\r\n");
				sqlstr.append("where nvl(t1.dr, 0) = 0").append("\r\n");
				sqlstr.append(" and   "+wheresql); 

				List<Map<String,Object>> projectList = (List<Map<String,Object>>) baseDAO.executeQuery(sqlstr.toString(), new MapListProcessor());
				result.setCode(IReturnStatus.SUCCEED);// 成功
				result.setMessage("项目档案查询成功");
				result.setResult(projectList);
				WuYeWgptUtil.createResultEsbVO(esb);
				esb.setResultInfo(result);
			}
		} catch (Exception e) {
			result.setCode(IReturnStatus.ERROR);// 异常
			result.setError(WuYeWgptUtil.getExceptionDetail(e));
			result.setMessage(result.getError());
			WuYeWgptUtil.createResultEsbVO(esb);// 初始化响应对象
			esb.getEsbInfo().setReturnStatus(IReturnStatus.ESB_ERROR);
			esb.getEsbInfo().setReturnMsg("异常");
			esb.setResultInfo(result);// 设置响应内容
		} finally {
			rsdata = com.alibaba.fastjson.JSONObject.toJSONString(esb);;

			WuYeWgptLogVO logVO = new WuYeWgptLogVO();
			logVO.setNc_id(nc_id);
			logVO.setOther_id("");
			logVO.setBillno("");
			logVO.setTranstypecode("");
			logVO.setItfcode("WGPT-NC-CG-006");
			logVO.setItfname("【NC->网购平台】项目档案查询接口");
			logVO.setBilldata(jsonobj == null ? "" : jsonobj.toString());
			logVO.setRsdata(rsdata);
			logVO.setMsg(null);
			logVO.setIssuccess(IReturnStatus.SUCCEED.equals(result.getCode()) ? "Y" : "N");

			WuYeWgptUtil.getIWuYeWgptLogService().insertLog_RequiresNew(logVO);
		}
		return rsdata;
	}

}
