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.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.ForgetPassword;
import com.ruoyi.system.service.IForgetPasswordService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 忘记密码申请Controller
 * 
 * @author ruoyi
 * @date 2022-11-03
 */
@Controller
@RequestMapping("/system/forgetPassword")
public class ForgetPasswordController extends BaseController
{
    private String prefix = "system/forgetPassword";

    @Autowired
    private IForgetPasswordService forgetPasswordService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private SysPasswordService passwordService;

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

    /**
     * 查询忘记密码申请列表
     */
    @RequiresPermissions("system:forgetPassword:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ForgetPassword forgetPassword)
    {
        startPage();
        List<ForgetPassword> list = forgetPasswordService.selectForgetPasswordList(forgetPassword);
        return getDataTable(list);
    }

    /**
     * 导出忘记密码申请列表
     */
    @RequiresPermissions("system:forgetPassword:export")
    @Log(title = "忘记密码申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ForgetPassword forgetPassword)
    {
        List<ForgetPassword> list = forgetPasswordService.selectForgetPasswordList(forgetPassword);
        ExcelUtil<ForgetPassword> util = new ExcelUtil<ForgetPassword>(ForgetPassword.class);
        return util.exportExcel(list, "forgetPassword");
    }

    /**
     * 新增忘记密码申请
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存忘记密码申请
     */
//    @RequiresPermissions("system:forgetPassword:add")
    @Log(title = "忘记密码申请", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ForgetPassword forgetPassword)
    {
        return toAjax(forgetPasswordService.insertForgetPassword(forgetPassword));
    }





    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave1(ForgetPassword forgetPassword)
    {
        return toAjax(forgetPasswordService.insertForgetPassword(forgetPassword));
    }
    /**
     * 修改忘记密码申请
     */
    @GetMapping("/edit/{pId}")
    public String edit(@PathVariable("pId") Long pId, ModelMap mmap)
    {
        ForgetPassword forgetPassword = forgetPasswordService.selectForgetPasswordById(pId);
        mmap.put("forgetPassword", forgetPassword);
        return prefix + "/edit";
    }

    /**
     * 修改保存忘记密码申请
     */
    @RequiresPermissions("system:forgetPassword:edit")
    @Log(title = "忘记密码申请", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ForgetPassword forgetPassword)
    {
        return toAjax(forgetPasswordService.updateForgetPassword(forgetPassword));
    }

    /**
     * 删除忘记密码申请
     */
    @RequiresPermissions("system:forgetPassword:remove")
    @Log(title = "忘记密码申请", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(forgetPasswordService.deleteForgetPasswordByIds(ids));
    }

    /**
     * 修改密码
     * @param forgetPassword
     * @return
     */
    @PostMapping("/changePassword")
    @ResponseBody
    @Transactional
    public AjaxResult changePassword(ForgetPassword forgetPassword)
    {
       int result=forgetPasswordService.updateForgetPassword(forgetPassword);
       if(result>0){
           //修改用户表密码
           //通过登录名获取用户
          SysUser user = userService.selectUserByLoginName(forgetPassword.getLoginName());
           user.setSalt(ShiroUtils.randomSalt());
           user.setPassword(passwordService.encryptPassword(user.getLoginName(), user.getPassword(), user.getSalt()));
           if (userService.resetUserPwd(user) > 0)
           {
               if (ShiroUtils.getUserId().longValue() == user.getUserId().longValue())
               {
                   ShiroUtils.setSysUser(userService.selectUserById(user.getUserId()));
               }
               return success();
           }
       }
        return error();
    }

}
