package io.renren.modules.admin.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
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.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.common.annotation.SysLog;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.EmployeeEntity;
import io.renren.modules.admin.service.EmployeeService;



/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-02-17 13:38:28
 */
@RestController
@RequestMapping("admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

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

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

    /**
     * 列表
     */
    @RequestMapping("/listtest")
    @RequiresPermissions("admin:employee:list")
    public R ListTest(@RequestParam Map<String, Object> params){
        String key = (String) params.get("key");
        int p = Integer.valueOf(params.get("page").toString());
        int limit = Integer.valueOf(params.get("limit").toString());
        PageUtils page = employeeService.queryList(p,limit, key);
        return R.ok().put("page",page);
    }

    /**
     * findAll
     */
    @RequestMapping("/findAllByisDel/{is_del}")
    @RequiresPermissions("admin:employee:findAllByisDel")
    public List<EmployeeEntity> findAllByisDel(@PathVariable("is_del") int isDel){
        List<EmployeeEntity> employeeEntityList = employeeService.findAllByisDel(isDel);
        return  employeeEntityList;
    }

    /**
     * findAllByTrueName
     */
    @RequestMapping("/findAllByTrueName")
    @RequiresPermissions("admin:employee:findAllByTrueName")
    public List<EmployeeEntity> findAllByTrueName(@RequestParam Map<String,Object> params){
        String truename = (String) params.get("truename");
        List<EmployeeEntity> allByTrueName = employeeService.findAllByTrueName(truename);
        return  allByTrueName;
    }

    /**
     * 信息
     */

    @RequestMapping("/info/{id}")
    @RequiresPermissions("admin:employee:info")
    public R info(@PathVariable("id") Long id){
		EmployeeEntity employee = employeeService.getById(id);

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

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("admin:employee:save")
    public R save(@RequestBody EmployeeEntity employee){
//        Date date = new Date();
////        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
////        String format = dateFormat.format(date);
        //创建时间
        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();
        }
        employee.setCreateTime(time);
		employeeService.save(employee);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("admin:employee:update")
    public R update(@RequestBody EmployeeEntity employee){


        //修改时间
        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();
        }
        employee.setModifyTime(time);
		employeeService.updateById(employee);
        return R.ok();
    }

    /**
     * 逻辑删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("admin:employee:delete")
    public R delete(@RequestBody Long[] ids){
		employeeService.delete(ids);
        return R.ok();
    }

//    /**
//     * 删除
//     */
//    @RequestMapping("/delete/{id}")
//    @RequiresPermissions("admin:employee:delete")
//    public R delete(@PathVariable("id") Long id){
//		//employeeService.removeByIds(Arrays.asList(ids));
//        boolean flag = employeeService.delete(id);
//        if(flag){
//            R.ok("逻辑删除成功");
//
//
//        }
//
//        return R.ok();
//    }

//    /**
//     * 重置密码
//     */
//    @SysLog("修改密码")
//    @PostMapping("/password/{id}")
//    @RequiresPermissions("admin:employee:password")
//    public R password(@PathVariable("id") Long id){
//        boolean flag = employeeService.updatePasswordByid(id);
//
//        if(flag){
//            return  R.ok("重置密码成功");
//        }
//        return R.ok();
//
//    }

    /**
     * 重置密码
     */
    @SysLog("修改密码")
    @PostMapping("/password")
    @RequiresPermissions("admin:employee:password")
    public R password(@RequestParam Map<String,Object> params){
        Long id = Long.valueOf(params.get("id").toString());
        boolean flag = employeeService.updatePasswordByid(id);

        if(flag){
            return  R.ok("重置密码成功");
        }
        return R.ok();
    }

    /**
     * 模糊查询name获取 employee(获得id在分配人员时使用)
     */
    @GetMapping("/listbyname")
    @RequiresPermissions("admin:employee:list")
    public R listByName(@RequestParam Map<String,Object> params){
        String truename = (String) params.get("truename");
        List<Map<String, Object>> list = employeeService.selectNameList(truename);
        return  R.ok().put("list", list);
    }

    /**
     * 模糊查询company_id获取 employee id,name(获得id在分配人员时使用)
     */
    
    @GetMapping("/listbycid/{cid}")
    @RequiresPermissions("admin:employee:list")
    public R selectNameListByCompanyId(@PathVariable("cid") Long cid) {
        Map<Long, String> list = employeeService.selectNameListByCompanyId(cid);
        return  R.ok().put("list", list);
    }
    // 默认单位id1
    @GetMapping("/listbycid")
    @RequiresPermissions("admin:employee:list")
    public R selectNameListByCompanyId() {
        Map<Long, String> list = employeeService.selectNameListByCompanyId(1L);
        return  R.ok().put("list", list);
    }
}
