package com.yc.ship.module.trade.controller.admin.orderjzdetail;

import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import static com.yc.ship.framework.common.pojo.CommonResult.success;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.ship.framework.excel.core.util.ExcelUtils;

import com.yc.ship.module.trade.controller.admin.orderjzdetail.vo.*;
import com.yc.ship.module.trade.dal.dataobject.orderjzdetail.OrderGiftVisitorDO;

import com.yc.ship.module.trade.service.orderjzdetail.OrderGiftVisitorService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

/**
 * 蹭送行程游客 Controller
 *
 * @author yc
 */
@Tag(name = "管理后台 - 蹭送行程游客")
@RestController
@RequestMapping("/trade/order-gift-visitor")
@Validated
public class OrderGiftVisitorController {

    @Resource
    private OrderGiftVisitorService orderGiftVisitorService;

    @PostMapping("/create")
    @Operation(summary = "创建蹭送行程游客")
    @PreAuthorize("@ss.hasPermission('trade:order-gift-visitor:create')")
    public CommonResult<Long> createOrderGiftVisitor(@Valid @RequestBody OrderGiftVisitorSaveReqVO createReqVO) {
        return success(orderGiftVisitorService.createOrderGiftVisitor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新蹭送行程游客")
    @PreAuthorize("@ss.hasPermission('trade:order-gift-visitor:update')")
    public CommonResult<Boolean> updateOrderGiftVisitor(@Valid @RequestBody OrderGiftVisitorSaveReqVO updateReqVO) {
        orderGiftVisitorService.updateOrderGiftVisitor(updateReqVO);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得蹭送行程游客")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:order-gift-visitor:query')")
    public CommonResult<OrderGiftVisitorRespVO> getOrderGiftVisitor(@RequestParam("id") Long id) {
        OrderGiftVisitorDO orderGiftVisitor = orderGiftVisitorService.getOrderGiftVisitor(id);
        return success(BeanUtils.toBean(orderGiftVisitor, OrderGiftVisitorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得蹭送行程游客分页")
    @PreAuthorize("@ss.hasPermission('trade:order-jz-detail-sign:query')")
    public CommonResult<PageResult<OrderGiftVisitorRespVO>> getOrderGiftVisitorPage(@Valid OrderGiftVisitorPageReqVO pageReqVO) {
        PageResult<OrderGiftVisitorDO> pageResult = orderGiftVisitorService.getOrderGiftVisitorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrderGiftVisitorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出蹭送行程接站明细 Excel")
    @PreAuthorize("@ss.hasPermission('trade:order-jz-detail-sign:query')")
    public void exportGiftVisitorExcel(@Valid OrderGiftVisitorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        List<OrderGiftVisitorDO> list = orderGiftVisitorService.getExportList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "赠送行程接站明细.xls", "数据", OrderJzDetailGiftExportVO.class,
                        BeanUtils.toBean(list, OrderJzDetailGiftExportVO.class));
    }

    // ==================== 签到核销相关 ====================

    @PostMapping("/sign")
    @Operation(summary = "签到核销")
    public CommonResult<Boolean> sign(@RequestBody OrderGiftVisitorSaveReqVO signReqVO) {
        orderGiftVisitorService.signOrderGiftVisitor(
                signReqVO
        );
        return success(true);
    }

    @DeleteMapping("/sign/clean")
    @Operation(summary = "清除签到")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> cleanSign(@RequestParam("id") Long id) {
        orderGiftVisitorService.cleanSign(id);
        return success(true);
    }

    @PostMapping("/batch-sign")
    @Operation(summary = "批量签到核销")
    public CommonResult<Integer> batchSign(@Valid @RequestBody OrderJzDetailBatchSignReqVO reqVO) {
        Integer count = orderGiftVisitorService.batchSignOrderGiftVisitor(
                reqVO.getItems(), reqVO.getSignRemark()
        );
        return success(count);
    }

    // ==================== 补登订单查询相关 ====================

    @GetMapping("/gift/order/query")
    @Operation(summary = "蹭送行程补登订单分页查询")
    @PreAuthorize("@ss.hasPermission('trade:order-gift-visitor:query')")
    public CommonResult<PageResult<OrderGiftVisitorRespVO>> getGiftOrderPage(@Valid OrderGiftVisitorPageReqVO pageReqVO) {
        IPage<OrderGiftVisitorDO> page = new Page<>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<OrderGiftVisitorDO> resultPage = orderGiftVisitorService.getGiftOrderPage(pageReqVO);
        return success(new PageResult<>(
            BeanUtils.toBean(resultPage.getRecords(), OrderGiftVisitorRespVO.class),
            resultPage.getTotal()
        ));
    }



    // ==================== 分车操作 ====================

    @PostMapping("/createDispatch")
    @Operation(summary = "创建蹭送行程分车（单个）")
    public CommonResult<Long> createGiftDispatch(@Valid @RequestBody OrderJzDispatchSaveReqVO createReqVO,
                                                 @RequestParam("visitorId") Long visitorId) {
        return success(orderGiftVisitorService.createGiftDispatch(createReqVO, visitorId));
    }

    @PostMapping("/batch-create")
    @Operation(summary = "批量保存蹭送行程分车")
    public CommonResult<Integer> batchSaveGiftDispatch(@Valid @RequestBody OrderJzDispatchBatchReqVO batchReqVO) {
        return success(orderGiftVisitorService.batchSaveGiftDispatch(batchReqVO));
    }

    @PutMapping("/updateDispatch")
    @Operation(summary = "更新蹭送行程分车")
    public CommonResult<Boolean> updateGiftDispatch(@Valid @RequestBody OrderJzDispatchSaveReqVO updateReqVO) {
        orderGiftVisitorService.updateGiftDispatch(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/deleteDispatch")
    @Operation(summary = "删除蹭送行程分车（根据游客ID）")
    @Parameter(name = "id", description = "ID", required = true)
    public CommonResult<Boolean> deleteGiftDispatch(@RequestParam("id") Long id) {
        orderGiftVisitorService.deleteGiftDispatchByVisitorId(id);
        return success(true);
    }

    // ==================== 分车记录查询 ====================

    @GetMapping("/record/page")
    @Operation(summary = "获得蹭送行程分车记录分页")
    public CommonResult<PageResult<OrderJzDispatchRecordRespVO>> getGiftDispatchRecordPage(@Valid OrderJzDispatchPageReqVO pageReqVO) {
        return success(orderGiftVisitorService.getGiftDispatchRecordPage(pageReqVO));
    }

    @GetMapping("/record/export-excel")
    @Operation(summary = "导出蹭送行程分车记录 Excel")
    public void exportGiftDispatchExcel(@Valid OrderJzDispatchPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        List<OrderJzDispatchRecordRespVO> list = orderGiftVisitorService.getExportGiftDispatchList(pageReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "蹭送行程分车记录.xls", "数据", OrderJzDispatchRecordRespVO.class, list);
    }
}
