package com.fs.baidu.api;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fs.ad.domain.AdHtmlClickLog;
import com.fs.ad.mapper.AdHtmlClickLogMapper;
import com.fs.baidu.domain.BdAccount;
import com.fs.baidu.domain.BdCreative;
import com.fs.baidu.domain.BdPlan;
import com.fs.baidu.domain.BdUnit;
import com.fs.baidu.param.*;
import com.fs.baidu.service.IBdAccountService;
import com.fs.baidu.service.IBdCreativeService;
import com.fs.baidu.service.IBdPlanService;
import com.fs.baidu.service.IBdUnitService;
import com.fs.baidu.utils.BdApiRequest;
import com.fs.baidu.vo.*;
import com.fs.common.core.domain.R;
import com.fs.common.utils.PubFun;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@AllArgsConstructor
public class BaiduApis {

    private final BdApiRequest request;
    private final IBdAccountService accountService;
    private final IBdPlanService planService;
    private final IBdCreativeService creativeService;
    private final IBdUnitService unitService;
    private final AdHtmlClickLogMapper adHtmlClickLogMapper;

    /**
     * 查询账户列表
     * @param apiId    apiId
     */
    public void listAccount(Long apiId, Long accountId){
        String url = "https://api.baidu.com/json/feed/v1/AccountFeedService/getAccountFeed";
        List<BdAccount> accountList;
        if (accountId == null) {
            accountList = accountService.listByApiId(apiId);
        }else{
            BdAccount account = accountService.getById(accountId);
            if(account == null) return;
            accountList = Collections.singletonList(account);
        }
        if(accountList.isEmpty()){
            return;
        }
        accountList = accountList.stream().filter(e -> e.getLoading() == 0).collect(Collectors.toList());
        if(accountList.isEmpty()){
            return;
        }
        accountList.forEach(account -> {
            try {
                String resultStr = request.request(url, apiId, account.getAccountName(), new QueryAccountParam());
                AccountResultVo accountResultVo = JSON.parseObject(resultStr).getJSONArray("data").toJavaList(AccountResultVo.class).get(0);
                account.setLoading(1);
                account.setAccountId(accountResultVo.getUserId());
                account.setSync(true);
            }catch (Exception ex){
                log.error("同步失败", ex);
            }
        });
        accountService.updateBatchById(accountList);
        List<BdAccount> list = accountList.stream().filter(BdAccount::isSync).collect(Collectors.toList());
        list.parallelStream().forEach(e -> {
            try {
                new Thread(() -> {
                    listPlan(e);
                    e.setLoading(0);
                    accountService.updateById(e);
                }).start();
            }catch (Exception ex){
                log.error("同步失败", ex);
            }
        });
    }
    /**
     * 查询计划列表
     * @param account 查询账户
     */
    public void listPlan(BdAccount account){
        String url = "https://api.baidu.com/json/feed/v1/CampaignFeedService/getCampaignFeed";
        String resultStr = request.request(url, account.getApiId(), account.getAccountName(), new QueryPlanParam());
        List<CampaignFeedVo> list = JSON.parseObject(resultStr).getJSONArray("data").toJavaList(CampaignFeedVo.class);
        if(list.isEmpty()){
            return;
        }
        List<BdPlan> saveOrUpdate = list.stream().map(e -> {
            BdPlan bdPlan = new BdPlan();
            bdPlan.setCampaignFeedId(e.getCampaignFeedId());
            bdPlan.setCampaignFeedName(e.getCampaignFeedName());
            bdPlan.setSubject(e.getSubject());
            bdPlan.setAppinfo(JSON.toJSONString(e.getAppinfo()));
            bdPlan.setSchedule(JSON.toJSONString(e.getSchedule()));
            bdPlan.setEshopType(e.getEshopType());
            bdPlan.setCampaignOcpxStatus(e.getCampaignOcpxStatus());
            bdPlan.setUseLiftBudget(e.getUseLiftBudget());
            bdPlan.setLiftBudget(e.getLiftBudget());
            bdPlan.setLiftStatus(e.getLiftStatus());
            bdPlan.setDeliveryType(e.getEshopType());
            bdPlan.setAppSubType(e.getAppSubType());
            bdPlan.setMiniProgramType(e.getMiniProgramType());
            bdPlan.setBidMode(e.getBidMode());
            bdPlan.setProductIds(e.getProductIds());
            bdPlan.setSaleType(e.getSaleType());
            bdPlan.setAccountId(account.getAccountId());
            bdPlan.setAccountName(account.getAccountName());
            bdPlan.setJson(JSON.toJSONString(e));
            return bdPlan;
        }).collect(Collectors.toList());
        if(!saveOrUpdate.isEmpty()){
            planService.saveOrUpdateBatch(saveOrUpdate);
        }

        PubFun.batchProcessing(50, list, batch -> {
            try {
                listUnit(PubFun.listToNewList(batch, CampaignFeedVo::getCampaignFeedId), 1, account);
            }catch (Exception ex){
                log.error("同步失败", ex);
            }
        });
    }

