package com.ruoyi.ccdi.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.ccdi.domain.dto.CcdiEmployeeAddDTO;
import com.ruoyi.ccdi.domain.dto.CcdiEmployeeEditDTO;
import com.ruoyi.ccdi.domain.dto.CcdiEmployeeQueryDTO;
import com.ruoyi.ccdi.domain.excel.CcdiEmployeeExcel;
import com.ruoyi.ccdi.domain.vo.CcdiEmployeeVO;
import com.ruoyi.ccdi.service.ICcdiEmployeeService;
import com.ruoyi.ccdi.utils.EasyExcelUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 员工信息Controller
 *
 * @author ruoyi
 * @date 2026-01-28
 */
@Tag(name = "员工信息管理")
@RestController
@RequestMapping("/ccdi/employee")
public class CcdiEmployeeController extends BaseController {

    @Resource
    private ICcdiEmployeeService employeeService;

    /**
     * 查询员工列表
     */
    @Operation(summary = "查询员工列表")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcdiEmployeeQueryDTO queryDTO) {
        // 使用MyBatis Plus分页
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Page<CcdiEmployeeVO> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        Page<CcdiEmployeeVO> result = employeeService.selectEmployeePage(page, queryDTO);
        return getDataTable(result.getRecords(), result.getTotal());
    }

    /**
     * 导出员工列表
     */
    @Operation(summary = "导出员工列表")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:export')")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcdiEmployeeQueryDTO queryDTO) {
        List<CcdiEmployeeExcel> list = employeeService.selectEmployeeListForExport(queryDTO);
        EasyExcelUtil.exportExcel(response, list, CcdiEmployeeExcel.class, "员工信息");
    }

    /**
     * 获取员工详细信息
     */
    @Operation(summary = "获取员工详细信息")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:query')")
    @GetMapping(value = "/{employeeId}")
    public AjaxResult getInfo(@PathVariable Long employeeId) {
        return success(employeeService.selectEmployeeById(employeeId));
    }

    /**
     * 新增员工
     */
    @Operation(summary = "新增员工")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:add')")
    @Log(title = "员工信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CcdiEmployeeAddDTO addDTO) {
        return toAjax(employeeService.insertEmployee(addDTO));
    }

    /**
     * 修改员工
     */
    @Operation(summary = "修改员工")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:edit')")
    @Log(title = "员工信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CcdiEmployeeEditDTO editDTO) {
        return toAjax(employeeService.updateEmployee(editDTO));
    }

    /**
     * 删除员工
     */
    @Operation(summary = "删除员工")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:remove')")
    @Log(title = "员工信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{employeeIds}")
    public AjaxResult remove(@PathVariable Long[] employeeIds) {
        return toAjax(employeeService.deleteEmployeeByIds(employeeIds));
    }

    /**
     * 下载带字典下拉框的导入模板
     * 使用@DictDropdown注解自动添加下拉框
     */
    @Operation(summary = "下载导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        EasyExcelUtil.importTemplateWithDictDropdown(response, CcdiEmployeeExcel.class, "员工信息");
    }

    /**
     * 导入员工信息
     */
    @Operation(summary = "导入员工信息")
    @PreAuthorize("@ss.hasPermi('ccdi:employee:import')")
    @Log(title = "员工信息", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        List<CcdiEmployeeExcel> list = EasyExcelUtil.importExcel(file.getInputStream(), CcdiEmployeeExcel.class);
        String message = employeeService.importEmployee(list, updateSupport);
        return success(message);
    }
}
