package com.xiaomai.cases.polar.goods.Tools;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xiaomai.client.DataUserInfo;
import com.xiaomai.enums.ApiModule;
import com.xiaomai.enums.RequestType;
import com.xiaomai.enums.Terminal;
import com.xiaomai.utils.RandomStringUtil;
import com.xiaomai.utils.XMBaseTest;

public class GoodTools extends XMBaseTest {

    /**
     * @param
     * @description:新建物品
     * @param multiSpec:是否是多规格；true：多规格；false：单规格
     * @author: yff
     **/

    public JSONObject testCreateGoods(boolean multiSpec,DataUserInfo...userInfos) {
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_createGoods")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        String color1 = "黑色";
        String color2 = "白色";
        String size1 = "大的";
        String size2 = "小的";
        String skin1 = "貂蝉";
        String skin2 = "王昭君";

        JSONObject jsonObject = new JSONObject();

        //多规格
        if (multiSpec==true) {

            //商品介绍不为空，纯文本的写法
            JSONObject description = new JSONObject();
            JSONArray items = new JSONArray();
            JSONObject item1 = new JSONObject();
            item1.put("type", "TEXT");
            item1.put("content", "自动化创建的多规格三个规格，每个规格都有两个规格值");
            item1.put("aspect", "");

            items.add(item1);

            description.put("items", items);
            jsonObject.put("description", description.toString());


            jsonObject.put("groupId", "1869984687228407810");
            jsonObject.put("multiSpec", true);
            jsonObject.put("name", "自动化创建三个规格" + RandomStringUtil.randomString(2));
            jsonObject.put("xcxSaleState", "YES");


            JSONObject multiSpecInfo = new JSONObject();

            //设置三个规格和每个都有两个规格值
            JSONArray specList = new JSONArray();
            JSONObject specList1 = new JSONObject();
            specList1.put("name", "颜色");

            JSONArray specList1SpecValues = new JSONArray();
            JSONObject specList1SpecValues1 = new JSONObject();
            specList1SpecValues1.put("name", color1);
            JSONObject specList1SpecValues2 = new JSONObject();
            specList1SpecValues2.put("name", color2);
            specList1SpecValues.add(specList1SpecValues1);
            specList1SpecValues.add(specList1SpecValues2);
            specList1.put("specValues", specList1SpecValues);

            JSONObject specList2 = new JSONObject();
            specList2.put("name", "尺码");

            JSONArray specList2SpecValues = new JSONArray();
            JSONObject specList2SpecValues1 = new JSONObject();
            specList2SpecValues1.put("name", size1);
            JSONObject specList2SpecValues2 = new JSONObject();
            specList2SpecValues2.put("name", size2);
            specList2SpecValues.add(specList2SpecValues1);
            specList2SpecValues.add(specList2SpecValues2);
            specList2.put("specValues", specList2SpecValues);


            JSONObject specList3 = new JSONObject();
            specList3.put("name", "限定皮肤");

            JSONArray specList3SpecValues = new JSONArray();
            JSONObject specList3SpecValues1 = new JSONObject();
            specList3SpecValues1.put("name", skin1);
            JSONObject specList3SpecValues2 = new JSONObject();
            specList3SpecValues2.put("name", skin2);
            specList3SpecValues.add(specList3SpecValues1);
            specList3SpecValues.add(specList3SpecValues2);
            specList3.put("specValues", specList3SpecValues);

            specList.add(specList1);
            specList.add(specList2);
            specList.add(specList3);

            multiSpecInfo.put("specList", specList);


            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            //排列出来的组合2*2*2
            JSONArray specCombinationList = new JSONArray();

            JSONObject specCombinationList1 = new JSONObject();

            JSONArray specCombinationList1Specs = new JSONArray();
            specCombinationList1Specs.add(color1);
            specCombinationList1Specs.add(size1);
            specCombinationList1Specs.add(skin1);
            specCombinationList1.put("specs", specCombinationList1Specs);
            specCombinationList1.put("price", "0.01");
            specCombinationList1.put("inventory", 10);

            specCombinationList.add(specCombinationList1);

            JSONObject specCombinationList2 = new JSONObject();
            JSONArray specCombinationList2Specs = new JSONArray();
            specCombinationList2Specs.add(color1);
            specCombinationList2Specs.add(size1);
            specCombinationList2Specs.add(skin2);
            specCombinationList2.put("specs", specCombinationList2Specs);
            specCombinationList2.put("price", "0.01");
            specCombinationList2.put("inventory", 10);

            specCombinationList.add(specCombinationList2);


            JSONObject specCombinationList3 = new JSONObject();
            JSONArray specCombinationList3Specs = new JSONArray();
            specCombinationList3Specs.add(color1);
            specCombinationList3Specs.add(size2);
            specCombinationList3Specs.add(skin1);
            specCombinationList3.put("specs", specCombinationList3Specs);
            specCombinationList3.put("price", "0.01");
            specCombinationList3.put("inventory", 10);

            specCombinationList.add(specCombinationList3);


            JSONObject specCombinationList4 = new JSONObject();
            JSONArray specCombinationList4Specs = new JSONArray();
            specCombinationList4Specs.add(color1);
            specCombinationList4Specs.add(size2);
            specCombinationList4Specs.add(skin2);
            specCombinationList4.put("specs", specCombinationList4Specs);
            specCombinationList4.put("price", "0.01");
            specCombinationList4.put("inventory", 10);

            specCombinationList.add(specCombinationList4);


            JSONObject specCombinationList5 = new JSONObject();
            JSONArray specCombinationList5Specs = new JSONArray();
            specCombinationList5Specs.add(color2);
            specCombinationList5Specs.add(size1);
            specCombinationList5Specs.add(skin1);
            specCombinationList5.put("specs", specCombinationList5Specs);
            specCombinationList5.put("price", "0.01");
            specCombinationList5.put("inventory", 10);

            specCombinationList.add(specCombinationList5);


            JSONObject specCombinationList6 = new JSONObject();
            JSONArray specCombinationList6Specs = new JSONArray();
            specCombinationList6Specs.add(color2);
            specCombinationList6Specs.add(size1);
            specCombinationList6Specs.add(skin2);
            specCombinationList6.put("specs", specCombinationList6Specs);
            specCombinationList6.put("price", "0.01");
            specCombinationList6.put("inventory", 10);

            specCombinationList.add(specCombinationList6);

            JSONObject specCombinationList7 = new JSONObject();
            JSONArray specCombinationList7Specs = new JSONArray();
            specCombinationList7Specs.add(color2);
            specCombinationList7Specs.add(size2);
            specCombinationList7Specs.add(skin1);
            specCombinationList7.put("specs", specCombinationList7Specs);
            specCombinationList7.put("price", "0.01");
            specCombinationList7.put("inventory", 10);

            specCombinationList.add(specCombinationList7);


            JSONObject specCombinationList8 = new JSONObject();
            JSONArray specCombinationList8Specs = new JSONArray();
            specCombinationList8Specs.add(color2);
            specCombinationList8Specs.add(size2);
            specCombinationList8Specs.add(skin2);
            specCombinationList8.put("specs", specCombinationList8Specs);
            specCombinationList8.put("price", "0.01");
            specCombinationList8.put("inventory", 10);

            specCombinationList.add(specCombinationList8);

            multiSpecInfo.put("specCombinationList", specCombinationList);
            jsonObject.put("multiSpecInfo", multiSpecInfo);
            jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
            jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
            jsonObject.put("operatorId", dataApi.getLoginInfo().getAdminId());
        }

        //单规格
        else if (multiSpec==false){

                //商品介绍不为空，纯文本的写法
                JSONObject description = new JSONObject();
                JSONArray items = new JSONArray();
                JSONObject item1 = new JSONObject();
                item1.put("type", "TEXT");
                item1.put("content", "自动化创建的");
                item1.put("aspect", "");

                items.add(item1);

                description.put("items", items);
                jsonObject.put("description", description.toString());

                jsonObject.put("groupId", "1869984687228407810");
                jsonObject.put("multiSpec", false);
                jsonObject.put("name", "自动化创建单规格" + RandomStringUtil.randomString(2));
                jsonObject.put("xcxSaleState", "YES");
                JSONObject jsonObject2 = new JSONObject();

                jsonObject2.put("inventory", "10");
                jsonObject2.put("price", "0.01");

                jsonObject.put("singleSpecInfo", jsonObject2);
                jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
                jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
                jsonObject.put("operatorId", dataApi.getLoginInfo().getAdminId());

        }

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();

    }

