package com.sy.coinage.log.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.util.DateUtils;
import com.sy.coinage.core.vo.CacheUserVO;

import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sy.coinage.log.model.*;
import com.sy.coinage.log.service.IOperateLogService;
import com.sy.coinage.log.req.*;

/**
 * 系统操作记录Controller
 */
@Api(value = "系统操作日志接口", tags = {"系统操作日志接口"})
@RestController
@RequestMapping(value = "/operateLog")
public class OperateLogController {

    @Autowired
    private IOperateLogService operateLogService;

    /**
     * 系统操作记录分页列表
     *
     * @param req
     * @param user
     * @return
     */
    @PostMapping("/list")
    public IPage<OperateLog> list(@RequestBody OperateLogListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<OperateLog> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(OperateLog::getTenantId, user.getTenantId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(OperateLog::getStartTime, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(OperateLog::getStartTime, DateUtils.getDayEndTime(req.getEndDate()));
        }
        Page<OperateLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<OperateLog> data = this.operateLogService.page(page, queryWrapper);
        return data;
    }


    /**
     * 租户管理员-系统操作记录分页列表
     *
     * @param req
     * @return
     */
    @PostMapping("/admin/list")
    public IPage<OperateLog> adminList(@RequestBody OperateLogListReq req) {
        LambdaQueryWrapper<OperateLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((req.getTenantId() != null && req.getTenantId() > 0), OperateLog::getTenantId,
                        req.getTenantId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(OperateLog::getStartTime, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(OperateLog::getStartTime, DateUtils.getDayEndTime(req.getEndDate()));
        }
        Page<OperateLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<OperateLog> data = this.operateLogService.page(page, queryWrapper);
        return data;
    }

}
