package com.maintain.business.controller;

import java.util.List;
import java.util.Arrays;

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.maintain.common.annotation.RepeatSubmit;
import com.maintain.common.annotation.Log;
import com.maintain.common.core.controller.BaseController;
import com.maintain.common.core.domain.PageQuery;
import com.maintain.common.core.domain.R;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.utils.poi.ExcelUtil;
import com.maintain.business.domain.vo.ErpMaterialPutawayVo;
import com.maintain.business.domain.bo.ErpMaterialPutawayBo;
import com.maintain.business.service.IErpMaterialPutawayService;
import com.maintain.common.core.page.TableDataInfo;

/**
 * 物料入库
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/materialPutaway")
public class ErpMaterialPutawayController extends BaseController {

    private final IErpMaterialPutawayService iErpMaterialPutawayService;

    /**
     * 查询物料入库列表
     */
    @SaCheckPermission("business:materialPutaway:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialPutawayVo> list(ErpMaterialPutawayBo bo, PageQuery pageQuery) {
        return iErpMaterialPutawayService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物料入库列表
     */
    @GetMapping("/listAll")
    public R listAll(ErpMaterialPutawayBo bo) {
        return R.ok(iErpMaterialPutawayService.queryList(bo));
    }

    /**
     * 导出物料入库列表
     */
    @SaCheckPermission("business:materialPutaway:export")
    @Log(title = "物料入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpMaterialPutawayBo bo, HttpServletResponse response) {
        List<ErpMaterialPutawayVo> list = iErpMaterialPutawayService.queryList(bo);
        ExcelUtil.exportExcel(list, "物料入库", ErpMaterialPutawayVo.class, response);
    }

    /**
     * 获取物料入库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:materialPutaway:query")
    @GetMapping("/{id}")
    public R<ErpMaterialPutawayVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpMaterialPutawayService.queryById(id));
    }

    /**
     * 新增物料入库
     */
    @SaCheckPermission("business:materialPutaway:add")
    @Log(title = "物料入库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialPutawayBo bo) {
        return toAjax(iErpMaterialPutawayService.insertByBo(bo));
    }

    /**
     * 批量确认物料入库
     */
    @SaCheckPermission("business:materialPicking:confirm")
    @Log(title = "批量确认物料入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/confirm/{ids}")
    public R<Void> confirm(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return toAjax(iErpMaterialPutawayService.confirmByIds(Arrays.asList(ids)));
    }

    /**
     * 修改物料入库
     */
    @SaCheckPermission("business:materialPutaway:edit")
    @Log(title = "物料入库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpMaterialPutawayBo bo) {
        return toAjax(iErpMaterialPutawayService.updateByBo(bo));
    }

    /**
     * 删除物料入库
     *
     * @param ids 主键串
     */
    @SaCheckPermission("business:materialPutaway:remove")
    @Log(title = "物料入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpMaterialPutawayService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
