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.common.utils.Check;
import com.ruixuan.launch.entity.AccountToken;
import com.ruixuan.launch.entity.KuaishouLaunchCampaign;
import com.ruixuan.launch.service.IAccountTokenService;
import com.ruixuan.launch.service.IKuaishouLaunchCampaignService;
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
 *
 * @date 2022-06-23
 */
@Api(tags = "广告计划")
@RestController
@RequestMapping("/launch/campaign")
public class KuaishouLaunchCampaignController extends BaseController {
    @Autowired
    private IKuaishouLaunchCampaignService kuaishouLaunchCampaignService;
    @Autowired
    private IAccountTokenService tokenService;

    /**
     * 查询广告计划信息列表
     */
    @ApiOperation(value = "查询广告计划信息列表")
    @GetMapping("/list")
    public TableDataInfo list(KuaishouLaunchCampaign kuaishouLaunchCampaign) {
        startPage();
        List<KuaishouLaunchCampaign> list = kuaishouLaunchCampaignService.selectKuaishouLaunchCampaignList(kuaishouLaunchCampaign);
        return getDataTable(list);
    }


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

    /**
     * 新增广告计划信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增广告计划")
    public ResultResponse add(@RequestBody JSONObject requestJson) {
        AccountToken token = tokenService.getAccountToken(requestJson.getLong("accountId"));
        Long campaignId = kuaishouLaunchCampaignService.insertKuaishouLaunchCampaign(requestJson, token.getAccessToken());
        if (Check.isNotNull(campaignId) && campaignId > 0) {
            return ResultResponse.success(campaignId);
        }
        return ResultResponse.error("创建失败");
    }

    /**
     * 修改广告计划信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改广告计划信息")
    public AjaxResult edit(@RequestBody KuaishouLaunchCampaign kuaishouLaunchCampaign) {
        return toAjax(kuaishouLaunchCampaignService.updateKuaishouLaunchCampaign(kuaishouLaunchCampaign));
    }

    /**
     * 修改广告计划信息
     */
    @PostMapping(value = "/getInfo")
    @ApiOperation(value = "获取信息")
    public ResultResponse getInfo(@ApiParam("accountId") @RequestParam(value = "accountId", required = true) Long accountId,
                                  @ApiParam("1-计划 2-组 3-创意") @RequestParam(value = "level", required = true) String level,
                                  @ApiParam("id") @RequestParam(value = "id", required = false) Long id,
                                  @ApiParam("开始时间") @RequestParam(value = "startTime", required = true) String startTime,
                                  @ApiParam("结束时间") @RequestParam(value = "endTime", required = true) String endTime

    ) {
        return kuaishouLaunchCampaignService.getCampaignInfo(accountId, id, level, startTime, endTime);
    }

}