package com.YuyeTech.TPlat.controller;

import com.YuyeTech.TPlat.VO.ResultVO;
import com.YuyeTech.TPlat.config.GameConfig;
import com.YuyeTech.TPlat.dataobject.*;
import com.YuyeTech.TPlat.form.GameInfoForm;
import com.YuyeTech.TPlat.service.*;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import com.YuyeTech.TPlat.utils.ImageUtils;
import com.YuyeTech.TPlat.utils.KeyUtil;
import com.YuyeTech.TPlat.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.FileOutputStream;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
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);

    }
}
