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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.mapper.UserAllocationMapper;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.Check;
import cn.com.ctop.common.module.utils.HttpUtils;
import cn.com.ctop.common.module.utils.KuaishouInterfaceConstant;
import cn.com.ctop.common.module.utils.PropertiesUtils;
import cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouCreativeLevelOperationRecordMapper;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDaily;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyAccount;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyAge;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyBusiness;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyCity;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyClient;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyGender;
import cn.com.ctop.kuaishou.modules.report.entity.KuaishouAudienceReportDailyProvince;
import cn.com.ctop.kuaishou.modules.report.enums.AudienceReportTableEnum;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyAgeMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyBusinessMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyCityMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyClientMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyGenderMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyMapper;
import cn.com.ctop.kuaishou.modules.report.mapper.KuaishouAudienceReportDailyProvinceMapper;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyAccountService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyAgeService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyBusinessService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyCityService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyClientService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyGenderService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyProvinceService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouAudienceReportDailyService;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人群分析报表
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-12-03
 */
@Slf4j
@Service
public class KuaishouAudienceReportDailyServiceImpl extends ServiceImpl<KuaishouAudienceReportDailyMapper, KuaishouAudienceReportDaily> implements IKuaishouAudienceReportDailyService {

    @Resource
    private KuaishouAudienceReportDailyMapper kuaishouAudienceReportDailyMapper;
    @Resource
    private KuaishouAudienceReportDailyProvinceMapper provinceMapper;
    @Resource
    private KuaishouAudienceReportDailyAgeMapper ageMapper;
    @Resource
    private KuaishouAudienceReportDailyCityMapper cityMapper;
    @Resource
    private KuaishouAudienceReportDailyClientMapper clientMapper;
    @Resource
    private KuaishouAudienceReportDailyBusinessMapper bussinessMapper;
    @Resource
    private KuaishouAudienceReportDailyGenderMapper genderMapper;
    @Autowired
    private IKuaishouAudienceReportDailyAccountService dailyAccountService;
    @Autowired
    private IKuaishouAudienceReportDailyProvinceService dailyProvinceService;
    @Autowired
    private IKuaishouAudienceReportDailyGenderService dailyGenderService;
    @Autowired
    private IKuaishouAudienceReportDailyClientService dailyClientService;
    @Autowired
    private IKuaishouAudienceReportDailyCityService dailyCityService;
    @Autowired
    private IKuaishouAudienceReportDailyBusinessService dailyBusinessService;
    @Autowired
    private IKuaishouAudienceReportDailyAgeService dailyAgeService;
    @Autowired
    private ICtopOauthTokenService tokenService;
    private String urlPath = PropertiesUtils.getConfig("kuaishou_api_url");

