package cn.com.ctop.kuaishou.modules.dimension.service.impl;

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.dimension.entity.KuaiShouDimensionVariable;
import cn.com.ctop.kuaishou.modules.dimension.mapper.KuaiShouDimensionVariableMapper;
import cn.com.ctop.kuaishou.modules.dimension.service.IKuaiShouDimensionVariableService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 多维度测试变量配置
 *
 * @author jeecg-boot
 * 2021-07-20
 * @version V1.0
 */
@Service
public class KuaiShouDimensionVariableServiceImpl extends ServiceImpl<KuaiShouDimensionVariableMapper, KuaiShouDimensionVariable> implements IKuaiShouDimensionVariableService {
    @Autowired
    private KuaiShouDimensionVariableMapper dimensionVariableMapper;

    @Override
    public JSONArray getDimensionVariableList(Long strategyId) {
        QueryWrapper<KuaiShouDimensionVariable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("strategy_id", strategyId);
        KuaiShouDimensionVariable dimensionVariable = this.getOne(queryWrapper);
        if (Check.isNull(dimensionVariable)) {
            return null;
        }
        List<List<JSONObject>> inputList = new ArrayList<>();
        //  地域数据组装
        String region = dimensionVariable.getRegion();
        JSONArray regionArray = JSONArray.parseArray(region);
        if (!Check.isNull(regionArray)) {
            List<JSONObject> regionList = new ArrayList<>();
            for (int i = 0; i < regionArray.size(); i++) {
                JSONObject regionJson = new JSONObject();
                regionJson.put("region", regionArray.getString(i));
                regionList.add(regionJson);
            }
            inputList.add(regionList);
        }
        // 年龄数据组装
        String agesRange = dimensionVariable.getAgesRange();
        JSONArray agesRangeArray = JSONArray.parseArray(agesRange);
        if (!Check.isNull(agesRangeArray)) {
            List<JSONObject> agesRangeList = new ArrayList<>();
            for (int i = 0; i < agesRangeArray.size(); i++) {
                JSONObject agesRangeJson = new JSONObject();
                agesRangeJson.put("ages_range", agesRangeArray.getString(i));
                agesRangeList.add(agesRangeJson);
            }
            inputList.add(agesRangeList);
        }

        // 性别数据组装
        String gender = dimensionVariable.getGender();
        JSONArray genderArray = JSONArray.parseArray(gender);
        if (!Check.isNull(genderArray)) {
            List<JSONObject> genderList = new ArrayList<>();
            for (int i = 0; i < genderArray.size(); i++) {
                JSONObject genderJson = new JSONObject();
                genderJson.put("gender", genderArray.getString(i));
                genderList.add(genderJson);
            }
            inputList.add(genderList);
        }
        // app行为
        String appIds = dimensionVariable.getAppIds();
        JSONArray appIdArray = JSONArray.parseArray(appIds);
        if (!Check.isNull(appIdArray)) {
            List<JSONObject> appIdList = new ArrayList<>();
            for (int i = 0; i < appIdArray.size(); i++) {
                JSONArray app_ids = appIdArray.getJSONArray(i);
                if (Check.isNull(app_ids)) {
                    continue;
                }
                JSONObject appIdJson = new JSONObject();
                appIdJson.put("app_ids", app_ids);
                appIdList.add(appIdJson);
            }
            inputList.add(appIdList);
        }

        // 投放时段
        String scheduleTime = dimensionVariable.getScheduleTime();
        JSONArray scheduleTimeArray = JSONArray.parseArray(scheduleTime);
        if (!Check.isNull(scheduleTimeArray)) {
            List<JSONObject> scheduleTimeList = new ArrayList<>();
            for (int i = 0; i < scheduleTimeArray.size(); i++) {
                JSONObject scheduleTimeJson = new JSONObject();
                scheduleTimeJson.put("schedule_time", scheduleTimeArray.getString(i));
                scheduleTimeList.add(scheduleTimeJson);
            }
            inputList.add(scheduleTimeList);
        }

        // 智能扩量
        String intelliExtend = dimensionVariable.getIntelliExtend();
        JSONArray intelliExtendArray = JSONArray.parseArray(intelliExtend);
        if (!Check.isNull(intelliExtendArray)) {
            List<JSONObject> intelliExtendList = new ArrayList<>();
            for (int i = 0; i < intelliExtendArray.size(); i++) {
                JSONObject intelliExtendJson = new JSONObject();
                intelliExtendJson.put("intelli_extend", intelliExtendArray.getString(i));
                intelliExtendList.add(intelliExtendJson);
            }
            inputList.add(intelliExtendList);
        }


        //  行为意向
        String intentionTarget = dimensionVariable.getIntentionTarget();
        JSONArray intentionTargetArray = JSONArray.parseArray(intentionTarget);
        if (!Check.isNull(intentionTargetArray)) {
            List<JSONObject> intelliExtendList = new ArrayList<>();
            for (int i = 0; i < intentionTargetArray.size(); i++) {
                JSONObject intentionTargetJson = new JSONObject();
                intentionTargetJson.put("intention_target", intentionTargetArray.getString(i));
                intelliExtendList.add(intentionTargetJson);
            }
            inputList.add(intelliExtendList);
        }


        JSONArray jsonArray = calculateCombination(inputList);
        return jsonArray;
    }

    @Override
    public void removeByStrategyId(String id) {
        QueryWrapper<KuaiShouDimensionVariable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("strategy_id", id);
        dimensionVariableMapper.delete(queryWrapper);
    }


    public static JSONArray calculateCombination(List<List<JSONObject>> inputList) {
        JSONArray array = new JSONArray();
        List<Integer> combination = new ArrayList<Integer>();
        int n = inputList.size();
        for (int i = 0; i < n; i++) {
            combination.add(0);
        }
        int i = 0;
        boolean isContinue = false;
        do {
            JSONObject dataJson = new JSONObject();
            //打印一次循环生成的组合
            for (int j = 0; j < n; j++) {
                JSONObject jsonObject = inputList.get(j).get(combination.get(j));
                jsonObject.forEach((key, value) -> {
                    dataJson.put(key, value);
                });

            }
            array.add(dataJson);

            i++;
            combination.set(n - 1, i);
            for (int j = n - 1; j >= 0; j--) {
                if (combination.get(j) >= inputList.get(j).size()) {
                    combination.set(j, 0);
                    i = 0;
                    if (j - 1 >= 0) {
                        combination.set(j - 1, combination.get(j - 1) + 1);
                    }
                }
            }
            isContinue = false;
            for (Integer integer : combination) {
                if (integer != 0) {
                    isContinue = true;
                }
            }
        } while (isContinue);
        return array;
    }


}
