package jnpf.attendance.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jnpf.attendance.service.AttendanceBookOperationLogService;
import jnpf.base.ActionResult;
import jnpf.entity.attendance.AttendanceBookOperationLogEntity;
import jnpf.model.attendance.dto.OperationLogQueryDto;
import jnpf.model.attendance.vo.OperationLogPageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 考勤本操作日志Controller
 *
 * @author Generated
 * @create 2026-04-15
 */
@Slf4j
@RestController
@RequestMapping("/attendance/book-operation-log")
@Tag(name = "考勤本操作日志", description = "考勤本操作日志管理")
public class AttendanceBookOperationLogController {

    @Resource
    private AttendanceBookOperationLogService attendanceBookOperationLogService;

    /**
     * 新增操作日志
     *
     * @param entity 操作日志实体
     * @return 操作结果
     */
    @Operation(summary = "新增操作日志")
    @PostMapping
    public ActionResult create(@Valid @RequestBody AttendanceBookOperationLogEntity entity) {
        try {
            log.info("新增操作日志，入参=>{}", JSONObject.toJSON(entity));
            attendanceBookOperationLogService.saveLog(entity);
            return ActionResult.success("操作成功");
        } catch (Exception e) {
            log.error("新增操作日志异常", e);
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * web-分页查询操作日志
     *
     * @param queryDto 查询参数
     * @return 分页结果
     */
    @Operation(summary = "web-分页查询操作日志")
    @GetMapping("/page")
    public ActionResult<Page<OperationLogPageVo>> queryPage(@Valid OperationLogQueryDto queryDto) {
        try {
            log.info("分页查询操作日志，入参=>{}", JSONObject.toJSON(queryDto));
            long st = System.currentTimeMillis();
            
            Page<OperationLogPageVo> result = attendanceBookOperationLogService.queryPage(queryDto);
            
            long ent = System.currentTimeMillis();
            log.info("分页查询操作日志，耗时=>{} 毫秒，返回记录数: {}", ent - st, result.getRecords().size());
            return ActionResult.success(result);
        } catch (Exception e) {
            log.error("分页查询操作日志异常", e);
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 获取操作日志详情
     *
     * @param id 日志ID
     * @return 操作结果
     */
    @Operation(summary = "获取操作日志详情")
    @GetMapping("/{id}")
    public ActionResult<AttendanceBookOperationLogEntity> getById(@PathVariable String id) {
        try {
            log.info("获取操作日志详情，id=>{}", id);
            AttendanceBookOperationLogEntity entity = attendanceBookOperationLogService.getById(id);
            if (entity == null) {
                return ActionResult.fail("记录不存在");
            }
            return ActionResult.success(entity);
        } catch (Exception e) {
            log.error("获取操作日志详情异常", e);
            return ActionResult.fail(e.getMessage());
        }
    }

    /**
     * 删除操作日志
     *
     * @param id 日志ID
     * @return 操作结果
     */
    @Operation(summary = "删除操作日志")
    @DeleteMapping("/{id}")
    public ActionResult delete(@PathVariable String id) {
        try {
            log.info("删除操作日志，id=>{}", id);
            boolean removed = attendanceBookOperationLogService.removeById(id);
            if (!removed) {
                return ActionResult.fail("删除失败，记录不存在");
            }
            return ActionResult.success("删除成功");
        } catch (Exception e) {
            log.error("删除操作日志异常", e);
            return ActionResult.fail(e.getMessage());
        }
    }
}