package com.YuyeTech.HeartRate.game.controller;

import com.YuyeTech.HeartRate.game.userinfo.projo.UserInfo;
import com.YuyeTech.HeartRate.dto.PageInfo;
import com.YuyeTech.HeartRate.dto.PageInfoDTO;
import com.YuyeTech.HeartRate.game.gameInfo.service.GameTagReferenceService;
import com.YuyeTech.HeartRate.game.userinfo.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private GameTagReferenceService gameTagReferenceService;



    /**
     * 标签列表
     * @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);
//        PageImpl page1 = new PageImpl<>();
//        map.put("tags", tags);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("/tag/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);
    }

    /**
     * DataTable分页
     * @param draw
     * @param start
     * @param length
     * @return
     */
    @RequestMapping(value = "/page",method = RequestMethod.POST)
    @ResponseBody
    public Object article_table(@RequestParam(value = "draw",required = true,defaultValue = "0")String draw,
                                               @RequestParam(value = "start",required = true,defaultValue = "0")String start,
                                               @RequestParam(value = "length",required = true,defaultValue = "10")String length){
        //转换为整形
        Integer drawInt = Integer.parseInt(draw);
        Integer startInt = Integer.parseInt(start);
        Integer lengthInt = Integer.parseInt(length);

        PageRequest request = PageRequest.of(startInt, lengthInt);
//        PageInfo为自定义dto对象
        PageInfo<PageInfoDTO> pageInfo = new PageInfo<>();
//        gameTagReferenceService.findByGameId("1595048084937150416", request)
        List<String> arrayList = new ArrayList<>();

//        arrayList.add(1)
        pageInfo.setData(gameTagReferenceService.findByGameId("1595048084937150416", request));
        pageInfo.setDraw(drawInt);
        pageInfo.setRecordsTotal(1);
        pageInfo.setRecordsFiltered(1);

        return pageInfo;
    }

}
