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.util.DateUtils;
import io.swagger.annotations.Api;

import javax.validation.Valid;

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.config.CustomConfig;
import com.sy.coinage.core.aop.annotation.Operation;
import com.sy.coinage.core.annotation.CurrentUser;
import com.sy.coinage.core.req.*;
import com.sy.coinage.core.vo.*;

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

/**
 * 登入登出记录Controller
 */
@Api(value = "登入登出日志接口", tags = {"登入登出日志接口"})
@RestController
@RequestMapping(value = "/loginLog")
public class LoginLogController {

    @Autowired
    private ILoginLogService loginLogService;

    /**
     * 登入登出记录分页列表
     *
     * @param req
     * @param user
     * @return
     */
    @PostMapping("/list")
    public IPage<LoginLog> list(@RequestBody LoginLogListReq req, @CurrentUser CacheUserVO user) {
        LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(LoginLog::getTenantId, user.getTenantId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(LoginLog::getCreateTime, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(LoginLog::getCreateTime, DateUtils.getDayEndTime(req.getEndDate()));
        }
        Page<LoginLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<LoginLog> data = this.loginLogService.page(page, queryWrapper);
        return data;
    }

    /**
     * 登入登出记录删除
     *
     * @param req {@link IdReq}
     * @return
     */
    @Operation(value = "删除登入登出记录")
    @PostMapping("/delBatch")
    public Object delBatch(@RequestBody @Valid IdsListReq req, @CurrentUser CacheUserVO user) {
        this.loginLogService.removeBatchByIds(req.getIds());
        return "";
    }

    private static String[] codes = {"id", "type", "userId", "realName", "username", "ip", "createTime"};
    private static String[] names = {"自增主键", "类型", "用户id", "姓名", "用户名", "操作ip", "创建时间"};

    @Autowired
    private CustomConfig customConfig;

    /**
     * 登入登出记录导出
     *
     * @param req {IdsListReq IdReq}
     * @return 文件url
     */
    // @PostMapping("/exp")
    // public Object exp(@RequestBody IdsListReq req) {
    // LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
    // List<LoginLog> list = this.loginLogService.list(queryWrapper);
    // ExcelExportUtil<LoginLog> excelExport = new ExcelExportUtil<>();
    // String resp = excelExport.exportData(names, codes, list, "登入登出记录.xlsx",
    // customConfig.getAttaPath(),
    // customConfig.getAttaUrl());
    // return resp;
    // }

    /**
     * 租户管理员-登入登出记录分页列表
     *
     * @param req {@link LoginLogListReq}
     * @return PageInfo<LoginLogListVO> {@link LoginLogListReq}
     */
    @PostMapping("/admin/list")
    public IPage<LoginLog> adminList(@RequestBody LoginLogListReq req) {
        LambdaQueryWrapper<LoginLog> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq((req.getTenantId() != null && req.getTenantId() > 0), LoginLog::getTenantId, req.getTenantId());
        if (req.getStartDate() != null) {
            queryWrapper.ge(LoginLog::getCreateTime, DateUtils.getDayStartTime(req.getStartDate()));
        }
        if (req.getEndDate() != null) {
            queryWrapper.le(LoginLog::getCreateTime, DateUtils.getDayEndTime(req.getEndDate()));
        }
        Page<LoginLog> page = new Page<>(req.getPage(), req.getRows());
        IPage<LoginLog> data = this.loginLogService.page(page, queryWrapper);
        return data;
    }

}
