package com.gxwebsoft.common.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.Order;
import com.gxwebsoft.common.system.entity.OrderGoods;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.param.OrderGoodsParam;
import com.gxwebsoft.common.system.service.MenuService;
import com.gxwebsoft.common.system.service.OrderGoodsService;
import com.gxwebsoft.common.system.service.OrderService;
import com.gxwebsoft.common.system.service.impl.MenuServiceImpl;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 订单商品控制器
 *
 * @author 科技小王子
 * @since 2024-10-26 12:18:05
 */
@Tag(name = "订单商品管理")
@RestController
@RequestMapping("/api/system/order-goods")
public class OrderGoodsController extends BaseController {
    @Resource
    private OrderGoodsService orderGoodsService;
    @Resource
    private OrderService orderService;
    @Resource
    private MenuService menuService;
    @Resource
    private MenuServiceImpl menuServiceImpl;

    @Operation(summary = "分页查询订单商品")
    @GetMapping("/page")
    public ApiResult<PageResult<OrderGoods>> page(OrderGoodsParam param) {
        // 使用关联查询
        final User loginUser = getLoginUser();
        if (loginUser == null) {
          return fail("请先登陆",null);
        }
        // 非平台权限
        if (!loginUser.getTenantId().equals(5)) {
          param.setUserId(loginUser.getUserId());
        }
        return success(orderGoodsService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('sys:order:list')")
    @Operation(summary = "查询全部订单商品")
    @GetMapping()
    public ApiResult<List<OrderGoods>> list(OrderGoodsParam param) {
        // 使用关联查询
        return success(orderGoodsService.listRel(param));
    }

    @PreAuthorize("hasAuthority('sys:order:list')")
    @Operation(summary = "根据id查询订单商品")
    @GetMapping("/{id}")
    public ApiResult<OrderGoods> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(orderGoodsService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('sys:order:save')")
    @Operation(summary = "添加订单商品")
    @PostMapping()
    public ApiResult<?> save(@RequestBody OrderGoods orderGoods) {
        // 记录当前登录用户id
        User loginUser = getLoginUser();
        if (loginUser != null) {
          orderGoods.setUserId(loginUser.getUserId());
        }
        if (orderGoodsService.save(orderGoods)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:order:update')")
    @Operation(summary = "修改订单商品")
    @PutMapping()
    public ApiResult<?> update(@RequestBody OrderGoods orderGoods) {
        if (orderGoodsService.updateById(orderGoods)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @Operation(summary = "修改订单商品状态")
    @PutMapping("/updateOrderStatus")
    public ApiResult<?> updateOrderStatus(@RequestBody OrderGoods orderGoods) {
      final User loginUser = getLoginUser();
      if (loginUser == null) {
        return fail("请先登录");
      }
      // 复制菜单
      menuServiceImpl.install(orderGoods.getMenuId());
      if (orderGoodsService.updateById(orderGoods)) {
        if (orderGoodsService.count(new LambdaQueryWrapper<OrderGoods>().eq(OrderGoods::getOrderStatus,0).eq(OrderGoods::getOrderId,orderGoods.getOrderId())) == 0) {
          orderService.update(new LambdaUpdateWrapper<Order>().eq(Order::getOrderId,orderGoods.getOrderId()).set(Order::getOrderStatus,1));
        }
        return success("修改成功");
      }
      return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:order:remove')")
    @Operation(summary = "删除订单商品")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (orderGoodsService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('sys:order:save')")
    @Operation(summary = "批量添加订单商品")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<OrderGoods> list) {
        if (orderGoodsService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('sys:order:update')")
    @Operation(summary = "批量修改订单商品")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<OrderGoods> batchParam) {
        if (batchParam.update(orderGoodsService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('sys:order:remove')")
    @Operation(summary = "批量删除订单商品")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (orderGoodsService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
