package com.bonus.material.back.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.bonus.common.log.enums.OperaType;
import com.bonus.material.back.domain.vo.BackApplyRequestVo;
import com.bonus.material.common.annotation.PreventRepeatSubmit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bonus.common.log.annotation.SysLog;
import com.bonus.common.security.annotation.RequiresPermissions;
import com.bonus.material.back.domain.BackApplyInfo;
import com.bonus.material.back.service.IBackApplyInfoService;
import com.bonus.common.core.web.controller.BaseController;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.core.utils.poi.ExcelUtil;
import com.bonus.common.core.web.page.TableDataInfo;

/**
 * 退料任务Controller
 * 
 * @author xsheng
 * @date 2024-10-16
 */
@Api(tags = "退料任务接口")
@RestController
@RequestMapping("/back_apply_info")
public class BackApplyInfoController extends BaseController {
    @Autowired
    private IBackApplyInfoService backApplyInfoService;

    /**
     * 查询退料任务列表
     */
    @ApiOperation(value = "查询退料任务列表")
    @RequiresPermissions("back:info:list")
    @GetMapping("/list")
    public TableDataInfo list(BackApplyInfo backApplyInfo) {
        backApplyInfo.setIsExport(false);
        startPage();
        List<BackApplyInfo> list = backApplyInfoService.selectBackApplyInfoList(backApplyInfo);
        return getDataTable(list);
    }


    @ApiOperation(value = "根据单位和工程id查询领料机具")
    @GetMapping("/getMachineById")
    public AjaxResult getMachineById(BackApplyInfo dto){
        return backApplyInfoService.getMachineById(dto);
    }

    /**
     * 导出退料任务列表
     */
    @ApiOperation(value = "导出退料任务列表")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:export")
    @SysLog(title = "退料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出退料任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, BackApplyInfo backApplyInfo) {
        backApplyInfo.setIsExport(true);
        List<BackApplyInfo> list = backApplyInfoService.selectBackApplyInfoList(backApplyInfo);
        ExcelUtil<BackApplyInfo> util = new ExcelUtil<BackApplyInfo>(BackApplyInfo.class);
        util.exportExcel(response, list, "退料任务数据");
    }

    /**
     * 获取退料任务详细信息
     */
    @ApiOperation(value = "获取退料任务详细信息")
    @RequiresPermissions("back:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(backApplyInfoService.selectBackApplyInfoById(id));
    }

    /**
     * 新增退料任务
     */
    @ApiOperation(value = "新增退料任务")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:add")
    @SysLog(title = "退料任务", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增退料任务")
    @PostMapping
    public AjaxResult add(@Valid @RequestBody BackApplyRequestVo dto) {
        try {
            return backApplyInfoService.insertBackApplyInfo(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 新增退料任务app
     * @param dto
     * @return
     */
    @ApiOperation(value = "新增退料任务app")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:add")
    @SysLog(title = "退料任务", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增退料任务app")
    @PostMapping("/insertApp")
    public AjaxResult insertApp(@RequestBody BackApplyRequestVo dto) {
        try {
            return backApplyInfoService.insertApp(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改退料任务
     */
    @ApiOperation(value = "修改退料任务")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:edit")
    @SysLog(title = "退料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改退料任务")
    @PostMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody BackApplyRequestVo dto) {
        try {
            return backApplyInfoService.updateBackApplyInfo(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    @ApiOperation(value = "修改退料任务打印状态")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:edit")
    @SysLog(title = "退料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改退料任务")
    @PostMapping("/editPrintStatus")
    public AjaxResult editPrintStatus(@RequestBody BackApplyInfo dto) {
        try {
            return backApplyInfoService.editPrintStatus(dto);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 退料申请提交
     */
    @ApiOperation(value = "退料申请提交")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:submit")
    @SysLog(title = "退料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->退料申请提交")
    @PostMapping("/submitBackApply")
    public AjaxResult submitBackApply(@RequestBody BackApplyInfo backApplyInfo) {
        try {
            return backApplyInfoService.submitBackApply(backApplyInfo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除退料任务
     */
    @ApiOperation(value = "删除退料任务")
    @PreventRepeatSubmit
    @RequiresPermissions("back:info:remove")
    @SysLog(title = "退料任务", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除退料任务")
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return backApplyInfoService.deleteBackApplyInfoById(id);
    }
}
