package io.renren.modules.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
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.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.SubjectEntity;
import io.renren.modules.admin.service.SubjectService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-26 16:13:09
 */
@RestController
@RequestMapping("admin/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

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

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


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:subject:info")
    public R info(@PathVariable("id") Long id){
		SubjectEntity subject = subjectService.getById(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:subject:save")
    public R save(@RequestBody SubjectEntity subject){
        //创建时间
        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();
        }
        subject.setCreateTime(time);
		subjectService.save(subject);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:subject:update")
    public R update(@RequestBody SubjectEntity subject){
		subjectService.updateById(subject);

        return R.ok();
    }

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

        return R.ok();
    }

    /**
     * findAllByName 根据name进行模糊搜索
     */

    @RequestMapping("/findAllByName")
    @RequiresPermissions("admin:subject:findAllByName")
    public List<SubjectEntity> findAllByName(@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        List<SubjectEntity> allByName = subjectService.findAllByName(name);
        return allByName;
    }

    /**
     * selectIsName 查询name是否存在
     */
    @RequestMapping("/selectIsName")
    @RequiresPermissions("admin:subject:selectIsName")
    public int selectIsName(@RequestParam Map<String,Object> params){
        String name = (String) params.get("name");
        int i = subjectService.selectIsName(name);
        return i;
    }
   
    @RequestMapping("/test")
    public int testTT(@RequestParam Map<String,Object> params){
        System.out.println(params);
        
        return 1;
    }
}
