package org.dromara.app.customer;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.utils.RedisOperator;
import com.wzj.soopin.order.business.IOrderBusiness;
import com.wzj.soopin.order.domain.bo.OrderBo;
import com.wzj.soopin.order.domain.bo.SaveOrderBO;
import com.wzj.soopin.order.domain.entity.Order;
import com.wzj.soopin.order.domain.vo.ManagerOrderDetailVO;
import com.wzj.soopin.order.domain.vo.OrderVO;
import com.wzj.soopin.order.service.IMemberOrderService;
import com.wzj.soopin.order.service.OrderService;
import com.wzj.soopin.order.service.VerificationCodeService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;


@Slf4j
@Api(tags = "订单相关接口")
@RequestMapping("/app/order")
@RestController
@RequiredArgsConstructor
public class AppOrderController {

    private final OrderService orderService;

    private final IMemberOrderService memberOrderService;
    private final VerificationCodeService verificationCodeService;

    private final IOrderBusiness orderBusiness;

    public RedisOperator redis;
    @Tag(name ="查询订单列表")
    @PostMapping("/page")
    public R<IPage<OrderVO>> page(@RequestBody OrderBo bo){
        LoginUser loginUser= LoginHelper.getLoginUser();

        bo.setMemberId(loginUser.getUserId());
        return R.ok(memberOrderService.page(bo));
    }

    /**
     * 获取订单详细信息
     * @param id 订单id
     * @return
     */
    @GetMapping(value = "/{id}")
    public R<OrderVO> getInfo(@PathVariable("id") Long id) {
        return R.ok(orderBusiness.info(id));
    }

    /**
     * 生成订单
     * @param saveOrderBO
     * @return
     */
    @Log(title = "生成订单", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<OrderVO> add(@RequestBody SaveOrderBO saveOrderBO) {
        return R.ok(orderService.saveOrder(saveOrderBO));
    }

    /**
     * 取消订单
     * @param orderId
     * @return
     */
    @Log(title = "取消订单", businessType = BusinessType.UPDATE)
    @PostMapping("/cancel/{orderId}")
    public R<OrderVO> cancel(@PathVariable("orderId") Long orderId) {
        orderService.cancel(orderId);
        return R.ok();
    }



    /**
     * 生成核销码
     */
    @GetMapping("/generate/{orderId}")
    @Tag(name = "生成核销码")
    public R generateCode(@PathVariable Long orderId) {
        return verificationCodeService.generateVerificationCode(orderId);
    }

}
