/*
 * 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.bizappointmentrecord.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.apache.commons.lang3.StringUtils;
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.bizorder.param.BizOrderPageParam;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.annotation.CommonNoRepeat;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.bizappointmentrecord.entity.BizAppointmentRecord;
import vip.xiaonuo.biz.modular.bizappointmentrecord.param.BizAppointmentRecordAddParam;
import vip.xiaonuo.biz.modular.bizappointmentrecord.param.BizAppointmentRecordEditParam;
import vip.xiaonuo.biz.modular.bizappointmentrecord.param.BizAppointmentRecordIdParam;
import vip.xiaonuo.biz.modular.bizappointmentrecord.param.BizAppointmentRecordPageParam;
import vip.xiaonuo.biz.modular.bizappointmentrecord.service.BizAppointmentRecordService;

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/24 14:47
 */
@Tag(name = "预约记录控制器")
@RestController
@Validated
public class BizAppointmentRecordController {

    @Resource
    private BizAppointmentRecordService bizAppointmentRecordService;

    /**
     * 获取预约记录分页
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "获取预约记录分页")
    @SaCheckPermission("/biz/bizappointmentrecord/page")
    @GetMapping("/biz/bizappointmentrecord/page")
    public CommonResult<Page<BizAppointmentRecord>> page(BizAppointmentRecordPageParam bizAppointmentRecordPageParam) {
        return CommonResult.data(bizAppointmentRecordService.page(bizAppointmentRecordPageParam));
    }

    /**
     * 添加预约记录
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "添加预约记录")
    @CommonLog("添加预约记录")
    @CommonNoRepeat
    @PostMapping("/biz/bizappointmentrecord/add")
    public CommonResult<String> add(@RequestBody @Valid BizAppointmentRecordAddParam bizAppointmentRecordAddParam) {
        bizAppointmentRecordService.add(bizAppointmentRecordAddParam);
        return CommonResult.ok();
    }

    /**
     * 编辑预约记录
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "编辑预约记录")
    @CommonLog("编辑预约记录")
    @PostMapping("/biz/bizappointmentrecord/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizAppointmentRecordEditParam bizAppointmentRecordEditParam) {
        bizAppointmentRecordService.edit(bizAppointmentRecordEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除预约记录
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "删除预约记录")
    @CommonLog("删除预约记录")
    @PostMapping("/biz/bizappointmentrecord/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                                   List<BizAppointmentRecordIdParam> bizAppointmentRecordIdParamList) {
        bizAppointmentRecordService.delete(bizAppointmentRecordIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取预约记录详情
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "获取预约记录详情")
    @GetMapping("/biz/bizappointmentrecord/detail")
    public CommonResult<BizAppointmentRecord> detail(@Valid BizAppointmentRecordIdParam bizAppointmentRecordIdParam) {
        return CommonResult.data(bizAppointmentRecordService.detail(bizAppointmentRecordIdParam));
    }


    /**
     * 审核
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "审核")
    @CommonLog("审核")
    @PostMapping("/biz/bizappointmentrecord/auditRecord")
    public CommonResult<String> auditRecord(@RequestBody @Valid BizAppointmentRecordEditParam bizAppointmentRecordEditParam) {
        bizAppointmentRecordService.auditRecord(bizAppointmentRecordEditParam);
        return CommonResult.ok();
    }


    /**
     * 导出预约报表
     */
    @Operation(summary = "导出预约报表")
    @GetMapping(value = "/biz/bizappointmentrecord/exportRecord", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void exportRecord(BizAppointmentRecordPageParam bizAppointmentRecordPageParam, HttpServletResponse response) throws IOException {
        bizAppointmentRecordService.exportRecord(bizAppointmentRecordPageParam,response);
    }

    /**
     * 地磅端校验是否有预约
     */
    @Operation(summary = "地磅端校验是否有预约")
    @GetMapping(value = "/biz/bizappointmentrecord/checkAppointmentRecord")
    public CommonResult<BizAppointmentRecord> checkAppointmentRecord(BizAppointmentRecordPageParam bizAppointmentRecordPageParam) {
        BizAppointmentRecord bizAppointmentRecord = bizAppointmentRecordService.checkAppointmentRecord(bizAppointmentRecordPageParam);
        if(StringUtils.equals(bizAppointmentRecord.getIsFlag(),"4")){
            return CommonResult.get(201,"没有待入场记录，不可过门禁！",null);
        }else if(StringUtils.equals(bizAppointmentRecord.getIsFlag(),"5")){
            return CommonResult.get(202,"没有已入场记录，不可一次过磅！",null);
        }else if(StringUtils.equals(bizAppointmentRecord.getIsFlag(),"7")){
            return CommonResult.get(203,"没有完成装货记录，不可二次过磅！",null);
        }else if(StringUtils.equals(bizAppointmentRecord.getIsFlag(),"9")){
            return CommonResult.get(204,"没有待出场记录，不可出门禁！",null);
        }
        return CommonResult.get(200,"操作成功",bizAppointmentRecord);
    }

    /**
     * 取消排队
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "取消排队")
    @CommonLog("取消排队")
    @PostMapping("/biz/bizappointmentrecord/cancelAppointmentRecord")
    public CommonResult<String> cancelAppointmentRecord(@RequestBody @Valid BizAppointmentRecordIdParam bizAppointmentRecordIdParam) {
        bizAppointmentRecordService.cancelAppointmentRecord(bizAppointmentRecordIdParam);
        return CommonResult.ok();
    }

    /**
     * 调整订单
     *
     * @author fanzherong
     * @date  2025/03/24 14:47
     */
    @Operation(summary = "调整订单")
    @CommonLog("调整订单")
    @PostMapping("/biz/bizappointmentrecord/adjustOrder")
    public CommonResult<String> adjustOrder(@RequestBody @Valid BizAppointmentRecordEditParam bizAppointmentRecordEditParam) {
        bizAppointmentRecordService.adjustOrder(bizAppointmentRecordEditParam);
        return CommonResult.ok();
    }
}
