package nccloud.api.so.so.saleorder;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.exception.DbException;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.pubitf.so.m30.api.ISaleOrderQueryAPI;
import nc.vo.pub.BusinessException;
import nc.vo.scmpub.res.billtype.SOBillType;
import nc.vo.scmpub.util.AppInfoContext;
import nc.vo.so.m30.entity.SaleOrderVO;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
import nccloud.pubitf.riart.pflow.ICloudScriptPFlowService;
import nccloud.ws.rest.resource.AbstractNCCRestResource;
import org.json.JSONString;

@Path("so/so/saleorder")
public class bipDelete extends AbstractNCCRestResource {

    @Override
    public String getModule() {
        return "so";
    }

    @POST
    @Path("/bipDelete")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString bipDelete(JSONString json) {
        try {
            JSONObject object = JSON.parseObject(json.toJSONString());
            if (object == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
            }
            // 校验必填字段
            String tipsStr = checkRequiredFields(object);
            if (!tipsStr.equals("")) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException(tipsStr));
            }
            String pk_org = (String) object.getOrDefault("pk_org", "");
            String vbillcode = (String) object.getOrDefault("vbillcode", "");
            String saleDelStr = getSaleorderPK(pk_org, vbillcode);
            if (saleDelStr == null || saleDelStr.equals("")) {
                JSONObject returnJson = new JSONObject();
                returnJson.put("state", 0);
                returnJson.put("Ascript", "删除失败，没有找到对应的销售订单!");
                returnJson.put("result", "");
                return ResultMessageUtil.toJSON(returnJson);
            }
            String[] ids = {saleDelStr};
            SaleOrderVO[] billvos = NCLocator.getInstance().lookup(ISaleOrderQueryAPI.class).queryVOByIDs(ids);

            CloudPFlowContext context = new CloudPFlowContext();
            context.setActionName("DELETE");
            context.setBillType(SOBillType.Order.getCode());
            context.setBillVos(billvos);
            AppInfoContext.setBtnCode("Delete");
            ICloudScriptPFlowService service = NCLocator.getInstance().lookup(ICloudScriptPFlowService.class);
            Object obj = service.exeScriptPFlow(context);

            JSONObject returnJson = new JSONObject();
            returnJson.put("state", 1);
            returnJson.put("Ascript", "删除成功!");
            returnJson.put("result", billvos);

            return ResultMessageUtil.toJSON(returnJson);
        } catch (DbException e) {
            JSONObject returnJson = new JSONObject();
            returnJson.put("state", 0);
            returnJson.put("Ascript", "删除失败" + e.getMessage());
            returnJson.put("result", "");
            return ResultMessageUtil.toJSON(returnJson);
        } catch (Exception e) {
            JSONObject returnJson = new JSONObject();
            returnJson.put("state", 0);
            returnJson.put("Ascript", "删除失败" + e.getMessage());
            returnJson.put("result", "");
            return ResultMessageUtil.toJSON(returnJson);
        }
    }

    // 方法：检查必填项并返回转化后的Map
    public static String checkRequiredFields(JSONObject data) throws Exception {
        String returnStr = "";
        // 2. 检查必填字段 pk_org
        String pkOrg = (String) data.getOrDefault("pk_org", "");
        if (pkOrg.isEmpty()) {
            return "字段pk_org缺失或为空!";
        }
        // 2. 检查必填字段 pk_org
        String vbillcode = (String) data.getOrDefault("vbillcode", "");
        if (vbillcode.isEmpty()) {
            return "字段vbillcode缺失或为空!";
        }
        return returnStr;
    }


    /*
     *创建时间：2024-11-28
     *作用：查询
     *传值：无
     *返回值：必填项map对象
     */
    private String getSaleorderPK(String pk_org, String vbillcode) throws BusinessException {
        String sql = " select h.csaleorderid from so_saleorder h\n"
                + "LEFT JOIN org_salesorg o ON h.pk_org = o.pk_salesorg\n"
                + "where o.code = '" + pk_org + "' and h.vbillcode = '" + vbillcode + "' ";
        String saleorderPK = (String) getQueryService().executeQuery(sql, new ColumnProcessor());
        return saleorderPK;
    }


    public IUAPQueryBS getQueryService() {
        return NCLocator.getInstance().lookup(IUAPQueryBS.class);
    }

}