package com.ruixuan.launch.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.domain.ResultResponse;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.launch.entity.AccountToken;
import com.ruixuan.launch.entity.KuaishouLaunchCreative;
import com.ruixuan.launch.service.IAccountTokenService;
import com.ruixuan.launch.service.IKuaishouLaunchCreativeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 广告创意信息Controller
 *
 * @author ruoyi
 * @date 2022-07-05
 */
@Api(tags = "广告创意")
@RestController
@RequestMapping("/launch/creative")
public class KuaishouLaunchCreativeController extends BaseController {
    @Autowired
    private IKuaishouLaunchCreativeService kuaishouLaunchCreativeService;
    @Autowired
    private IAccountTokenService tokenService;

    /**
     * 查询广告创意信息列表
     */
    @ApiOperation(value = "查询广告创意信息列表")
    @GetMapping("/list")
    public TableDataInfo list(KuaishouLaunchCreative kuaishouLaunchCreative) {
        startPage();
        List<KuaishouLaunchCreative> list = kuaishouLaunchCreativeService.selectKuaishouLaunchCreativeList(kuaishouLaunchCreative);
        return getDataTable(list);
    }

    /**
     * 获取广告创意信息详细信息
     */
    @ApiOperation(value = "获取广告创意信息详细信息")
    @GetMapping("/getById")
    public AjaxResult getInfo(@ApiParam("id") @RequestParam(value = "id", required = true) String id) {
        return AjaxResult.success(kuaishouLaunchCreativeService.selectKuaishouLaunchCreativeById(id));
    }

    /**
     * 新增广告创意信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增广告创意信息")
    public ResultResponse add(@RequestBody JSONObject requestJson) {
        AccountToken token = tokenService.getAccountToken(requestJson.getLong("accountId"));
        return kuaishouLaunchCreativeService.insertKuaishouLaunchCreative(requestJson, token.getAccessToken());
    }

    /**
     * 修改广告创意信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改广告创意信息")
    public AjaxResult edit(@RequestBody KuaishouLaunchCreative kuaishouLaunchCreative) {
        return toAjax(kuaishouLaunchCreativeService.updateKuaishouLaunchCreative(kuaishouLaunchCreative));
    }

}