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.KuaiShouDimensionAccount;
import cn.com.ctop.kuaishou.modules.dimension.mapper.KuaiShouDimensionAccountMapper;
import cn.com.ctop.kuaishou.modules.dimension.service.IKuaiShouDimensionAccountService;
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.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 多维度测试关联账户表
 *
 * @author jeecg-boot
 * 2021-07-19
 * @version V1.0
 */
@Service
public class KuaiShouDimensionAccountServiceImpl extends ServiceImpl<KuaiShouDimensionAccountMapper, KuaiShouDimensionAccount> implements IKuaiShouDimensionAccountService {
    @Autowired
    private KuaiShouDimensionAccountMapper dimensionAccountMapper;

    @Override
    public void insertAccountIds(Long id, JSONArray accountInfos, JSONArray creativeArray, JSONArray crowdArray) {

        QueryWrapper<KuaiShouDimensionAccount> dimensionAccountQueryWrapper = new QueryWrapper<>();
        dimensionAccountQueryWrapper.eq("strategy_id", id);
        this.remove(dimensionAccountQueryWrapper);
        Map<Long, JSONObject> creativeMap = new HashMap<>();
        if (!Check.isNull(creativeArray)) {
            for (int i = 0; i < creativeArray.size(); i++) {
                JSONObject creativeJson = creativeArray.getJSONObject(i);
                Long accountId = creativeJson.getLong("accountId");
                creativeMap.put(accountId, creativeJson);
            }
        }
        Map<Long, JSONObject> crowdMap = new HashMap<>();
        if (!Check.isNull(crowdArray)) {
            for (int i = 0; i < crowdArray.size(); i++) {
                JSONObject jsonObject = crowdArray.getJSONObject(i);
                Long accountId = jsonObject.getLong("accountId");
                crowdMap.put(accountId, jsonObject);
            }
        }

        List<KuaiShouDimensionAccount> addList = new ArrayList<>();
        for (int i = 0; i < accountInfos.size(); i++) {
            JSONObject jsonObject = accountInfos.getJSONObject(i);
            if (Check.isNull(jsonObject)) {
                continue;
            }
            Long accountId = jsonObject.getLong("accountId");
            KuaiShouDimensionAccount dimensionAccount = new KuaiShouDimensionAccount();
            dimensionAccount.setStrategyId(id);
            dimensionAccount.setAccountId(accountId);
            Integer accountStatus = jsonObject.getInteger("accountStatus");
            if (Check.isNull(accountStatus)) {
                dimensionAccount.setAccountStatus(0);
            } else {
                dimensionAccount.setAccountStatus(accountStatus);
            }
            if (!Check.isNull(jsonObject.getLong("appId"))) {
                dimensionAccount.setAppId(jsonObject.getLong("appId"));
            }
            if (!Check.isNull(jsonObject.getString("clickTrackUrl"))) {
                dimensionAccount.setClickTrackUrl(jsonObject.getString("clickTrackUrl"));
            }
            if (!Check.isNull(jsonObject.getString("actionbarClickUrl"))) {
                dimensionAccount.setActionbarClickUrl(jsonObject.getString("actionbarClickUrl"));
            }
            if (!Check.isNull(jsonObject.getInteger("useSchemaUri"))) {
                dimensionAccount.setUseSchemaUri(jsonObject.getInteger("useSchemaUri"));
            }
            if (!Check.isNull(jsonObject.getString("schemaUri"))) {
                dimensionAccount.setSchemaUri(jsonObject.getString("schemaUri"));
            }
            if (!Check.isNullMap(creativeMap)) {
                JSONObject creativeJson = creativeMap.get(accountId);
                if (!Check.isNull(creativeJson)) {
                    if (!Check.isNull(creativeJson.getInteger("creativeCategory"))) {
                        dimensionAccount.setCreativeCategory(creativeJson.getInteger("creativeCategory"));
                    }
                    if (!Check.isNull(creativeJson.getJSONArray("creativeTag"))) {
                        dimensionAccount.setCreativeTag(creativeJson.getString("creativeTag"));
                    }
                }
            }

            if (!Check.isNullMap(crowdMap)) {
                JSONObject crowdJson = crowdMap.get(accountId);
                if (!Check.isNull(crowdJson)) {
                    JSONArray population = crowdJson.getJSONArray("population");
                    if (!Check.isNull(population)) {
                        dimensionAccount.setPopulation(population.toJSONString());
                    }
                    JSONArray excludePopulation = crowdJson.getJSONArray("excludePopulation");
                    if (!Check.isNull(excludePopulation)) {
                        dimensionAccount.setExcludePopulation(excludePopulation.toJSONString());
                    }

                    JSONArray paidAudience = crowdJson.getJSONArray("paidAudience");
                    if (!Check.isNull(paidAudience)) {
                        dimensionAccount.setPaidAudience(paidAudience.toJSONString());
                    }
                }
            }

            addList.add(dimensionAccount);
        }
        if (!Check.isNull(addList)) {
            dimensionAccountMapper.addBatch(addList);

        }

    }

    /**
     * 根据策略id查询所有关联账户
     *
     * @param id
     * @return
     */
    @Override
    public List<KuaiShouDimensionAccount> getListByStrategyId(Long id, Integer accountStatus) {
        QueryWrapper<KuaiShouDimensionAccount> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("strategy_id", id);
        if (!Check.isNull(accountStatus)) {
            queryWrapper.eq("account_status", accountStatus);
        }

        return this.list(queryWrapper);
    }

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


}
