package com.template.controller;

import com.template.annotation.PassToken;
import com.template.api.LoginControllerAPI;
import com.template.model.enumModel.eStatu;
import com.template.model.pojo.RepairAdmin;
import com.template.model.pojo.SmartUser;
import com.template.model.request.changePasswordRequest;
import com.template.model.request.loginRequest;
import com.template.model.result.CommonResult;
import com.template.model.vo.LoginVO;
import com.template.services.RepairAdminService;
import com.template.common.utils.AesUtils;
import com.template.common.utils.paramUtils;
import com.template.common.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: binguo
 * @Date: 2023/7/5 星期三 9:28
 * @Description: com.template.controller
 * @Version: 1.0
 */
@RestController
public class LoginController implements LoginControllerAPI {

    @Autowired
    private RepairAdminService repairAdminService;

    /**
     * 查看系统版本号
     * @return
     */
    @Override
    @PassToken
    public CommonResult queryReduce() {
        return CommonResult.ok("当前系统版本为：1V");
    }

    /**
     * 注册接口
     * @param registerdo
     * account 账号
     * password 密码
     * username 昵称
     * phone 手机号
     * @return
     */
    @Override
    @PassToken
    public CommonResult Register(@RequestBody RepairAdmin registerdo, BindingResult bindingResult) {
        if(registerdo == null){
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }
        int result = 0;
        try{
            result = repairAdminService.insertRepairAdmin(registerdo);
        }catch (Exception e){
            if(e.getCause().getMessage().contains("'repair_admin.account_un'")){
                return CommonResult.fail("该账号已存在！");
            }
            return CommonResult.fail("系统异常，注册失败！");
        }
        if(result > 0){
            return CommonResult.ok("注册成功！");
        }
        return CommonResult.fail("注册失败！");
    }

    /**
     * 登录接口
     * @param loginRequest
     * account 账号
     * password 密码
     * @return
     *
     */
    @Override
    @PassToken
    public CommonResult Login(@RequestBody loginRequest loginRequest, BindingResult bindingResult) {
        if(loginRequest == null){
            return CommonResult.fail("请传递参数");
        }
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        RepairAdmin result = repairAdminService.getRepairByAccount(loginRequest.getAccount());
        if(result == null){
            return CommonResult.fail("账号或密码错误");
        }

        if(result.getStatu() == eStatu.Freeze.getValue()){
            return CommonResult.fail("该账号已被冻结");
        }

        String encPassword = AesUtils.encrypt(loginRequest.getPassword());
        if(!encPassword.equals(result.getPassword())){
            return CommonResult.fail("密码错误");
        }
        SmartUser user = new SmartUser();
        String token = JWTUtil.getToken(user,null);
        LoginVO login = new LoginVO();
        login.setToken(token);
        login.setTokenTtl(JWTUtil.getExpired());
        login.setUserName(result.getUsername());
        login.setUserhead(AesUtils.encrypt(result.getId()));

        return CommonResult.ok("登录成功",login);
    }

    /**
     * 修改密码
     * @param cpr
     * oldPassword 旧密码
     * newPassword 新密码
     * confirmPassword 确认密码
     * @param userhead
     * @param bindingResult
     * @return
     */
    @Override
    public CommonResult ChangePassword(changePasswordRequest cpr, @RequestHeader("user_head") String userhead, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String st = paramUtils.getParamError(bindingResult);
            return CommonResult.fail(st);
        }

        if(!cpr.getNewPassword().equals(cpr.getConfirmPassword())){
            return CommonResult.fail("确认密码和新密码不一致！");
        }

        String userID = AesUtils.decrypt(userhead);
        RepairAdmin operateData = repairAdminService.getRepairById(userID);
        if(operateData == null){
            return CommonResult.fail("当前账号不合法！");
        }

        if(operateData.getStatu() == eStatu.Freeze.getValue()){
            return CommonResult.fail("该账号已被冻结");
        }

        if(!AesUtils.encrypt(cpr.getOldPassword()).equals(operateData.getPassword())){
            return CommonResult.fail("原密码错误！");
        }

        RepairAdmin ra = new RepairAdmin();
        ra.setId(userID);
        ra.setPassword(AesUtils.encrypt(cpr.getNewPassword()));
        int result = repairAdminService.updateRepairAdmin(ra);

        return result > 0 ? CommonResult.ok("修改成功") : CommonResult.fail("修改失败");
    }
}
