package com.gxwebsoft.common.system.controller;

import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.Cart;
import com.gxwebsoft.common.system.entity.OrderGoods;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.param.CartParam;
import com.gxwebsoft.common.system.service.CartService;
import com.gxwebsoft.common.system.service.OrderGoodsService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

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

/**
 * 购物车控制器
 *
 * @author 科技小王子
 * @since 2024-10-26 10:54:51
 */
@Tag(name = "购物车管理")
@RestController
@RequestMapping("/api/system/cart")
public class CartController extends BaseController {
    @Resource
    private CartService cartService;
    @Resource
    private OrderGoodsService orderGoodsService;

    @Operation(summary = "分页查询购物车")
    @GetMapping("/page")
    public ApiResult<PageResult<Cart>> page(CartParam param) {
        // 使用关联查询
        return success(cartService.pageRel(param));
    }

    @Operation(summary = "查询全部购物车")
    @GetMapping()
    public ApiResult<List<Cart>> list(CartParam param) {
        // 使用关联查询
        return success(cartService.listRel(param));
    }

    @Operation(summary = "根据id查询购物车")
    @GetMapping("/{id}")
    public ApiResult<Cart> get(@PathVariable("id") Long id) {
        // 使用关联查询
        return success(cartService.getByIdRel(id));
    }

    @Operation(summary = "添加购物车")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Cart cart) {
        System.out.println("cart = " + cart);
        final ArrayList<OrderGoods> orderGoods = new ArrayList<>();
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          cart.setUserId(loginUser.getUserId());
          cart.setTenantId(loginUser.getTenantId());
          if (cart.getType().equals(1)) {
            cart.getList().forEach(item -> {
              final OrderGoods og = new OrderGoods();
              og.setType(cart.getType());
              og.setItemId(item.getCompanyId());
              og.setPayPrice(item.getPrice());
              og.setMonth(cart.getMonth());
              og.setTotalNum(cart.getCartNum());
              og.setPayStatus(false);
              og.setOrderStatus(0);
              og.setTenantId(loginUser.getTenantId());
              og.setUserId(loginUser.getUserId());
              og.setComments("购买".concat(item.getTenantName()));
              orderGoods.add(og);
            });
          }
          if (cartService.save(cart)) {
            orderGoodsService.saveBatch(orderGoods);
            return success("添加成功");
          }
        }
        return fail("添加失败");
    }

    @Operation(summary = "修改购物车")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Cart cart) {
        if (cartService.updateById(cart)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "删除购物车")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (cartService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @Operation(summary = "批量添加购物车")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Cart> list) {
        if (cartService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @Operation(summary = "批量修改购物车")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Cart> batchParam) {
        if (batchParam.update(cartService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "批量删除购物车")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (cartService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
