package com.ym.mec.web.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
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.RestController;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.EmployeeInfo;
import com.ym.mec.biz.dal.page.EmployeeInfoQueryInfo;
import com.ym.mec.biz.service.EmployeeInfoService;
import com.ym.mec.common.controller.BaseController;

@RequestMapping("employeeInfo")
@Api(tags = "员工信息服务")
@RestController
public class EmployeeInfoController extends BaseController {

    @Autowired
    private EmployeeInfoService employeeInfoService;
    
	@Autowired
	private SysUserFeignService sysUserFeignService;


    @ApiOperation(value = "新增员工信息")
    @PostMapping("/insert")
    @PreAuthorize("@pcs.hasPermissions('employeeInfo/insert')")
    public Object insert(@RequestBody EmployeeInfo employeeInfo) {
    	SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		employeeInfo.setOperatorId(sysUser.getId());
    	Date date = new Date();
    	employeeInfo.setUpdateTime(date);
    	employeeInfo.setCreateTime(date);
    	employeeInfoService.insert(employeeInfo);
        return succeed();
    }

    @ApiOperation(value = "修改员工信息")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('employeeInfo/update')")
    public Object update(@RequestBody EmployeeInfo employeeInfo) {
    	SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("用户信息获取失败");
		}
		employeeInfo.setOperatorId(sysUser.getId());
    	Date date = new Date();
    	employeeInfo.setUpdateTime(date);
    	employeeInfoService.update(employeeInfo);
        return succeed();
    }

    @ApiOperation(value = "分页查询员工列表")
    @GetMapping("/queryPage")
    @PreAuthorize("@pcs.hasPermissions('employeeInfo/queryPage')")
    public Object queryPage(EmployeeInfoQueryInfo queryInfo) {
        return succeed(employeeInfoService.queryPage(queryInfo));
    }

}
