package com.ruoyi.oa.controller;

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

import com.ruoyi.oa.domain.bo.SysOaWarehouseBo;
import com.ruoyi.oa.domain.vo.SysOaWarehouseVo;
import com.ruoyi.oa.service.ISysOaWarehouseDetailService;
import com.ruoyi.oa.service.ISysOaWarehouseService;
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.SysOaWarehouseMasterVo;
import com.ruoyi.oa.domain.bo.SysOaWarehouseMasterBo;
import com.ruoyi.oa.service.ISysOaWarehouseMasterService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库单管理
 *
 * @author hdak
 * @date 2025-03-19
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/oaWarehouseMaster")
public class SysOaWarehouseMasterController extends BaseController {

    private final ISysOaWarehouseMasterService iSysOaWarehouseMasterService;

    private final ISysOaWarehouseService warehouseService;

    /**
     * 查询出库单管理列表
     */
    @GetMapping("/list")
    public TableDataInfo<SysOaWarehouseMasterVo> list(SysOaWarehouseMasterBo bo, PageQuery pageQuery) {
        return iSysOaWarehouseMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出出库单管理列表
     */
    @Log(title = "出库单管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SysOaWarehouseMasterBo bo, HttpServletResponse response) {
        List<SysOaWarehouseMasterVo> list = iSysOaWarehouseMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "出库单管理", SysOaWarehouseMasterVo.class, response);
    }

    /**
     * 获取出库单管理详细信息
     *
     * @param masterId 主键
     */
    @GetMapping("/{masterId}")
    public R<SysOaWarehouseMasterVo> getInfo(@NotNull(message = "主键不能为空")
                                             @PathVariable Long masterId) {
        return R.ok(iSysOaWarehouseMasterService.queryById(masterId));
    }

    /**
     * 新增出库单管理
     */

    @Log(title = "出库单管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SysOaWarehouseMasterBo bo) {
        return toAjax(iSysOaWarehouseMasterService.insertByBo(bo));
    }

    /**
     * 新增仓库管理
     */
    @RepeatSubmit()
    @PostMapping("/in")
    public R<Void> addIn(@Validated(AddGroup.class) @RequestBody SysOaWarehouseBo bo) {
        Long id =  warehouseService.insertByBo(bo);
        System.out.println(warehouseService.queryById(id));
        return toAjax(iSysOaWarehouseMasterService.insertInWarehouse(bo,id));
    }

    /**
     * 修改出库单管理
     */
    @Log(title = "出库单管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SysOaWarehouseMasterBo bo) {
        return toAjax(iSysOaWarehouseMasterService.updateByBo(bo));
    }

    /**
     * 修改出库单管理
     */
    @RepeatSubmit()
    @PutMapping("/updateMasterAndInsertBatchWare")
    public R<Void> updateMasterAndInsertBatchWare(@Validated(EditGroup.class) @RequestBody SysOaWarehouseMasterBo bo) {
        return toAjax(iSysOaWarehouseMasterService.updateMasterAndInsertBatchWare(bo));
    }

    /**
     * 删除出库单管理
     *
     * @param masterIds 主键串
     */
    @Log(title = "出库单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{masterIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] masterIds) {
        return toAjax(iSysOaWarehouseMasterService.deleteWithValidByIds(Arrays.asList(masterIds), true));
    }

    /**
     * 撤回出库单
     * 这个傻逼库房非得让写个撤回 勾八自己写错了信息化背锅
     */
    @GetMapping("/redo/{masterId}")
    public R<Void> redo(@PathVariable("masterId") Long masterId) {
        return toAjax(iSysOaWarehouseMasterService.redoMaster(masterId));
    }
}
