/*
 * 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.record.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.record.param.*;
import vip.xiaonuo.biz.modular.record.result.CarResult;
import vip.xiaonuo.biz.modular.record.result.CompanyResult;
import vip.xiaonuo.biz.modular.record.result.GoodsResult;
import vip.xiaonuo.common.annotation.CommonLog;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.biz.modular.record.entity.BizRecord;
import vip.xiaonuo.biz.modular.record.service.BizRecordService;

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

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

/**
 * 过磅记录控制器
 *
 * @author 徐超
 * @date 2025/01/02 16:34
 */
@Tag(name = "过磅记录控制器")
@RestController
@Validated
public class BizRecordController {

    @Resource
    private BizRecordService bizRecordService;

    /**
     * 获取过磅记录分页
     *
     * @author 徐超
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "获取过磅记录分页")
    @SaCheckPermission("/biz/record/page")
    @GetMapping("/biz/record/page")
    public CommonResult<Page<BizRecord>> page(BizRecordPageParam bizRecordPageParam) {
        return CommonResult.data(bizRecordService.page(bizRecordPageParam));
    }

    /**
     * 添加过磅记录
     *
     * @author 徐超
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "添加过磅记录")
    @CommonLog("添加过磅记录")
    @SaCheckPermission("/biz/record/add")
    @PostMapping("/biz/record/add")
    public CommonResult<String> add(@RequestBody @Valid BizRecordAddParam bizRecordAddParam) {
        bizRecordService.add(bizRecordAddParam);
        return CommonResult.ok();
    }

    /**
     * 地磅端推送过磅记录
     *
     * @author
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "地磅端推送过磅记录")
    @CommonLog("地磅端推送过磅记录")
    @PostMapping("/biz/record/push")
    public CommonResult<String> push(@RequestBody @Valid BizRecordEditParam bizRecordEditParam) {
        bizRecordService.push(bizRecordEditParam);
        return CommonResult.ok();
    }

    /**
     * 编辑过磅记录
     *
     * @author 徐超
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "编辑过磅记录")
    @CommonLog("编辑过磅记录")
    @PostMapping("/biz/record/edit")
    public CommonResult<String> edit(@RequestBody @Valid BizRecordEditParam bizRecordEditParam) {
        bizRecordService.edit(bizRecordEditParam);
        return CommonResult.ok();
    }

    /**
     * 删除过磅记录
     *
     * @author 徐超
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "删除过磅记录")
    @CommonLog("删除过磅记录")
    @SaCheckPermission("/biz/record/delete")
    @PostMapping("/biz/record/delete")
    public CommonResult<String> delete(@RequestBody @Valid @NotEmpty(message = "集合不能为空")
                                       List<BizRecordIdParam> bizRecordIdParamList) {
        bizRecordService.delete(bizRecordIdParamList);
        return CommonResult.ok();
    }

    /**
     * 获取过磅记录详情
     *
     * @author 徐超
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "获取过磅记录详情")
    @SaCheckPermission("/biz/record/detail")
    @GetMapping("/biz/record/detail")
    public CommonResult<BizRecord> detail(@Valid BizRecordIdParam bizRecordIdParam) {
        return CommonResult.data(bizRecordService.detail(bizRecordIdParam));
    }

    /**
     * 导出
     */
    @Operation(summary = "过磅记录导出")
    @SaCheckPermission("/biz/record/export")
    @PostMapping(value = "/biz/record/export", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void export(@RequestBody BizRecordPageParam exportParam, HttpServletResponse response) throws IOException {
        bizRecordService.export(exportParam, response);
    }

    /**
     * 获取过磅记录统计图表
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取过磅记录统计图表（近七日）")
    @GetMapping("/biz/record/sevenDaysList")
    public CommonResult<List<Map<String, Object>>> sevenDaysList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.sevenDaysList(sevenDaysCountParam));
    }

    /**
     * 获取过磅记录统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取过磅记录统计")
    @GetMapping("/biz/record/sevenDaysCountWeight")
    public CommonResult<Map<String, Object>> sevenDaysCountWeight(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.sevenDaysCountWeight(sevenDaysCountParam));
    }

    /**
     * 获取收货单位统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取收货单位统计")
    @GetMapping("/biz/record/receiptCompanyCountList")
    public CommonResult<List<CompanyResult>> receiptCompanyCountList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.receiptCompanyCountList(sevenDaysCountParam));
    }

    /**
     * 收货单位统计导出
     * @param sevenDaysCountParam
     * @param response
     * @throws IOException
     */
    @Operation(summary = "收货单位统计导出")
    @PostMapping(value = "/biz/record/receiptCompanyExport", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void receiptCompanyExport(@RequestBody SevenDaysCountParam sevenDaysCountParam, HttpServletResponse response) throws IOException {
        bizRecordService.receiptCompanyExport(sevenDaysCountParam, response);
    }

    /**
     * 获取发货单位统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取发货单位统计")
    @GetMapping("/biz/record/shippingCompanyCountList")
    public CommonResult<List<CompanyResult>> shippingCompanyCountList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.shippingCompanyCountList(sevenDaysCountParam));
    }

    /**
     * 发货单位统计导出
     * @param sevenDaysCountParam
     * @param response
     * @throws IOException
     */
    @Operation(summary = "发货单位统计导出")
    @PostMapping(value = "/biz/record/shippingCompanyExport", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void shippingCompanyExport(@RequestBody SevenDaysCountParam sevenDaysCountParam, HttpServletResponse response) throws IOException {
        bizRecordService.shippingCompanyExport(sevenDaysCountParam, response);
    }

    /**
     * 获取运输单位统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取运输单位统计")
    @GetMapping("/biz/record/transportCompanyCountList")
    public CommonResult<List<CompanyResult>> transportCompanyCountList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.transportCompanyCountList(sevenDaysCountParam));
    }

    /**
     * 运输单位统计导出
     * @param sevenDaysCountParam
     * @param response
     * @throws IOException
     */
    @Operation(summary = "运输单位统计导出")
    @PostMapping(value = "/biz/record/transportCompanyExport", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void transportCompanyExport(@RequestBody SevenDaysCountParam sevenDaysCountParam, HttpServletResponse response) throws IOException {
        bizRecordService.transportCompanyExport(sevenDaysCountParam, response);
    }


    /**
     * 获取货品统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取获取货品统计")
    @GetMapping("/biz/record/goodsCountList")
    public CommonResult<List<GoodsResult>> goodsCountList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.goodsCountList(sevenDaysCountParam));
    }

    /**
     * 货品统计导出
     * @param sevenDaysCountParam
     * @param response
     * @throws IOException
     */
    @Operation(summary = "运输单位统计导出")
    @PostMapping(value = "/biz/record/goodsCountExport", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void goodsCountExport(@RequestBody SevenDaysCountParam sevenDaysCountParam, HttpServletResponse response) throws IOException {
        bizRecordService.goodsCountExport(sevenDaysCountParam, response);
    }


    /**
     * 获取车辆统计
     * @param sevenDaysCountParam
     * @return
     */
    @Operation(summary = "获取车辆统计")
    @GetMapping("/biz/record/carCountList")
    public CommonResult<List<CarResult>> carCountList(SevenDaysCountParam sevenDaysCountParam) {
        return CommonResult.data(bizRecordService.carCountList(sevenDaysCountParam));
    }


    /**
     * 车辆统计导出
     * @param sevenDaysCountParam
     * @param response
     * @throws IOException
     */
    @Operation(summary = "车辆统计导出")
    @PostMapping(value = "/biz/record/carCountExport", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
    public void carCountExport(@RequestBody SevenDaysCountParam sevenDaysCountParam, HttpServletResponse response) throws IOException {
        bizRecordService.carCountExport(sevenDaysCountParam, response);
    }

    /**
     * 发货确认
     *
     * @author
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "发货确认")
    @PostMapping("/biz/record/confirmRecord")
    public CommonResult<BizRecord> confirmRecord(@RequestBody @Valid BizRecordIdParam bizRecordIdParam) {
        bizRecordService.confirmRecord(bizRecordIdParam);
        return CommonResult.ok();
    }

    /**
     * 二次过磅司机签名确认
     *
     * @author
     * @date 2025/01/02 16:34
     */
    @Operation(summary = "二次过磅司机签名确认")
    @CommonLog("二次过磅司机签名确认")
    @PostMapping("/biz/record/updateDriverSign")
    public CommonResult<String> updateDriverSign(@RequestBody @Valid BizRecordEditParam bizRecordEditParam) {
        bizRecordService.updateDriverSign(bizRecordEditParam);
        return CommonResult.ok();
    }

}
