package com.orangesale.cn.controller;

import com.orangesale.cn.entity.OrangeUser;
import com.orangesale.cn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author zhijun
 * @Date 2019/12/13
 */
@RestController
@RequestMapping("/orange/user")
@CrossOrigin
public class OrangeUserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录验证
     *
     * @param orangeUser
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Map<String, Object> userLogin(@RequestBody OrangeUser orangeUser) {
        Map<String, Object> map = new HashMap<>();
        OrangeUser user = userService.userLogin(orangeUser);
        if (Objects.isNull(user)) {
            map.put("flag", false);
            map.put("msg", "login error");
            return map;
        }
        map.put("flag", true);
        map.put("data", user);
        return map;
    }

    /**
     * 用户注册验证
     *
     * @param orangeUser
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Map<String, Object> userRegister(@RequestBody OrangeUser orangeUser) {
        Map<String, Object> map = new HashMap<>();
        Integer insertFlag = userService.userRegister(orangeUser);
        if (insertFlag > 0) {
            map.put("flag", true);
            map.put("msg", "register success");
            return map;
        }
        map.put("flag", false);
        map.put("msg", "register success");
        return map;
    }

}
