package cn.com.ctop.toutiao.modules.adlab.service.impl;

import cn.com.ctop.common.module.service.ICtopOauthTokenService;
import cn.com.ctop.toutiao.modules.adlab.entity.CtopAdlabProjectDetail;
import cn.com.ctop.toutiao.modules.adlab.mapper.CtopAdlabProjectDetailMapper;
import cn.com.ctop.toutiao.modules.adlab.service.ICtopAdlabProjectDetailService;
import cn.com.ctop.toutiao.modules.adlab.service.IGetProjectService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 投放管家项目详情
 */
@Service
public class CtopAdlabProjectDetailServiceImpl extends ServiceImpl<CtopAdlabProjectDetailMapper, CtopAdlabProjectDetail> implements ICtopAdlabProjectDetailService {

    @Autowired
    ICtopOauthTokenService ctopOauthTokenService;

    @Autowired
    private IGetProjectService getProjectService;

    @Autowired
    ICtopAdlabProjectDetailService adlabProjectDetailService;

    /**
     * 新建投放管家项目入库
     */
    @Override
    public void putStorage(List<JSONObject> params){
        params.forEach(it->{
            JSONObject group = getProjectService.getGroup(ctopOauthTokenService.getTokenByAccountId(it.getLong("accountId")), it.getLong("accountId"), it.getLong("groupId"));
            if(group.getInteger("code")==0){
                CtopAdlabProjectDetail ctopAdlabProjectDetail=new CtopAdlabProjectDetail();
                JSONObject projectDetailJSONObject=group.getJSONObject("data").getJSONObject("group_info");
                ctopAdlabProjectDetail.setId(it.getLong("groupId"));
                ctopAdlabProjectDetail.setAccountId(it.getLong("accountId"));
                ctopAdlabProjectDetail.setUserId(it.getString("userId"));
                ctopAdlabProjectDetail.setName(projectDetailJSONObject.getString("name"));
                ctopAdlabProjectDetail.setCampaignId(projectDetailJSONObject.getLong("campaign_id"));
                ctopAdlabProjectDetail.setScenario(projectDetailJSONObject.getString("scenario"));
                ctopAdlabProjectDetail.setDeliveryRange(projectDetailJSONObject.getString("delivery_range"));
                ctopAdlabProjectDetail.setStatus(projectDetailJSONObject.getString("status"));
                ctopAdlabProjectDetail.setOptStatus(projectDetailJSONObject.getString("opt_status"));
                ctopAdlabProjectDetail.setStatusName(projectDetailJSONObject.getString("status_name"));
                ctopAdlabProjectDetail.setStep(projectDetailJSONObject.getInteger("step"));
                ctopAdlabProjectDetail.setPlatform(projectDetailJSONObject.getString("platform"));
                ctopAdlabProjectDetail.setDownloadType(projectDetailJSONObject.getString("download_type"));
                ctopAdlabProjectDetail.setPlatform(projectDetailJSONObject.getString("platform"));
                ctopAdlabProjectDetail.setAppType(projectDetailJSONObject.getString("app_type"));
                ctopAdlabProjectDetail.setUrl(projectDetailJSONObject.getString("url"));
                ctopAdlabProjectDetail.setWebUrl(projectDetailJSONObject.getString("web_url"));
                ctopAdlabProjectDetail.setAdlabPackage(projectDetailJSONObject.getString("package"));
                ctopAdlabProjectDetail.setConvertName(projectDetailJSONObject.getString("convert_name"));
                ctopAdlabProjectDetail.setConvertId(projectDetailJSONObject.getInteger("convert_id"));
                ctopAdlabProjectDetail.setConvertMode(projectDetailJSONObject.getString("convert_mode"));
                ctopAdlabProjectDetail.setInventoryType(projectDetailJSONObject.getString("inventory_type"));
                ctopAdlabProjectDetail.setUnionVideoType(projectDetailJSONObject.getString("union_video_type"));
                ctopAdlabProjectDetail.setAudience(projectDetailJSONObject.getString("audience"));
                ctopAdlabProjectDetail.setAdCreative(projectDetailJSONObject.getString("ad_creative"));
                ctopAdlabProjectDetail.setStartTime(projectDetailJSONObject.getString("start_time"));
                ctopAdlabProjectDetail.setEndTime(projectDetailJSONObject.getString("end_time"));
                ctopAdlabProjectDetail.setWeekTime(projectDetailJSONObject.getString("week_time"));
                ctopAdlabProjectDetail.setBid(projectDetailJSONObject.getBigDecimal("bid"));
                ctopAdlabProjectDetail.setBudget(projectDetailJSONObject.getBigDecimal("budget"));
                ctopAdlabProjectDetail.setBudgetMode(projectDetailJSONObject.getString("budget_mode"));
                ctopAdlabProjectDetail.setCompensation(projectDetailJSONObject.getString("compensation"));
                ctopAdlabProjectDetail.setDeepBid(projectDetailJSONObject.getBigDecimal("deep_bid"));
                ctopAdlabProjectDetail.setDeepBidType(projectDetailJSONObject.getString("deep_bid_type"));
                ctopAdlabProjectDetail.setDeepExternalAction(projectDetailJSONObject.getString("deep_external_action"));
                ctopAdlabProjectDetail.setStopStage(projectDetailJSONObject.getString("stop_stage"));
                ctopAdlabProjectDetail.setTargetMode(projectDetailJSONObject.getString("target_mode"));
                ctopAdlabProjectDetail.setDownloadMode(projectDetailJSONObject.getString("download_mode"));
                ctopAdlabProjectDetail.setOpenUrl(projectDetailJSONObject.getString("open_url"));
                ctopAdlabProjectDetail.setInventoryTypes(projectDetailJSONObject.getString("inventory_types"));
                ctopAdlabProjectDetail.setAdTarget(projectDetailJSONObject.getString("ad_target"));
                ctopAdlabProjectDetail.setLandingType(projectDetailJSONObject.getString("landing_type"));

                adlabProjectDetailService.saveOrUpdate(ctopAdlabProjectDetail);
            }
        });
    }

}
