package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Dangan;
import com.ruoyi.system.domain.UserRelate;
import com.ruoyi.system.service.IDanganService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.IUserRelateService;
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.*;

import java.util.Date;
import java.util.List;

/**
 * 子系统用户账号关联Controller
 * 
 * @author ruoyi
 * @date 2022-11-02
 */
@Controller
@RequestMapping("/system/userRelate")
public class UserRelateController extends BaseController
{
    private String prefix = "system/userRelate";

    @Autowired
    private IUserRelateService userRelateService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IDanganService danganService;

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

    /**
     * 查询子系统用户账号关联列表
     */
    @RequiresPermissions("system:userRelate:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(UserRelate userRelate)
    {
        startPage();
        List<UserRelate> list = userRelateService.selectUserRelateList(userRelate);
        return getDataTable(list);
    }

    /**
     * 导出子系统用户账号关联列表
     */
    @RequiresPermissions("system:userRelate:export")
    @Log(title = "子系统用户账号关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(UserRelate userRelate)
    {
        List<UserRelate> list = userRelateService.selectUserRelateList(userRelate);
        ExcelUtil<UserRelate> util = new ExcelUtil<UserRelate>(UserRelate.class);
        return util.exportExcel(list, "userRelate");
    }

    /**
     * 新增子系统用户账号关联
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        //获取用户列表
        List<SysUser> userList= userService.selectUserList(new SysUser());
        mmap.put("userList",userList);
        //获取档案列表
        List<Dangan> danganList = danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);
        return prefix + "/add";
    }

    /**
     * 新增保存子系统用户账号关联
     */
    @RequiresPermissions("system:userRelate:add")
    @Log(title = "子系统用户账号关联", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(UserRelate userRelate)
    {
        userRelate.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(userRelateService.insertUserRelate(userRelate));
    }

    /**
     * 修改子系统用户账号关联
     */
    @GetMapping("/edit/{relateId}")
    public String edit(@PathVariable("relateId") Long relateId, ModelMap mmap)
    {
        UserRelate userRelate = userRelateService.selectUserRelateById(relateId);
        mmap.put("userRelate", userRelate);

        //获取档案列表
        List<Dangan> danganList = danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);
        return prefix + "/edit";
    }

    /**
     * 修改保存子系统用户账号关联
     */
    @RequiresPermissions("system:userRelate:edit")
    @Log(title = "子系统用户账号关联", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(UserRelate userRelate)
    {
        return toAjax(userRelateService.updateUserRelate(userRelate));
    }

    @RequiresPermissions("system:userRelate:edit")
    @Log(title = "子系统用户账号关联", businessType = BusinessType.UPDATE)
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(UserRelate userRelate)
    {
        userRelate.setRelateTime(new Date());
        return toAjax(userRelateService.updateUserRelate(userRelate));
    }




    /**
     * 删除子系统用户账号关联
     */
    @RequiresPermissions("system:userRelate:remove")
    @Log(title = "子系统用户账号关联", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(userRelateService.deleteUserRelateByIds(ids));
    }

    /**
     * 查看
     */
    @GetMapping("/look/{relateId}")
    public String look(@PathVariable("relateId") Long relateId, ModelMap mmap)
    {
        UserRelate userRelate = userRelateService.selectUserRelateById(relateId);
        mmap.put("userRelate", userRelate);
        return prefix + "/look";
    }

    /**
     * 复制
     */
    @GetMapping("/copy")
    public String copy(ModelMap mmap)
    {
        //获取档案列表
        List<Dangan> danganList = danganService.selectDanganList(new Dangan());
        mmap.put("danganList",danganList);
        return prefix + "/copy";
    }
}
