package com.ycwl.basic.controller.mobile;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.annotation.IgnoreToken;
import com.ycwl.basic.biz.OrderBiz;
import com.ycwl.basic.biz.PriceBiz;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.mapper.FaceMapper;
import com.ycwl.basic.model.jwt.JwtInfo;
import com.ycwl.basic.model.mobile.order.IsBuyBatchRespVO;
import com.ycwl.basic.model.mobile.order.IsBuyRespVO;
import com.ycwl.basic.model.mobile.order.OrderAppPageReq;
import com.ycwl.basic.model.mobile.order.RefundOrderReq;
import com.ycwl.basic.model.pc.face.resp.FaceRespVO;
import com.ycwl.basic.model.pc.order.req.CreateBatchOrderReqVO;
import com.ycwl.basic.model.pc.order.req.CreateOrderReqVO;
import com.ycwl.basic.model.pc.order.resp.OrderAppRespVO;
import com.ycwl.basic.pay.entity.PayResponse;
import com.ycwl.basic.service.pc.OrderService;
import com.ycwl.basic.utils.ApiResponse;
import com.ycwl.basic.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author：longbinbin
 * @Date：2024/12/4 17:16
 */
@RestController
@RequestMapping("/api/mobile/order/v1")
// 订单相关接口
public class AppOrderController {

    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderBiz orderBiz;
    @Autowired
    private PriceBiz priceBiz;
    @Autowired
    private FaceMapper faceMapper;

    // 用户端订单列表查询
    @PostMapping("/page")
    public ApiResponse<PageInfo<OrderAppRespVO>> pageQuery(@RequestBody OrderAppPageReq orderReqQuery) {
        String userId = BaseContextHandler.getUserId();
        orderReqQuery.setMemberId(Long.valueOf(userId));
        return orderService.appPageQuery(orderReqQuery);
    }

    // 用户端订单详情查询
    @GetMapping("getOrderDetails/{id}")
    @IgnoreToken
    public ApiResponse<OrderAppRespVO> getOrderDetails(@PathVariable("id") Long id) {
        return orderService.appDetail(id);
    }

    // 用户端订单新增
    @PostMapping("/addOrder")
    public ApiResponse<Map<String, Object>> addOrder(@RequestBody CreateOrderReqVO orderAddReq) throws Exception {
        JwtInfo worker = JwtTokenUtil.getWorker();
        return orderService.createOrder(worker.getUserId(), orderAddReq);
    }

    // 查询订单
    @GetMapping("/queryOrder")
    public ApiResponse<PayResponse> queryOrder(@RequestParam("orderId") Long orderId) {
        return ApiResponse.success(orderService.queryOrder(orderId));
    }

    // 用户端打包订单新增
    @PostMapping("/addBatchOrder")
    public ApiResponse<Map<String, Object>> addOrder(@RequestBody CreateBatchOrderReqVO batchOrderReqVO) throws Exception {
        JwtInfo worker = JwtTokenUtil.getWorker();
        return orderService.createBatchOrder(worker.getUserId(), batchOrderReqVO);
    }


    // 获取用户订单数量
    @GetMapping("/getUserOrderCount")
    public ApiResponse<Integer> getUserOrderCount() {
        Long userId = Long.parseLong(BaseContextHandler.getUserId());

        return orderService.getOrderCountByUserId(userId);
    }

    // 发起退款
    @PostMapping("/refundOrder")
    public ApiResponse<?> refundOrder(@RequestBody RefundOrderReq refundOrderReq) {
        return orderService.refundOrder(refundOrderReq);
    }

    @GetMapping("/scenic/{scenicId}/query")
    public ApiResponse<IsBuyRespVO> isBuy(@PathVariable("scenicId") Long scenicId, @RequestParam("type") Integer type, @RequestParam("goodsId") Long goodsId, @RequestParam(value = "faceId", required = false) Long faceId) {
        Long userId = Long.parseLong(BaseContextHandler.getUserId());
        return ApiResponse.success(orderBiz.isBuy(scenicId, userId, faceId, type, goodsId));
    }

    @GetMapping("/scenic/{scenicId}/queryBatchPrice")
    public ApiResponse<IsBuyBatchRespVO> queryPrice(@PathVariable("scenicId") Long scenicId, @RequestParam("type") Integer type, @RequestParam(value = "faceId", required = false) Long faceId, @RequestParam(value = "goodsIds", required = false) String goodsIds) {
        Long userId = Long.parseLong(BaseContextHandler.getUserId());
        if (faceId == null) {
            FaceRespVO lastFaceByUserId = faceMapper.findLastFaceByUserId(BaseContextHandler.getUserId());
            if (lastFaceByUserId == null) {
                return ApiResponse.fail("您还未上传人脸");
            }
            faceId = lastFaceByUserId.getId();
        }
        IsBuyBatchRespVO buy = priceBiz.isOnePriceBuy(userId, faceId, scenicId, type, goodsIds);
        if (buy == null) {
            return ApiResponse.fail("该套餐暂未开放购买");
        }
        return ApiResponse.success(buy);
    }
}
