package org.dromara.ygpassport.controller;

import java.util.ArrayList;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.json.JSONObject;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.bo.SysUserPasswordBo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.service.ISysUserService;
import org.dromara.ygpassport.domain.vo.StudentImportVo;
import org.dromara.ygpassport.listener.StudentImportListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.ygpassport.domain.vo.StudentVo;
import org.dromara.ygpassport.domain.bo.StudentBo;
import org.dromara.ygpassport.service.IStudentService;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生详情
 *
 * @author Lion Li
 * @date 2024-03-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/student")
public class StudentController extends BaseController{

    private final IStudentService studentService;

    private final ISysUserService userService;

    /**
     * 获取学生详情详细信息
     *
     * @param jsonObject
     */
    @SaCheckPermission("ygpassport:student:query")
    @PostMapping("/getInfoByStuNo")
    public R<StudentVo> getInfoByStuNo(@RequestBody JSONObject jsonObject) {
        String stuNo = jsonObject.getStr("stuNo");
        return R.ok(studentService.queryByStuNo(stuNo));
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @PostMapping(value = "/importStudentData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        StudentImportListener studentImportListener = new StudentImportListener(updateSupport);

        ExcelResult<StudentVo> result = ExcelUtil.importExcel(file.getInputStream(), StudentVo.class, studentImportListener);
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "学生信息", StudentImportVo.class, response);
    }

    /**
     * 新增学生信息
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StudentBo bo) {
        return toAjax(studentService.insertByBo(bo));
    }

    /**
     * 修改学生信息
     */
    @SaCheckPermission("student:student:edit")
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StudentBo bo) {
        return toAjax(studentService.updateByBo(bo));
    }

    /**
     * 重置密码
     *
     * @param bo 新旧密码
     */
    @Log(title = "个人信息", businessType = BusinessType.UPDATE)
    @SaIgnore
    @PutMapping("/updatePwd")
    public R<Void> updatePwd(@Validated @RequestBody SysUserPasswordBo bo) {
        SysUserVo user = userService.selectUserById(LoginHelper.getUserId());
        String password = user.getPassword();
        if (!BCrypt.checkpw(bo.getOldPassword(), password)) {
            return R.fail("修改密码失败，旧密码错误");
        }
        if (BCrypt.checkpw(bo.getNewPassword(), password)) {
            return R.fail("新密码不能与旧密码相同");
        }

        if (userService.resetUserPwd(user.getUserId(), BCrypt.hashpw(bo.getNewPassword())) > 0) {
            return R.ok();
        }
        return R.fail("修改密码异常，请联系管理员");
    }

}
