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

import cn.com.ctop.kuaishou.modules.ai.mapper.IAiKuaiShouReportAnalyzeMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IAiKuaiShouReportAnalyzeService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
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;

@Service
public class AiKuaiShouReportAnalyzeServiceImpl implements IAiKuaiShouReportAnalyzeService {

    @Autowired
    private IAiKuaiShouReportAnalyzeMapper aiKuaiShouReportAnalyzeMapper;

    @Override
    public List<JSONObject> getStrategyAndTarget(Long accountId) {
        List<JSONObject> strategies = aiKuaiShouReportAnalyzeMapper.queryStrategyByAccountId(accountId);
        if (!strategies.isEmpty()) {
            List<JSONObject> deletes = new ArrayList<>();
            strategies.forEach(strategy -> {
                List<JSONObject> targets = aiKuaiShouReportAnalyzeMapper.queryTargetByStrategyId(strategy.getLong("id"));
                if (targets.isEmpty() || targets.get(0) == null) {
                    deletes.add(strategy);
                } else {
                    strategy.put("targets", targets);
                }
            });
            strategies.removeAll(deletes);
        }
        return strategies;
    }

    @Override
    public List<JSONObject> getStrategyAndMaterial(Long accountId) {
        List<JSONObject> strategies = aiKuaiShouReportAnalyzeMapper.queryStrategyByAccountId(accountId);
        if (!strategies.isEmpty()) {
            List<JSONObject> deletes = new ArrayList<>();
            strategies.forEach(strategy -> {
                List<JSONObject> materials = aiKuaiShouReportAnalyzeMapper.queryMaterialByStrategyId(strategy.getLong("id"));
                if (materials.isEmpty() || materials.get(0) == null) {
                    deletes.add(strategy);
                } else {
                    strategy.put("materials", materials);
                }
            });
            strategies.removeAll(deletes);
        }
        return strategies;
    }

    @Override
    public PageInfo<JSONObject> getTargetReportBy(JSONObject params) {
        Long accountId = params.getLong("accountId");
        Long strategyId = params.getLong("strategyId");
        JSONArray targetIds = params.getJSONArray("targetIds");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        int pageNo = params.getInteger("pageNo");
        int pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(aiKuaiShouReportAnalyzeMapper.queryMaterialReportBy(accountId, strategyId, targetIds, startDate, endDate));
    }

    @Override
    public PageInfo<JSONObject> getMaterialReportBy(JSONObject params) {
        Long accountId = params.getLong("accountId");
        Long strategyId = params.getLong("strategyId");
        JSONArray codes = params.getJSONArray("codes");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        int pageNo = params.getInteger("pageNo");
        int pageSize = params.getInteger("pageSize");
        PageHelper.startPage(pageNo, pageSize);
        return new PageInfo<>(aiKuaiShouReportAnalyzeMapper.queryTargetReportBy(accountId, strategyId, codes, startDate, endDate));
    }

    @Override
    public Map<String, Object> getTargetChartBy(JSONObject params) {
        Map<String,Object> result =new HashMap<>();
        Long strategyId = params.getLong("strategyId");
        JSONArray targetIds = params.getJSONArray("targetIds");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        result.put("age",aiKuaiShouReportAnalyzeMapper.queryAgeChartByTarget(strategyId,targetIds,startDate,endDate));
        result.put("gender",aiKuaiShouReportAnalyzeMapper.queryGenderChartByTarget(strategyId,targetIds,startDate,endDate));
        result.put("region",aiKuaiShouReportAnalyzeMapper.queryRegionChartByTarget(strategyId,targetIds,startDate,endDate));
        return result;
    }

    @Override
    public Map<String, Object> getMaterialChartBy(JSONObject params) {
        Map<String,Object> result =new HashMap<>();
        Long strategyId = params.getLong("strategyId");
        JSONArray codes = params.getJSONArray("codes");
        String startDate = params.getString("startDate");
        String endDate = params.getString("endDate");
        result.put("age",aiKuaiShouReportAnalyzeMapper.queryAgeChartByMaterial(strategyId,codes,startDate,endDate));
        result.put("gender",aiKuaiShouReportAnalyzeMapper.queryGenderChartByMaterial(strategyId,codes,startDate,endDate));
        result.put("region",aiKuaiShouReportAnalyzeMapper.queryRegionChartByMaterial(strategyId,codes,startDate,endDate));
        return result;    }
}
