package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dto.request.AddUserRequest;
import com.example.demo.dto.request.UserRequest;
import com.example.demo.dto.response.BaseResponse;
import com.example.demo.dto.response.UserDto;
import com.example.demo.entity.User;
import com.example.demo.entity.WrongBook;
import com.example.demo.mapper.UserMapper;
import com.example.demo.mapper.WrongBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author hxh
 * @Date 2020-09-23 19:46
 * @Descrption TODO
 */
@Service
public class UserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    WrongBookMapper wrongBookMapper;

    String[] _grade = new String[]{"一年级","二年级","三年级","四年级","五年级","六年级"};
    String[] _subject = new String[]{"语文","数学","英语"};

    public BaseResponse<UserDto> login(UserRequest request){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",request.getUsername());
        User user = userMapper.selectOne(wrapper);
        UserDto userDto = new UserDto(user.getId(),user.getUsername(),user.getNickname());
        BaseResponse<UserDto> response = new BaseResponse(0,"",null);
        if(user == null){
            response.setCode(110);
            response.setMessage("用户名不存在！");
        }else {
            if(request.getPassword().compareTo(user.getPassword())!=0){
                response.setMessage("密码错误");
                response.setCode(111);
                userDto.setNickname("");
            }else{
                response.setMessage("登录成功");
                response.setData(userDto);
                response.setCode(200);
            }
        }
        return response;
    }

    public BaseResponse<UserDto> register(AddUserRequest request){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username",request.getUsername());
        User user = userMapper.selectOne(wrapper);

        BaseResponse<UserDto> response = new BaseResponse(0,"",null);
        if(user != null){
            response.setCode(110);
            response.setMessage("账户名已存在！");
        }else{
            User insertUser = new User(null,request.getUsername(),request.getPassword(),request.getNickname());
            userMapper.insert(insertUser);
            response.setCode(200);
            response.setMessage("注册成功");
            //每创建一个用户新增错题本
            for(String grade:_grade){
                for(String subject:_subject){
                    WrongBook wrongBook = new WrongBook(grade+subject+"","小学",grade,subject,"",insertUser.getId());
                    wrongBookMapper.insert(wrongBook);
                }
            }
            //
            response.setData(new UserDto(insertUser.getId(),insertUser.getUsername(),insertUser.getNickname()));
        }
        return response;
    }
}
