package com.izouma.nineth.web.mobile.open.v1;


import com.izouma.nineth.domain.User;
import com.izouma.nineth.dto.PageQuery;
import com.izouma.nineth.dto.UserDTO;
import com.izouma.nineth.dto.UserRegister;
import com.izouma.nineth.enums.AuthorityName;
import com.izouma.nineth.exception.BusinessException;
import com.izouma.nineth.repo.UserRepo;
import com.izouma.nineth.security.Authority;
import com.izouma.nineth.service.UserRegisterService;
import com.izouma.nineth.service.UserService;
import com.izouma.nineth.utils.SecurityUtils;
import com.izouma.nineth.web.BaseController;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Map;

@AllArgsConstructor
@RestController
@RequestMapping("/user")
public class UserControllerOpenV1{
    private UserRepo userRepo;
    private UserService userService;

    @PostMapping("/all")
    public Page<UserDTO> all(@RequestBody PageQuery pageQuery) {
        if (!(SecurityUtils.getAuthenticatedUser() != null && SecurityUtils.getAuthenticatedUser().isAdmin())) {
            pageQuery.getQuery().put("hasRole", "ROLE_MINTER");
        }
        return userService.toDTO(userService.all(pageQuery));
    }

    @GetMapping("/get/{id}")
    public UserDTO get(@PathVariable Long id) {
        UserDTO userDTO = userService.toDTO(userRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
        return userDTO;
    }


    @PostMapping("/searchByPhone")
    public Map<String, Object> searchByPhone(@RequestParam String phone) {
        return userService.searchByPhone(phone);
    }

}


