package nccloud.openapi.ic.m4c;

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

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import nc.bs.framework.common.InvocationInfoProxy;
import org.json.JSONString;

import nc.bs.framework.common.NCLocator;
import nc.bs.scmpub.query.SCMBillQuery;
import nc.vo.ic.m4c.entity.SaleOutVO;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.ws.opm.pub.utils.result.APIErrCodeEnum;
import nccloud.api.ic.m4c.IAPISaleOutMaintain;
import nccloud.api.ic.m4c.IAPISaleOutQuery;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.openapi.scmpub.pub.NCCPubRestResource;

/**
 *
 * @Description: 库存销售出库单资源类
 *
 * @author: 曹军
 * @date:   2019-5-17 上午10:21:00
 * @version NCC1909
 */

@Path("ic/saleout")
public class SaleOutResource extends NCCPubRestResource {

  private Integer pageNo = defaultPageIndex;

  private Integer pageSize = defaultPageNum;

  private static String HEADTABLE = "ic_saleout_h";

  private static String BODYTABLE = "ic_saleout_b";

  @POST
  @Path("commonquery")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString queryVOByCommon(Map<String, Object> paramMap) {
    if (paramMap == null || !paramMap.containsKey(ICPubMetaNameConst.PK_ORG)
        || !paramMap.containsKey(ICPubMetaNameConst.DBILLDATE)) {
    	return ResultMessageUtil.exceptionToJSON("传入参数错误,组织、单据日期条件必输", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
    }
    try {
      // 处理传过来的分页信息
      if (paramMap.get(PAGE_NO) != null) {
        Double pageno = (Double) paramMap.get(PAGE_NO);
        this.pageNo = pageno.intValue();
      }
      if (paramMap.get(PAGE_SIZE) != null) {
        Double pagesize = (Double) paramMap.get(PAGE_SIZE);
        this.pageSize = pagesize.intValue();
      }
      IAPISaleOutQuery iQuery =
          NCLocator.getInstance().lookup(IAPISaleOutQuery.class);
      SaleOutVO[] saleoutVOs =
          iQuery.queryPageVOByScheme(paramMap, this.pageNo,
              this.pageSize);
      return ResultMessageUtil.toJSON(saleoutVOs,
              nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID(
                  "4008027_0", "04008027-0460")/* @res "销售出库单查询成功" */);
    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    	}
  }

  @POST
  @Path("schemequery")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString queryVOByScheme(Map<String, Object> paramMap) {
    if (paramMap == null) {
    	return ResultMessageUtil.exceptionToJSON("传入参数为空,请检查", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
    }
    try {
      //调用查询接口
      IAPISaleOutQuery query =
          NCLocator.getInstance().lookup(IAPISaleOutQuery.class);
      SaleOutVO[] saleOutVOs = query.queryVOByScheme(paramMap);
      return ResultMessageUtil.toJSON(saleOutVOs,
          nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
              "04008027-0460")/* @res "销售出库单查询成功" */);
    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    }
  }

  @POST
  @Path("save")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString save(List<Map<String, Object>> paramList) {
    try {
      for (Map<String, Object> map : paramList) {
        if (!map.containsKey(HEADTABLE) || !map.containsKey(BODYTABLE)) {
        	return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
        }
      }
      // 保存
      SaleOutVO[] saleOutVOs =
          NCLocator.getInstance().lookup(IAPISaleOutMaintain.class)
              .save(paramList);
      return ResultMessageUtil.toJSON(saleOutVOs,
          nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
              "04008027-0462")/* @res "销售出库单保存成功" */);
    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    }
  }

