package io.renren.modules.admin.controller;

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.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.entity.ReportEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.ReportService;
import io.renren.modules.admin.entity.ScheduleEntity;
import io.renren.modules.admin.service.ScheduleService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;


/**
 * 报告管理
 *
 * @author huwhois
 * @email huwhois@163.com
 * @date 2020-12-29 10:27:09
 */
@RestController
@RequestMapping("admin/report")
public class ReportController {
    @Autowired
    private ReportService reportService;
    @Autowired
    private AttendersService attendersService;
    @Autowired
    private ScheduleService scheduleService;

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

        PageUtils page = reportService.queryPage(params);

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

    /**
     * 信息
     */
    @GetMapping("/info/{aid}")
    @RequiresPermissions("admin:report:info")
    public R info(@PathVariable("aid") Long aid){
        ReportEntity report = reportService.getById(aid);

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

    /**
     * 新增
     * @param report
     * @return
     */
    @PostMapping("/save")
    @RequiresPermissions("admin:report:update")
    public R save(@RequestBody ReportEntity report){
        AttendersEntity attenders = attendersService.getById(report.getAid());
        if (attenders==null) {
            return R.error("参会人员不存在");
        }
        attenders.setIsReport(1);
        attenders.setReportTitle(report.getTitle());
        attendersService.updateById(attenders);

        ScheduleEntity schedule = new ScheduleEntity();
        schedule.setAttendersId(report.getAid());
        schedule.setBranchId(report.getBranchId());
        schedule.setSort(report.getSort());
        scheduleService.save(schedule);
        
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @RequiresPermissions("admin:report:update")
    public R update(@RequestBody ReportEntity report){
        AttendersEntity attenders = attendersService.getById(report.getAid());
        if (attenders==null) {
            return R.error("参会人员不存在");
        }
        attenders.setIsReport(1);
        attenders.setReportTitle(report.getTitle());

        attendersService.updateById(attenders);

        ScheduleEntity schedule = new ScheduleEntity();
        schedule.setAttendersId(report.getAid());
        schedule.setBranchId(report.getBranchId());
        schedule.setSort(report.getSort());
        if (report.getId()==null || report.getId()==0L) {
            scheduleService.save(schedule);
        } else {
            schedule.setId(report.getId());
            scheduleService.updateById(schedule);
        }
        
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete/{aid}")
    @RequiresPermissions("admin:report:delete")
    public R delete(@PathVariable("aid") Long aid){
        // reportService.removeByIds(Arrays.asList(aids));
        ReportEntity report = reportService.getById(aid);
        if (report!=null) {
            scheduleService.removeById(report.getId());
        }

        attendersService.removeReport(aid);

        return R.ok();
    }

    /**
     * 分配会场
     */
    @PostMapping("/branch")
    @RequiresPermissions("admin:report:update")
    public R branch(@RequestBody Map<String, Object> params){
        Long aid = Long.valueOf(params.get("aid").toString());
        Long branchId = Long.valueOf(params.get("branchId").toString());
        Long id = params.containsKey("id") ? Long.valueOf(params.get("id").toString()) : 0L;

        ScheduleEntity schedule = new ScheduleEntity();
        schedule.setAttendersId(aid);
        schedule.setBranchId(branchId);
        if (id==0L) {
            scheduleService.save(schedule);
        } else {
            schedule.setId(id);
            scheduleService.updateById(schedule);
        }

        return R.ok();
    }

    /**
     * listbybranch
     */
    @GetMapping("/listbranch/{mid}/{bid}/{sort}")
    @RequiresPermissions("admin:report:list")
    public R listByBranch(@PathVariable("mid") Long mid, @PathVariable("bid") Long bid, @PathVariable("sort") String sort){
        List<ReportEntity> list = reportService.listByBranch(mid, bid, sort);

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

    @GetMapping("/listbranch/{mid}/{bid}")
    @RequiresPermissions("admin:report:list")
    public R listByBranch(@PathVariable("mid") Long mid, @PathVariable("bid") Long bid){
        List<ReportEntity> list = reportService.listByBranch(mid, bid, null);

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

    @GetMapping("/listbranch/{mid}")
    @RequiresPermissions("admin:report:list")
    public R listByBranch(@PathVariable("mid") Long mid){
        List<ReportEntity> list = reportService.listByBranch(mid, null);

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

    /**
     * 修改报告时间
     */
    @PostMapping("/time")
    @RequiresPermissions("admin:report:update")
    public R update(@RequestBody ScheduleEntity schedule){
        scheduleService.updateById(schedule);
        
        return R.ok();
    }

    /**
     * 根据索引排序
     */
    @PostMapping("/sort")
    @RequiresPermissions("admin:report:update")
    public R update(@RequestBody List<ScheduleEntity> scheduleList){
        // scheduleService.updateById(schedule);
        for (ScheduleEntity schedule : scheduleList) {
            scheduleService.updateById(schedule);
        }

        return R.ok();
    }
}
