package com.admin.web.controller.third;

import cn.hutool.core.lang.Assert;
import com.admin.common.constant.Constants;
import com.admin.common.core.domain.AjaxResult;
import com.admin.common.core.domain.entity.SysUser;
import com.admin.common.exception.ServiceException;
import com.admin.framework.web.service.SysLoginService;
import com.admin.system.service.ISysUserService;
import com.admin.web.controller.third.request.SsoRequest;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class SSOLoginController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Value("${sso.secret}")
    private String secret;


    /**
     * 登录方法
     *
     * @return 结果
     */
    @PostMapping("/ssoLogin")
    @ApiOperation(value = "sso登陆接口")
    public AjaxResult login( @RequestBody SsoRequest request)
    {
        String username = request.getUsername();
        String strSysDatetime = request.getStrSysDatetime();
        String jsName = request.getJsName();
        String verify = request.getVerify();

        String mtxt = SdxyMd5Sign.MD5(username + secret + strSysDatetime + jsName);

        Assert.notBlank(verify,"签名不能为空,请从统一登录页面访问");
        if( mtxt.equals(verify)){
            AjaxResult ajax = AjaxResult.success();
            // 对第一次进来的创建一个默认账号
            SysUser sysUser = userService.selectUserByUserName(username);
            if( sysUser == null ){
                this.createDefaultAccount(username,jsName);
            }

            // 生成令牌
            String token = loginService.login(username, "abc.123", null,null);
            ajax.put(Constants.TOKEN, token);
            return ajax;
        }

        throw new ServiceException("统一登陆验证签名不正确");
    }


    private void createDefaultAccount(String username,String jsName){
        SysUser sysUser = new SysUser();
        sysUser.setPassword(bCryptPasswordEncoder.encode("abc.123"));// 密码不重要
        sysUser.setUserName(username);
        sysUser.setNickName(jsName);
        sysUser.setStatus("0");
        sysUser.setDelFlag("0");
        sysUser.setPostIds(new Long[]{2L});
        sysUser.setRoleIds(new Long[]{100L});
        userService.insertUser(sysUser);
    }

    /**
     * 100837 石先德老师
     * 100819 余姗珊老师
     * 100891 韦芳良老师
     * @param args
     */
    public static void main(String[] args) {
        String username = "100891";
        String strSysDatetime = "2022";
        String jsName = "100891";
//        String verify = request.getVerify();

        String mtxt = SdxyMd5Sign.MD5(username + "gdsdxy" + strSysDatetime + jsName);
        System.out.println(mtxt);
    }
}
