package com.YuyeTech.HeartRate.game.userinfo.controller;

import com.YuyeTech.HeartRate.annotation.Decrypt;
import com.YuyeTech.HeartRate.annotation.Encrypt;
import com.YuyeTech.HeartRate.common.pojo.PageInfo;
import com.YuyeTech.HeartRate.common.pojo.Result;
import com.YuyeTech.HeartRate.game.userinfo.projo.UserInfo;
import com.YuyeTech.HeartRate.game.userinfo.service.MainInfoService;
import com.YuyeTech.HeartRate.game.userinfo.service.UserInfoService;
import com.YuyeTech.HeartRate.game.userinfo.vo.MainInfoVo;
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.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

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

    @Autowired
    private MainInfoService mainInfoService;

    @Autowired
    private UserInfoService userInfoService;



    /**
     * 前端view跳转,配置菜单路径
     * 设置默认参数
     *
     * @return
     */
    @GetMapping("gotoView")
    public ModelAndView gotoView() {
        ModelAndView modelAndView = new ModelAndView("client/userInfo");
        return modelAndView;
    }

    /**
     * 分页查询数据
     *
     * @param vo
     * @return
     */
    @PostMapping("findAll")
    @Decrypt
    @Encrypt
    public Result<PageInfo<MainInfoVo>> findAll(MainInfoVo vo) {
        return mainInfoService.page(vo);
    }


    /**
     * 用户列表
     *
     * @param page 第几页, 从1页开始
     * @param size 一页有多少条数据
     * @return
     */
    @GetMapping("/list")
    @Deprecated
    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
     * @return
     */
    @PostMapping("/findUserDetail")
    public Result findUserDetail(@RequestParam("userId") String userId) {
        //预删除数据库数据
        return userInfoService.get(userId);
    }


    /**
     * 删除用户数据
     *
     * @param userId
     * @return
     */
    @PostMapping("/deleteUser")
    public Result deleteUser(@RequestParam("userId") String userId) {
        //预删除数据库数据
        String msg = mainInfoService.deleteMainInfoById(userId,"deleteUser");
        return Result.of(msg);
    }

    /**
     * 恢复挂起的用户数据
     * @param userId
     * @return
     */
    @PostMapping("/restoreUser")
    public Result restoreUser(@RequestParam("userId") String userId) {
        //预删除数据库数据
        MainInfoVo mainInfoVo = mainInfoService.restoreMainInfoById(userId);
        return Result.of(mainInfoVo);
    }

    /**
     * 用户详情
     *
     * @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);
//        }
//
//    }
}
