package nccloud.api.so.so.UpCloses;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.bs.framework.common.NCLocator;
import nc.itf.so.m30.closemanage.ISaleOrderCloseManageMaintain;
import nc.pubitf.so.m30.api.ISaleOrderQueryAPI;
import nc.pubitf.uapbd.IRateTypePubService;
import nc.vo.so.m30.entity.SaleOrderBVO;
import nc.vo.so.m30.entity.SaleOrderVO;
import nc.vo.so.m30.entity.SaleOrderViewVO;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.framework.core.json.IJson;
import nccloud.framework.service.ServiceLocator;
import nccloud.framework.web.json.JsonFactory;
import nccloud.pubitf.scmpub.pub.batchopr.dto.ISCMBatchOprContext;
import nccloud.pubitf.scmpub.pub.batchopr.dto.SCMBatchOprContext;
import nccloud.pubitf.scmpub.pub.batchopr.dto.SCMBatchResultDTO;
import nccloud.web.scmpub.pub.resexp.PfResumeExceptionNccUtils;
import nccloud.web.scmpub.pub.utils.SCMBatchOperatorResult;
import nccloud.web.scmpub.pub.utils.batchopr.SCMBatchOperator;
import nccloud.web.so.saleorderclose.entity.SaleOrderCloseInfo;
import nccloud.api.so.so.UpCloses.SaleOrderCloseUtil;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
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.Map;

@Path("so/so/Close")
public class CloseSoResource extends AbstractNCCRestResource {
    @Override
    public String getModule() {
        return "so";
    }
    @POST
    @Path("/ClosesoWith")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString CloseSoResource(JSONString json) {
        JSONObject errojson = new JSONObject();
        try {
            JSONObject jObject = JSON.parseObject(json.toJSONString());
            if (jObject == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
            }
            JSONArray array = jObject.getJSONArray("bills");
            if (array.isEmpty()) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("请选择销售订单"));
            }
            String[] ids = new String[array.size()];
            for (int i = 0; i < array.size(); i++) {
                SaleOrderVO saleOrderVO = new SaleOrderVO();
                String pk = (String) array.getJSONObject(i).getOrDefault("pk", "");
                ids[i] = pk;

            }
            ISaleOrderQueryAPI iSaleOrderQueryAPI= NCLocator.getInstance()
                    .lookup(ISaleOrderQueryAPI.class);
            SaleOrderVO[] billvos = iSaleOrderQueryAPI.queryVOByIDs(ids);
//            ISCMBatchOprContext context = new SCMBatchOprContext();
//            context.setInterfaceName("nc.itf.so.m30.closemanage.ISaleOrderCloseManageMaintain");
//            context.setMethodName("closeSaleOrder");
//            context.setParamTypes(new Class[]{SaleOrderVO[].class});
//            context.setParams(new Object[]{billvos});
//            context.setBillPos(0);

//            SCMBatchResultDTO resulet = SCMBatchOperator.doRemoteCall(context);
            ISaleOrderCloseManageMaintain iSaleOrderCloseManageMaintain = NCLocator.getInstance()
                    .lookup(ISaleOrderCloseManageMaintain.class);
            SaleOrderVO[] saleOrderVOS= iSaleOrderCloseManageMaintain.closeSaleOrder(billvos);
            if(saleOrderVOS.length == 0){
                 errojson.put("status", "0");
                 errojson.put("message", "关闭失败");
                 errojson.put("codeList", "");
                 return ResultMessageUtil.toJSON(errojson);

            }
            errojson.put("status", "1");
            errojson.put("message", "成功关闭");
            errojson.put("codeList", "");
            // 返回表示操作成功的JSON字符串
            return ResultMessageUtil.toJSON(errojson);



        } catch (Exception e) {
            errojson.put("status", "0");
            errojson.put("message", e.getMessage());
            errojson.put("codeList", "");
            // 返回表示操作失败的JSON字符串
            return ResultMessageUtil.toJSON(errojson);
        }
    }
    @POST
    @Path("/OpenClose")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString OpenClose(JSONString json) {
        JSONObject errojson = new JSONObject();
        try {
            JSONObject jObject = JSON.parseObject(json.toJSONString());
            if (jObject == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
            }
            JSONArray array = jObject.getJSONArray("bids");
            if (array.isEmpty()) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("请选择销售订单"));
            }

            IJson json1 = JsonFactory.create();
            SaleOrderCloseInfo combineinfo = (SaleOrderCloseInfo)json1.fromJson(json.toJSONString(), SaleOrderCloseInfo.class);
            SaleOrderViewVO[] result = SaleOrderCloseUtil.buttonClick(combineinfo, "billOpen");
            if (result.length  == 0 ){
                errojson.put("status", "0");
                errojson.put("message", "打开失败");
                errojson.put("codeList", "");
                return ResultMessageUtil.toJSON(errojson);

            }
            errojson.put("status", "1");
            errojson.put("message", "打开成功");
            errojson.put("codeList", "");
            // 返回表示操作成功的JSON字符串
            return ResultMessageUtil.toJSON(errojson);



        } catch (Exception e) {
            errojson.put("status", "0");
            errojson.put("message", e.getMessage());
            errojson.put("codeList", "");
            // 返回表示操作失败的JSON字符串
            return ResultMessageUtil.toJSON(errojson);
        }
    }
    private void setCloseOpenReason(SaleOrderVO[] bills,String reason) {
        for(SaleOrderVO bill : bills) {
            SaleOrderBVO[] bodys = bill.getChildrenVO();

            for(SaleOrderBVO body : bodys) {
                body.setVclosereason(reason);
            }
        }

    }



}
