package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.Employee;
import com.ym.mec.biz.dal.enums.EmployeeOperateEnum;
import com.ym.mec.biz.dal.page.EmployeeQueryInfo;
import com.ym.mec.biz.service.EmployeeService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.StudentManageService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @Author Joburgess
 * @Date 2019/9/17
 */
@RequestMapping("employee")
@Api(tags = "员工管理")
@RestController
public class EmployeeController extends BaseController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private StudentManageService studentManageService;
    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "根据部门获取下面的员工")
    @GetMapping("/queryEmployByOrganId")
    @PreAuthorize("@pcs.hasPermissions('employee/queryEmployByOrganId')")
    public Object queryEmployByOrganId(EmployeeQueryInfo queryInfo) throws Exception {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed("用户信息获取失败");
        }
		if (!sysUser.getIsSuperAdmin()) {
            Employee employee = employeeService.get(sysUser.getId());
			if (StringUtils.isEmpty(queryInfo.getOrganId())) {
				queryInfo.setOrganId(employee.getOrganIdList());
			}else if(StringUtils.isEmpty(employee.getOrganIdList())){
                return failed("用户所在分部异常");
            }else {
                List<String> list = Arrays.asList(employee.getOrganIdList().split(","));
                if(!list.containsAll(Arrays.asList(queryInfo.getOrganId().split(",")))){
                    return failed("非法请求");
                }
            }
		}
        return succeed(employeeService.queryEmployByOrganId(queryInfo));
    }

    @ApiOperation(value = "员工操作")
    @PostMapping("/employeeOperate")
    @PreAuthorize("@pcs.hasPermissions('employee/employeeOperate')")
    public Object employeeOperate(Long employeeId, EmployeeOperateEnum operate){
        employeeService.employeeOperate(employeeId,operate);
        return succeed();
    }

    @ApiOperation(value = "新增员工")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('employee/add')")
    public Object add(@RequestBody Employee employee) throws Exception {
        employeeService.add(employee);
        return succeed();
    }

    @ApiOperation(value = "修改员工")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('employee/update')")
    public Object update(@RequestBody Employee employee) {
        employee.setUpdateTime(new Date());
        employeeService.updateEmployee(employee);
        return succeed();
    }

    @ApiOperation(value = "修改用户")
    @PostMapping("/updateUser")
    public Object update(SysUser sysUser) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user != null) {
            Date date = new Date();
            sysUser.setId(user.getId());
            sysUser.setUpdateTime(date);
            studentManageService.updateUser(sysUser);
            return succeed();
        }
        return failed("获取用户失败");
    }

//    @ApiOperation(value = "删除员工")
//    @PostMapping("/delete/{userId}")
//    @PreAuthorize("@pcs.hasPermissions('employee/delete')")
//    public Object update(@PathVariable("userId") Long userId) {
//        employeeService.deleteEmployee(userId);
//        return succeed();
//    }

    @ApiOperation(value = "获取所有员工")
    @GetMapping("/findAll")
    @PreAuthorize("@pcs.hasPermissions('employee/findAll')")
    public Object findAll() {
        return succeed(employeeService.findAll(null));
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/queryUserInfo")
    public Object apiQueryUserInfo() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if(sysUser != null && sysUser.getId() != null){
            Employee employee = employeeService.get(sysUser.getId());
            sysUser.setOrganName(organizationService.getOrganName(employee.getOrganIdList()));
            return succeed(sysUser);
        }
        return failed("获取用户信息失败");
    }
}
