//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package nccloud.api.uapbd.customermanage.customer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import nc.bs.dao.BaseDAO;
import nc.bs.framework.common.InvocationInfoProxy;
import nc.bs.framework.common.NCLocator;
import nc.itf.bd.cust.assign.ICustAssignService;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.pub.BusinessException;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.api.uapbd.common.utils.OpenApiPagenationUtils;
import nccloud.vo.sql.util.InSqlUtil;
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.List;

@Path("uapbd/customermanageext/customer")
public class AssignCustomerExtResource extends AbstractNCCRestResource {
    public AssignCustomerExtResource() {
    }

    @POST
    @Path("assign")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString assign(JSONString json) {
        JSONObject jObject = JSON.parseObject(json.toJSONString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
        } else {
            JSONObject object = jObject.getJSONObject("ufinterface");
            if (object == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("ufinterface:null"));
            } else {
                JSONObject data = object.getJSONObject("data");
                if (data == null) {
                    return ResultMessageUtil.exceptionToJSON(new NullPointerException("data:null"));
                } else {
                    JSONArray customer = data.getJSONArray("customer");
                    JSONArray org = data.getJSONArray("org");
                    List<String> customerList = OpenApiPagenationUtils.jSONArrayToList(customer);
                    List<String> orgList = OpenApiPagenationUtils.jSONArrayToList(org);
                    if (customerList.size() == 0) {
                        return ResultMessageUtil.exceptionToJSON(new BusinessException("传入参数中客户编码为空，请确认传入参数！"));
                    } else if (orgList.size() == 0) {
                        return ResultMessageUtil.exceptionToJSON(new BusinessException("传入参数待分配组织编码为空，请确认传入参数！"));
                    } else {
                        String pk_group = InvocationInfoProxy.getInstance().getGroupId();
                        String userid = InvocationInfoProxy.getInstance().getUserId();

                        try {
                            String[] customerPks = this.translateCustomers(customerList, pk_group);
                            String[] assignOrgPks = this.translateOrgPk(orgList, pk_group);
                            ((ICustAssignService) NCLocator.getInstance().lookup(ICustAssignService.class)).assignCustomerByPks(customerPks, assignOrgPks, (String[]) null);
                            return ResultMessageUtil.toJSON("客户分配成功！");
                        } catch (BusinessException e) {
                            return ResultMessageUtil.exceptionToJSON(e);
                        }
                    }
                }
            }
        }
    }

    @POST
    @Path("assignByPk")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString assignByPk(JSONString json) {
        JSONObject jObject = JSON.parseObject(json.toJSONString());
        if (jObject == null) {
            return ResultMessageUtil.exceptionToJSON(new NullPointerException("JSONString:null"));
        } else {
            JSONObject object = jObject.getJSONObject("ufinterface");
            if (object == null) {
                return ResultMessageUtil.exceptionToJSON(new NullPointerException("ufinterface:null"));
            } else {
                JSONObject data = object.getJSONObject("data");
                if (data == null) {
                    return ResultMessageUtil.exceptionToJSON(new NullPointerException("data:null"));
                } else {
                    JSONArray customer = data.getJSONArray("customer");
                    JSONArray org = data.getJSONArray("org");
                    List<String> customerList = OpenApiPagenationUtils.jSONArrayToList(customer);
                    List<String> orgList = OpenApiPagenationUtils.jSONArrayToList(org);
                    if (customerList.size() == 0) {
                        return ResultMessageUtil.exceptionToJSON(new BusinessException("传入参数中客户编码为空，请确认传入参数！"));
                    } else if (orgList.size() == 0) {
                        return ResultMessageUtil.exceptionToJSON(new BusinessException("传入参数待分配组织编码为空，请确认传入参数！"));
                    } else {
                        try {
                            String[] customerPks = customerList.toArray(new String[0]);
                            String[] assignOrgPks = orgList.toArray(new String[0]);
                            ((ICustAssignService) NCLocator.getInstance().lookup(ICustAssignService.class)).assignCustomerByPks(customerPks, assignOrgPks, null);
                            return ResultMessageUtil.toJSON("客户分配成功！");
                        } catch (BusinessException e) {
                            return ResultMessageUtil.exceptionToJSON(e);
                        }
                    }
                }
            }
        }
    }

    private String[] translateCustomers(List<String> codes, String pk_group) throws BusinessException {
        String sql = "select pk_customer from bd_customer where pk_group = '" + pk_group + "' and " + InSqlUtil.buildInSql("code", "or", "in", (String[]) codes.toArray(new String[0]));
        List<String> pks = (List) (new BaseDAO()).executeQuery(sql, new ColumnListProcessor());
        return pks != null && pks.size() > 0 ? (String[]) pks.toArray(new String[0]) : null;
    }

    private String[] translateOrgPk(List<String> codes, String pk_group) throws BusinessException {
        String sql = "select pk_org from org_orgs where isbusinessunit = 'Y' and pk_group = '" + pk_group + "' and " + InSqlUtil.buildInSql("code", "or", "in", (String[]) codes.toArray(new String[0]));
        List<String> pks = (List) (new BaseDAO()).executeQuery(sql, new ColumnListProcessor());
        return pks != null && pks.size() > 0 ? (String[]) pks.toArray(new String[0]) : null;
    }

    public String getModule() {
        return "uapbd";
    }
}
