/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.bizorder.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.biz.modular.customer.entity.BizCustomer;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.bizorder.entity.BizOrder;
import vip.xiaonuo.biz.modular.bizorder.param.BizOrderAddParam;
import vip.xiaonuo.biz.modular.bizorder.param.BizOrderEditParam;
import vip.xiaonuo.biz.modular.bizorder.param.BizOrderIdParam;
import vip.xiaonuo.biz.modular.bizorder.param.BizOrderPageParam;
import vip.xiaonuo.biz.modular.bizorder.service.BizOrderService;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;

import java.io.IOException;
import java.util.List;

/**
 * 订单管理控制器
 *
 * @author fanzherong
 * @date  2025/03/21 17:16
 */
@Tag(name = "订单管理控制器")
@RestController
@Validated
public class BizOrderController {

    @Resource
    private BizOrderService bizOrderService;

    /**
     * 获取订单管理分页
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "获取订单管理分页")
    @SaCheckPermission("/biz/bizorder/page")
    @GetMapping("/biz/bizorder/page")
    public CommonResult<Page<BizOrder>> page(BizOrderPageParam bizOrderPageParam) {
        return CommonResult.data(bizOrderService.page(bizOrderPageParam));
    }

    /**
     * 添加订单管理
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "添加订单管理")
    @CommonLog("添加订单管理")
    @PostMapping("/biz/bizorder/add")
    public CommonResult<String> add(@RequestBody @Valid BizOrderAddParam bizOrderAddParam) {
        bizOrderService.add(bizOrderAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑订单管理
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "编辑订单管理")
    @CommonLog("编辑订单管理")
    @PostMapping("/biz/bizorder/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizOrderEditParam bizOrderEditParam) {
        bizOrderService.edit(bizOrderEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除订单管理
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "删除订单管理")
    @CommonLog("删除订单管理")
    @PostMapping("/biz/bizorder/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<BizOrderIdParam> bizOrderIdParamList) {
        bizOrderService.delete(bizOrderIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取订单管理详情
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "获取订单管理详情")
    @GetMapping("/biz/bizorder/detail")
    public CommonResult<BizOrder> detail(@Valid BizOrderIdParam bizOrderIdParam) {
        return CommonResult.data(bizOrderService.detail(bizOrderIdParam));
    }

    /**
     * 获取客户信息
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "获取客户信息")
    @GetMapping("/biz/bizorder/getCustomerById")
    public CommonResult<List<BizCustomer>> getCustomerById(@Valid BizOrderIdParam bizOrderIdParam) {
        return CommonResult.data(bizOrderService.getCustomerById(bizOrderIdParam));
    }

    /**
     * 绑定客户ID
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "绑定客户ID")
    @CommonLog("绑定客户ID")
    @PostMapping("/biz/bizorder/bindCustomerId")
    public CommonResult<String> bindCustomerId(@RequestBody @Valid BizOrderEditParam bizOrderEditParam) {
        bizOrderService.bindCustomerId(bizOrderEditParam);
        return CommonResult.ok();
    }

    /**
     * 导出订单报表
     */
    @Operation(summary = "导出订单报表")
    @GetMapping(value = "/biz/bizorder/exportRecord", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportRecord(BizOrderPageParam bizOrderPageParam, HttpServletResponse response) throws IOException {
        bizOrderService.exportRecord(bizOrderPageParam,response);
    }


    /**
     * 结束订单
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "结束订单")
    @CommonLog("结束订单")
    @PostMapping("/biz/bizorder/endOrder")
    public CommonResult<String> endOrder(@RequestBody @Valid BizOrderIdParam bizOrderIdParam) {
        bizOrderService.endOrder(bizOrderIdParam);
        return CommonResult.ok();
    }

    /**
     * 订单列表
     *
     * @author fanzherong
     * @date  2025/03/21 17:16
     */
    @Operation(summary = "订单列表")
    @CommonLog("订单列表")
    @GetMapping("/biz/bizorder/getList")
    public CommonResult<List<BizOrder>> getList() {
        return CommonResult.data(bizOrderService.getList());
    }
}
