package com.playlet.web.controller.system;

import java.util.List;

import com.playlet.system.domain.PlayletUser;
import com.playlet.system.service.IPlayletUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.playlet.common.annotation.Log;
import com.playlet.common.enums.BusinessType;
import com.playlet.system.domain.PlayletUserLogin;
import com.playlet.system.service.IPlayletUserLoginService;
import com.playlet.common.core.controller.BaseController;
import com.playlet.common.core.domain.AjaxResult;
import com.playlet.common.utils.poi.ExcelUtil;
import com.playlet.common.core.page.TableDataInfo;

/**
 * 用户登录记录Controller
 *
 * @author ruoyi
 * @date 2024-05-05
 */
@Controller
@RequestMapping("/system/playlet/login")
public class PlayletUserLoginController extends BaseController
{
    private String prefix = "system/playlet/login";

    @Autowired
    private IPlayletUserLoginService playletUserLoginService;

    @Autowired
    private IPlayletUserService playletUserService;

    @RequiresPermissions("playlet:login:view")
    @GetMapping()
    public String login()
    {
        return prefix + "/login";
    }

    /**
     * 查询用户登录记录列表
     */
    @RequiresPermissions("playlet:login:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PlayletUserLogin playletUserLogin)
    {
        startPage();
        List<PlayletUserLogin> list = playletUserLoginService.selectPlayletUserLoginList(playletUserLogin);
        list.forEach(model->{
            PlayletUser user = playletUserService.getById(model.getUserId());
            if(user != null){
                model.setUserName(user.getNickName());
            }
        });
        return getDataTable(list);
    }

    /**
     * 导出用户登录记录列表
     */
    @RequiresPermissions("playlet:login:export")
    @Log(title = "用户登录记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PlayletUserLogin playletUserLogin)
    {
        List<PlayletUserLogin> list = playletUserLoginService.selectPlayletUserLoginList(playletUserLogin);
        list.forEach(model->{
            PlayletUser user = playletUserService.getById(model.getUserId());
            if(user != null){
                model.setUserName(user.getNickName());
            }
        });
        ExcelUtil<PlayletUserLogin> util = new ExcelUtil<PlayletUserLogin>(PlayletUserLogin.class);
        return util.exportExcel(list, "用户登录记录数据");
    }

    /**
     * 新增用户登录记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存用户登录记录
     */
    @RequiresPermissions("playlet:login:add")
    @Log(title = "用户登录记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PlayletUserLogin playletUserLogin)
    {
        return toAjax(playletUserLoginService.insertPlayletUserLogin(playletUserLogin));
    }

    /**
     * 修改用户登录记录
     */
    @RequiresPermissions("playlet:login:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PlayletUserLogin playletUserLogin = playletUserLoginService.selectPlayletUserLoginById(id);
        mmap.put("playletUserLogin", playletUserLogin);
        return prefix + "/edit";
    }

    /**
     * 修改保存用户登录记录
     */
    @RequiresPermissions("playlet:login:edit")
    @Log(title = "用户登录记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PlayletUserLogin playletUserLogin)
    {
        return toAjax(playletUserLoginService.updatePlayletUserLogin(playletUserLogin));
    }

    /**
     * 删除用户登录记录
     */
    @RequiresPermissions("playlet:login:remove")
    @Log(title = "用户登录记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(playletUserLoginService.deletePlayletUserLoginByIds(ids));
    }
}
