package org.dromara.zhishu.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.zhishu.domain.dto.WaveDetailRequestDto;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.zhishu.service.IWaveDetailService;

import java.util.HashMap;
import java.util.Map;

/**
 * 波次信息
 *
 * @author Lion Li
 * @date 2026-01-22
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/wave/detail")
public class WaveDetailController extends BaseController {

    private final IWaveDetailService waveDetailService;

    /**
     * 新增波次信息
     */
    @Log(title = "波次信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @SaIgnore
    @PostMapping("/insert")
    public R<Void> insert(@RequestBody WaveDetailRequestDto requestDto) {
        Boolean result = waveDetailService.insertWaveDetail(requestDto.getUserId(), requestDto);
        return result ? R.ok() : R.fail();
    }


    /**
     * 获取波次ID
     */
    @Log(title = "波次信息", businessType = BusinessType.OTHER)
    @RepeatSubmit()
    @SaIgnore
    @GetMapping("/getWaveId")
    public Map<String, Object> getWaveId(@RequestParam("employeeName") String employeeName, @RequestParam("userId") String userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            String waveId = waveDetailService.getEmployeeId(employeeName, userId);
            result.put("code", 200);
            result.put("data", waveId);
            result.put("message", "获取波次ID成功");
        } catch (Exception e) {
            result.put("code", 500);
            result.put("data", null);
            result.put("message", "获取波次ID失败: " + e.getMessage());
        }
        return result;
    }


    /**
     * 统计波次工作数量
     */
    @Log(title = "波次信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @SaIgnore
    @PostMapping("/count")
    public R<Void> count(@RequestParam String waveId, @RequestParam String employeeId, @RequestParam Integer submitCount) {
        Boolean result = waveDetailService.countVerWave(waveId, employeeId, submitCount);
        return result ? R.ok() : R.fail();
    }
}
