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.common.log.annotation.SysLog;
import com.bonus.common.log.enums.OperaType;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import com.bonus.material.part.domain.*;
import com.bonus.material.part.mapper.PartArrivedMapper;
import com.bonus.material.part.service.IPartArrivedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
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;

/**
 * 配件新购管理Controller
 * 
 * @author hongchao
 * @date 2025-01-08
 */
@Api(tags = "配件新购管理接口")
@RestController
@RequestMapping("/part_arrived")
public class partArrivedController extends BaseController {

    @Autowired
    private IPartArrivedService partArrivedService;

    @Autowired
    private PartArrivedMapper partArrivedMapper;

    /**
     * 获取配件新购类型树结构
     */
    @ApiOperation(value = "获取配件新购类型树结构")
//    @RequiresPermissions("scrap:details:list")
    @GetMapping("/typeTree")
    public AjaxResult getTypeTree(@RequestParam(required = false) Long partId, @RequestParam(required = false) String partName) {
        List<PartTypeDto> listByMaType = partArrivedService.getTypeTree(partId, partName);
        return success(listByMaType);
    }

    /**
     * 新增新购验收任务
     */
    @ApiOperation("新增新购验收任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:add")
    @SysLog(title = "新购验收任务", businessType = OperaType.INSERT, module = "配件新购->新增新购验收任务")
    @PostMapping
    public AjaxResult add(@Valid @NotNull @RequestBody PartTypeCheckDto partTypeCheckDto) {
        try {
            return partArrivedService.insertPartTypeCheckInfo(partTypeCheckDto);
        } catch (final Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 查询新购验收任务列表
     */
    @ApiOperation("查询新购验收任务列表")
    //@RequiresPermissions("purchase:info:list")
    @GetMapping("/list")
    public AjaxResult list(PartTypeQueryDto partTypeQueryDto) {
        final List<PartTypeCheckInfo> list = partArrivedService.selectPartTypeCheckInfoList(partTypeQueryDto);
        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));
    }

    /**
     * 获取新购验收任务详细信息
     */
    @ApiOperation("获取新购验收任务详细信息")
    //@RequiresPermissions("purchase:info:query")
    @GetMapping(value = "/getInfo")
    public AjaxResult getInfo(@NotNull(message = "请求对象不能为空") PartTypeQueryDto partTypeQueryDto) {
        return success(partArrivedService.selectPartTypeCheckInfoById(partTypeQueryDto));
    }

    /**
     * 修改新购验收任务
     */
    @ApiOperation("修改新购验收任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:edit")
    @SysLog(title = "新购验收任务", businessType = OperaType.UPDATE, module = "配件新购->修改新购验收任务")
    @PutMapping
    public AjaxResult edit(@RequestBody @NotNull(message = "请求参数不能为空") @Valid PartTypeCheckDto partTypeCheckDto) {
        try {
            return toAjax(partArrivedService.updatePartTypeCheckInfo(partTypeCheckDto));
        } catch (final Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 导出配件新购任务列表
     */
    @ApiOperation("导出配件新购任务列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:export")
    @SysLog(title = "配件新购任务", businessType = OperaType.EXPORT, module = "配件新购->导出配件新购任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PartTypeQueryDto partTypeQueryDto) {
        List<PartTypeCheckInfo> list = partArrivedService.selectPartTypeCheckInfoList(partTypeQueryDto);
        ExcelUtil<PartTypeCheckInfo> util = new ExcelUtil<>(PartTypeCheckInfo.class);
        util.exportExcel(response, list, "配件新购任务数据");
    }

    /**
     * 导出配件新购任务详细列表
     */
    @ApiOperation("导出配件新购任务详细列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("purchase:info:export")
    @SysLog(title = "配件新购任务详细", businessType = OperaType.EXPORT, logType = 1,module = "配件新购->导出配件新购任务详细")
    @PostMapping("/exportDetails")
    public void exportDetails(HttpServletResponse response, PartTypeQueryDto partTypeQueryDto) {
        List<PartTypeCheckDetails> list = partArrivedMapper.selectPartTypeCheckDetailsListByQueryDto(partTypeQueryDto);
        ExcelUtil<PartTypeCheckDetails> util = new ExcelUtil<>(PartTypeCheckDetails.class);
        util.exportExcel(response, list, "配件新购任务详细数据");
    }

    /**
     * 删除配件新购任务
     */
    @ApiOperation("删除配件新购任务")
    //@RequiresPermissions("purchase:info:remove")
    @PostMapping("/delete")
    public AjaxResult deletePartInfo(@RequestBody PartTypeQueryDto partTypeQueryDto) {
        return toAjax(partArrivedService.deletePartInfo(partTypeQueryDto));
    }

    @ApiOperation(value = "查询附件列表")
//    @RequiresPermissions("basic:file:manage")
    @GetMapping("/getPartFilelist")
    public AjaxResult getPartFilelist(BmPartFileInfo bmPartFileInfo)
    {
        List<BmPartFileInfo> list = partArrivedService.getPartFilelist(bmPartFileInfo);
        return AjaxResult.success(list);
    }

    /**
     * 根据任务id查询入库单
     */
    @ApiOperation("根据任务id查询入库单")
    //@RequiresPermissions("purchase:info:query")
    @GetMapping(value = "/getInBoundForm")
    public AjaxResult getInBoundForm( PartTypeQueryDto partTypeQueryDto) {
        return success(partArrivedService.getInBoundForm(partTypeQueryDto));
    }
}
