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

import cn.com.ctop.common.module.entity.TagInfo;
import cn.com.ctop.common.module.utils.ElasticsearchUtil;
import cn.com.ctop.kuaishou.modules.label.entity.LabelStandardInfo;
import cn.com.ctop.kuaishou.modules.label.entity.MaterialLabelInfo;
import cn.com.ctop.kuaishou.modules.label.mapper.MaterialLabelMapper;
import cn.com.ctop.kuaishou.modules.label.service.MaterialLabelService;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Description: 素材标签
 * @Author: zzy
 * @Date: 2021-07-30
 * @Version: V1.0
 */
@Service
@Slf4j
public class MaterialLabelServiceImpl implements MaterialLabelService {


    @Resource
    MaterialLabelMapper materialLabelMapper;

    @Autowired
    ElasticsearchUtil elasticsearchUtil;


    /**
     * 清洗各素材指标入库
     *
     * @param
     * @return
     */
    @Override
    public Result filterMaterial(String type) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        Result result = new Result();
        try {
            List<String> materiaSignature = new ArrayList<>();
            if (type.equals("1")) {
                //查询所有素材标签
                materiaSignature = materialLabelMapper.getMateriaSignature();
            } else {
                //查询增量素材
                Calendar calc = Calendar.getInstance();
                calc.setTime(new Date());
                calc.add(Calendar.DAY_OF_MONTH, -1);
                materiaSignature = materialLabelMapper.getMateriaSignatureCharge(sdf.format(calc.getTime()));
            }

            if (!materiaSignature.isEmpty()) {
                materiaSignature.stream().forEach(str -> {
                    if (!str.isEmpty()) {
                        //获取素材产生消耗的最早日期
                        String materialFirstDate = materialLabelMapper.getMaterialFirstDate(str);
                        if (materialFirstDate != null) {
                            //获取30天之后的日期
                            String afterDate = getDateByNumber(materialFirstDate, 30);
                            //获取2天之后的日期
                            String afterDate2 = getDateByNumber(materialFirstDate, 2);
                            //根据日期查询素材各项指标入库
                            LabelStandardInfo labermessage = materialLabelMapper.getLabermessage(str, materialFirstDate, afterDate);
                            if (labermessage != null) {
                                //查询两天内数据
                                LabelStandardInfo labermessageByTwoDays = materialLabelMapper.getLabermessageByTwoDays(str, materialFirstDate, afterDate2);
                                if (labermessageByTwoDays.getActivationTwoDays() != null) {
                                    labermessage.setActivationTwoDays(labermessageByTwoDays.getActivationTwoDays());
                                }
                                if (labermessageByTwoDays.getFormCountTwoDays() != null) {
                                    labermessage.setFormCountTwoDays(labermessageByTwoDays.getFormCountTwoDays());
                                }
                                labermessage.setSignature(str);
                                labermessage.setChargeDate(materialFirstDate);
                                labermessage.setCreateTime(sdf.format(new Date()));
                                labermessage.setUpdateTime(sdf.format(new Date()));
                                //查询素材标签标准表是否存在此素材
                                LabelStandardInfo materialRecord = materialLabelMapper.getMaterialRecord(str);
                                if (materialRecord != null) {
                                    //存在则更新
                                    materialLabelMapper.updateMaterialRecord(labermessage);
                                } else {
                                    //不存在插入
                                    materialLabelMapper.saveLabelMessage(labermessage);
                                }

                            }
                        }

                    }

                });
            }
            result.success("success");
        } catch (Exception e) {
            result.error500("error");
            log.info("清洗标签指标数据错误:{}", e.toString());
        }

