package com.YuyeTech.HeartRate.game.controller;

import com.YuyeTech.HeartRate.VO.ResultVO;
import com.YuyeTech.HeartRate.dataobject.*;
import com.YuyeTech.HeartRate.game.gameInfo.projo.GameInfo;
import com.YuyeTech.HeartRate.game.gameInfo.service.RecommendService;
import com.YuyeTech.HeartRate.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author:slambb
 * @date:2020/6/15
 */
@RestController
@RequestMapping("/backstage_recommend")
@Slf4j
public class BackstageRecommendController {
    @Autowired
    private RecommendService recommendService;

    @PostMapping("/add")
    public ResultVO addGame(
            @RequestParam(value = "gameId") String gameId,
            @RequestParam(value = "gameType") Integer gameType,
            @RequestParam(value = "endTime") String endTime,
            @RequestParam(value = "showTime") String showTime,
            Map<String, Object> map) {

        GameInfo gameInfo = new GameInfo();
        map.put("gameInfo", gameInfo);

        Recommend recommend = new Recommend();
        recommend.setRObjectId(gameId);
        if(gameType.equals(1)){
            recommend.setRecommendType(2);
        }else{
            recommend.setRecommendType(1);
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");//注意月份是MM
        ParsePosition pos = new ParsePosition(0);
        Date date = simpleDateFormat.parse(endTime,pos);
        recommend.setCreateTime(date);
        ParsePosition pos2 = new ParsePosition(0);
        Date date2 = simpleDateFormat.parse(showTime,pos2);
        recommend.setShowTime(date2);
        recommend = recommendService.addRecommend(recommend);

        return ResultVOUtil.success(recommend);
    }

    @PostMapping("/delete")
    public ResultVO delete(@RequestParam(value = "gameId") String gameId,
                           Map<String, Object> map) {

        log.info("删除推荐的游戏：" + gameId);
        String str = recommendService.deleteByGameId(gameId);
        map.put("msg", str);
        return ResultVOUtil.success(map);

    }
}
