package com.keao.edu.user.controller;


import com.keao.edu.auth.api.client.SysUserFeignService;
import com.keao.edu.auth.api.entity.SysUser;
import com.keao.edu.common.controller.BaseController;
import com.keao.edu.common.entity.HttpResponseResult;
import com.keao.edu.common.page.PageInfo;
import com.keao.edu.common.tenant.TenantContextHolder;
import com.keao.edu.user.entity.Employee;
import com.keao.edu.user.page.EmployeeQueryInfo;
import com.keao.edu.user.service.EmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("employee")
@Api(tags = "员工服务")
public class EmployeeController extends BaseController {

	@Autowired
	private EmployeeService employeeService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	
	@ApiOperation("员工服务分页查询")
	@GetMapping(value = "/list")
	public HttpResponseResult<PageInfo<Employee>> getList(EmployeeQueryInfo queryInfo) {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		queryInfo.setUserId(sysUser.getId());
		/*if(queryInfo.getOrganId() != null){
			queryInfo.setOrganIds(organizationService.getChildOrganIds(queryInfo.getOrganId(),true));
		}*/
		return succeed(employeeService.queryPage(queryInfo));
	}

	@ApiOperation("查询员工详情")
	@ApiImplicitParam(name = "id", value = "员工ID", required = true, dataType = "Integer", paramType = "path")
	@GetMapping(value = "/query")
	public HttpResponseResult<Employee> query(Integer id) {
		return succeed(employeeService.getEmployee(id));
	}

	@ApiOperation("新增员工")
	@PostMapping(value = "/add")
	public HttpResponseResult add(@RequestBody Employee employee) {
//        SysUser sysUser = sysUserFeignService.queryUserInfo();
//        employee.setOrganId(sysUser.getId());
		employee.setTenantId(TenantContextHolder.getTenantId());
	    employeeService.add(employee);
		return succeed();
	}

	@ApiOperation("更新员工")
	@PostMapping(value = "/update")
	public HttpResponseResult update(@RequestBody Employee employee) {
		employeeService.updateEmployee(employee);
		return succeed();
	}

	@ApiOperation(value = "获取用户信息")
	@GetMapping("/queryUserInfo")
	public Object apiQueryUserInfo() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if(sysUser != null && sysUser.getId() != null){
//			Employee employee = employeeService.get(sysUser.getId());
//			sysUser.setRoles(employeeDao.queryUserRole(sysUser.getId()));
//			sysUser.setContactAddress(employee.getContactAddress());
//			sysUser.setPostalCode(employee.getPostalCode());
			return succeed(sysUser);
		}
		return failed("获取用户信息失败");
	}

	@ApiOperation("删除")
	@PostMapping(value = "/del/{id}")
	public HttpResponseResult add(@PathVariable("id") Integer id) {
		employeeService.del(id);
		return succeed();
	}

}
