package com.gxwebsoft.shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.service.CategoryService;
import com.gxwebsoft.shop.entity.Category;
import com.gxwebsoft.shop.param.CategoryParam;
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 io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 商品分类表控制器
 *
 * @author 科技小王子
 * @since 2022-11-22 17:45:37
 */
@Api(tags = "商品分类表管理")
@RestController
@RequestMapping("/api/shop/category")
public class CategoryController extends BaseController {
    @Resource
    private CategoryService categoryService;

    @PreAuthorize("hasAuthority('shop:category:list')")
    @OperationLog
    @ApiOperation("分页查询商品分类表")
    @GetMapping("/page")
    public ApiResult<PageResult<Category>> page(CategoryParam param) {
        PageParam<Category, CategoryParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        page.setDefaultOrder("sort_number");
        return success(categoryService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(categoryService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:category:list')")
    @OperationLog
    @ApiOperation("查询全部商品分类表")
    @GetMapping()
    public ApiResult<List<Category>> list(CategoryParam param) {
        System.out.println("====== param ======");
        System.out.println(param);
        PageParam<Category, CategoryParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
        QueryWrapper<Category> wrapper = new QueryWrapper<>();
        page.setDefaultOrder("sort_number");
        if (param.getParentId() != null && param.getParentId() == -1){
            wrapper.ne("parent_id", "0");
        }
        return success(categoryService.list(wrapper));
        // 使用关联查询
        //return success(categoryService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:category:list')")
    @OperationLog
    @ApiOperation("根据id查询商品分类表")
    @GetMapping("/{id}")
    public ApiResult<Category> get(@PathVariable("id") Integer id) {
        return success(categoryService.getById(id));
        // 使用关联查询
        //return success(categoryService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:category:save')")
    @OperationLog
    @ApiOperation("添加商品分类表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Category category) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          category.setUserId(loginUser.getUserId());
        }
        if (categoryService.save(category)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:category:update')")
    @OperationLog
    @ApiOperation("修改商品分类表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Category category) {
        if (categoryService.updateById(category)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:category:remove')")
    @OperationLog
    @ApiOperation("删除商品分类表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (categoryService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:category:save')")
    @OperationLog
    @ApiOperation("批量添加商品分类表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Category> list) {
        if (categoryService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:category:update')")
    @OperationLog
    @ApiOperation("批量修改商品分类表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Category> batchParam) {
        if (batchParam.update(categoryService, "category_id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:category:remove')")
    @OperationLog
    @ApiOperation("批量删除商品分类表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (categoryService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
