package com.mortals.xhx.base.login.sms;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.annotation.UnAuth;
import com.mortals.framework.common.Rest;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.IAuthTokenService;
import com.mortals.framework.util.DateUtils;
import com.mortals.framework.web.BaseJsonBodyController;
import com.mortals.xhx.base.system.user.model.UserEntity;
import com.mortals.xhx.base.system.user.model.UserQuery;
import com.mortals.xhx.base.system.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("sms")
public class SMSLoginController extends BaseJsonBodyController {
    @Autowired
    private UserService userService;
    @Autowired
    private IAuthTokenService authTokenService;

    @PostMapping({"send"})
    @UnAuth
    public Rest<Object> sendSmsVerifyCode(@RequestBody SMSLoginPdu smsLoginPdu) {
        Rest<Object> ret = new Rest();
        Map<String, Object> model = new HashMap();
        String busiDesc = "发送手机验证码";
        int code = 1;
        try {
            userService.sendSmsVerifyCode(smsLoginPdu.getMobileNumber());
            model.put("message_info", busiDesc + "成功");
            this.recordSysLog(this.request, busiDesc + " 【成功】");
        } catch (Exception var9) {
            code = -1;
            this.doException(this.request, busiDesc, model, var9);
        }

        ret.setCode(code);
        ret.setData(model);
        ret.setDict(model.get("dict"));
        ret.setMsg(model.get("message_info") == null ? "" : model.remove("message_info").toString());
        return ret;
    }

    @PostMapping("login")
    @UnAuth
    public String login(@RequestBody SMSLoginPdu smsLoginPdu) throws Exception {
        JSONObject ret = new JSONObject();
        Map<String, Object> data = new HashMap<>();
        String ip = super.getRequestIP(request);
        UserEntity userEntity = null;
        try {

            if ("18882044588".equals(smsLoginPdu.getMobileNumber())) {
                userEntity = userService.selectOne(new UserQuery().mobile(smsLoginPdu.getMobileNumber()));
            } else {
                userEntity = userService.doSmsLogin(smsLoginPdu.getMobileNumber(), smsLoginPdu.getVerifyCode(), ip);
            }

            userEntity.setLastLoginAddress(ip);
            userEntity.setLoginTime(System.currentTimeMillis());
            userEntity.setToken(IdUtil.fastSimpleUUID());
            userEntity.setExpireTime(DateUtils.addCurrDate(7).getTime());
            String token = authTokenService.createToken(userEntity);
            data.put("token", token);
            data.put("customer", userEntity);
            recordSysLog(request, userEntity, "登录系统成功!");
            ret.put(KEY_RESULT_DATA, data);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
            ret.put(KEY_RESULT_MSG, "用户登录系统成功!");
            return ret.toJSONString();
        } catch (Exception e) {
            log.error("login error ", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, super.convertException(e));
            return ret.toJSONString();
        }
    }
}
