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.KuaishouLaunchGroup;
import com.ruixuan.launch.entity.KuaishouLaunchGroupRecord;
import com.ruixuan.launch.service.IAccountTokenService;
import com.ruixuan.launch.service.IKuaishouLaunchGroupService;
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/group")
public class KuaishouLaunchGroupController extends BaseController {

    @Autowired
    private IKuaishouLaunchGroupService kuaishouLaunchGroupService;

    @Autowired
    private IAccountTokenService tokenService;

    /**
     * 查询广告组信息列表
     */
    @ApiOperation(value = "查询广告组信息列表")
    @GetMapping("/list")
    public TableDataInfo list(KuaishouLaunchGroup kuaishouLaunchGroup) {
        startPage();
        List<KuaishouLaunchGroup> list = kuaishouLaunchGroupService.selectKuaishouLaunchGroupList(kuaishouLaunchGroup);
        return getDataTable(list);
    }

    /**
     * 查询创建记录
     */
    @ApiOperation(value = "查询创建记录")
    @GetMapping("/queryGroupRecord")
    public TableDataInfo queryGroupRecord(@ApiParam("账户Id") @RequestParam(value = "accountId", required = true) Long accountId,
                                          @ApiParam("策略ID") @RequestParam(value = "strategyId", required = true) Long strategyId) {
        startPage();
        List<KuaishouLaunchGroupRecord> list = kuaishouLaunchGroupService.queryGroupRecord(accountId, strategyId);
        return getDataTable(list);
    }


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

    /**
     * 新增广告组信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增广告组")
    public ResultResponse add(@RequestBody JSONObject requestJson) {
        try {
            AccountToken token = tokenService.getAccountToken(requestJson.getLong("accountId"));
            kuaishouLaunchGroupService.insertKuaishouLaunchGroup(requestJson, token.getAccessToken());
            return ResultResponse.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultResponse.error();
    }

    /**
     * 修改广告组信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改广告组信息")
    public AjaxResult edit(@RequestBody KuaishouLaunchGroup kuaishouLaunchGroup) {
        return toAjax(kuaishouLaunchGroupService.updateKuaishouLaunchGroup(kuaishouLaunchGroup));
    }

}