//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.entity.User;
import com.luozhihui.project.mapper.PhoneMapper;
import com.luozhihui.project.mapper.UserRegMapper;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api"})
@CrossOrigin
public class UserRegController {
    @Resource
    UserRegMapper userRegMapper;
    @Resource
    PhoneMapper phoneMapper;

    public UserRegController() {
    }

    @CrossOrigin
    @PostMapping({"/user_register"})
    public ApiResponse UserReg(User user, String code, @RequestHeader(name = "Content-Type",required = false) String contentType) {
        if (this.isPhoneRegistered(user.getPhone())) {
            return new ApiResponse((Object)null, 400, "该手机号已被注册");
        } else {
            List<String> codes = this.phoneMapper.findByCode(user.getPhone());
            if (codes != null && !codes.isEmpty()) {
                String storedCode = (String)codes.get(0);
                String userInputCode = user.getCode();
                if (this.codeMatches(userInputCode, storedCode)) {
                    this.userRegMapper.save(user);
                    this.deleteCode(user.getPhone());
                    return new ApiResponse((Object)null, 200, "注册成功");
                } else {
                    return new ApiResponse((Object)null, 400, "验证码不匹配");
                }
            } else {
                return new ApiResponse((Object)null, 400, "注册失败");
            }
        }
    }

    private boolean codeMatches(String userInputCode, String storedCode) {
        return userInputCode != null && storedCode != null ? userInputCode.equals(storedCode) : false;
    }

    private boolean isPhoneRegistered(String phone) {
        if (phone == null) {
            return false;
        } else {
            User existingUser = this.userRegMapper.findByPhone(phone);
            return existingUser != null;
        }
    }

    private void deleteCode(String phone) {
        if (phone != null) {
            this.phoneMapper.deleteCode(phone);
        }
    }
}