  @POST
  @Path("update")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString update(Map<String, Object> paramMap) {
    try {
      if (paramMap == null || !paramMap.containsKey(HEADTABLE)) {
        return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
      }
      List<Map<String, Object>> paramMapList= new ArrayList<>();
      paramMapList.add(paramMap);
      //锐制需要修改保存时走签字
      String userCode = InvocationInfoProxy.getInstance().getUserCode();
      if("rzmes".equals(userCode)){
        // 修改并签字--事务一致
        SaleOutVO[] signVOs =NCLocator.getInstance().lookup(IAPISaleOutMaintain.class).updateAndSign(paramMapList);


        return ResultMessageUtil.toJSON(signVOs,
                nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
                        "04008027-0464")/* @res "销售出库单修改签字成功" */);
      }else{
        //修改
        SaleOutVO[] updateVO =
                NCLocator.getInstance().lookup(IAPISaleOutMaintain.class)
                        .update(paramMapList);
        return ResultMessageUtil.toJSON(updateVO,
                nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
                        "04008027-0464")/* @res "销售出库单修改成功" */);
      }

    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    }
  }

  @POST
  @Path("delete")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString delete(String[] hids) {
    if (hids == null || hids.length == 0) {
    	return ResultMessageUtil.exceptionToJSON("传入参数为空,请检查", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
    }
    else {
      try {
        SCMBillQuery<SaleOutVO> queryTool =
            new SCMBillQuery<SaleOutVO>(SaleOutVO.class);
        SaleOutVO[] saleOutVOs = queryTool.queryVOByIDs(hids);
        if (saleOutVOs.length == 0) {
        	return ResultMessageUtil.exceptionToJSON("根据传入数据未匹配到相关数据", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
        }
        else {
          NCLocator.getInstance()
              .lookup(IAPISaleOutMaintain.class).delete(saleOutVOs);
          return ResultMessageUtil.toJSON(new String[0], nc.vo.ml.NCLangRes4VoTransl
                  .getNCLangRes().getStrByID("4008027_0", "04008027-0466")/*
                                                                           * @res
                                                                           * "销售出库单删除成功"
                                                                           */);
        }
      }
      catch (Exception e) {
        return ResultMessageUtil.exceptionToJSON(e);
      }
    }
  }

  @POST
  @Path("sign")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString sign(String[] hids) {
    if (hids == null || hids.length == 0) {
    	return ResultMessageUtil.exceptionToJSON("传入参数为空,请检查", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
    }
    else {
      try {
        SCMBillQuery<SaleOutVO> queryTool =
            new SCMBillQuery<SaleOutVO>(SaleOutVO.class);
        SaleOutVO[] saleOutVOs = queryTool.queryVOByIDs(hids);
        if (saleOutVOs.length == 0) {
        	return ResultMessageUtil.exceptionToJSON("根据传入数据未匹配到相关数据", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
        }
        else {
          saleOutVOs =
              NCLocator.getInstance()
                  .lookup(IAPISaleOutMaintain.class).sign(saleOutVOs);
          return ResultMessageUtil.toJSON(saleOutVOs, nc.vo.ml.NCLangRes4VoTransl
                  .getNCLangRes().getStrByID("4008027_0", "04008027-0468")/*
                                                                           * @res
                                                                           * "销售出库单签字成功"
                                                                           */);
        }
      }
      catch (Exception e) {
        return ResultMessageUtil.exceptionToJSON(e);
      }
    }
  }

  @POST
  @Path("saveRefSaleOrder")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString saveRefSaleOrder(Map<String, Object> paramMap) {
    try {
      if (!paramMap.containsKey(HEADTABLE) || !paramMap.containsKey(BODYTABLE)) {
      	return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
      }
      List<Map<String, Object>> paramMapList= new ArrayList<>();
      paramMapList.add(paramMap);
      
      IAPISaleOutMaintain service =
              NCLocator.getInstance().lookup(IAPISaleOutMaintain.class);
          SaleOutVO[] resultVOs =
              service.saveRefSaleOrder(paramMapList);
      // 返回结果
      return ResultMessageUtil.toJSON(resultVOs, "销售出库单保存成功");
    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    }
  }

  @POST
  @Path("saveRefDelivery")
  @Consumes("application/json")
  @Produces("application/json")
  public JSONString saveRefDelivery(Map<String, Object> paramMap) {
    try {
      if (!paramMap.containsKey(HEADTABLE) || !paramMap.containsKey(BODYTABLE)) {
      	return ResultMessageUtil.exceptionToJSON("传入数据异常，参数要包含表头信息和表体信息", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
      }
      List<Map<String, Object>> paramMapList= new ArrayList<>();
      paramMapList.add(paramMap);
      IAPISaleOutMaintain service =
          NCLocator.getInstance().lookup(IAPISaleOutMaintain.class);
      SaleOutVO[] resultVOs =
          service.saveRefDelivery(paramMapList);
      return ResultMessageUtil.toJSON(resultVOs, "销售出库单保存成功");
    }
    catch (Exception e) {
      return ResultMessageUtil.exceptionToJSON(e);
    }
  }

}