    /**
     * @param
     * @description:获取物品列表
     * @author: yff
     **/

    public JSONObject studioGoods( DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_pageStudioGoods")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("current", 0);
        jsonObject.put("size", 20);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:删除物品
     * @author: yff
     **/

    public JSONObject deleteGoods(String id, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_delete")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id", id);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:新建物品的重名校验
     * @param name：物品名称
     * @author: yff
     **/

    public JSONObject checkRepeatNameForStudio(String name, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_checkRepeatNameForStudio")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name", name);
        jsonObject.put("id","");
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }


    /**
     * @param
     * @description:获取B端的物品详情
     * @param id：物品ID
     * @author: yff
     **/

    public JSONObject getDetail_goods(String id, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_getDetail_goods")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",id);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:获取多规格物品的库存详情；注意仅多规格，单规格没有这个接口
     * @param id：物品ID
     * @author: yff
     **/

    public JSONObject listMultiSpecInventorRecords(String id, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_listMultiSpecInventorRecords")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",id);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }


    /**
     * @param
     * @description:新建分組
     * @param name：分組名称
     * @author: yff
     **/

    public JSONObject createGoodsGroup(String name, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_createGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name",name);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:获取分组列表
     * @author: yff
     **/

    public JSONObject listPageGoodsGroup( DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_listPageGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:分组重名校验
     * @author: yff
     **/

    public JSONObject checkNameRepeatGoodsGroup(String name, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_checkNameRepeatGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("name",name);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }


    /**
     * @param
     * @description:删除分组
     * @param id：分组ID
     * @author: yff
     **/

    public JSONObject deleteGoodsGroup(String id, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_deleteGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",id);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:删除分组前是否存在物品的校验
     * @param id：分组ID
     * @author: yff
     **/

    public JSONObject checkAllowDelete(String id, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_checkAllowDelete")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",id);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:编辑分组
     * @param id：分组ID
     * @param name：修改后的分组名称
     * @author: yff
     **/

    public JSONObject modifyGoodsGroup(String id,String name, DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_modifyGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("id",id);
        jsonObject.put("name",name);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:新建物品时拉取物品分组
     * @author: yff
     **/

    public JSONObject listGoodsGroup(DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_listGoodsGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }

    /**
     * @param
     * @description:批量修改物品的分组
     * @param groupId：分组ID
     * @param id：物品ID
     * @author: yff
     **/

    public JSONObject batchModifyGoodsBelongGroup(String groupId,String id,DataUserInfo...userInfos){
        dataApi.setApiModule(ApiModule.Polar_Goods)
                .setApiName("API_batchModifyGoodsBelongGroup")
                .setTerminal(Terminal.B);

        super.beforeDataRequest(userInfos);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("groupId",groupId);

        JSONArray ids=new JSONArray();
        ids.add(id);
        jsonObject.put("ids",ids);
        jsonObject.put("brandId", dataApi.getLoginInfo().getBrandId());
        jsonObject.put("studioId", dataApi.getLoginInfo().getStudioId());
        jsonObject.put("operatorId",dataApi.getLoginInfo().getAdminId());

        dataApi.doRequest(RequestType.JSON,dataparams,jsonObject.toString(),dataheadrs).assetsSuccess(true);
        return dataApi.getBodyInJSON();
    }


}
