package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.SysOaWarehouseTaskVo;
import com.ruoyi.oa.domain.bo.SysOaWarehouseTaskBo;
import com.ruoyi.oa.service.ISysOaWarehouseTaskService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 采购计划
 *
 * @author hdka
 * @date 2025-03-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/oaWarehouseTask")
public class SysOaWarehouseTaskController extends BaseController {

    private final ISysOaWarehouseTaskService iSysOaWarehouseTaskService;

    /**
     * 查询采购计划列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysOaWarehouseTaskVo> list(SysOaWarehouseTaskBo bo, PageQuery pageQuery) {
        return iSysOaWarehouseTaskService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出采购计划列表
     */
    @PostMapping("/export")
    public void export(SysOaWarehouseTaskBo bo, HttpServletResponse response) {
        List<SysOaWarehouseTaskVo> list = iSysOaWarehouseTaskService.queryList(bo);
        ExcelUtil.exportExcel(list, "采购计划", SysOaWarehouseTaskVo.class, response);
    }

    /**
     * 获取采购计划详细信息
     *
     * @param taskId 主键
     */
    @GetMapping("/{taskId}")
    public R<SysOaWarehouseTaskVo> getInfo(@NotNull(message = "主键不能为空")
                                           @PathVariable Long taskId) {
        return R.ok(iSysOaWarehouseTaskService.queryById(taskId));
    }

    /**
     * 获取采购计划详细信息
     *
     * @param masterId 主键
     */
    @GetMapping("/masterId/{masterId}")
    public R<List<SysOaWarehouseTaskVo>> getInfoByMasterId(@NotNull(message = "主键不能为空")
                                           @PathVariable Long masterId) {
        return R.ok(iSysOaWarehouseTaskService.queryByMasterId(masterId));
    }


    /**
     * 新增采购计划
     */
    @Log(title = "采购计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOaWarehouseTaskBo bo) {
        return toAjax(iSysOaWarehouseTaskService.insertByBo(bo));
    }



    /**
     * 新增采购计划
     */
    @Log(title = "采购计划", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/insert")
    public R<Void> addBatch(@RequestBody List<SysOaWarehouseTaskBo> boList) {
        return toAjax(iSysOaWarehouseTaskService.addBatch(boList));
    }

    /**
     * 修改采购计划
     */
    @Log(title = "采购计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOaWarehouseTaskBo bo) {
        return toAjax(iSysOaWarehouseTaskService.updateByBo(bo));
    }

    /**
     * 修改采购计划
     */
    @Log(title = "采购计划", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/editBatch")
    public R<Void> editBatch(@Validated(EditGroup.class) @RequestBody List<SysOaWarehouseTaskBo> bo) {
        return toAjax(iSysOaWarehouseTaskService.updateByBoBatch(bo));
    }


    /**
     * 处理单个物料入库
     */
    @RepeatSubmit()
    @PutMapping("/status")
    public R<Void> status(@Validated(EditGroup.class) @RequestBody SysOaWarehouseTaskBo bo) {
        return toAjax(iSysOaWarehouseTaskService.updateToWare(bo));
    }


    /**
     * 处理批量物料入库
     */
    @RepeatSubmit()
    @PutMapping("/status-list")
    public R<Void> status(@Validated(EditGroup.class) @RequestBody List<SysOaWarehouseTaskBo> boList) {
        boList.forEach(bo -> {
            int i = iSysOaWarehouseTaskService.updateToWare(bo);
        });
        return toAjax(1);
    }


    /**
     * 删除采购计划
     *
     * @param taskIds 主键串
     */
    @DeleteMapping("/{taskIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] taskIds) {
        return toAjax(iSysOaWarehouseTaskService.deleteWithValidByIds(Arrays.asList(taskIds), true));
    }
}
