package com.yonge.cooleshow.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dao.EmployeeDao;
import com.yonge.cooleshow.biz.dal.dto.EmployeeDto;
import com.yonge.cooleshow.biz.dal.dto.search.EmployeeSearch;
import com.yonge.cooleshow.biz.dal.enums.GenderEnum;
import com.yonge.toolset.mybatis.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.EmployeeVo;
import com.yonge.cooleshow.biz.dal.vo.StudentVo;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.UserLockFlag;
import com.yonge.cooleshow.common.enums.UserStatusEnum;
import com.yonge.cooleshow.common.enums.YesOrNoEnum;
import com.yonge.toolset.base.page.PageInfo;

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

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import com.yonge.cooleshow.biz.dal.entity.Employee;
import com.yonge.cooleshow.biz.dal.service.EmployeeService;
import com.yonge.cooleshow.common.controller.BaseController;

import javax.annotation.Resource;
import javax.validation.Valid;

@RequestMapping("employee")
@Api(tags = "员工管理")
@RestController
public class EmployeeController extends BaseController {
    @Autowired
    private EmployeeService employeeService;
    @Resource
    private SysUserFeignService sysUserFeignService;
    @Resource
    private EmployeeDao employeeDao;

    @GetMapping("/detail/{id}")
    @ApiOperation(value = "详情", notes = "传入student")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", paramType = "path", dataType = "long", required = true),
    })
    @PreAuthorize("@pcs.hasPermissions('employee/detail')")
    public HttpResponseResult<EmployeeVo> detail(@PathVariable("id") Long id) {
        EmployeeVo detail = employeeService.detail(id);
        return succeed(detail);
    }

    /**
     * 查询分页
     */
    @ApiOperation(value = "查询分页", notes = "传入student")
    @PostMapping("/page")
    @PreAuthorize("@pcs.hasPermissions('employee/page')")
    public HttpResponseResult<PageInfo<EmployeeVo>> page(@RequestBody EmployeeSearch query) {
        query.setSort("id_");
    	
    	if(StringUtils.isNotBlank(query.getUserStatus())){
    		switch (query.getUserStatus()) {
			case "LOCKED":
				query.setDelFlag(YesOrNoEnum.NO);
				query.setLockFlag(UserLockFlag.LOCKED);
				break;
			case "CLOSED":
				query.setDelFlag(YesOrNoEnum.YES);
				break;

			default:
				query.setDelFlag(YesOrNoEnum.NO);
				query.setLockFlag(UserLockFlag.NORMAL);
				break;
			}
    	}
        IPage<EmployeeVo> pages = employeeService.selectPage(PageUtil.getPage(query), query);
		List<EmployeeVo> rows = pages.getRecords();
		
		for(EmployeeVo vo : rows){
			if(vo.getDelFlag() == YesOrNoEnum.YES){
				vo.setUserStatus(UserStatusEnum.CLOSED);
			}else{
				if(vo.getLockFlag() == UserLockFlag.LOCKED){
					vo.setUserStatus(UserStatusEnum.LOCKED);
				}else{
					vo.setUserStatus(UserStatusEnum.NORMAL);
				}
			}
		}
        return succeed(PageUtil.pageInfo(pages));
    }

    @ApiOperation(value = "新增员工")
    @PostMapping("/add")
    @PreAuthorize("@pcs.hasPermissions('employee/add')")
    public HttpResponseResult<EmployeeDto> add(@Valid @RequestBody EmployeeDto employee) throws Exception {
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        return employeeService.addEmployee(employee);
    }

    @ApiOperation(value = "修改员工")
    @PostMapping("/update")
    @PreAuthorize("@pcs.hasPermissions('employee/update')")
    public HttpResponseResult<EmployeeDto> update(@Valid @RequestBody EmployeeDto employee) throws Exception {
        employee.setUpdateTime(new Date());
        return employeeService.updateEmployee(employee);
    }

    @ApiOperation(value = "获取用户信息")
    @GetMapping("/queryUserInfo")
    public HttpResponseResult<EmployeeVo> apiQueryUserInfo() {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser != null && sysUser.getId() != null) {
            Employee employee = employeeService.get(sysUser.getId());
            EmployeeVo employeeVo = new EmployeeVo().buildVo(employee);
            employeeVo.setUserId(sysUser.getId());
            employeeVo.setRoleIds(employeeDao.queryUserRole(sysUser.getId()));
            employeeVo.setSysUser(sysUser);

            BeanUtils.copyProperties(sysUser, employeeVo, "gender");
            employeeVo.setGender(GenderEnum.valueOf(sysUser.getGender()));
            return succeed(employeeVo);
        }
        return failed("获取用户信息失败");
    }

}