package com.ruoyi.dpc.controller;

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.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.dpc.domain.DpcEmployee;
import com.ruoyi.dpc.domain.dto.DpcEmployeeAddDTO;
import com.ruoyi.dpc.domain.dto.DpcEmployeeEditDTO;
import com.ruoyi.dpc.domain.dto.DpcEmployeeQueryDTO;
import com.ruoyi.dpc.domain.vo.DpcEmployeeVO;
import com.ruoyi.dpc.service.IDpcEmployeeService;
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
 */
@RestController
@RequestMapping("/dpc/employee")
public class DpcEmployeeController extends BaseController {

    @Resource
    private IDpcEmployeeService employeeService;

    /**
     * 查询员工列表
     */
    @PreAuthorize("@ss.hasPermi('dpc:employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(DpcEmployeeQueryDTO queryDTO) {
        startPage();
        List<DpcEmployeeVO> list = employeeService.selectEmployeeList(queryDTO);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @PreAuthorize("@ss.hasPermi('dpc:employee:export')")
    @Log(title = "员工信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DpcEmployeeQueryDTO queryDTO) {
        List<DpcEmployee> list = employeeService.selectEmployeeListForExport(queryDTO);
        ExcelUtil<DpcEmployee> util = new ExcelUtil<>(DpcEmployee.class);
        util.exportExcel(response, list, "员工信息数据");
    }

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

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

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

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

    /**
     * 下载导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<DpcEmployee> util = new ExcelUtil<>(DpcEmployee.class);
        util.importTemplateExcel(response, "员工信息数据");
    }

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

    /**
     * 转换为AddDTO列表
     */
    private List<DpcEmployeeAddDTO> convertToAddDTOList(List<DpcEmployee> list) {
        return list.stream().map(entity -> {
            DpcEmployeeAddDTO dto = new DpcEmployeeAddDTO();
            dto.setName(entity.getName());
            dto.setTellerNo(entity.getTellerNo());
            dto.setOrgNo(entity.getOrgNo());
            dto.setIdCard(entity.getIdCard());
            dto.setPhone(entity.getPhone());
            dto.setHireDate(entity.getHireDate());
            dto.setStatus(entity.getStatus());
            return dto;
        }).toList();
    }
}
