package com.cpop.mall.business.controller.backstage;

import com.cpop.core.base.R;
import com.cpop.mall.business.bo.LogisticsOrderBo;
import com.cpop.mall.business.bo.OrderPageBo;
import com.cpop.mall.business.service.OrderService;
import com.cpop.mall.business.vo.OrderPageVo;
import com.mybatisflex.core.paginate.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static com.cpop.mall.business.entity.table.OrderTableDef.ORDER;

/**
 * 商城订单表 控制层。
 *
 * @author DB
 * @since 2023-10-24
 */
@RestController
@Api(tags = "商城后台订单管理模块")
@RequestMapping("/backstage/order")
public class BackstageOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * @descriptions 分页查询商城-订单
     * @author DB
     * @date 2023/10/23 11:56
     * @param bo 分页参数
     * @return: com.cpop.core.base.R<com.mybatisflex.core.paginate.Page<com.cpop.mall.business.entity.Product>>
     */
    @GetMapping("/getOrderPage")
    @ApiOperation("分页查询商城-订单")
    public R<Page<OrderPageVo>> getProductPage(@ApiParam("分页参数") OrderPageBo bo) {
        Page<OrderPageVo> page = orderService.getOrderPage(bo);
        return R.ok(page);
    }

    /**
     * @descriptions 输入物流订单
     * @author DB
     * @date 2023/10/12 10:48
     * @param bo 请求参数
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @ApiOperation("输入物流订单")
    @PostMapping("/inputLogisticsOrder")
    public R<Void> inputLogisticsOrder(@RequestBody @Validated LogisticsOrderBo bo) {
        orderService.inputLogisticsOrder(bo);
        return R.ok();
    }

    /**
     * @Description: 转变订单状态
     * @param id 订单id
     * @param orderStatus 订单状态
     * @return R<Void>
     * @Author DB
     * @Date: 2023/10/28 15:49
     */
    @PutMapping("/changeOrderStatus")
    @ApiOperation("转变订单状态")
    public R<Void> changeOrderStatus(@RequestParam("id") @ApiParam(value = "订单id",required = true) String id,
                                     @RequestParam("orderStatus") @ApiParam(value = "订单状态") Integer orderStatus) {
        orderService.updateChain()
                .setRaw(ORDER.PREVIOUS_STATUS, ORDER.ORDER_STATUS)
                .set(ORDER.ORDER_STATUS, orderStatus + 1)
                .where(ORDER.ID.eq(id)).update();
        return R.ok();
    }

    /**
     * @descriptions 后台确定订单完成
     * @author DB
     * @date 2023/11/07 9:37
     * @param id 订单id
     * @return: com.cpop.core.base.R<java.lang.Void>
     */
    @PutMapping("/orderFinish")
    @ApiOperation("后台确定订单完成")
    public R<Void> orderFinish(@RequestParam("id") @ApiParam(value = "订单id",required = true) String id) {
        orderService.orderFinish(id);
        return R.ok();
    }

    /**
     * @Description: 删除订单
     * @param id 主键
     * @return R<Void>
     * @Author DB
     * @Date: 2023/10/28 16:20
     */
    @ApiOperation("删除订单")
    @DeleteMapping("/removeOrderById/{id}")
    public R<Void> removeOrderById(@PathVariable String id) {
        orderService.removeById(id);
        return R.ok();
    }

}
