package com.ruixuan.launch.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.DateUtils;
import com.ruixuan.common.utils.KuaishouAPIConstant;
import com.ruixuan.launch.entity.KuaishouLaunchCreative;
import com.ruixuan.launch.entity.KuaishouLaunchCreativeRecord;
import com.ruixuan.launch.mapper.KuaishouLaunchCreativeMapper;
import com.ruixuan.launch.mapper.KuaishouLaunchCreativeRecordMapper;
import com.ruixuan.launch.service.IKuaishouLaunchCreativeService;
import com.ruixuan.launch.unitls.APIUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 广告创意信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-05
 */
@Slf4j
@Service
public class KuaishouLaunchCreativeServiceImpl implements IKuaishouLaunchCreativeService {
    @Autowired
    private KuaishouLaunchCreativeMapper kuaishouLaunchCreativeMapper;
    @Autowired
    private KuaishouLaunchCreativeRecordMapper kuaishouLaunchCreativeRecordMapper;

    /**
     * 查询广告创意信息
     *
     * @param id 广告创意信息主键
     * @return 广告创意信息
     */
    @Override
    public KuaishouLaunchCreative selectKuaishouLaunchCreativeById(String id) {
        return kuaishouLaunchCreativeMapper.selectKuaishouLaunchCreativeById(id);
    }

    /**
     * 查询广告创意信息列表
     *
     * @param kuaishouLaunchCreative 广告创意信息
     * @return 广告创意信息
     */
    @Override
    public List<KuaishouLaunchCreative> selectKuaishouLaunchCreativeList(KuaishouLaunchCreative kuaishouLaunchCreative) {
        return kuaishouLaunchCreativeMapper.selectKuaishouLaunchCreativeList(kuaishouLaunchCreative);
    }

    /**
     * 新增广告创意信息
     */
    @Override
    public ResultResponse insertKuaishouLaunchCreative(JSONObject requestJson, String token) {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("advertiser_id", requestJson.getLong("accountId"));
        param.put("unit_id", requestJson.getLong("unitId"));
        param.put("name", requestJson.getString("creativeName"));

        Integer liveCreativeType = requestJson.getInteger("liveCreativeType");
        if (Check.isNotNull(liveCreativeType) && liveCreativeType == 1) {
            param.put("cover_url", "");
            param.put("cover_width", 0);
            param.put("cover_height", 0);
            param.put("photo_id", 0);
            param.put("creative_material_type", 0);
            param.put("action_bar", "");
            param.put("description", "");
        } else {
            param.put("cover_url", requestJson.getString("coverUrl"));
            param.put("cover_width", requestJson.getLong("coverWidth"));
            param.put("cover_height", requestJson.getLong("coverHeight"));
            param.put("photo_id", requestJson.getLong("photoId"));
            param.put("creative_material_type", requestJson.getInteger("creativeMaterialType"));
            param.put("action_bar", requestJson.getString("actionBar"));
            param.put("description", requestJson.getString("description"));
        }
        if (Check.isNotNull(requestJson.getString("recommendation"))) {
            param.put("recommendation", requestJson.getString("recommendation"));
        }
        if (Check.isNotNull(liveCreativeType)) {
            param.put("live_creative_type", liveCreativeType);
        }
        if (Check.isNotNull(requestJson.getString("newExposeText"))) {
            JSONObject newExposeTag = new JSONObject();
            newExposeTag.put("text", requestJson.getString("newExposeText"));
            newExposeTag.put("url", requestJson.getString("newExposeUrl"));
            param.put("new_expose_tag", newExposeTag);
        }
        Long creativeId = -1L;
        KuaishouLaunchCreativeRecord record = JSONObject.parseObject(requestJson.toJSONString(), KuaishouLaunchCreativeRecord.class);
        JSONObject result = APIUtil.getApiResult(KuaishouAPIConstant.CREATE_CREATIVE, token, param);
        if (result.getInteger("code") == 0) {
            result = result.getJSONObject("data");
            JSONObject detail = result.getJSONArray("detail").getJSONObject(0);
            KuaishouLaunchCreative creative = JSONObject.parseObject(requestJson.toJSONString(), KuaishouLaunchCreative.class);
            creativeId = detail.getLong("id");
            creative.setCreativeId(creativeId);
            creative.setId(UUID.randomUUID().toString().replace("-", ""));
            creative.setStatDate(DateUtils.getDate());
            kuaishouLaunchCreativeMapper.insertKuaishouLaunchCreative(creative);
            record.setMessage("success");
            record.setCreativeId(creativeId);
        } else {
            record.setMessage(result.getString("message"));
            log.error("广告创意创建失败，账户{}，{}", requestJson.getLong("accountId"), result.toJSONString());
        }
        record.setStrategyId(requestJson.getLong("id"));
        record.setStatDate(DateUtils.getDate());
        record.setId(UUID.randomUUID().toString().replace("-", ""));
        kuaishouLaunchCreativeRecordMapper.insertKuaishouLaunchCreativeRecord(record);
        return ResultResponse.error("创建失败，" + result.getString("message"));
    }

    /**
     * 修改广告创意信息
     *
     * @param kuaishouLaunchCreative 广告创意信息
     * @return 结果
     */
    @Override
    public int updateKuaishouLaunchCreative(KuaishouLaunchCreative kuaishouLaunchCreative) {
        return kuaishouLaunchCreativeMapper.updateKuaishouLaunchCreative(kuaishouLaunchCreative);
    }
}