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.ErpMaterialPickingVo;
import com.maintain.business.domain.bo.ErpMaterialPickingBo;
import com.maintain.business.service.IErpMaterialPickingService;
import com.maintain.common.core.page.TableDataInfo;

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

    private final IErpMaterialPickingService iErpMaterialPickingService;

    /**
     * 查询物料出库列表
     */
    @SaCheckPermission("business:materialPicking:list")
    @GetMapping("/list")
    public TableDataInfo<ErpMaterialPickingVo> list(ErpMaterialPickingBo bo, PageQuery pageQuery) {
        return iErpMaterialPickingService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询物料出库列表
     */
    @SaCheckPermission("business:materialPicking:list")
    @GetMapping("/info/list")
    public TableDataInfo<ErpMaterialPickingVo> infoList(ErpMaterialPickingBo bo, PageQuery pageQuery) {
        return iErpMaterialPickingService.queryPageInfoList(bo, pageQuery);
    }

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

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

    /**
     * 新增物料出库
     */
    @SaCheckPermission("business:materialPicking:add")
    @Log(title = "物料出库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpMaterialPickingBo bo) {
        return toAjax(iErpMaterialPickingService.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(iErpMaterialPickingService.confirmByIds(Arrays.asList(ids)));
    }

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

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

    /**
     * 新增物料出库退料
     */
    @SaCheckPermission("business:pickingReturn:add")
    @Log(title = "物料出库退料", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/return")
    public R<Void> pickingReturn(@Validated(AddGroup.class) @RequestBody ErpMaterialPickingBo bo) {
        return toAjax(iErpMaterialPickingService.insertReturnByBo(bo));
    }

    /**
     * 删除物料出库退料
     */
    @SaCheckPermission("business:pickingReturn:delete")
    @Log(title = "物料出库退料", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @DeleteMapping("/return/{ids}")
    public R<Void> delPickingReturn(@PathVariable String ids) {
        return toAjax(true);
    }
}
