package com.YuyeTech.TPlat.controller;

import com.YuyeTech.TPlat.dataobject.UserInfo;
import com.YuyeTech.TPlat.enums.ResultEnum;
import com.YuyeTech.TPlat.exception.UserException;
import com.YuyeTech.TPlat.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author:slambb
 * @date:2020/6/3
 */
@Controller
@RequestMapping("/backstage_user")
@Slf4j
public class BackstageUserController {

    @Autowired
    private UserInfoService userInfoService;


    /**
     * 用户列表
     * @param page 第几页, 从1页开始
     * @param size 一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                             @RequestParam(value = "size", defaultValue = "10") Integer size,
                             Map<String, Object> map) {
        PageRequest request = PageRequest.of(page - 1, size);
        Page<UserInfo> userInfos = userInfoService.findAllUserInfoByPageable(request);
        map.put("userInfo", userInfos);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("/user/list", map);
    }

    /**
     * 用户详情
     * @param userId
     * @param map
     * @return
     */
    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam("userId") String userId,
                               Map<String, Object> map) {

        //todo 1.查询用户信息
        UserInfo userInfo = userInfoService.findUserInfoById(userId,true);
        if(userInfo == null){
            map.put("msg","用户信息不存在！");
            map.put("url","/backstage_user/list");
            return new ModelAndView("common/error",map);
        }
        map.put("userInfo", userInfo);

        return new ModelAndView("user/detail", map);
    }

    @RequestMapping(value = "/")
    public String index() {
        return "index";
    }


    /**
     * 根据用户名搜索信息
     * @return
     */
    @PostMapping("/searchUserInfo")
    public ModelAndView searchUserInfo(
            @RequestParam(value = "username")String username,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            Map<String, Object> map) {
        if(page<1){
            map.put("msg", "page不能小于1.");
            map.put("url", "/backstage_user/list");
            return new ModelAndView("common/error", map);
        }
        if(StringUtils.isEmpty(username)){
            map.put("msg", ResultEnum.USER_NAME_ERROR.getMessage());
            map.put("url", "/backstage_user/list");
            return new ModelAndView("common/error", map);
        }
        //todo 根据用户名搜索,查询用户信息
        try {
            PageRequest request = PageRequest.of(page-1, size);
            Page<UserInfo> userInfoPage =  userInfoService.searchByUsername(request,username);
            map.put("userInfo",userInfoPage);
            map.put("username",username);
            map.put("currentPage", page);
            map.put("size", size);
            return new ModelAndView("/user/list", map);
        } catch (UserException e) {
            map.put("msg", e.getMessage());
            map.put("url", "/backstage_user/list");
            return new ModelAndView("common/error", map);
        }

    }
}
