package nccloud.api.uapbd.material.materialclass;


import nc.bs.framework.common.NCLocator;
import nc.itf.bd.material.marbasclass.IMaterialBasClassQueryService;
import nc.vo.bd.material.marbasclass.MarBasClassVO;
import nccloud.api.rest.utils.ApiResourceParamUtils;
import nccloud.api.rest.utils.IJsonForAPI;
import nccloud.api.rest.utils.JsonFactoryForAPI;
import nccloud.api.rest.utils.ResultMessageUtil;
import nccloud.api.rest.utils.vo.ApiDataVO;
import nccloud.api.rest.utils.vo.ApiQueryParam;
import nccloud.api.rest.utils.vo.PageInfo;
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;
import java.util.Map;

@Path("/uapbd/materialclass")
public class MaterialClassQueryResources extends AbstractNCCRestResource {

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

    @POST
    @Path("query")
    @Consumes({"application/json"})
    @Produces({"application/json"})
    public JSONString query(JSONString json) {
        IJsonForAPI iJsonForAPI = JsonFactoryForAPI.create();
        ApiQueryParam apiQueryParam = (ApiQueryParam) iJsonForAPI.fromJson(json.toJSONString(), ApiQueryParam.class);
        String sender = apiQueryParam.getUfinterface().getSender();
        ApiDataVO apiDataVO = apiQueryParam.getUfinterface().getData();
        PageInfo pageInfo = apiQueryParam.getUfinterface().getPageInfo();
        Map<String, Object> param = apiDataVO.getParamdata();
        if (param.containsKey("name") && param.get("name") != null) {
            List<String> names = (List<String>) param.get("name");
            if (!names.isEmpty()) {
                param.put("name", names.toArray(new String[0]));
            } else {
                param.remove("name");
            }
        }
        if (param.containsKey("code") && param.get("code") != null) {
            List<String> codes = (List<String>) param.get("code");
            if (!codes.isEmpty()) {
                param.put("code", codes.toArray(new String[0]));
            } else {
                param.remove("code");
            }
        }

        try {
            String querycondition = (new ApiResourceParamUtils()).parseParmToSqlByCode(new MarBasClassVO(), param);
            MarBasClassVO[] vos = NCLocator.getInstance().lookup(IMaterialBasClassQueryService.class).queryMaterialClassByCondition(null, querycondition);
            return vos != null && vos.length > 0 ? ResultMessageUtil.toJSON(vos) : null;
        } catch (Exception ex) {
            return ResultMessageUtil.exceptionToJSON(ex);
        }
    }

}
