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.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;


/**
 * 
 * @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;
    @Autowired
    private AttendersviewService attendersviewService;
    @Autowired
    private MemberService memberService;


    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody PaperviewEntity paperForm){
        Long meetingId = paperForm.getMeetingId();
        if (meetingId == null || meetingId == 0L) {
            return R.error("会议id不可为空");
        }

        if (paperForm.getAttendersId()==null || paperForm.getAttendersId()==0L) {
            String symbol = MyUtils.md5(paperForm.getTruename() + paperForm.getPhone());

            AttendersviewEntity attendersview = attendersviewService.getBySymbolAndMid(symbol, meetingId);
            
            if (attendersview==null) {
                MemberEntity member = memberService.saveMember(paperForm.getTruename(), paperForm.getPhone(), paperForm.getOrganization(), null);

                Long memberId = member.getId();
                // 存参会信息 
                AttendersEntity attenders = new AttendersEntity();
                attenders.setMemberId(memberId);
                attenders.setMeetingId(meetingId);
                attendersService.save(attenders);
                paperForm.setAttendersId(attenders.getId());
            } else {
                paperForm.setAttendersId(attendersview.getId());
            }
        }

        PaperEntity paper = new PaperEntity();
        paper.setAttendersId(paperForm.getAttendersId());
        paper.setTitle(paperForm.getTitle());
        paper.setUrl(paperForm.getUrl());
        paper.setType(paperForm.getType());
        paper.setStatus(0);
        paper.setRemark(paperForm.getRemark());
        paper.setCreateTime(new Date());

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