package com.bonus.material.part.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.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.ma.domain.PartType;
import com.bonus.material.part.domain.MaPartTypeKeeper;
import com.bonus.material.part.domain.PartLeaseDetails;
import com.bonus.material.part.domain.PartLeaseInfo;
import com.bonus.material.part.service.PartLeaseService;
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;

/**
 * @author : 阮世耀
 * @version : 1.0
 * @PackagePath: com.bonus.material.lease.controller
 * @CreateTime: 2024-12-27  13:42
 * @Description: 描述
 */
@Api(tags = "配件领用接口")
@RestController
@RequestMapping("/part_lease")
public class PartLeaseController extends BaseController {

    @Resource
    private PartLeaseService partLeaseService;

    /**
     * 查询配件申请级别列表
     * @param partType
     * @return
     */
    @ApiOperation(value = "查询配件申请级别列表")
    @GetMapping("/selectPartTreeList")
    public AjaxResult selectPartTreeList(PartType partType) {
        return partLeaseService.selectPartTreeList(partType);
    }

    /**
     * 根据三级id查询一二级列表
     * @param partType
     * @return
     */
    @ApiOperation(value = "根据三级id查询一二级列表")
    @GetMapping("/selectPartTreeListByLevel")
    public AjaxResult selectPartTreeListByLevel(PartType partType) {
        return partLeaseService.selectPartTreeListByLevel(partType);
    }

    @ApiOperation(value = "查询配件领料任务列表")
    @GetMapping("/selectPartLeaseInfoList")
    public AjaxResult selectPartLeaseInfoList(PartLeaseInfo partLeaseInfo) {
        Integer pageIndex = Convert.toInt(ServletUtils.getParameter("pageNum"), 1);
        Integer pageSize = Convert.toInt(ServletUtils.getParameter("pageSize"), 10);
        List<PartLeaseInfo> list = partLeaseService.selectPartLeaseInfoList(partLeaseInfo);
        return AjaxResult.success(ListPagingUtil.paging(pageIndex, pageSize, list));
    }

    /**
     * 导出配件列表
     * @param response
     * @param partLeaseInfo
     */
    @ApiOperation("导出配件列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartLeaseInfo partLeaseInfo)
    {
        List<PartLeaseInfo> list = partLeaseService.selectPartLeaseInfoList(partLeaseInfo);
        ExcelUtil<PartLeaseInfo> util = new ExcelUtil<>(PartLeaseInfo.class);
        util.exportExcel(response, list, "导出配件列表");
    }

    @ApiOperation(value = "根据任务id查询配件领料明细")
    @GetMapping("/selectPartLeaseByTaskId")
    public AjaxResult selectPartLeaseByTaskId(PartLeaseDetails partDto) {
        return AjaxResult.success(partLeaseService.selectPartLeaseDetailsList(partDto));
    }

    @ApiOperation(value = "新增配件领料任务")
    @PostMapping
    public AjaxResult add(@NotNull(message = "领料任务不能为空") @RequestBody PartLeaseInfo partLeaseInfo) {
        return toAjax(partLeaseService.insertPartLeaseInfo(partLeaseInfo));
    }

    @ApiOperation(value = "审核领料任务")
    @PostMapping("/audit")
    public AjaxResult audit(@NotNull(message = "领料任务不能为空") @RequestBody PartLeaseInfo partLeaseInfo) {
        return partLeaseService.auditPartLeaseInfo(partLeaseInfo);
    }

    /**
     * 新增库管员配置
     */
    @ApiOperation(value = "新增配件库管员配置")
    @PreventRepeatSubmit
    @PostMapping("/add")
    public AjaxResult add(@RequestBody MaPartTypeKeeper typeKeepers) {
        try {
            return partLeaseService.insertTypeKeeper(typeKeepers);
        } catch (Exception e) {
            return error("绑定失败，请联系管理员");
        }
    }

    /**
     * 解绑配件库管员配置
     */
    @ApiOperation(value = "解绑配件库管员配置")
    @PreventRepeatSubmit
    @PostMapping("/unBind")
    public AjaxResult unBind(@RequestBody List<MaPartTypeKeeper> typeKeepers) {
        try {
            return partLeaseService.deleteTypeKeeper(typeKeepers);
        } catch (Exception e) {
            return error("解绑失败，请联系管理员");
        }
    }

    /**
     * 根据二级id查询三级列表
     * @param partType
     * @return
     */
    @ApiOperation(value = "根据二级id查询三级列表")
    @GetMapping("/selectPartList")
    public AjaxResult selectPartList(PartType partType) {
        return partLeaseService.selectPartList(partType);
    }

}