        return result;
    }

    /**
     * 根据素材指标制定素材标签
     *
     * @param
     * @return
     */
    @Override
    public Result materialSpecificationLabel(String type) {
        Result result = new Result();
        Map<String, String> tgaMap = new HashMap<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<LabelStandardInfo> allMaterialData = new ArrayList<>();
        try {
            String date = sdf.format(new Date());
            //查询所有标签
            List<TagInfo> tagInfo = materialLabelMapper.getTagInfo();
            if (!tagInfo.isEmpty()) {
                tagInfo.stream().forEach(tag -> {
                    tgaMap.put(tag.getTagName(), tag.getTagCode() + ";" + tag.getId());
                });
            }
            if (type.equals("0")) {
                //查询全量素材标签判断标准数据
                allMaterialData = materialLabelMapper.getAllMaterialData();
            } else {
                allMaterialData = materialLabelMapper.getAllMaterialDataCharge(sdf.format(new Date()));
            }

            if (!allMaterialData.isEmpty()) {
                allMaterialData.stream().forEach(obj -> {
                    //三十天内累计激活数
                    String activationThirtyDays = obj.getActivationThirtyDays();
                    //两天内累计激活数
                    String activationTwoDays = obj.getActivationTwoDays();
                    //30天内表单提交数
                    String formCountThirtyDays = obj.getFormCountThirtyDays();
                    //两天内表单提交数
                    String formCountTwoDays = obj.getFormCountTwoDays();
                    //30天内3s完播率
                    String play3sRatio = obj.getPlay3sRatio();
                    //30天内转化率
                    String conversionRatio = obj.getConversionRatio();
                    //30天内行为率
                    String behaviorRation = obj.getBehaviorRation();
                    //30天内次留率
                    String eventNextDayStayRatio = obj.getEventNextDayStayRatio();
                    //应用下载--合格素材
                    if (activationThirtyDays != null && formCountThirtyDays != null && Integer.parseInt(activationThirtyDays) >= 100 && Integer.parseInt(formCountThirtyDays) == 0) {
                        String[] split = tgaMap.get("应用下载--合格素材").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载--合格素材", split[0], split[1], "1", date);
                        }
                    }

                    //应应用下载--起量快
                    if (activationTwoDays != null && formCountTwoDays != null && Integer.parseInt(activationTwoDays) >= 96 && Integer.parseInt(formCountTwoDays) == 0) {
                        String[] split = tgaMap.get("应用下载--起量快").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载--起量快", split[0], split[1], "1", date);
                        }
                    }

                    //应用下载-高-3s完播率
                    if (activationThirtyDays != null && play3sRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(play3sRatio) > 0.657) {
                        String[] split = tgaMap.get("应用下载-高-3s完播率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-高-3s完播率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-低-3s完播率
                    if (activationThirtyDays != null && play3sRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(play3sRatio) < 0.132) {
                        String[] split = tgaMap.get("应用下载-低-3s完播率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-低-3s完播率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-高-转化率
                    if (activationThirtyDays != null && conversionRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(conversionRatio) > 0.366) {
                        String[] split = tgaMap.get("应用下载-高-转化率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-高-转化率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-低-转化率
                    if (activationThirtyDays != null && conversionRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(conversionRatio) < 0.009) {
                        String[] split = tgaMap.get("应用下载-低-转化率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-低-转化率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-高-行为率
                    if (activationThirtyDays != null && behaviorRation != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(behaviorRation) > 0.059) {
                        String[] split = tgaMap.get("应用下载-高-行为率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-高-行为率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-低-行为率
                    if (activationThirtyDays != null && behaviorRation != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(behaviorRation) < 0.003) {
                        String[] split = tgaMap.get("应用下载-低-行为率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-低-行为率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-高-次留率
                    if (activationThirtyDays != null && eventNextDayStayRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(eventNextDayStayRatio) > 0.453) {
                        String[] split = tgaMap.get("应用下载-高-次留率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-高-次留率", split[0], split[1], "1", date);
                        }
                    }

                    // 应用下载-低-次留率
                    if (activationThirtyDays != null && eventNextDayStayRatio != null && Integer.parseInt(activationThirtyDays) >= 100 && Double.parseDouble(eventNextDayStayRatio) == 0.000) {
                        String[] split = tgaMap.get("应用下载-低-次留率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "应用下载-低-次留率", split[0], split[1], "1", date);
                        }
                    }


                    // 表单提交--合格素材
                    if (formCountThirtyDays != null && activationThirtyDays != null && Integer.parseInt(formCountThirtyDays) >= 20 && Integer.parseInt(activationThirtyDays) == 0) {
                        String[] split = tgaMap.get("表单提交--合格素材").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交--合格素材", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交--起量快
                    if (formCountTwoDays != null && activationTwoDays != null && Integer.parseInt(formCountTwoDays) >= 30 && Integer.parseInt(activationTwoDays) == 0) {
                        String[] split = tgaMap.get("表单提交--起量快").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交--起量快", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交-低-3s完播率
                    if (formCountThirtyDays != null && play3sRatio != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(play3sRatio) < 0.157) {
                        String[] split = tgaMap.get("表单提交-低-3s完播率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-低-3s完播率", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交-高-3s完播率
                    if (formCountThirtyDays != null && play3sRatio != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(play3sRatio) > 0.515) {
                        String[] split = tgaMap.get("表单提交-高-3s完播率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-高-3s完播率", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交-低-转化率
                    if (formCountThirtyDays != null && conversionRatio != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(conversionRatio) < 0.017) {
                        String[] split = tgaMap.get("表单提交-低-转化率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-低-转化率", split[0], split[1], "1", date);
                        }
                    }


                    // 表单提交-高-转化率
                    if (formCountThirtyDays != null && conversionRatio != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(conversionRatio) > 0.159) {
                        String[] split = tgaMap.get("表单提交-高-转化率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-高-转化率", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交-低-行为率
                    if (formCountThirtyDays != null && behaviorRation != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(behaviorRation) < 0.004) {
                        String[] split = tgaMap.get("表单提交-低-行为率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-低-行为率", split[0], split[1], "1", date);
                        }
                    }

                    // 表单提交-高-行为率
                    if (formCountThirtyDays != null && behaviorRation != null && Integer.parseInt(formCountThirtyDays) >= 20 && Double.parseDouble(behaviorRation) > 0.015) {
                        String[] split = tgaMap.get("表单提交-高-行为率").split(";");
                        int count = materialLabelMapper.getDataByCodeAndSignature(obj.getSignature(), split[0]);
                        //次素材没有打此标签
                        if (count == 0) {
                            materialLabelMapper.saveMaterialTag(obj.getSignature(), "表单提交-高-行为率", split[0], split[1], "1", date);
                        }
                    }

                });
            }
            result.success("success");
        } catch (Exception e) {
            result.error500("error");
            log.info("素材规定标签失败:{}", e.toString());
        }
        return result;

    }

    /**
     * 同步标签到ES
     *
     * @param
     * @return
     */
    @Override
    public Result SynchronousLabel(String type) {
        Result result = new Result();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        List<MaterialLabelInfo> materialLabelData = new ArrayList<>();
        try {
            if (type.equals("0")) {
                //查询所有素材
                materialLabelData = materialLabelMapper.getMaterialLabelData(null);
            } else {
                //查询增量素材
                materialLabelData = materialLabelMapper.getMaterialLabelData(sdf.format(new Date()));
            }
            //创建索引
            boolean indexIfNotExist = elasticsearchUtil.createIndexIfNotExist(MaterialLabelInfo.class);
            //同步ES
            if (materialLabelData.size() > 0) {
                //每5000条数据, 拆成一个list
                List<List<MaterialLabelInfo>> repayEntityVoList = getSubList(100, materialLabelData);
                if (!repayEntityVoList.isEmpty()) {
                    for (List<MaterialLabelInfo> materialLabelInfos : repayEntityVoList) {
                        elasticsearchUtil.batchSaveOrUpdate(materialLabelInfos);
                        //防止es队列满
                        Thread.sleep(500);
                    }
                }
            }
            result.setSuccess(true);
            log.info("同步标签至es成功:{}");
        } catch (Exception e) {
            result.setSuccess(false);
            log.info("同步标签至ES失败:{}", e.toString());
        }
        return result;
    }


    /**
     * 获取指定日期number天之后之前的日期
     *
     * @param
     * @return
     */
    public String getDateByNumber(String date, int number) {
        Calendar calc = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String AfterDate = null;
        try {
            calc.setTime(sdf.parse(date));
            calc.add(Calendar.DAY_OF_MONTH, number);
            AfterDate = sdf.format(calc.getTime());
        } catch (Exception e) {
            log.info("获取日期失败:{}", e.toString());
        }
        return AfterDate;
    }


    /**
     * 将list拆分成指定数量的小list
     * 注: 使用的subList方式,返回的是list的内部类,不可做元素的删除,修改,添加操作
     *
     * @param length 数量
     * @param list   大list
     */
    public List<List<MaterialLabelInfo>> getSubList(int length, List<MaterialLabelInfo> list) {
        int size = list.size();
        int temp = size / length + 1;
        boolean result = size % length == 0;
        List<List<MaterialLabelInfo>> subList = new ArrayList<>();
        for (int i = 0; i < temp; i++) {
            if (i == temp - 1) {
                if (result) {
                    break;
                }
                subList.add(list.subList(length * i, size));
            } else {
                subList.add(list.subList(length * i, length * (i + 1)));
            }
        }
        return subList;
    }

}
