package com.bonus.material.lease.controller;

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.core.web.page.TableDataInfo;
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.lease.domain.LeaseApplyInfo;
import com.bonus.material.lease.domain.vo.LeaseApplyRequestVo;
import com.bonus.material.lease.domain.vo.LeaseOutRequestVo;
import com.bonus.material.lease.service.ILeaseApplyInfoService;
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;

/**
 * 领料任务Controller
 * 
 * @author xsheng
 * @date 2024-10-16
 */
@Api(tags = "领料任务接口")
@RestController
@RequestMapping("/lease_apply_info")
public class LeaseApplyInfoController extends BaseController {

    @Resource
    private ILeaseApplyInfoService leaseApplyInfoService;

    /**
     * 查询领料任务列表
     */
    @ApiOperation(value = "查询领料任务列表")
    //@RequiresPermissions("lease:info:list")
    @GetMapping("/list")
    public TableDataInfo list(LeaseApplyInfo leaseApplyInfo) {
        startPage();
        List<LeaseApplyInfo> list = leaseApplyInfoService.selectLeaseApplyInfoList(leaseApplyInfo);
        return getDataTable(list);
    }

    /**
     * 导出领料任务列表
     */
    @ApiOperation(value = "导出领料任务列表")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:export")
    @SysLog(title = "领料任务", businessType = OperaType.EXPORT, logType = 1,module = "仓储管理->导出领料任务")
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaseApplyInfo leaseApplyInfo) {
        List<LeaseApplyInfo> list = leaseApplyInfoService.selectLeaseApplyInfoList(leaseApplyInfo);
        ExcelUtil<LeaseApplyInfo> util = new ExcelUtil<>(LeaseApplyInfo.class);
        util.exportExcel(response, list, "领料任务数据");
    }

    /**
     * 获取领料任务详细信息
     */
    @ApiOperation(value = "获取领料任务详细信息")
    //@RequiresPermissions("lease:info:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@NotNull(message = "领料任务ID不能为空") @PathVariable("id")  Long id) {
        return success(leaseApplyInfoService.selectLeaseApplyInfoById(id));
    }

    /**
     * 新增领料任务
     */
    @ApiOperation(value = "新增领料任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:add")
    @SysLog(title = "领料任务", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->新增领料任务")
    @PostMapping
    public AjaxResult add(@NotNull(message = "领料任务不能为空") @RequestBody LeaseApplyRequestVo leaseApplyRequestVo) {
        try {
            return leaseApplyInfoService.insertLeaseApplyInfo(leaseApplyRequestVo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 修改领料任务
     */
    @ApiOperation(value = "修改领料任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:edit")
    @SysLog(title = "领料任务", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->修改领料任务")
    @PutMapping
    public AjaxResult edit(@RequestBody @NotNull LeaseApplyRequestVo leaseApplyRequestVo) {
        try {
            return toAjax(leaseApplyInfoService.updateLeaseApplyInfo(leaseApplyRequestVo));
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }


    /**
     * 领料任务发布
     */
    @ApiOperation(value = "领料任务发布")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务发布", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->发布领料任务")
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody @NotNull(message = "任务信息不能为空") LeaseApplyInfo leaseApplyInfo) {
        return leaseApplyInfoService.publish(leaseApplyInfo);
    }

    /**
     * 领料任务批量发布
     */
    @ApiOperation(value = "领料任务批量发布")
    @PreventRepeatSubmit
    @SysLog(title = "领料任务批量发布", businessType = OperaType.UPDATE, logType = 1,module = "仓储管理->批量发布领料任务")
    @PostMapping("/publishBatch")
    public AjaxResult publishBatch(@RequestBody @NotNull(message = "任务信息不能为空") List<LeaseApplyInfo> leaseApplyInfos) {
        for (LeaseApplyInfo leaseApplyInfo : leaseApplyInfos) {
            leaseApplyInfoService.publish(leaseApplyInfo);
        }
        return success("批量发布完成");
    }


    /**
     * 领料：出库
     */
    @ApiOperation(value = "领料出库")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:add")
    @SysLog(title = "领料出库", businessType = OperaType.INSERT, logType = 1,module = "仓储管理->领料出库")
    @PostMapping("/leaseOut")
    public AjaxResult leaseOut(@NotNull(message = "领料出库信息不能为空") @RequestBody LeaseOutRequestVo leaseOutRequestVo) {
        try {
            return leaseApplyInfoService.leaseOut(leaseOutRequestVo);
        } catch (Exception e) {
            return error("系统错误, " + e.getMessage());
        }
    }

    /**
     * 删除领料任务
     */
    @ApiOperation(value = "删除领料任务")
    @PreventRepeatSubmit
    //@RequiresPermissions("lease:info:remove")
    @SysLog(title = "领料任务", businessType = OperaType.DELETE, logType = 1,module = "仓储管理->删除领料任务")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(leaseApplyInfoService.deleteLeaseApplyInfoByIds(ids));
    }
}
