package com.maintain.business.controller;

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

import com.maintain.business.listener.ErpWarehouseImportListener;
import com.maintain.common.excel.ExcelResult;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.http.MediaType;
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.ErpWarehouseVo;
import com.maintain.business.domain.bo.ErpWarehouseBo;
import com.maintain.business.service.IErpWarehouseService;
import com.maintain.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 维修站点-站点-仓库
 *
 * @author liushuai
 * @date 2025-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/siteWarehouse")
public class ErpWarehouseController extends BaseController {

    private final IErpWarehouseService iErpWarehouseService;

    /**
     * 查询仓库列表
     */
    @SaCheckPermission("business:siteWarehouse:list")
    @GetMapping("/list")
    public TableDataInfo<ErpWarehouseVo> list(ErpWarehouseBo bo, PageQuery pageQuery) {
        return iErpWarehouseService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询维修站点-站点-仓库列表
     */
    @GetMapping("/listAll")
    public R list(ErpWarehouseBo bo) {
        return R.ok(iErpWarehouseService.queryList(bo));
    }

    /**
     * 导出维修站点-站点-仓库列表
     */
    @SaCheckPermission("business:siteWarehouse:export")
    @Log(title = "维修站点仓库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpWarehouseBo bo, HttpServletResponse response) {
        List<ErpWarehouseVo> list = iErpWarehouseService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修站点仓库", ErpWarehouseVo.class, response);
    }

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpWarehouseBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "站点仓库导入模板", ErpWarehouseVo.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<ErpWarehouseVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpWarehouseVo.class, new ErpWarehouseImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取维修站点-站点-仓库详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:siteWarehouse:query")
    @GetMapping("/{id}")
    public R<ErpWarehouseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpWarehouseService.queryById(id));
    }

    /**
     * 新增维修站点-站点-仓库
     */
    @SaCheckPermission("business:siteWarehouse:add")
    @Log(title = "维修站点-站点-仓库", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpWarehouseBo bo) {
        return toAjax(iErpWarehouseService.insertByBo(bo));
    }

    /**
     * 修改维修站点-站点-仓库
     */
    @SaCheckPermission("business:siteWarehouse:edit")
    @Log(title = "维修站点-站点-仓库", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpWarehouseBo bo) {
        return toAjax(iErpWarehouseService.updateByBo(bo));
    }

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