package com.gxwebsoft.cms.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.CmsStatisticsService;
import com.gxwebsoft.cms.entity.CmsStatistics;
import com.gxwebsoft.cms.param.CmsStatisticsParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 站点统计信息表控制器
 *
 * @author 科技小王子
 * @since 2025-07-25 12:32:06
 */
@Tag(name = "站点统计信息表管理")
@RestController
@RequestMapping("/api/cms/cms-statistics")
public class CmsStatisticsController extends BaseController {
    @Resource
    private CmsStatisticsService cmsStatisticsService;

    @Operation(summary = "分页查询站点统计信息表")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsStatistics>> page(CmsStatisticsParam param) {
        // 使用关联查询
        return success(cmsStatisticsService.pageRel(param));
    }

    @Operation(summary = "查询全部站点统计信息表")
    @GetMapping()
    public ApiResult<List<CmsStatistics>> list(CmsStatisticsParam param) {
        // 使用关联查询
        return success(cmsStatisticsService.listRel(param));
    }

    @Operation(summary = "根据id查询站点统计信息表")
    @GetMapping("/{id}")
    public ApiResult<CmsStatistics> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(cmsStatisticsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:save')")
    @OperationLog
    @Operation(summary = "添加站点统计信息表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsStatistics cmsStatistics) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
         cmsStatistics.setUserId(loginUser.getUserId());
        }
        if (cmsStatisticsService.save(cmsStatistics)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:update')")
    @OperationLog
    @Operation(summary = "修改站点统计信息表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsStatistics cmsStatistics) {
        if (cmsStatisticsService.updateById(cmsStatistics)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:remove')")
    @OperationLog
    @Operation(summary = "删除站点统计信息表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsStatisticsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:save')")
    @OperationLog
    @Operation(summary = "批量添加站点统计信息表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<CmsStatistics> list) {
        if (cmsStatisticsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:update')")
    @OperationLog
    @Operation(summary = "批量修改站点统计信息表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<CmsStatistics> batchParam) {
        if (batchParam.update(cmsStatisticsService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('cms:cmsStatistics:remove')")
    @OperationLog
    @Operation(summary = "批量删除站点统计信息表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (cmsStatisticsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
