package io.renren.modules.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;

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.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.TypesOfAttendersEntity;
import io.renren.modules.admin.service.TypesOfAttendersService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 参会人员类型表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-24 10:37:48
 */
@RestController
@RequestMapping("admin/typesofattenders")
public class TypesOfAttendersController {
    @Autowired
    private TypesOfAttendersService typesOfAttendersService;

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

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


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:typesofattenders:info")
    public R info(@PathVariable("id") Long id){
		TypesOfAttendersEntity typesOfAttenders = typesOfAttendersService.getById(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:typesofattenders:save")
    public R save(@RequestBody TypesOfAttendersEntity typesOfAttenders){
        //创建时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
        java.util.Date time = null;
        try {
            time= sdf.parse(sdf.format(new Date()));

        } catch (ParseException e) {
            e.printStackTrace();
        }
        typesOfAttenders.setCreateTime(time);
		typesOfAttendersService.save(typesOfAttenders);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:typesofattenders:update")
    public R update(@RequestBody TypesOfAttendersEntity typesOfAttenders){
		typesOfAttendersService.updateById(typesOfAttenders);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:typesofattenders:delete")
    public R delete(@RequestBody Long[] ids){
		typesOfAttendersService.delete(ids);

        return R.ok();
    }

    /**
     * 通过会议id参会人员类型列表
     * @param params
     * @return
     */
    @GetMapping("/listbymid/{mid}")
    @RequiresPermissions("admin:typesofattenders:list")
    public R listBymeetingId(@PathVariable("mid") Long mid) {
        List<TypesOfAttendersEntity> list = typesOfAttendersService.list(
            new QueryWrapper<TypesOfAttendersEntity>().eq("meeting_id", mid)
        );
        return R.ok().put("list", list);
    }

    /**
     * 通过类型进行查询
     */
    @RequestMapping("/findAllByType")
    @RequiresPermissions("admin:typesofattenders:findAllByType")
    public List<TypesOfAttendersEntity> findAllByType (@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        List<TypesOfAttendersEntity> allByType = typesOfAttendersService.findAllByType(name);
        return  allByType;
    }

    /**
     * 类型和会议id 是否存在
     */
    @RequestMapping("/selectIsnameAndId")
    @RequiresPermissions("admin:typesofattenders:selectIsnameAndId")
    public int selectIsnameAndId(@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        Long meetingId = Long.valueOf(params.get("meeting_id").toString());
        int i = typesOfAttendersService.selectIsnameAndId(name, meetingId);
        return i;
    }

}
