package nccloud.openapi.ic.m4d;

import nc.bs.framework.common.NCLocator;
import nc.bs.logging.Log;
import nc.bs.scmpub.query.SCMBillQuery;
import nc.vo.ic.m4d.entity.MaterialOutVO;
import nc.vo.ic.pub.define.ICPubMetaNameConst;
import nc.ws.opm.pub.utils.result.APIErrCodeEnum;
import nccloud.api.ic.m4d.IAPIMaterialOutMaintain;
import nccloud.api.ic.m4d.IAPIMaterialOutQuery;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.openapi.scmpub.pub.NCCPubRestResource;
import org.json.JSONString;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version NCC1909
 * @Description: 库存材料出库单资源类
 * @author: 曹军
 * @date: 2019-5-17 上午10:50:42
 */

@Path("ic/materialout")
public class MaterialOutResource extends NCCPubRestResource {

    private Integer pageNo = defaultPageIndex;

    private Integer pageSize = defaultPageNum;

    private static String HEADTABLE = "ic_material_h";

    private static String BODYTABLE = "ic_material_b";

    private static final Log log = Log.getInstance("wmslog");

    @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();
            }
            IAPIMaterialOutQuery iQuery =
                    (IAPIMaterialOutQuery) NCLocator.getInstance().lookup(
                            IAPIMaterialOutQuery.class.getName());
            MaterialOutVO[] materialOutVOs =
                    iQuery.queryPageVOByScheme(paramMap, this.pageNo,
                            this.pageSize);
            return ResultMessageUtil.toJSON(materialOutVOs, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
                    .getStrByID("4008027_0", "04008027-0470")/* @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 {
            // 处理传过来的分页信息
            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();
            }

            IAPIMaterialOutQuery iQuery =
                    (IAPIMaterialOutQuery) NCLocator.getInstance().lookup(
                            IAPIMaterialOutQuery.class.getName());
            MaterialOutVO[] materialOutVOs =
                    iQuery.queryPageVOByScheme(paramMap, defaultPageIndex,
                            defaultPageNum);
            return ResultMessageUtil.toJSON(materialOutVOs, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
                    .getStrByID("4008027_0", "04008027-0470")/* @res "材料出库单查询成功" */);
        } catch (Exception e) {
            return ResultMessageUtil.exceptionToJSON(e);
        }

    }

    @POST
    @Path("operation/save")
    @Consumes("application/json")
    @Produces("application/json")
    public JSONString save(Map<String, Object> param) {
        List<Map<String, Object>> paramList = new ArrayList<Map<String, Object>>();
        paramList.add(param);
        JSONString result = save(paramList);
        return result;
    }

    @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());
                }
            }
            IAPIMaterialOutMaintain materialOut =
                    NCLocator.getInstance().lookup(IAPIMaterialOutMaintain.class);
            MaterialOutVO[] materialOutVOs = materialOut.save(paramList);
            return ResultMessageUtil.toJSON(materialOutVOs, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
                    .getStrByID("4008027_0", "04008027-0472")/* @res "材料出库单保存成功" */);
        } catch (Exception e) {
            return ResultMessageUtil.exceptionToJSON(e);
        }
    }

    @POST
    @Path("batchsave")
    @Consumes("application/json")
    @Produces("application/json")
    public JSONString batchsave(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());
                }
            }
            IAPIMaterialOutMaintain materialOut =
                    NCLocator.getInstance().lookup(IAPIMaterialOutMaintain.class);
            MaterialOutVO[] materialOutVOs = materialOut.batchsave(paramList);

            return ResultMessageUtil.toJSON(materialOutVOs, nc.vo.ml.NCLangRes4VoTransl.getNCLangRes()
                    .getStrByID("4008027_0", "04008027-0472")/* @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);

            IAPIMaterialOutMaintain materialOut =
                    NCLocator.getInstance().lookup(IAPIMaterialOutMaintain.class);
            // vbatchcode 批次号字段上游需要传递给WMS
            MaterialOutVO[] updateVO = materialOut.update(paramMapList);
            return ResultMessageUtil.toJSON(updateVO,
                    nc.vo.ml.NCLangRes4VoTransl.getNCLangRes().getStrByID("4008027_0",
                            "04008027-0474")/* @res "材料出库单修改成功" */);
        } catch (Exception e) {
            return ResultMessageUtil.exceptionToJSON(e);
        }
    }
    @POST
    @Path("operation/delete")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString delete(Map<String, Object> param) {
        Object[] paramArray = ((ArrayList)param.get("cgeneralhid")).toArray();
        String[] paramStrings = new String[paramArray.length];

        for(int i = 0; i < paramArray.length; ++i) {
            paramStrings[i] = paramArray[i].toString();
        }

        JSONString result = this.delete(paramStrings);
        return result;
    }


    @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<MaterialOutVO> queryTool =
                        new SCMBillQuery<MaterialOutVO>(MaterialOutVO.class);
                MaterialOutVO[] materialOutVOs = queryTool.queryVOByIDs(hids);
                if (materialOutVOs.length == 0) {
                    return ResultMessageUtil.exceptionToJSON("根据传入数据未匹配到相关数据", APIErrCodeEnum.BUSINESSEXCCODE.getCode());
                } else {
                    NCLocator.getInstance()
                            .lookup(IAPIMaterialOutMaintain.class).delete(materialOutVOs);
                    return ResultMessageUtil.toJSON(new String[0], nc.vo.ml.NCLangRes4VoTransl
                            .getNCLangRes().getStrByID("4008027_0", "04008027-0476")/*
                     * @res
                     * "材料出库单删除成功"
                     */);
                }
            } catch (Exception e) {
                return ResultMessageUtil.exceptionToJSON(e);
            }
        }
    }

    @POST
    @Path("operation/sign")
    @Consumes("application/json")
    @Produces("application/json")
    public JSONString sign(Map<String, Object> param) {
        Object[] paramArray = ((ArrayList<String>) param.get("cgeneralhid")).toArray();
        String[] paramStrings = new String[paramArray.length];
        for (int i = 0; i < paramArray.length; i++) {
            paramStrings[i] = paramArray[i].toString();
        }
        JSONString result = sign(paramStrings);
        return result;
    }

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

    @POST
    @Path("operation/unSign")
    @Consumes("application/json")
    @Produces("application/json")
    public JSONString unSign(Map<String, Object> param) {
        Object[] paramArray = ((ArrayList<String>) param.get("cgeneralhid")).toArray();
        String[] paramStrings = new String[paramArray.length];
        for (int i = 0; i < paramArray.length; i++) {
            paramStrings[i] = paramArray[i].toString();
        }
        JSONString result = unSign(paramStrings);
        return result;
    }

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

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

    @Override
    public String getModule() {
        // TODO Auto-generated method stub
        return null;
    }

}
