package cn.com.ctop.job.bytedance.handler;

import cn.com.ctop.common.module.entity.UserAllocation;
import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.common.module.service.ISendMessageService;
import cn.com.ctop.common.module.service.IUserAllocationService;
import cn.com.ctop.toutiao.modules.material.entity.ByteDanceAdvertisePlan;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceAdvertisePlanService;
import cn.com.ctop.toutiao.modules.material.service.IByteDanceCreativeService;
import com.alibaba.fastjson.JSONObject;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.jeecg.common.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ByteDanceCheckAccountMonitoringLinkJob {

    @Autowired
    private IUserAllocationService allocationService;
    @Autowired
    private IByteDanceAdvertisePlanService advertisePlanService;
    @Autowired
    private IByteDanceCreativeService creativeService;
    @Autowired
    private ISendMessageService sendMessageService;
    @Autowired
    private ICtopOauthTokenService oauthTokenService;

    //TODO 待废弃
    @XxlJob("byteDanceCheckAccountMonitoringLinkJob")
    public void execute() throws Exception{

        //查询开启监测链接预警的账户
        //Todo 暂时单线程
        List<UserAllocation> userAllocations=allocationService.getUserAllocations("1",0);
        userAllocations.forEach(userAllocation->{
            List<ByteDanceAdvertisePlan> plans = advertisePlanService.getAllPlans(userAllocation.getAccountId(), DateUtils.formatDate(new Date()));
            if(null!=plans&&!plans.isEmpty()){
                for (ByteDanceAdvertisePlan plan:plans) {
                    Map<String, Object> creativeDetail = creativeService.getCreativeDetail(oauthTokenService.getTokenByAccountId(userAllocation.getAccountId()), plan.getId());
                    if(null!=creativeDetail.get("data")){
                        JSONObject data = (JSONObject) creativeDetail.get("data");
                        checkMonitorLink(data,plan,userAllocation);
                    }
                }
            }
        });
        XxlJobHelper.log("监测链接检查完成");
    }

    private void checkMonitorLink(JSONObject data, ByteDanceAdvertisePlan plan, UserAllocation allocation) {
        String downloadUrl = plan.getDownloadUrl();
        boolean errorflag = false;
        String errorMsg = "";
        String token = plan.getName().split("-")[0];
        if(null!=token&& "衍生计划".equals(token)){
            token = plan.getName().split("-")[1];
        }
        String tokenInfo = "surl_token="+token;
        String appType = plan.getAppType();

        if(null!=downloadUrl){
            if("APP_IOS".equals(appType)){
                if(!downloadUrl.contains("https://apps.apple.com/cn/app")){
                    errorMsg+= "应用下载链接填写异常;";
                    errorflag = true;
                }
            }
            if("APP_ANDROID".equals(appType)){
                if(!downloadUrl.contains(token)){
                    errorMsg+= "应用下载链接填写异常;";
                    errorflag = true;
                }
            }
        }
        //展示监测链接
        String trackUrl = data.getString("trackUrl");
        if(null!=trackUrl&&!"".equals(trackUrl.trim())){
            if(!trackUrl.contains(tokenInfo)){
                errorMsg+= "展示监测链接填写异常;";
                errorflag = true;
            }
        }
        //点击监测链接
        String actionTrackUrl = data.getString("actionTrackUrl");
        if(null!=actionTrackUrl&&!"".equals(actionTrackUrl.trim())){
            if("APP_IOS".equals(appType)){
                if(!actionTrackUrl.contains(tokenInfo)||!actionTrackUrl.contains("app_platform=ios")){
                    errorMsg+= "点击监测链接填写异常;";
                    errorflag = true;
                }
            }
            if("APP_ANDROID".equals(appType)){
                if(!actionTrackUrl.contains(tokenInfo)||!actionTrackUrl.contains("tt/")){
                    errorMsg+= "点击监测链接填写异常;";
                    errorflag = true;
                }
            }
        }
        //视频有效播放监测链接
        String videoPlayEffectiveTrackUrl = data.getString("videoPlayEffectiveTrackUrl");
        if(null!=videoPlayEffectiveTrackUrl&&!"".equals(videoPlayEffectiveTrackUrl.trim())){
            if(!videoPlayEffectiveTrackUrl.contains(tokenInfo)){
                errorMsg+= "视频有效播放监测链接填写异常;";
                errorflag = true;
            }
        }
        if(errorflag){
            sendMessage(allocation,"警告：账户:"+allocation.getAuthName()+"(id:"+allocation.getAccountId()+")下的广告计划:"+plan.getName()+"(id:"+plan.getId()+")"+"监测链接异常>>"+errorMsg);
        }
    }
    private void sendMessage(UserAllocation allocation, String errorMsg) {
        sendMessageService.sendMessage(allocation.getUserId(),errorMsg);
    }
}
