package com.fs.ad.controller;

import com.fs.baidu.api.BaiduApis;
import com.fs.baidu.domain.*;
import com.fs.baidu.service.*;
import com.fs.common.annotation.Log;
import com.fs.common.core.controller.BaseController;
import com.fs.common.core.domain.AjaxResult;
import com.fs.common.core.domain.R;
import com.fs.common.core.page.TableDataInfo;
import com.fs.common.enums.BusinessType;
import com.fs.common.utils.poi.ExcelUtil;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * 百度账号Controller
 * 
 * @author fs
 * @date 2025-01-20
 */
@RestController
@AllArgsConstructor
@RequestMapping("/bd/BdAccount")
public class BdAccountController extends BaseController {
    private final IBdAccountService bdAccountService;
    private final IBdPlanService bdPlanService;
    private final IBdUnitService bdUnitService;
    private final IBdCreativeService bdCreativeService;
    private final IBdApiService bdApiService;
    private final BaiduApis baiduApis;

    /**
     * 查询百度账号列表
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:list')")
    @GetMapping("/list")
    public TableDataInfo list(BdAccount bdAccount){
        startPage();
        List<BdAccount> list = bdAccountService.selectBdAccountList(bdAccount);
        return getDataTable(list);
    }

    /**
     * 导出百度账号列表
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:export')")
    @Log(title = "百度账号", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BdAccount bdAccount)
    {
        List<BdAccount> list = bdAccountService.selectBdAccountList(bdAccount);
        ExcelUtil<BdAccount> util = new ExcelUtil<BdAccount>(BdAccount.class);
        return util.exportExcel(list, "百度账号数据");
    }

    /**
     * 获取百度账号详细信息
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bdAccountService.selectBdAccountById(id));
    }

    /**
     * 新增百度账号
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:add')")
    @Log(title = "百度账号", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BdAccount bdAccount)
    {
        return toAjax(bdAccountService.insertBdAccount(bdAccount));
    }

    /**
     * 修改百度账号
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:edit')")
    @Log(title = "百度账号", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BdAccount bdAccount)
    {
        return toAjax(bdAccountService.updateBdAccount(bdAccount));
    }

    /**
     * 删除百度账号
     */
    @PreAuthorize("@ss.hasPermi('baidu:BdAccount:remove')")
    @Log(title = "百度账号", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bdAccountService.deleteBdAccountByIds(ids));
    }


    /**
     * 查询百度账号列表
     */
    @GetMapping("/listAll")
    public R listAll(BdAccount bdAccount){
        return R.ok().put("data", bdAccountService.selectBdAccountList(bdAccount));
    }
    @GetMapping("/listAllPlan")
    public R listAllPlan(BdPlan plan){
        return R.ok().put("data", bdPlanService.selectBdPlanList(plan));
    }
    @GetMapping("/listAllUnit")
    public R listAllUnit(BdUnit unit){
        return R.ok().put("data", bdUnitService.selectBdUnitList(unit));
    }
    @GetMapping("/listAllCreative")
    public R listAllCreative(BdCreative creative){
        return R.ok().put("data", bdCreativeService.selectBdCreativeList(creative));
    }
    @GetMapping("/authorizationUrl")
    public R authorizationUrl(Long id){
        Optional<BdApi> first = bdApiService.list().stream().findFirst();
        if(!first.isPresent()){
            return R.error("未找到API信息");
        }
        return R.ok().put("url", bdApiService.authorizationUrl(first.get().getId()));
    }
    @GetMapping("/syncAccount")
    public R syncAccount(Long id){
        Optional<BdApi> first = bdApiService.list().stream().findFirst();
        if(!first.isPresent()){
            return R.error("未找到API信息");
        }
        baiduApis.listAccount(1L, id);
        return R.ok();
    }
}
