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

import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.kuaishou.modules.report.entity.EtlKuaishouAudienceAccountDaily;
import cn.com.ctop.kuaishou.modules.report.enums.CheckParametersEnum;
import cn.com.ctop.kuaishou.modules.report.mapper.EtlKuaishouAudienceAccountDailyMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyAccountMapper;
import cn.com.ctop.kuaishou.modules.report.service.IEtlKuaishouAudienceAccountDailyService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 人群分析报表——年龄、性别数据
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2021-03-09
 */
@Service
public class EtlKuaishouAudienceAccountDailyServiceImpl extends ServiceImpl<EtlKuaishouAudienceAccountDailyMapper, EtlKuaishouAudienceAccountDaily> implements IEtlKuaishouAudienceAccountDailyService {
    @Autowired
    private KuaishouAudienceReportDailyAccountMapper accountMapper;
    @Autowired
    private EtlKuaishouAudienceAccountDailyMapper accountDailyMapper;

    @Override
    public void queryEveryDayAudienceInfo() {
        List<EtlKuaishouAudienceAccountDaily> list = accountMapper.queryEveryDayAudienceInfo();
        if (!Check.isNull(list)) {
            accountDailyMapper.replaceBatch(list);
        }
    }

    @Override
    public Result<Object> getSexInfo(JSONObject request) {
        Long accountId = request.getLong("accountId");
        String value = request.getString("value");
        String dataType = request.getString("dataType");
        if (Check.isNull(accountId) || Check.isNull(value)) {
            return Result.error("必填参数为空");
        }
        String parType = "more";
        if (CheckParametersEnum.isInclude(value)) {
            value = CheckParametersEnum.getSqlName(value);
            parType = "alone";
        }
        String audienceType = "gender";
        String type = "gender";
        List<JSONObject> list = null;
        List<String> typeList = accountDailyMapper.getTypeList(type, audienceType);
        if ("pie".equals(dataType)) {
            list = accountDailyMapper.queryAudienceTypeInfo(value, type, accountId, audienceType, parType);
            if (Check.isNull(list)) {
                return Result.ok("未查询到数据");
            }
            completeData(list, typeList);
        } else if ("line".equals(dataType)) {
            String startTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 8);
            String endTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 2);
            List<String> dayList = DateUtils.getDays(startTime, endTime);
            JSONObject data = new JSONObject();
            for (String day : dayList) {
                list = accountDailyMapper.queryEveryDayInfo(value, type, accountId, audienceType, parType, day);
                completeData(list, typeList);
                data.put(day, list);
            }
            return Result.ok(data);
        }
        return Result.ok(list);
    }

    @Override
    public Result<Object> getAgeInfo(JSONObject request) {
        Long accountId = request.getLong("accountId");
        String value = request.getString("value");
        String dataType = request.getString("dataType");
        if (Check.isNull(accountId) || Check.isNull(value) || Check.isNull(dataType)) {
            return Result.error("必填参数为空");
        }
        String parType = "more";
        if (CheckParametersEnum.isInclude(value)) {
            value = CheckParametersEnum.getSqlName(value);
            parType = "alone";
        }
        String audienceType = "ageSegment";
        String type = "age_segment";
        List<JSONObject> list = null;
        List<String> typeList = accountDailyMapper.getTypeList(type, audienceType);
        if ("pie".equals(dataType)) {
            list = accountDailyMapper.queryAudienceTypeInfo(value, type, accountId, audienceType, parType);
            if (Check.isNull(list)) {
                return Result.ok("未查询到数据");
            }
            completeData(list, typeList);
        } else if ("line".equals(dataType)) {
            String startTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 8);
            String endTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 2);
            List<String> dayList = DateUtils.getDays(startTime, endTime);
            JSONObject data = new JSONObject();
            for (String day : dayList) {
                list = accountDailyMapper.queryEveryDayInfo(value, type, accountId, audienceType, parType, day);
                completeData(list, typeList);
                data.put(day, list);
            }
            return Result.ok(data);
        }
        return Result.ok(list);
    }

    @Override
    public Result<Object> getSceneInfo(JSONObject request) {
        Long accountId = request.getLong("accountId");
        String value = request.getString("value");
        String dataType = request.getString("dataType");
        if (Check.isNull(accountId) || Check.isNull(value) || Check.isNull(dataType)) {
            return Result.error("必填参数为空");
        }
        String parType = "more";
        if (CheckParametersEnum.isInclude(value)) {
            value = CheckParametersEnum.getSqlName(value);
            parType = "alone";
        }
        List<JSONObject> list = null;
        List<String> typeList = accountDailyMapper.getSceneTypeList();
        typeList.remove("全部");
        if ("pie".equals(dataType)) {
            list = accountDailyMapper.getSceneInfo(value, accountId, parType);
            if (Check.isNull(list)) {
                return Result.ok("未查询到数据");
            }
            completeData(list, typeList);
        } else if ("line".equals(dataType)) {
            String startTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 8);
            String endTime = DateUtils.getLastDay(DateUtils.formatDate(new Date()), 2);
            List<String> dayList = DateUtils.getDays(startTime, endTime);
            JSONObject data = new JSONObject();
            for (String day : dayList) {
                list = accountDailyMapper.getSceneEveryDayInfo(value, accountId, parType, day);
                completeData(list, typeList);
                data.put(day, list);
            }
            return Result.ok(data);
        }
        return Result.ok(list);
    }

    /**
     * 补全数据
     */
    private void completeData(List<JSONObject> list, List<String> typeList) {
        List<String> newDayList = new ArrayList<>();
        for (JSONObject data : list) {
            if (!newDayList.contains(data.getString("type"))) {
                newDayList.add(data.getString("type"));
            }
        }
        for (String type : typeList) {
            if (!newDayList.contains(type)) {
                JSONObject obj = new JSONObject();
                obj.put("type", type);
                obj.put("value", 0);
                list.add(obj);
            }
        }
        userRechargeListSort(list);
    }

    private static void userRechargeListSort(List<JSONObject> list) {
        Collections.sort(list, new Comparator<JSONObject>() {
            @Override
            public int compare(JSONObject o1, JSONObject o2) {
                try {
                    String st1 = o1.getString("type");
                    String st2 = o2.getString("type");
                    return st1.compareTo(st2);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return 0;
            }
        });
    }
}
