package nccloud.openapi.pu.m23;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import javax.ws.rs.Consumes;
import nccloud.openapi.scmpub.util.CallReturnBuildUtil;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import nc.bs.framework.common.NCLocator;
import nc.vo.pu.m23.entity.ArriveVO;
import nc.vo.pub.BusinessException;
import nc.vo.pubapp.pattern.exception.ExceptionUtils;
import nc.ws.opm.pub.utils.result.APIErrCodeEnum;
import nccloud.api.pu.m23.IAPIPuArriveMaitain;
import nccloud.api.pu.m23.IAPIPuArriveQuery;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.openapi.pu.PURestResource;
import org.json.JSONString;
import nc.pubitf.pu.m23.pubquery.IArrivePubQuery;
import nccloud.pubitf.scmpub.commit.service.IBatchRunScriptService;
import nccloud.api.rest.utils.NCCRestUtils;
import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;

/**
 * 采购订单生成到货单操作资源类
 * 
 * @Description:
 *
 * @author: lanli
 * @date: 2019-6-17 上午10:24:57
 * @version NCC1909
 */
@Path("pu/arrive")
public class PuArriveResource extends PURestResource {

	private static String HEADTABLE = "po_arriveorder";
	private static String BODYTABLE = "po_arriveorder_b";

	/**
	 * 根据订单生成到货单
	 * 
	 * @param param
	 * @return
	 */

	@SuppressWarnings("unchecked")
	@POST
	@Path("operation/saveFromOrder")
	@Consumes("application/json")
	@Produces("application/json")
	public JSONString saveFromOrder(Map<String, Object> param) {
		List<Map<String, Object>> paramList = (List<Map<String, Object>>) param.get("data");
		JSONString result = saveFromOrder(paramList);
		return result;
	}

	@POST
	@Path("saveFromOrder")
	@Consumes("application/json")
	@Produces("application/json")
	public JSONString saveFromOrder(List<Map<String, Object>> paramList) {
		if (paramList.isEmpty()) {
			return ResultMessageUtil.exceptionToJSON("根据传入数据未匹配到相关数据", APIErrCodeEnum.BUSINESSEXCCODE.getCode());

		}
		// 设置集团
		for (Map<String, Object> paramMap : paramList) {
			if (!paramMap.containsKey(HEADTABLE) || !paramMap.containsKey(BODYTABLE)) {
				return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息",
						APIErrCodeEnum.BUSINESSEXCCODE.getCode());

			}
		}
		ArriveVO[] proinVOs = null;
		try {
			proinVOs = nc.bs.framework.common.NCLocator.getInstance().lookup(IAPIPuArriveMaitain.class)
					.saveFromOrder(paramList);
		} catch (BusinessException e) {
			ExceptionUtils.wrappException(e);
			return ResultMessageUtil.exceptionToJSON(e);
		}
		ArriveVO vos = proinVOs[0];
//			proinVOList.add(vos);
//		}
		return ResultMessageUtil.toJSON(vos, "到货单保存成功");
	}

	@POST
	@Path("querybyscheme")
	@Consumes("application/json")
	@Produces("application/json")
	public JSONString queryByScheme(Map<String, Object> paramMap) {
		ArriveVO[] resultVOs = null;
		try {
			resultVOs = NCLocator.getInstance().lookup(IAPIPuArriveQuery.class).queryVOByScheme(paramMap);
			// 包装返回信息
			return ResultMessageUtil.toJSON(resultVOs, "查询到货单成功");
		} catch (Exception e) {
			return ResultMessageUtil.exceptionToJSON(e);
		}
	}

	@POST
	@Path("approveArrival")
	@Consumes({ "application/json" })
	@Produces({ "application/json" })
	public JSONString approveArrival(Map<String, Object> paramMap) {
		try {
			List<Map<String, String>> bodyInfo = (List<Map<String, String>>) paramMap.get("pk_arriveorder");

			ArrayList<String> arr = new ArrayList<>();
			for (int i = 0; i < bodyInfo.size(); i++) {
				Map<String, String> bodyMap = (Map<String, String>) bodyInfo.get(i);
				arr.add(bodyMap.get("id"));
			}
			String[] arr1 = arr.toArray(new String[arr.size()]);

			IArrivePubQuery service = (IArrivePubQuery) NCLocator.getInstance().lookup(IArrivePubQuery.class);
			ArriveVO[] vos = service.queryAggVOByHids(arr1);

			if (vos.length == 0) {
				return NCCRestUtils.toJSONString(CallReturnBuildUtil.buildFailResult(vos, "到货单没有找到数据，查询失败"));
			}
			ArrayList<ArriveVO> list = new ArrayList<>();
			for (int i = 0; i < vos.length; i++) {
				Integer hvo = vos[i].getHVO().getFbillstatus();
				if (hvo == 0) {
					list.add(vos[i]);
				}
			}
			if (list.size() == 0) {
				return NCCRestUtils.toJSONString(CallReturnBuildUtil.buildFailResult(list, "到货单没有待审核数据，查询失败"));
			}

			CloudPFlowContext context = new CloudPFlowContext();
			context.setBillType("23");
			context.setActionName("SAVE");
			context.setBillVos(list.toArray(new ArriveVO[list.size()]));
			SCMScriptResultDTO dto = ((IBatchRunScriptService) NCLocator.getInstance()
					.lookup(IBatchRunScriptService.class)).runBacth(context, ArriveVO.class);

			return NCCRestUtils.toJSONString(CallReturnBuildUtil.buildSuccessResult(null, "到货单审核成功"));
		} catch (Exception var6) {
			return NCCRestUtils
					.toJSONString(CallReturnBuildUtil.buildFailResult((Object) null, "到货单审核失败:" + var6.getMessage()));
		}
	}

}
