package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpWarehouseLocationBo;
import com.maintain.business.domain.vo.ErpWarehouseLocationImportVo;
import com.maintain.business.domain.vo.ErpWarehouseLocationVo;
import com.maintain.business.listener.ErpWarehouseLocationImportListener;
import com.maintain.business.service.IErpWarehouseLocationService;
import com.maintain.common.annotation.Log;
import com.maintain.common.annotation.RepeatSubmit;
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.page.TableDataInfo;
import com.maintain.common.core.validate.AddGroup;
import com.maintain.common.core.validate.EditGroup;
import com.maintain.common.enums.BusinessType;
import com.maintain.common.excel.ExcelResult;
import com.maintain.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 仓库货位
 *
 * @author liushuai
 * @date 2025-04-03
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/warehouseLocation")
public class ErpWarehouseLocationController extends BaseController {

    private final IErpWarehouseLocationService iErpWarehouseLocationService;

    /**
     * 查询仓库货位列表
     */
    @SaCheckPermission("business:warehouseLocation:list")
    @GetMapping("/list")
    public TableDataInfo<ErpWarehouseLocationVo> list(ErpWarehouseLocationBo bo, PageQuery pageQuery) {
        return iErpWarehouseLocationService.queryPageList(bo, pageQuery);
    }

    /**
     * 仓库货位导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpWarehouseLocationBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "仓库货位模板", ErpWarehouseLocationImportVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "仓库货位导入", businessType = BusinessType.IMPORT)
    @PostMapping(value = "/importData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<ErpWarehouseLocationImportVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpWarehouseLocationImportVo.class, new ErpWarehouseLocationImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出仓库货位列表
     */
    @SaCheckPermission("business:warehouseLocation:export")
    @Log(title = "仓库货位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpWarehouseLocationBo bo, HttpServletResponse response) {
        List<ErpWarehouseLocationVo> list = iErpWarehouseLocationService.queryList(bo);
        ExcelUtil.exportExcel(list, "仓库货位", ErpWarehouseLocationVo.class, response);
    }

    /**
     * 获取仓库货位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:warehouseLocation:query")
    @GetMapping("/{id}")
    public R<ErpWarehouseLocationVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpWarehouseLocationService.queryById(id));
    }

    /**
     * 新增仓库货位
     */
    @SaCheckPermission("business:warehouseLocation:add")
    @Log(title = "仓库货位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpWarehouseLocationBo bo) {
        return toAjax(iErpWarehouseLocationService.insertByBo(bo));
    }

    /**
     * 修改仓库货位
     */
    @SaCheckPermission("business:warehouseLocation:edit")
    @Log(title = "仓库货位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpWarehouseLocationBo bo) {
        return toAjax(iErpWarehouseLocationService.updateByBo(bo));
    }

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