package com.chushang.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.core.web.Result;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.security.annotation.InnerAuth;
import com.chushang.security.annotation.RequiresPermissions;
import com.chushang.system.entity.dto.ListLoginLogDTO;
import com.chushang.system.entity.po.SysLoginInfo;
import com.chushang.system.feign.RemoteLoginInfoService;
import com.chushang.system.service.SysLoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;

@RestController
@RequestMapping(value = "/log/login/info")
public class SysLoginInfoController {

    @Autowired
    private SysLoginInfoService sysLoginInfoService;

    @GetMapping(value = "/list")
    @RequiresPermissions("system:log:list")
    public AjaxResult listLoginInfo(SysLoginInfo loginInfo){
        CommonParam commonParam = CommonParam.buildPageRequest();
        return AjaxResult.success(sysLoginInfoService.pagePostList(loginInfo, commonParam));
    }

    @DeleteMapping(value = "/{infoId}")
    @SysLog(value = "登录日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:log:del")
    public AjaxResult delLoginInfo(@PathVariable String infoId){
        sysLoginInfoService.removeBatchByIds(Arrays.stream(infoId.split(",")).toList());
        return AjaxResult.success();
    }

    /**
     * 清除登录日志
     */
    @DeleteMapping(value = "clean")
    @SysLog(value = "登录日志", businessType = BusinessType.DELETE)
    @RequiresPermissions("system:log:del")
    public AjaxResult cleanLoginInfo(){
        sysLoginInfoService.remove(new LambdaQueryWrapper<>());
        return AjaxResult.success();
    }
}
