package com.gxwebsoft.cms.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.CmsCase;
import com.gxwebsoft.cms.param.CmsCaseParam;
import com.gxwebsoft.cms.service.CmsCaseService;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 客户案例控制器
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Tag(name = "客户案例管理")
@RestController
@RequestMapping("/api/cms/cms-case")
public class CmsCaseController extends BaseController {
    @Resource
    private CmsCaseService cmsCaseService;

    @Operation(summary = "分页查询客户案例")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsCase>> page(CmsCaseParam param) {
        return success(cmsCaseService.pageRel(param));
    }

    @Operation(summary = "查询全部客户案例")
    @GetMapping()
    public ApiResult<java.util.List<CmsCase>> list(CmsCaseParam param) {
        return success(cmsCaseService.listRel(param));
    }

    @Operation(summary = "根据id查询客户案例")
    @GetMapping("/{id}")
    public ApiResult<CmsCase> get(@PathVariable("id") Integer id) {
        return success(cmsCaseService.getByIdRel(id));
    }

    @Operation(summary = "添加客户案例")
    @PreAuthorize("hasAuthority('cms:cmsCase:save')")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsCase cmsCase) {
        if (cmsCaseService.save(cmsCase)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改客户案例")
    @PreAuthorize("hasAuthority('cms:cmsCase:update')")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsCase cmsCase) {
        if (cmsCaseService.updateById(cmsCase)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除客户案例")
    @PreAuthorize("hasAuthority('cms:cmsCase:remove')")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsCaseService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "修改案例状态(发布/下线)")
    @PreAuthorize("hasAuthority('cms:cmsCase:update')")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody CmsCase cmsCase) {
        CmsCase entity = new CmsCase();
        entity.setCaseId(cmsCase.getCaseId());
        entity.setStatus(cmsCase.getStatus());
        if (cmsCaseService.updateById(entity)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "统计概览")
    @GetMapping("/stats")
    public ApiResult<Map<String, Object>> stats() {
        Map<String, Object> map = new HashMap<>();
        map.put("total", cmsCaseService.count());
        map.put("published", cmsCaseService.count(new LambdaQueryWrapper<CmsCase>().eq(CmsCase::getStatus, 1)));
        map.put("offline", cmsCaseService.count(new LambdaQueryWrapper<CmsCase>().eq(CmsCase::getStatus, 0)));
        return success(map);
    }
}
