package com.czg.controller.admin;

import com.czg.account.dto.BkOrderDTO;
import com.czg.account.entity.BkContactList;
import com.czg.account.entity.BkOrder;
import com.czg.account.entity.BkOrderTable;
import com.czg.account.service.BkContactListService;
import com.czg.account.service.BkOrderService;
import com.czg.account.vo.BkTableVO;
import com.czg.account.vo.BookingOrderStatisticsVO;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.Set;


/**
 * 预约端
 *
 * @author ww
 * @description
 */
@RestController
@RequestMapping("/admin/bk/")
@Slf4j
public class BkContactListController {

    @Resource
    private BkContactListService contactListService;
    @Resource
    private BkOrderService bkOrderService;

    /**
     * 通讯录 获取联系人订单数等
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkContactList:list", name = "预约端-联系人列表")
    @PostMapping("/contactList")
    public CzgResult<List<BkContactList>> getUserList(@RequestBody Set<String> phones) {
        return CzgResult.success(contactListService.getUserList(StpKit.USER.getShopId(), phones));
    }

    /**
     * 预约端-台桌：查询台桌列表
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:table", name = "预约端-台桌：查询台桌列表")
    @GetMapping("/bkOrder/table")
    public CzgResult<List<BkTableVO>> table(@RequestParam(value = "areaId", required = false) Long areaId,
                                 @RequestParam LocalDate day,
                                 @RequestParam(value = "seatTimeType", required = false) String seatTimeType) {
        List<BkTableVO> tables = bkOrderService.table(StpKit.USER.getShopId(), areaId, day, seatTimeType);
        return CzgResult.success(tables);
    }

    /**
     * 预约端-预定单：预约单统计
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bookings:statistics", name = "预约端-预约单：预约单统计")
    @GetMapping("/bkOrder/booking/statistics")
    public CzgResult<BookingOrderStatisticsVO> bookingOrderStatistics(@RequestParam(required = false) String search,
                                                                      @RequestParam(required = false) LocalDate start,
                                                                      @RequestParam(required = false) LocalDate end,
                                                                      @RequestParam(required = false) String status) {
        BookingOrderStatisticsVO statistics = bkOrderService.bookingOrderStatistics(StpKit.USER.getShopId(), search, start, end, status);
        return CzgResult.success(statistics);
    }

    /**
     * 预约端-预定单：查询预约单列表
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:bookings", name = "预约端-预约单：列表")
    @GetMapping("/bkOrder/bookings")
    public CzgResult<List<BkOrder>> bookings(@RequestParam(required = false) String search,
                                            @RequestParam(required = false) LocalDate start,
                                            @RequestParam(required = false) LocalDate end,
                                            @RequestParam(required = false) String status) {
        List<BkOrder> tables = bkOrderService.bookings(StpKit.USER.getShopId(), search, start, end, status);
        return CzgResult.success(tables);
    }

    /**
     * 预约端-预定单：查询预约单选中台桌
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:tables", name = "预约端-预约单：查询预约单选中的台桌")
    @GetMapping("/bkOrder/bookings/tables")
    public CzgResult<List<BkOrderTable>> bookingTables(Long id) {
        List<BkOrderTable> tables = bkOrderService.bookingTables(StpKit.USER.getShopId(), id);
        return CzgResult.success(tables);
    }


    /**
     * 预约端-预约单：预约/修改预约
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:booking", name = "预约端-预约单：预约/修改")
    @PostMapping("/bkOrder/booking")
    public CzgResult<Void> booking(@RequestBody BkOrderDTO bkOrder) {
        Long shopId = StpKit.USER.getShopId();
        if (bkOrder.getShopId() != null && !bkOrder.getShopId().equals(shopId)) {
            return CzgResult.failure("违规操作");
        }
        bkOrderService.booking(shopId, bkOrder);
        return CzgResult.success();
    }

    /**
     * 预约端-预约单：取消预约
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:cancel", name = "预约端-预约单：取消预约")
    @PostMapping("/bkOrder/cancel")
    public CzgResult<Void> cancel(@RequestBody BkOrderDTO bkOrder) {
        AssertUtil.isNull(bkOrder.getId(), "需要撤销的预约单id不能为空");
        bkOrderService.upStatus(StpKit.USER.getShopId(), "已取消", bkOrder.getId(), bkOrder.getCancelReason());
        return CzgResult.success();
    }


    /**
     * 预约端-预约单：已到店
     */
    @SaAdminCheckPermission(parentName = "预约端", value = "bk:bkOrder:storeArrival", name = "预约端-预约单：已到店")
    @PostMapping("/bkOrder/storeArrival")
    public CzgResult<Void> storeArrival(@RequestBody BkOrderDTO bkOrder) {
        AssertUtil.isNull(bkOrder.getId(), "已到店的预约单id不能为空");
        bkOrderService.upStatus(StpKit.USER.getShopId(), "已到店", bkOrder.getId(), "");
        return CzgResult.success();
    }
}























