package io.renren.modules.admin.controller;

import java.util.Arrays;
import java.util.Date;
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.modules.admin.entity.MeetingEntity;
import io.renren.modules.admin.service.MeetingService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 会议表
 *
 * @author huwhois
 * @email huwhois@163.com
 * @date 2020-12-09 14:33:56
 */
@RestController
@RequestMapping("admin/meeting")
public class MeetingController {
    @Autowired
    private MeetingService meetingService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:meeting:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = meetingService.queryPage(params);

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

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:meeting:info")
    public R info(@PathVariable("id") Long id){
        MeetingEntity meeting = meetingService.getById(id);

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

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:meeting:save")
    public R save(@RequestBody MeetingEntity meeting){
        meeting.setCreateTime(new Date());
        meetingService.save(meeting);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:meeting:update")
    public R update(@RequestBody MeetingEntity meeting){
        meeting.setModifyTime(new Date());
        meetingService.updateById(meeting);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:meeting:delete")
    public R delete(@RequestBody Long[] ids){
        meetingService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

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

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

    /**
     * 保存分会
     */
    @PostMapping("/savebranch")
    @RequiresPermissions("admin:meeting:save")
    public R saveBranch(@RequestBody MeetingEntity meeting){
        if (meeting.getPid()==null || meeting.getPid()==0L) {
            return R.error("会议父id(pid)不能为空或0");
        }

        meeting.setCreateTime(new Date());
        meetingService.save(meeting);

        return R.ok();
    }
}
