package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpPostBo;
import com.maintain.business.domain.vo.ErpPostVo;
import com.maintain.business.listener.ErpPostImportListener;
import com.maintain.business.service.IErpPostService;
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/post")
public class ErpPostController extends BaseController {

    private final IErpPostService iErpPostService;

    /**
     * 查询维修人员-岗位列表
     */
    @SaCheckPermission("business:post:list")
    @GetMapping("/list")
    public TableDataInfo<ErpPostVo> list(ErpPostBo bo, PageQuery pageQuery) {
        return iErpPostService.queryPageList(bo, pageQuery);
    }

    /**
     * 岗位导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(ErpPostBo bo, HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "维修人员信息-岗位", ErpPostVo.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<ErpPostVo> result = ExcelUtil.importExcel(file.getInputStream(), ErpPostVo.class, new ErpPostImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    /**
     * 导出维修人员-岗位列表
     */
    @SaCheckPermission("business:post:export")
    @Log(title = "维修人员-岗位", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ErpPostBo bo, HttpServletResponse response) {
        List<ErpPostVo> list = iErpPostService.queryList(bo);
        ExcelUtil.exportExcel(list, "维修人员-岗位", ErpPostVo.class, response);
    }

    /**
     * 获取维修人员-岗位详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("business:post:query")
    @GetMapping("/{id}")
    public R<ErpPostVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpPostService.queryById(id));
    }

    /**
     * 新增维修人员-岗位
     */
    @SaCheckPermission("business:post:add")
    @Log(title = "维修人员-岗位", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpPostBo bo) {
        return toAjax(iErpPostService.insertByBo(bo));
    }

    /**
     * 修改维修人员-岗位
     */
    @SaCheckPermission("business:post:edit")
    @Log(title = "维修人员-岗位", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpPostBo bo) {
        return toAjax(iErpPostService.updateByBo(bo));
    }

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