    /**
     * 人群分析报表入库
     *
     * @param type      province: 省级地域分布, city: 地级地域分布,gender: 性别分布,ageSegment: 年龄分布clientId: 系统分布 businessInterestTags: 商业兴趣分布
     * @param
     * @return
     */
    @Override
    public void crowdAnalysisReport(CtopOauthToken token, String date, String type, Long plan, Long unit, Integer page, Integer pageSize) {
        long start = System.currentTimeMillis();
        try {
            String url = urlPath + KuaishouInterfaceConstant.AUDIENCE_REPORT;
            JSONArray reportDims = new JSONArray();
            reportDims.add(type);
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", token.getAccessToken());
            headers.put("Content-Type", "application/json");
            JSONObject requestJson = new JSONObject();
            requestJson.put("advertiser_id", token.getAccountId());
            requestJson.put("start_date", date);
            requestJson.put("end_date", date);
            requestJson.put("report_dims", reportDims);
            requestJson.put("page", page);
            requestJson.put("page_size", pageSize);
            if (!Check.isNull(plan)) {
                JSONArray plans = new JSONArray();
                plans.add(plan);
                requestJson.put("campaign_ids", plans);
            }
            if (!Check.isNull(unit)) {
                JSONArray units = new JSONArray();
                units.add(unit);
                requestJson.put("unit_ids", units);
            }
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
            Thread.sleep(500);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    JSONObject datas = resultJson.getJSONObject("data");
                    JSONArray details = datas.getJSONArray("details");
                    if (!Check.isNull(details)) {
                        //账户级数据
                        if (Check.isNull(plan) && Check.isNull(unit)) {
                            List<KuaishouAudienceReportDailyAccount> list = new ArrayList<>();
                            for (int i = 0; i < details.size(); i++) {
                                JSONObject obj = details.getJSONObject(i);
                                KuaishouAudienceReportDailyAccount entity = JSONObject.toJavaObject(obj, KuaishouAudienceReportDailyAccount.class);
                                entity.setPhotoLike(obj.getLong("like"));
                                entity.setComments(obj.getLong("comment"));
                                entity.setPhotoShow(obj.getLong("show"));
                                entity.setShares(obj.getString("share"));
                                entity.setAccountId(Long.valueOf(token.getAccountId()));
                                entity.setAudienceType(type);
                                list.add(entity);
                            }
                            dailyAccountService.saveBatch(list);
                        } else {
                            List<KuaishouAudienceReportDailyProvince> provinceList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyCity> cityList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyGender> genderList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyAge> ageList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyClient> clientList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyBusiness> businessList = new ArrayList<>();
                            //组级
                            for (int i = 0; i < details.size(); i++) {
                                JSONObject obj = details.getJSONObject(i);
                                KuaishouAudienceReportDaily entity = JSONObject.toJavaObject(obj, KuaishouAudienceReportDaily.class);
                                entity.setPhotoLike(obj.getLong("like"));
                                entity.setComments(obj.getLong("comment"));
                                entity.setPhotoShow(obj.getLong("show"));
                                entity.setShares(obj.getString("share"));
                                entity.setAccountId(Long.valueOf(token.getAccountId()));
                                entity.setPlanId(plan);
                                entity.setUnitId(unit);
                                if ("province".equals(type)) {
                                    KuaishouAudienceReportDailyProvince provinceEntity = new KuaishouAudienceReportDailyProvince();
                                    BeanUtils.copyProperties(entity, provinceEntity);
                                    provinceList.add(provinceEntity);
                                } else if ("city".equals(type)) {
                                    KuaishouAudienceReportDailyCity cityEntity = new KuaishouAudienceReportDailyCity();
                                    BeanUtils.copyProperties(entity, cityEntity);
                                    cityList.add(cityEntity);
                                } else if ("gender".equals(type)) {
                                    KuaishouAudienceReportDailyGender genderEntity = new KuaishouAudienceReportDailyGender();
                                    BeanUtils.copyProperties(entity, genderEntity);
                                    genderList.add(genderEntity);
                                } else if ("ageSegment".equals(type)) {
                                    KuaishouAudienceReportDailyAge ageEntity = new KuaishouAudienceReportDailyAge();
                                    BeanUtils.copyProperties(entity, ageEntity);
                                    ageList.add(ageEntity);
                                } else if ("clientId".equals(type)) {
                                    KuaishouAudienceReportDailyClient clientEntity = new KuaishouAudienceReportDailyClient();
                                    BeanUtils.copyProperties(entity, clientEntity);
                                    clientList.add(clientEntity);
                                } else if ("businessInterestTags".equals(type)) {
                                    KuaishouAudienceReportDailyBusiness bussinessEntity = new KuaishouAudienceReportDailyBusiness();
                                    BeanUtils.copyProperties(entity, bussinessEntity);
                                    businessList.add(bussinessEntity);
                                }
                            }
                            if ("province".equals(type)) {
                                provinceMapper.replaceBatch(provinceList);
                            } else if ("city".equals(type)) {
                                cityMapper.replaceBatch(cityList);
                            } else if ("gender".equals(type)) {
                                genderMapper.replaceBatch(genderList);
                            } else if ("ageSegment".equals(type)) {
                                ageMapper.replaceBatch(ageList);
                            } else if ("clientId".equals(type)) {
                                clientMapper.replaceBatch(clientList);
                            } else if ("businessInterestTags".equals(type)) {
                                bussinessMapper.replaceBatch(businessList);
                            }
                        }
                    } else {
                        log.error("-----------未查询到人群分析报表数据：unitId：{}，日期：{}", unit, date);
                    }
                } else {
                    log.error("查询人群分析报表数据返回信息错误：accountId：{}，返回信息：{}", token.getAccountId(), resultJson);
                }
            }
        } catch (Exception e) {
            log.error("插入人群分析报表数据异常，数据维度：{}", type, e);
        }
    }
    @Resource
    cn.com.ctop.kuaishou.modules.ai.mapper.AiKuaishouCreativeLevelOperationRecordMapper creativeLevelOperationRecordMapper;

    @Resource
    UserAllocationMapper userAllocationMapper;

    @Override
    public void crowdAnalysisReport2(CtopOauthToken token, String date, String type, Long plan, Long unit, Integer page, Integer pageSize) {
        long start = System.currentTimeMillis();
        try {
            String url = urlPath + KuaishouInterfaceConstant.AUDIENCE_REPORT;
            JSONArray reportDims = new JSONArray();
            reportDims.add(type);
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", token.getAccessToken());
            headers.put("Content-Type", "application/json");
            JSONObject requestJson = new JSONObject();
            requestJson.put("advertiser_id", token.getAccountId());
            requestJson.put("start_date", date);
            requestJson.put("end_date", date);
            requestJson.put("report_dims", reportDims);
            requestJson.put("page", page);
            requestJson.put("page_size", pageSize);
            if (!Check.isNull(plan)) {
                JSONArray plans = new JSONArray();
                plans.add(plan);
                requestJson.put("campaign_ids", plans);
            }
            if (!Check.isNull(unit)) {
                JSONArray units = new JSONArray();
                units.add(unit);
                requestJson.put("unit_ids", units);
            }
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    JSONObject datas = resultJson.getJSONObject("data");
                    Integer totalCount = datas.getInteger("total_count");
                    JSONArray details = datas.getJSONArray("details");
                    if (!Check.isNull(details)) {
                        //账户级数据
                        if (Check.isNull(plan) && Check.isNull(unit)) {
                            List<KuaishouAudienceReportDailyAccount> list = new ArrayList<>();
                            for (int i = 0; i < details.size(); i++) {
                                JSONObject obj = details.getJSONObject(i);
                                KuaishouAudienceReportDailyAccount entity = JSONObject.toJavaObject(obj, KuaishouAudienceReportDailyAccount.class);
                                entity.setPhotoLike(obj.getLong("like"));
                                entity.setComments(obj.getLong("comment"));
                                entity.setPhotoShow(obj.getLong("show"));
                                entity.setShares(obj.getString("share"));
                                entity.setAccountId(Long.valueOf(token.getAccountId()));
                                entity.setAudienceType(type);
                                list.add(entity);
                            }
                            dailyAccountService.saveBatch(list);
                        } else {
                            List<KuaishouAudienceReportDailyProvince> provinceList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyCity> cityList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyGender> genderList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyAge> ageList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyClient> clientList = new ArrayList<>();
                            List<KuaishouAudienceReportDailyBusiness> businessList = new ArrayList<>();
                            //组级
                            for (int i = 0; i < details.size(); i++) {
                                JSONObject obj = details.getJSONObject(i);
                                KuaishouAudienceReportDaily entity = JSONObject.toJavaObject(obj, KuaishouAudienceReportDaily.class);
                                entity.setPhotoLike(obj.getLong("like"));
                                entity.setComments(obj.getLong("comment"));
                                entity.setPhotoShow(obj.getLong("show"));
                                entity.setShares(obj.getString("share"));
                                entity.setAccountId(Long.valueOf(token.getAccountId()));
                                entity.setPlanId(plan);
                                entity.setUnitId(unit);

                                String signature = creativeLevelOperationRecordMapper.selectSignatureByUnitId(unit);
                                if(!Check.isNull(signature)){
                                    entity.setSignature(signature);
                                }

                                Long projectId = userAllocationMapper.getProjectIdByAccountId(token.getAccountId());
                                if(!Check.isNull(projectId)){
                                    entity.setProjectId(projectId);
                                }

                                if ("province".equals(type)) {
                                    KuaishouAudienceReportDailyProvince provinceEntity = new KuaishouAudienceReportDailyProvince();
                                    BeanUtils.copyProperties(entity, provinceEntity);
                                    provinceList.add(provinceEntity);
                                } else if ("city".equals(type)) {
                                    KuaishouAudienceReportDailyCity cityEntity = new KuaishouAudienceReportDailyCity();
                                    BeanUtils.copyProperties(entity, cityEntity);
                                    cityList.add(cityEntity);
                                } else if ("gender".equals(type)) {
                                    KuaishouAudienceReportDailyGender genderEntity = new KuaishouAudienceReportDailyGender();
                                    BeanUtils.copyProperties(entity, genderEntity);
                                    genderList.add(genderEntity);
                                } else if ("ageSegment".equals(type)) {
                                    KuaishouAudienceReportDailyAge ageEntity = new KuaishouAudienceReportDailyAge();
                                    BeanUtils.copyProperties(entity, ageEntity);
                                    ageList.add(ageEntity);
                                } else if ("clientId".equals(type)) {
                                    KuaishouAudienceReportDailyClient clientEntity = new KuaishouAudienceReportDailyClient();
                                    BeanUtils.copyProperties(entity, clientEntity);
                                    clientList.add(clientEntity);
                                } else if ("businessInterestTags".equals(type)) {
                                    KuaishouAudienceReportDailyBusiness bussinessEntity = new KuaishouAudienceReportDailyBusiness();
                                    BeanUtils.copyProperties(entity, bussinessEntity);
                                    businessList.add(bussinessEntity);
                                }
                            }
                            if ("province".equals(type)) {
                                provinceMapper.replaceBatchNew(provinceList);
                            } else if ("city".equals(type)) {
                                cityMapper.replaceBatchNew(cityList);
                            } else if ("gender".equals(type)) {
                                genderMapper.replaceBatchNew(genderList);
                            } else if ("ageSegment".equals(type)) {
                                ageMapper.replaceBatchNew(ageList);
                            } else if ("clientId".equals(type)) {
                                clientMapper.replaceBatchNew(clientList);
                            } else if ("businessInterestTags".equals(type)) {
                                bussinessMapper.replaceBatchNew(businessList);
                            }
                        }
                        Integer totalPage = (totalCount - 1) / pageSize + 1;
                        if (page < totalPage) {
                            crowdAnalysisReport2(token,date, type, plan, unit, page + 1, pageSize);
                        }
                    } else {
                        if(totalCount != 0){
                            log.error("-----------未查询到人群分析报表数据：unitId：{}，日期：{}, 总量：{}", unit, date,totalCount);
                        }
                    }
                } else {
                    if(code == 500000 || code == 400001){
                        System.out.print("》》5秒后重试，accountId:"+unit+",日期:"+date+"《《 "+resultJson);
                        try{Thread.sleep(5000);} catch (InterruptedException e){};
                        crowdAnalysisReport2(token,date, type, plan, unit, page, pageSize);
                    }else {
                        log.error("查询人群分析报表数据返回信息错误：unit：{}，日期{}，返回信息：{}", unit,date, resultJson);
                    }
                }
            }
        } catch (Exception e) {
            log.error("插入人群分析报表数据异常，数据维度：{}", type, e);
        }
    }


    @Override
    public void crowdAnalysisReport3(CtopOauthToken token, String date, String type, Long plan, JSONArray unit, Integer page, Integer pageSize) {
        long start = System.currentTimeMillis();
        try {
            String url = urlPath + KuaishouInterfaceConstant.AUDIENCE_REPORT;
            JSONArray reportDims = new JSONArray();
            reportDims.add(type);
            Map<String, String> headers = new HashMap<>();
            headers.put("Access-Token", token.getAccessToken());
            headers.put("Content-Type", "application/json");
            JSONObject requestJson = new JSONObject();
            requestJson.put("advertiser_id", token.getAccountId());
            requestJson.put("start_date", date);
            requestJson.put("end_date", date);
            requestJson.put("report_dims", reportDims);
            requestJson.put("page", page);
            requestJson.put("page_size", pageSize);
            if (!Check.isNull(plan)) {
                JSONArray plans = new JSONArray();
                plans.add(plan);
                requestJson.put("campaign_ids", plans);
            }
            if (!Check.isNull(unit)) {
                requestJson.put("unit_ids", unit);
            }
            String result = HttpUtils.kuaiShouhttpPostRequest(url, requestJson.toJSONString(), headers);
            JSONObject resultJson = JSONObject.parseObject(result);
            if (!Check.isNull(resultJson)) {
                Integer code = resultJson.getInteger("code");
                if (code == 0) {
                    JSONObject datas = resultJson.getJSONObject("data");
                    Integer totalCount = datas.getInteger("total_count");
                    JSONArray details = datas.getJSONArray("details");
                    System.out.println("datas"+datas.toJSONString());
                }
            }
        } catch (Exception e) {
            log.error("插入人群分析报表数据异常，数据维度：{}", type, e);
        }
    }

    @Override
    public void aggregationDataByProjectId(String date, Long projectId) {
        this.aggregationData(date,projectId,AudienceReportTableEnum.CITY.getOriginTable(),AudienceReportTableEnum.CITY.getOriginTableKey(),AudienceReportTableEnum.CITY.getAggregationTable(),AudienceReportTableEnum.CITY.getAggregationTableKey());
        this.aggregationData(date,projectId,AudienceReportTableEnum.AGE.getOriginTable(),AudienceReportTableEnum.AGE.getOriginTableKey(),AudienceReportTableEnum.AGE.getAggregationTable(),AudienceReportTableEnum.AGE.getAggregationTableKey());
        this.aggregationData(date,projectId,AudienceReportTableEnum.CLIENT.getOriginTable(),AudienceReportTableEnum.CLIENT.getOriginTableKey(),AudienceReportTableEnum.CLIENT.getAggregationTable(),AudienceReportTableEnum.CLIENT.getAggregationTableKey());
        this.aggregationData(date,projectId,AudienceReportTableEnum.GENDER.getOriginTable(),AudienceReportTableEnum.GENDER.getOriginTableKey(),AudienceReportTableEnum.GENDER.getAggregationTable(),AudienceReportTableEnum.GENDER.getAggregationTableKey());
//        this.aggregationData(date,projectId,AudienceReportTableEnum.BUSINESS.getOriginTable(),AudienceReportTableEnum.BUSINESS.getOriginTableKey(),AudienceReportTableEnum.BUSINESS.getAggregationTable(),AudienceReportTableEnum.BUSINESS.getAggregationTableKey());
        this.aggregationData(date,projectId,AudienceReportTableEnum.PROVINCE.getOriginTable(),AudienceReportTableEnum.PROVINCE.getOriginTableKey(),AudienceReportTableEnum.PROVINCE.getAggregationTable(),AudienceReportTableEnum.PROVINCE.getAggregationTableKey());
    }

    public void aggregationData(String date,Long projectId,String originTable,String originTableKey,String aggregationTable,String aggregationTableKey){
        //查询总数据量
        Integer count = kuaishouAudienceReportDailyMapper.countTotal(date,projectId,originTable,originTableKey);
        if(null == count||count == 0){
            return;
        }
        int startIndex = 0;
        while (count>0){
            kuaishouAudienceReportDailyMapper.aggregationData(projectId,date,originTable,originTableKey,aggregationTable,aggregationTableKey,startIndex);
            startIndex+=100000;
            count-=100000;
        }
    }
}