    /**
     * 查询单元列表
     * @param ids      查询ID集合
     * @param idType   ID类型1计划2单元
     * @param account 查询账户
     */
    public void listUnit(List<Long> ids, Integer idType, BdAccount account){
        String url = "https://api.baidu.com/json/feed/v1/AdgroupFeedService/getAdgroupFeed";
        QueryUnitParam param = new QueryUnitParam();
        param.setIdType(idType);
        param.setIds(ids);
        String resultStr = request.request(url, account.getApiId(), account.getAccountName(), param);
        List<UnitResultVo> list = JSON.parseObject(resultStr).getJSONArray("data").toJavaList(UnitResultVo.class);
        if(list.isEmpty()){
            return;
        }
        List<BdUnit> saveOrUpdate = list.stream().map(e -> {
            BdUnit unit = new BdUnit();
            unit.setAdgroupFeedId(e.getAdgroupFeedId());
            unit.setAccountId(account.getAccountId());
            unit.setAccountName(account.getAccountName());
            unit.setCampaignFeedId(e.getCampaignFeedId());
            unit.setAdgroupFeedName(e.getAdgroupFeedName());
            unit.setPause(e.getPause());
            unit.setStatus(e.getStatus());
            unit.setBid(e.getBid());
            unit.setBidtype(e.getBidtype());
            unit.setJson(JSON.toJSONString(e));
            return unit;
        }).collect(Collectors.toList());
        if(!saveOrUpdate.isEmpty()){
            unitService.saveOrUpdateBatch(saveOrUpdate);
        }


        PubFun.batchProcessing(50, list, batch -> {
            try {
                listCreative(PubFun.listToNewList(batch, UnitResultVo::getAdgroupFeedId), 2, account);
            }catch (Exception ex){
                log.error("同步失败", ex);
            }
        });
    }

    /**
     * 查询创意
     * @param ids      ID集合
     * @param idType   ID类型1计划ID，2单元ID，3创意ID
     * @param account  查询账户
     */
    public void listCreative(List<Long> ids, Integer idType, BdAccount account){
        String url = "https://api.baidu.com/json/feed/v1/CreativeFeedService/getCreativeFeed";
        QueryCreativeParam param = new QueryCreativeParam();
        param.setIdType(idType);
        param.setIds(ids);
        String resultStr = request.request(url, account.getApiId(), account.getAccountName(), param);
        List<CreativeResultVo> list = JSON.parseObject(resultStr).getJSONArray("data").toJavaList(CreativeResultVo.class);
        if(list.isEmpty()){
            return;
        }
        List<BdCreative> saveOrUpdate = list.stream().map(e -> {
            BdCreative creative = new BdCreative();
            creative.setCreativeFeedId(e.getCreativeFeedId());
            creative.setAccountId(account.getAccountId());
            creative.setAccountName(account.getAccountName());
            creative.setAdgroupFeedId(e.getAdgroupFeedId());
            creative.setMaterialstyle(e.getCreativeFeedId());
            creative.setCreativeFeedName(e.getCreativeFeedName());
            creative.setPause(e.getPause());
            creative.setStatus(e.getStatus());
            creative.setMaterial(e.getMaterial());
            creative.setRefusereason(e.getRefusereason());
            creative.setCommentnum(e.getCommentnum());
            creative.setReadnum(e.getReadnum());
            creative.setPlaynum(e.getPlaynum());
            creative.setIdeaType(e.getIdeaType());
            creative.setShowMt(e.getShowMt());
            creative.setAddtime(e.getAddtime());
            creative.setProgFlag(JSON.toJSONString(e.getProgFlag()));
            creative.setVideoId(e.getVideoId());
            creative.setVideoUrl(e.getVideoUrl());
            creative.setTemplateJson(JSON.toJSONString(e.getTemplate()));
            creative.setJson(JSON.toJSONString(e));
            return creative;
        }).collect(Collectors.toList());
        if(!saveOrUpdate.isEmpty()){
            creativeService.saveOrUpdateBatch(saveOrUpdate);
        }
    }



    public R getReportData(BdAccount account, QueryReportDataParam params){
        String url = "https://api.baidu.com/json/sms/service/OpenApiReportService/getReportData";
        if(params.getReportType() == 2324048){
            params.getColumns().add("prov");
            params.getColumns().add("city");
        }
        if(params.getReportType() == 2503330){
            params.getColumns().add("genderName");
        }
        if(params.getReportType() == 2510393){
            params.getColumns().add("ageName");
        }
        if(params.getReportType() == 2532512){
            params.getColumns().add("showWord");
            params.getColumns().add("feedWord");
        }
        if(params.getPage() != null && params.getRowCount() != null){
            params.setStartRow(params.getPage() * params.getRowCount());
        }
        String resultStr = request.request(url, account.getApiId(), account.getAccountName(), params);
        JSONObject data = JSON.parseObject(resultStr).getJSONArray("data").getJSONObject(0);
        Integer total = data.getInteger("totalRowCount");
        List<ReportDataVo> list = data.getJSONArray("rows").toJavaList(ReportDataVo.class);
        if(!list.isEmpty()){
            list.forEach(e -> {
                if(e.getCost() != null){
                    e.setCost(e.getCost().setScale(2, RoundingMode.HALF_UP));
                }
                if(e.getCpc() != null){
                    e.setCpc(e.getCpc().setScale(2, RoundingMode.HALF_UP));
                }
                if(e.getCpm() != null){
                    e.setCpm(e.getCpm().setScale(2, RoundingMode.HALF_UP));
                }
                if(e.getCtr() != null){
                    e.setCtr(e.getCtr().setScale(4, RoundingMode.HALF_UP));
                }
            });
            list.stream().filter(e -> e.getCtr().doubleValue() > 0).forEach(e -> {
                e.setCtr(e.getCtr().multiply(BigDecimal.valueOf(100)));
            });
        }
        if(!list.isEmpty()){
            List<Long> uidList = PubFun.listToNewList(list, ReportDataVo::getAdGroupId);
            List<AdHtmlClickLog> logList = adHtmlClickLogMapper.staticData(uidList);
            if(!logList.isEmpty()){

            }
        }
        return R.ok().put("data", list).put("total", total);
    }

    public R bdDataStatic() {
        return R.ok();
    }
}
