package com.gxwebsoft.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.web.*;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.Cart;
import com.gxwebsoft.shop.param.CartParam;
import com.gxwebsoft.shop.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 购物车记录表控制器
 *
 * @author 科技小王子
 * @since 2022-11-25 19:04:43
 */
@Api(tags = "购物车记录表管理")
@RestController
@RequestMapping("/api/shop/cart")
public class CartController extends BaseController {
    @Resource
    private CartService cartService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @PreAuthorize("hasAuthority('shop:cart:list')")
    @OperationLog
    @ApiOperation("分页查询购物车记录表")
    @GetMapping("/page")
    public ApiResult<PageResult<Cart>> page(CartParam param) {
        PageParam<Cart, CartParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(cartService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(cartService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('shop:cart:list')")
    @OperationLog
    @ApiOperation("查询全部购物车记录表")
    @GetMapping()
    public ApiResult<List<Cart>> list(CartParam param) {
//        PageParam<Cart, CartParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        return success(cartService.list(page.getOrderWrapper()));
        // 使用关联查询

        return success(cartService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:cart:list')")
    @OperationLog
    @ApiOperation("根据id查询购物车记录表")
    @GetMapping("/{id}")
    public ApiResult<Cart> get(@PathVariable("id") Integer id) {
        return success(cartService.getById(id));
        // 使用关联查询
        //return success(cartService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('shop:cart:save')")
    @OperationLog
    @ApiOperation("添加购物车记录表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Cart cart) {
        // 记录当前登录用户id、租户id
        User loginUser = getLoginUser();
        // 存入redis
        Map<String,Object> userMap = BeanUtil.beanToMap(loginUser);
        String userMapKey = "oa:cart:".concat(loginUser.getUserId().toString());
        stringRedisTemplate.opsForHash().putAll(userMapKey,userMap);
        stringRedisTemplate.expire(userMapKey,30, TimeUnit.MINUTES);

        if (loginUser != null) {
          cart.setUserId(loginUser.getUserId());
        }
        if (cartService.save(cart)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:cart:update')")
    @OperationLog
    @ApiOperation("修改购物车记录表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Cart cart) {
        if (cartService.updateById(cart)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('shop:cart:remove')")
    @OperationLog
    @ApiOperation("删除购物车记录表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cartService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('shop:cart:save')")
    @OperationLog
    @ApiOperation("批量添加购物车记录表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Cart> list) {
        if (cartService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('shop:cart:update')")
    @OperationLog
    @ApiOperation("批量修改购物车记录表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Cart> batchParam) {
        if (batchParam.update(cartService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

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

}
