package io.renren.modules.admin.controller;

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

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.common.utils.MyUtils;
import io.renren.common.utils.PageUtils;
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.admin.service.PaperviewService;


/**
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-12 10:52:23
 */
@RestController
@RequestMapping("admin/paper")
public class PaperController {
    @Autowired
    private PaperService paperService;
    @Autowired
    private AttendersService attendersService;
    @Autowired
    private AttendersviewService attendersviewService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private PaperviewService paperviewService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:paper:list")
    public R list(@RequestParam Map<String, Object> params){
        if (!params.containsKey("meetingId")) {
            return R.error("会议id不可为空");
        }
        PageUtils page = paperviewService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:paper:info")
    public R info(@PathVariable("id") Long id){
		PaperviewEntity paper = paperviewService.getById(id);
      
        return R.ok().put("paper", paper);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:paper:save")
    public R save(@RequestBody PaperviewEntity paperForm){
        Long meetingId = paperForm.getMeetingId();
        if (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());
        System.out.println(paperForm.getStatus());
        paper.setStatus(0);
        paper.setMoney(paperForm.getMoney());
        paper.setTax(paperForm.getTax());
        paper.setMail(paperForm.getMail());
        paper.setRemark(paperForm.getRemark());
        paper.setCreateTime(new Date());

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

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:paper:update")
    public R update(@RequestBody PaperviewEntity paperForm){
        Long meetingId = paperForm.getMeetingId();
        Long aid = paperForm.getAttendersId();
        if (meetingId == 0L || aid==0L) {
            return R.error("会议id不可为空, 参会名单id不能为空");
        }

        PaperEntity paper = new PaperEntity();
        paper.setId(paperForm.getId());
        paper.setAttendersId(paperForm.getAttendersId());
        paper.setTitle(paperForm.getTitle());
        paper.setUrl(paperForm.getUrl());
        paper.setType(paperForm.getType());
        paper.setStatus(paperForm.getStatus());
        paper.setMoney(paperForm.getMoney());
        paper.setTax(paperForm.getTax());
        paper.setMail(paperForm.getMail());
        paper.setRemark(paperForm.getRemark());

		paperService.updateById(paper);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:paper:delete")
    public R delete(@RequestBody Long[] ids){
		paperService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
