package com.fs.company.controller.newAdv;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fs.common.result.Result;
import com.fs.newAdv.domain.Site;
import com.fs.newAdv.service.ISiteService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 站点管理控制器
 *
 * @author zhangqin
 * @date 2025-11-03
 */
@Slf4j
@RestController
@RequestMapping("/adv/site")
public class SiteController {

    @Autowired
    private ISiteService siteService;

    @GetMapping("/page")
    public Result<IPage<Site>> pageSiteStatistics(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) Long siteName,
            @RequestParam(required = false) Long launchType,
            @RequestParam(required = false) Long advertiserId
    ) {
        Page<Site> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Site> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(siteName != null, Site::getSiteName, siteName);
        wrapper.eq(launchType != null, Site::getLaunchType, launchType);
        wrapper.eq(advertiserId != null, Site::getAdvertiserId, advertiserId);
        wrapper.orderByAsc(Site::getCreateTime);
        IPage<Site> result = siteService.page(page, wrapper);
        return Result.success(result);
    }


    @GetMapping("/list")
    public Result<List<Site>> list() {
        List<Site> list = siteService.list(new QueryWrapper<>());
        return Result.success(list);
    }

    /**
     * 查询站点详情
     */
    @GetMapping("/{id}")
    public Result<Site> getById(@PathVariable Long id) {
        Site site = siteService.getById(id);
        return Result.success(site);
    }

    /**
     * 创建站点
     */
    @PostMapping
    public Result<Void> create(@RequestBody Site site) {
        siteService.createSite(site);
        return Result.success();
    }

    /**
     * 更新站点
     */
    @PutMapping("/{id}")
    public Result<Void> update(@PathVariable Long id, @RequestBody Site site) {
        site.setId(id);
        siteService.updateById(site);
        return Result.success();
    }

    /**
     * 删除站点
     */
    @DeleteMapping("/{id}")
    public Result<Void> delete(@PathVariable Long id) {
        siteService.removeById(id);
        return Result.success();
    }


    /**
     * 启用停用站点
     */
    @PostMapping("enable/{id}")
    public Result<Void> enable(@PathVariable Long id) {
        Site byId = siteService.getById(id);
        byId.setStatus(byId.getStatus() == 1 ? 0 : 1);
        siteService.updateById(byId);
        return Result.success();
    }
}

