package com.ycwl.basic.controller.mobile.manage;

import com.github.pagehelper.PageInfo;
import com.ycwl.basic.constant.BaseContextHandler;
import com.ycwl.basic.mapper.ScenicAccountMapper;
import com.ycwl.basic.model.pc.order.req.OrderReqQuery;
import com.ycwl.basic.model.pc.order.resp.OrderRespVO;
import com.ycwl.basic.model.pc.scenic.entity.ScenicAccountEntity;
import com.ycwl.basic.service.pc.OrderService;
import com.ycwl.basic.service.pc.ScenicAccountService;
import com.ycwl.basic.utils.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/mobile/scenic/order/v1")
// 景区账号相关接口
public class AppScenicOrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private ScenicAccountService service;

    @PostMapping("/list")
    @Deprecated
    public ApiResponse<List<OrderRespVO>> list(@RequestBody OrderReqQuery query) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = service.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        query.setScenicId(account.getScenicId().getFirst());
        return orderService.list(query);
    }

    @PostMapping("/{scenicId}/list")
    public ApiResponse<List<OrderRespVO>> list(@PathVariable Long scenicId, @RequestBody OrderReqQuery query) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = service.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        if (!account.getScenicId().contains(scenicId)) {
            return ApiResponse.fail("无权限查看该景区订单");
        }
        query.setScenicId(scenicId);
        return orderService.list(query);
    }

    @PostMapping("/page")
    @Deprecated
    public ApiResponse<PageInfo<OrderRespVO>> page(@RequestBody OrderReqQuery query) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = service.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        query.setScenicId(account.getScenicId().getFirst());
        return orderService.pageQueryDetail(query);
    }

    @PostMapping("/{scenicId}/page")
    public ApiResponse<PageInfo<OrderRespVO>> page(@PathVariable Long scenicId, @RequestBody OrderReqQuery query) {
        String userId = BaseContextHandler.getUserId();
        ScenicAccountEntity account = service.getScenicAccountById(Long.valueOf(userId));
        if (account == null || account.getScenicId().isEmpty()) {
            return ApiResponse.fail("景区账号未绑定景区");
        }
        if (!account.getScenicId().contains(scenicId)) {
            return ApiResponse.fail("无权限查看该景区订单");
        }
        query.setScenicId(scenicId);
        return orderService.pageQueryDetail(query);
    }
}
