package com.bonus.material.purchase.controller;

import cn.hutool.core.convert.Convert;
import com.bonus.common.biz.config.ListPagingUtil;
import com.bonus.common.core.utils.ServletUtils;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.common.biz.domain.purchase.PurchaseDto;
import com.bonus.material.purchase.domain.PurchaseCheckInfo;
import com.bonus.material.purchase.domain.dto.PurchaseQueryDto;
import com.bonus.material.purchase.service.IPurchaseBindService;
import com.bonus.material.purchase.domain.vo.PurchaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.util.List;

import static com.bonus.common.core.web.page.TableSupport.PAGE_NUM;
import static com.bonus.common.core.web.page.TableSupport.PAGE_SIZE;

/**
 * 新购验收绑定接口
 * @Author ma_sh
 * @create 2024/10/21 18:41
 */
@Api(tags = "新购验收绑定接口")
@RestController
@RequestMapping("/purchase/bind")
public class PurchaseBindController extends BaseController {

    @Resource
    private IPurchaseBindService purchaseBindService;

    /**
     * 查询新购验收绑定任务列表
     * @param purchaseQueryDto
     * @return
     */
    @ApiOperation("查询新购验收绑定任务列表")
    @GetMapping("/list")
    public AjaxResult list(PurchaseQueryDto purchaseQueryDto) {
        final List<PurchaseCheckInfo> list = purchaseBindService.selectPurchaseCheckInfoList(purchaseQueryDto);
        final Integer pageIndex = Convert.toInt(ServletUtils.getParameter(PAGE_NUM), 1);
        final Integer pageSize = Convert.toInt(ServletUtils.getParameter(PAGE_SIZE), 10);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 获取新购验收任务详细信息
     * @param purchaseQueryDto
     * @return
     */
    @ApiOperation("获取新购验收任务详细信息")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@NotNull(message = "请求对象不能为空") PurchaseQueryDto purchaseQueryDto) {
        return success(purchaseBindService.getSelectPurchaseCheckInfoById(purchaseQueryDto));
    }

    /**
     * 绑定
     * @param dto
     * @return
     */
    @ApiOperation(value = "编码绑定")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:bind:add")
    @PostMapping("/bind")
    public AjaxResult bind(@RequestBody @NotNull(message = "参数不能为空") PurchaseDto dto) {
        return purchaseBindService.bind(dto);
    }

    /**
     * 盘点绑定
     * @param dto
     * @return
     */
    @ApiOperation(value = "盘点绑定")
    @PreventRepeatSubmit
//    @RequiresPermissions("purchase:bind:add")
    @PostMapping("/inventoryBind")
    public AjaxResult inventoryBind(@RequestBody @NotNull(message = "参数不能为空") PurchaseVo dto) {
        return purchaseBindService.inventoryBind(dto);
    }

    /**
     * 获取盘点绑定绑定扫描后详细信息
     */
    @ApiOperation(value = "获取盘点绑定绑定扫描后详细信息")
//    @RequiresPermissions("purchase:bind:query")
    @GetMapping(value = "/getInventoryInfo")
    public AjaxResult getInventoryInfo(PurchaseDto dto) {
        return purchaseBindService.getInventoryInfo(dto);
    }


    /**
     * 获取新购验收绑定详细信息
     */
    @ApiOperation(value = "获取新购验收绑定详细信息")
    @RequiresPermissions("purchase:bind:query")
    @GetMapping(value = "/getById")
    public AjaxResult getInfo(PurchaseDto dto) {
        return purchaseBindService.selectPurchaseCheckInfoById(dto);
    }

    /**
     * 二维码生成下载一体操作
     * @param response
     * @param purchaseDto
     */
    @ApiOperation(value = "二维码生成下载")
    @PostMapping(value = "/downloadQrCode")
    //@PreventRepeatSubmit
    //@RequiresPermissions("purchase:bind:download")
    public void downloadQrCode(HttpServletResponse response, PurchaseDto purchaseDto) {
        purchaseBindService.downloadQrCode(response, purchaseDto);
    }

    /**
     * 驳回或批量驳回
     * @param dto
     * @return
     */
    @ApiOperation(value = "驳回或批量驳回操作")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:bind:reject")
    @PostMapping("/reject")
    public AjaxResult reject(@RequestBody @NotNull(message = "参数不能为空") PurchaseDto dto) {
        return purchaseBindService.reject(dto);
    }

    /**
     * 导出新购验收绑定详细列表
     */
    @ApiOperation(value = "导出新购验收绑定详细列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:bind:export")
    @SysLog(title = "新购验收绑定任务", businessType = OperaType.EXPORT, module = "仓储管理->导出新购验收绑定任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PurchaseDto dto) {
        List<PurchaseVo> list = purchaseBindService.selectAll(dto);
        ExcelUtil<PurchaseVo> util = new ExcelUtil<PurchaseVo>(PurchaseVo.class);
        util.exportExcel(response, list, "新购验收绑定任务数据");
    }


    /**
     * 获取新购验收绑定详细信息
     */
    @ApiOperation(value = "获取新购验收绑定详细信息")
  //  @RequiresPermissions("purchase:bind:qrcode")
    @GetMapping(value = "/getTypeByQrcode")
    public AjaxResult getTypeByQrcode(PurchaseDto dto) {
        return purchaseBindService.getTypeByQrcode(dto);
    }
}
