package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpGroupBo;
import com.maintain.business.domain.vo.ErpGroupVo;
import com.maintain.business.listener.ErpGroupImportListener;
import com.maintain.business.service.IErpGroupService;
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-03-25
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/siteGroup")
public class ErpGroupController extends BaseController {

    private final IErpGroupService iErpGroupService;

    /**
     * 班组列表
     */
    @SaCheckPermission("business:siteGroup:list")
    @GetMapping("/list")
    public TableDataInfo<ErpGroupVo> list(ErpGroupBo bo, PageQuery pageQuery) {
        return iErpGroupService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出维修站点-站点-班组列表
     */
    @SaCheckPermission("business:siteGroup:export")
    @Log(title = "维修班组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpGroupBo bo, HttpServletResponse response) {
        List<ErpGroupVo> list = iErpGroupService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修班组", ErpGroupVo.class, response);
    }

    /**
     * 下载维修班组导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpGroupBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修班组导入模板", ErpGroupVo.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<ErpGroupVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpGroupVo.class, new ErpGroupImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

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

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

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

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