package io.renren.modules.admin.controller;

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

import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl.On;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import java.util.Date;
import java.util.List;

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.AttendersEntity;
import io.renren.modules.admin.entity.LectureEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.LectureService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

/**
 * 演讲(报告)表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-11 14:36:45
 */
@RestController
@RequestMapping("admin/lecture")
public class LectureController {
    @Autowired
    private LectureService lectureService;
    @Autowired
    private AttendersService attendersService;
    /**
     * 列表
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:lecture:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = lectureService.myQueryPage(params);

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

     /**
     * 列表2
     */
    @GetMapping("/listbybranch/{bid}")
    @RequiresPermissions("admin:lecture:list")
    public R list(@PathVariable("bid") Long bid) {
        List<Map<String, Object>> list= lectureService.getBranchList(bid);

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

    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @RequiresPermissions("admin:lecture:info")
    public R info(@PathVariable("id") Long id) {
        LectureEntity lecture = lectureService.getById(id);
        Long aId = lecture.getAttendersId();
        Map<String, Object> aEntity = attendersService.getNameById(aId);
        return R.ok().put("lecture", lecture).put("attenders", aEntity);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:lecture:save")
    public R save(@RequestBody LectureEntity lecture) {
        lecture.setCreateTime(new Date());
        lectureService.save(lecture);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:lecture:update")
    public R update(@RequestBody LectureEntity lecture) {
        lectureService.updateById(lecture);

        return R.ok();
    }

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

}
