package com.gxwebsoft.cms.controller;

import com.gxwebsoft.cms.entity.CmsProduct;
import com.gxwebsoft.cms.param.CmsProductParam;
import com.gxwebsoft.cms.service.CmsProductService;
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.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

/**
 * 产品控制器
 *
 * @author WorkBuddy
 * @since 2026-07-17
 */
@Tag(name = "产品管理")
@RestController
@RequestMapping("/api/cms/cms-product")
public class CmsProductController extends BaseController {
    @Resource
    private CmsProductService cmsProductService;

    @Operation(summary = "分页查询产品")
    @GetMapping("/page")
    public ApiResult<PageResult<CmsProduct>> page(CmsProductParam param) {
        return success(cmsProductService.pageRel(param));
    }

    @Operation(summary = "查询全部产品")
    @GetMapping()
    public ApiResult<List<CmsProduct>> list(CmsProductParam param) {
        return success(cmsProductService.listRel(param));
    }

    @Operation(summary = "根据id查询产品")
    @GetMapping("/{id}")
    public ApiResult<CmsProduct> get(@PathVariable("id") Integer id) {
        return success(cmsProductService.getByIdRel(id));
    }

    @Operation(summary = "添加产品")
    @PreAuthorize("hasAuthority('cms:cmsProduct:save')")
    @PostMapping()
    public ApiResult<?> save(@RequestBody CmsProduct cmsProduct) {
        User loginUser = getLoginUser();
        if (loginUser != null) {
            cmsProduct.setUserId(loginUser.getUserId());
        }
        if (cmsProductService.save(cmsProduct)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改产品")
    @PreAuthorize("hasAuthority('cms:cmsProduct:update')")
    @PutMapping()
    public ApiResult<?> update(@RequestBody CmsProduct cmsProduct) {
        if (cmsProductService.updateById(cmsProduct)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除产品")
    @PreAuthorize("hasAuthority('cms:cmsProduct:remove')")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cmsProductService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "修改产品状态(在售/下架)")
    @PreAuthorize("hasAuthority('cms:cmsProduct:update')")
    @PutMapping("/status")
    public ApiResult<?> updateStatus(@RequestBody CmsProduct cmsProduct) {
        CmsProduct entity = new CmsProduct();
        entity.setProductId(cmsProduct.getProductId());
        entity.setStatus(cmsProduct.getStatus());
        if (cmsProductService.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", cmsProductService.count());
        map.put("onSale", cmsProductService.count(new LambdaQueryWrapper<CmsProduct>().eq(CmsProduct::getStatus, 1)));
        map.put("offSale", cmsProductService.count(new LambdaQueryWrapper<CmsProduct>().eq(CmsProduct::getStatus, 0)));
        return success(map);
    }
}
