package com.ruoyi.oa.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.vo.OaSalaryMasterVo;
import com.ruoyi.oa.domain.bo.OaSalaryMasterBo;
import com.ruoyi.oa.domain.dto.BatchInsertSalaryStringDto;
import com.ruoyi.oa.service.IOaSalaryMasterService;
import com.ruoyi.oa.utils.SalaryStringConverter;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工资发放主
 *
 * @author Joshi
 * @date 2025-09-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oa/salaryMaster")
public class OaSalaryMasterController extends BaseController {

    private final IOaSalaryMasterService iOaSalaryMasterService;

    /**
     * 查询工资发放主列表
     */
    @GetMapping("/list")
    public TableDataInfo<OaSalaryMasterVo> list(OaSalaryMasterBo bo, PageQuery pageQuery) {
        return iOaSalaryMasterService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出工资发放主列表
     */
    @Log(title = "工资发放主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(OaSalaryMasterBo bo, HttpServletResponse response) {
        List<OaSalaryMasterVo> list = iOaSalaryMasterService.queryList(bo);
        ExcelUtil.exportExcel(list, "工资发放主", OaSalaryMasterVo.class, response);
    }

    /**
     * 获取工资发放主详细信息
     *
     * @param masterId 主键
     */
    @GetMapping("/{masterId}")
    public R<OaSalaryMasterVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long masterId) {
        return R.ok(iOaSalaryMasterService.queryById(masterId));
    }

    /**
     * 新增工资发放主
     */
    @Log(title = "工资发放主", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody OaSalaryMasterBo bo) {
        return toAjax(iOaSalaryMasterService.insertByBo(bo));
    }

    /**
     * 修改工资发放主
     */
    @Log(title = "工资发放主", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody OaSalaryMasterBo bo) {
        return toAjax(iOaSalaryMasterService.updateByBo(bo));
    }

    /**
     * 删除工资发放主
     *
     * @param masterIds 主键串
     */
    @Log(title = "工资发放主", businessType = BusinessType.DELETE)
    @DeleteMapping("/{masterIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] masterIds) {
        return toAjax(iOaSalaryMasterService.deleteWithValidByIds(Arrays.asList(masterIds), true));
    }

    /**
     * 批量插入工资明细（方式一：使用OaSalaryMasterBo）
     *
     * @param bo 包含主表信息和明细列表的业务对象
     */
    @Log(title = "批量插入工资明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/batchInsertDetails")
    public R<Void> batchInsertDetails(@Validated(AddGroup.class) @RequestBody OaSalaryMasterBo bo) {
        return toAjax(iOaSalaryMasterService.batchInsertDetails(bo));
    }

    /**
     * 批量插入工资明细（处理字符串数字格式）
     * 专门用于处理前端传递的驼峰命名但数字为字符串格式的数据
     *
     * @param requestDto 字符串格式的请求DTO
     */
    @Log(title = "批量插入工资明细", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/batchInsertDetailsWithStringConvert")
    public R<Void> batchInsertDetailsWithStringConvert(@Validated @RequestBody BatchInsertSalaryStringDto requestDto) {
        // 将字符串DTO转换为后端BO
        OaSalaryMasterBo bo = SalaryStringConverter.convertToMasterBo(requestDto);
        return toAjax(iOaSalaryMasterService.batchInsertDetails(bo));
    }

}
