package com.gxwebsoft.apps.controller;

import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.HualalaCartFoodService;
import com.gxwebsoft.apps.entity.HualalaCartFood;
import com.gxwebsoft.apps.param.HualalaCartFoodParam;
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.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 2023-01-14 14:46:44
 */
@Api(tags = "购物车商品管理")
@RestController
@RequestMapping("/api/apps/hualala-cart-food")
public class HualalaCartFoodController extends BaseController {
    @Resource
    private HualalaCartFoodService hualalaCartFoodService;

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:list')")
    @OperationLog
    @ApiOperation("分页查询购物车商品")
    @GetMapping("/page")
    public ApiResult<PageResult<HualalaCartFood>> page(HualalaCartFoodParam param) {
        PageParam<HualalaCartFood, HualalaCartFoodParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(hualalaCartFoodService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(hualalaCartFoodService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:list')")
    @OperationLog
    @ApiOperation("查询全部购物车商品")
    @GetMapping()
    public ApiResult<List<HualalaCartFood>> list(HualalaCartFoodParam param) {
        PageParam<HualalaCartFood, HualalaCartFoodParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(hualalaCartFoodService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(hualalaCartFoodService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:list')")
    @OperationLog
    @ApiOperation("根据id查询购物车商品")
    @GetMapping("/{id}")
    public ApiResult<HualalaCartFood> get(@PathVariable("id") Integer id) {
        return success(hualalaCartFoodService.getById(id));
        // 使用关联查询
        //return success(hualalaCartFoodService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:save')")
    @OperationLog
    @ApiOperation("添加购物车商品")
    @PostMapping()
    public ApiResult<?> save(@RequestBody HualalaCartFood hualalaCartFood) {
        if (hualalaCartFoodService.save(hualalaCartFood)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:update')")
    @OperationLog
    @ApiOperation("修改购物车商品")
    @PutMapping()
    public ApiResult<?> update(@RequestBody HualalaCartFood hualalaCartFood) {
        if (hualalaCartFoodService.updateById(hualalaCartFood)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:remove')")
    @OperationLog
    @ApiOperation("删除购物车商品")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (hualalaCartFoodService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:save')")
    @OperationLog
    @ApiOperation("批量添加购物车商品")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<HualalaCartFood> list) {
        if (hualalaCartFoodService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:update')")
    @OperationLog
    @ApiOperation("批量修改购物车商品")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<HualalaCartFood> batchParam) {
        if (batchParam.update(hualalaCartFoodService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:hualalaCartFood:remove')")
    @OperationLog
    @ApiOperation("批量删除购物车商品")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (hualalaCartFoodService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
