package org.jeecg.modules.bytedance.advertise.service.impl;

import com.alibaba.fastjson.JSONArray;
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.jeecg.modules.bytedance.advertise.entity.*;
import org.jeecg.modules.bytedance.advertise.mapper.BytedanceMaterialRejectReasonMapper;
import org.jeecg.modules.bytedance.advertise.mapper.BytedancePlanRejectReasonMapper;
import org.jeecg.modules.bytedance.advertise.mapper.LowQualityRejectReasonMapper;
import org.jeecg.modules.bytedance.advertise.mapper.RuleDataAccountMapper;
import org.jeecg.modules.bytedance.advertise.service.BytedanceRejectReasonService;
import org.jeecg.modules.bytedance.advertise.service.IRuleDataAccountService;
import org.jeecg.modules.bytedance.common.entity.CtopOauthToken;
import org.jeecg.modules.bytedance.common.utils.Check;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CancellationException;

/**
 * 规则账户清洗数据
 *
 * @author jeecg-boot
 * @version V1.0
 * @date 2020-11-16
 */
@Service
public class BytedanceRejectReasonServiceImpl implements BytedanceRejectReasonService {

    @Resource
    private BytedancePlanRejectReasonMapper bytedancePlanRejectReasonMapper;

    @Resource
    private BytedanceMaterialRejectReasonMapper BytedanceMaterialRejectReasonMapper;

    @Resource
    LowQualityRejectReasonMapper lowQualityRejectReasonMapper;
    public void saveRejectReasonToDb(JSONArray jsonArray,Long accountId){
        for(int i=0; i<jsonArray.size(); i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            JSONObject ad_reject = jsonObject.getJSONObject("ad_reject");
            JSONArray reject_data = ad_reject.getJSONArray("reject_data");
            if(!Check.isNull(reject_data)){
                for(int j=0; j<reject_data.size();j++){
                    JSONObject object = reject_data.getJSONObject(j);
                    BytedancePlanRejectReason bytedancePlanRejectReason = new BytedancePlanRejectReason();
                    bytedancePlanRejectReason.setAdId(ad_reject.getLong("ad_id"));
                    bytedancePlanRejectReason.setRejectItem(object.getString("reject_item"));
                    bytedancePlanRejectReason.setRejectReason(object.getString("reject_reason"));
                    bytedancePlanRejectReason.setIsLowQuality(0);
                    bytedancePlanRejectReason.setAccountId(accountId);
                    String[] rejectReasons = bytedancePlanRejectReason.getRejectReason().split("广告体验优化建议：");
                    if(rejectReasons.length == 0){
                        LowQualityRejectReason lowQualityRejectReason = lowQualityRejectReasonMapper.getByRejectReason(bytedancePlanRejectReason.getRejectReason().trim());
                        if(!Check.isNull(lowQualityRejectReason)){
                            bytedancePlanRejectReason.setIsLowQuality(1);
                        }
                    }else {
                        for(int k=1;k<rejectReasons.length;k++){
                            LowQualityRejectReason lowQualityRejectReason = lowQualityRejectReasonMapper.getByRejectReason("广告体验优化建议："+rejectReasons[k].trim());
                            if(!Check.isNull(lowQualityRejectReason)){
                                bytedancePlanRejectReason.setIsLowQuality(1);
                            }
                        }
                    }

                    int res = bytedancePlanRejectReasonMapper.replaceIntoDb(bytedancePlanRejectReason);
                    if(res == 0){
                        System.out.println(bytedancePlanRejectReason.toString());
                    }
                }
            }

            JSONArray material_reject = jsonObject.getJSONArray("material_reject");
            if(!Check.isNull(material_reject)){
                for(int j=0; j<material_reject.size();j++){
                    JSONObject object = material_reject.getJSONObject(j);
                    BytedanceMaterialRejectReason bytedanceMaterialRejectReason = new BytedanceMaterialRejectReason();
                    bytedanceMaterialRejectReason.setRejectReason(object.getString("reject_reason"));
                    bytedanceMaterialRejectReason.setIsLowQuality(0);
                    bytedanceMaterialRejectReason.setAccountId(accountId);
                    String[] rejectReasons = bytedanceMaterialRejectReason.getRejectReason().split("广告体验优化建议：");
                    for(int k=1;k<rejectReasons.length;k++){
                        LowQualityRejectReason lowQualityRejectReason = lowQualityRejectReasonMapper.getByRejectReason("广告体验优化建议："+rejectReasons[k].trim());
                        if(!Check.isNull(lowQualityRejectReason)){
                            bytedanceMaterialRejectReason.setIsLowQuality(1);
                        }
                    }
                    if(rejectReasons.length == 0){
                        LowQualityRejectReason lowQualityRejectReason = lowQualityRejectReasonMapper.getByRejectReason(bytedanceMaterialRejectReason.getRejectReason().trim());
                        if(!Check.isNull(lowQualityRejectReason)){
                            bytedanceMaterialRejectReason.setIsLowQuality(1);
                        }
                    }else {
                        for(int k=1;k<rejectReasons.length;k++){
                            LowQualityRejectReason lowQualityRejectReason = lowQualityRejectReasonMapper.getByRejectReason("广告体验优化建议："+rejectReasons[k].trim());
                            if(!Check.isNull(lowQualityRejectReason)){
                                bytedanceMaterialRejectReason.setIsLowQuality(1);
                            }
                        }
                    }
                    if(!Check.isNull(object.getString("title"))){
                        bytedanceMaterialRejectReason.setTitle(object.getString("title"));
                    }
                    if(!Check.isNull(object.getJSONArray("image_id"))){
                        bytedanceMaterialRejectReason.setImageId(object.getJSONArray("image_id").toJSONString());
                    }
                    if(!Check.isNull(object.getString("video_id"))){
                        bytedanceMaterialRejectReason.setVideoId(object.getString("video_id"));
                    }
                    if(!Check.isNull(ad_reject.getLong("ad_id"))){
                        bytedanceMaterialRejectReason.setAdId(ad_reject.getLong("ad_id"));
                    }
                    int res =  BytedanceMaterialRejectReasonMapper.replaceIntoDb(bytedanceMaterialRejectReason);
                    if(res == 0){
                        System.out.println(bytedanceMaterialRejectReason.toString());
                    }
//                    BytedanceMaterialRejectReasonMapper.insert(bytedanceMaterialRejectReason);
                }
            }
        }
    }


}
