package shop.alien.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import shop.alien.entity.result.R;
import shop.alien.entity.store.OperationLog;
import shop.alien.mapper.OperationLogMapper;
import shop.alien.store.annotation.ChangeRecordLog;
import shop.alien.store.enums.OperationType;

import java.util.Date;

/**
 * 操作日志Controller
 *
 * @author ssk
 * @since 2025-12-09
 */
@Api(tags = {"操作日志管理"})
@RestController
@RequestMapping("/operation/log")
@RequiredArgsConstructor
public class OperationLogController {

    private final OperationLogMapper operationLogMapper;

    @ApiOperation("查询操作日志列表")
    @GetMapping("/list")
    @ChangeRecordLog(module = "操作日志管理", type = OperationType.QUERY, content = "查询了操作日志列表")
    public R<IPage<OperationLog>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize,
            @ApiParam("姓名") @RequestParam(required = false) String name,
            @ApiParam("开始时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
            @ApiParam("结束时间") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
            @ApiParam("用户类型") @RequestParam(required = false) String userType) {
        
        Page<OperationLog> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<OperationLog> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.like(name != null, OperationLog::getName, name)
               .ge(startTime != null, OperationLog::getOperationTime, startTime)
               .le(endTime != null, OperationLog::getOperationTime, endTime)
               .eq(userType != null, OperationLog::getUserType, userType)
               .orderByDesc(OperationLog::getOperationTime);
        
        IPage<OperationLog> result = operationLogMapper.selectPage(page, wrapper);
        return R.data(result);
    }

    @ApiOperation("根据ID查询操作日志详情")
    @GetMapping("/{id}")
    @ChangeRecordLog(module = "操作日志管理", type = OperationType.QUERY, content = "查询了操作日志详情,id=#{#id}")
    public R<OperationLog> getById(@ApiParam("日志ID") @PathVariable Long id) {
        OperationLog operationLog = operationLogMapper.selectById(id);
        return R.data(operationLog);
    }

    @ApiOperation("直接插入操作日志")
    @PostMapping("/insert")
    public R insertOperationLog(@RequestBody OperationLog operationLog) {
        try {
            // 如果操作时间为空，设置为当前时间
            if (operationLog.getOperationTime() == null) {
                operationLog.setOperationTime(new Date());
            }
            
            // 直接保存到数据库
            operationLogMapper.insert(operationLog);
            return R.data("操作日志保存成功");
        } catch (Exception e) {
            return R.fail("操作日志保存失败：" + e.getMessage());
        }
    }
}

