package io.renren.modules.app.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.MyUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.PaperEntity;
import io.renren.modules.admin.entity.PaperviewEntity;
import io.renren.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.entity.AttendersviewEntity;
import io.renren.modules.admin.entity.MemberEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.AttendersviewService;
import io.renren.modules.admin.service.MemberService;
import io.renren.modules.admin.service.PaperService;
import io.renren.modules.app.annotation.Login;


/**
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-12 10:52:23
 */
@RestController
@RequestMapping("app/paper")
public class AppPaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private AttendersService attendersService;

    /**
     * 保存
     */
    @Login
    @PostMapping("/save")
    public R save(@RequestBody PaperEntity paper, @RequestAttribute("userId") Integer userId){
        Long meetingId = paper.getMeetingId();
        if (meetingId == null || meetingId == 0L) {
            return R.error("会议id不可为空");
        }
        Long uid = userId.longValue();

        AttendersEntity attenders = attendersService.getByMMId(uid, meetingId);

        if (attenders == null) {
            return R.error("报名信息为空, 请先报名参会");
        }

        paper.setAttendersId(attenders.getId());
        paper.setTitle(paper.getTitle());
        paper.setUrl(paper.getUrl());
        paper.setType(paper.getType());
        paper.setStatus(0);
        paper.setRemark(paper.getRemark());
        paper.setCreateTime(new Date());

        paperService.save(paper);
        
        return R.ok();
    }
}
