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

import cn.com.ctop.common.module.entity.CtopOauthToken;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.utils.ResultMapUtils;
import cn.com.ctop.common.module.utils.StatusCode;
import cn.com.ctop.kuaishou.modules.ai.entity.KuaishouAccountSpeedExploreLog;
import cn.com.ctop.kuaishou.modules.ai.mapper.KuaishouAccountSpeedExploreLogMapper;
import cn.com.ctop.kuaishou.modules.ai.service.IKuaishouAccountSpeedExploreLogService;
import cn.com.ctop.kuaishou.modules.ai.vo.SpeedExploreGroupVO;
import cn.com.ctop.kuaishou.modules.batch.service.IKuaishouInterfaceService;
import cn.com.ctop.kuaishou.modules.report.service.IKuaishouGroupExploreService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 快手账户加速探索触发记录信息
 * @author jeecg-boot
 * @date   2021-03-08
 * @version V1.0
 */
@Service
public class KuaishouAccountSpeedExploreLogServiceImpl extends ServiceImpl<KuaishouAccountSpeedExploreLogMapper, KuaishouAccountSpeedExploreLog> implements IKuaishouAccountSpeedExploreLogService {
    @Autowired
    private ICtopOauthTokenService tokenService;
    @Autowired
    private IKuaishouInterfaceService kuaishouInterfaceService;
    @Autowired
    private IKuaishouGroupExploreService kuaishouGroupExploreService;
    @Resource
    private KuaishouAccountSpeedExploreLogMapper kuaishouAccountSpeedExploreLogMapper;
    @Override
    public Map<String, Object> openSpeedExplore(KuaishouAccountSpeedExploreLog kuaishouAccountSpeedExploreLog) throws Exception{
        Map<String,Object> result = new HashMap<>();
        //查询当前账户是否加速探索中
        KuaishouAccountSpeedExploreLog getEntity = this.getByParams(kuaishouAccountSpeedExploreLog.getAccountId(),1);
        if(null!=getEntity){
            ResultMapUtils.setResultMap(result, StatusCode.KUAISHOU_API_ACCOUNT_SPEED_EXPLORING);
            return result;
        }
        //创建加速探索记录
        kuaishouAccountSpeedExploreLog.setStatus(1);
        this.save(kuaishouAccountSpeedExploreLog);
        //触发加速探索任务
        this.startSpeedExplore(kuaishouAccountSpeedExploreLog);
        return result;
    }

    private void startSpeedExplore(KuaishouAccountSpeedExploreLog kuaishouAccountSpeedExploreLog) throws Exception{
        Date endDate = new Date();
        String anotherDay = DateUtils.getAnotherDay("yyyy-MM-dd", DateUtils.formatDate(endDate), -1);
        SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = sim.parse(anotherDay);
        CtopOauthToken token = tokenService.getTokenByAccountId(kuaishouAccountSpeedExploreLog.getAccountId());
        //1: 拉取账户下所有的广告组信息
        kuaishouInterfaceService.getGroupList(token, startDate, endDate);
        //2: 查询处于学习期的广告组数据
// TODO       kuaishouInterfaceService.getAdvertiserGroupReportDaily(token,startDate,endDate);
        //3:根据转化目标查询相应的符合条件的广告组数据
        //优化目标 2 行为数 180 激活数 53 表单数 190 付费 191 首日ROI
        //查询相关符合条件的广告组数据
        List<SpeedExploreGroupVO> speedExploreGroupVos = this.getSpeedExploreGroupVOSByParams(DateUtils.formatDate(endDate),kuaishouAccountSpeedExploreLog.getAccountId(),kuaishouAccountSpeedExploreLog.getConvertType(),kuaishouAccountSpeedExploreLog.getConvertCount(),kuaishouAccountSpeedExploreLog.getGroupCount());
        if(null==speedExploreGroupVos||speedExploreGroupVos.isEmpty()){
            speedExploreGroupVos.forEach(speedExploreGroupVO -> {
                //触发加速探索逻辑
                kuaishouGroupExploreService.unitOpenSpeedExplore(speedExploreGroupVO.getAccountId(),speedExploreGroupVO.getUnitId(),kuaishouAccountSpeedExploreLog.getGroupAccount(),kuaishouAccountSpeedExploreLog.getId());
            });
        }


    }

    private List<SpeedExploreGroupVO> getSpeedExploreGroupVOSByParams(String date, Long accountId, Integer convertType, Integer convertCount,Integer groupCount) {
        return kuaishouAccountSpeedExploreLogMapper.getSpeedExploreGroupVosByParams(date,accountId,convertType,convertCount,groupCount);
    }

    @Override
    public KuaishouAccountSpeedExploreLog getByParams(Long accountId, Integer status) {
        QueryWrapper<KuaishouAccountSpeedExploreLog> queryWrapper = new QueryWrapper<>();
        if(null!=accountId&&accountId!=0){
            queryWrapper.eq("account_id",accountId);
        }
        if(null!=status){
            queryWrapper.eq("status",accountId);
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }
}
