package com.maintain.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.maintain.business.domain.bo.ErpPostBo;
import com.maintain.business.domain.bo.ErpUserBo;
import com.maintain.business.domain.vo.ErpUserVo;
import com.maintain.business.listener.ErpUserImportListener;
import com.maintain.business.service.IErpUserService;
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/user")
public class ErpUserController extends BaseController {

    private final IErpUserService iErpUserService;

    /**
     * 查询维修人员-人员列表
     */
    @SaCheckPermission("business:user:list")
    @GetMapping("/list")
    public TableDataInfo<ErpUserVo> list(ErpUserBo bo, PageQuery pageQuery) {
        return iErpUserService.queryPageList(bo, pageQuery);
    }

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

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

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

    /**
     * 根据岗位ID获取员工工号
     */
    @Log(title = "根据岗位ID获取员工工号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/getUserCode/{postId}")
    public R getUserCodeByPostId(@NotNull(message = "岗位ID不能为空") @PathVariable Long postId) {
        return R.ok("操作成功", iErpUserService.getUserCodeByPostId(postId));
    }

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

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

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