package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.*;
import com.unis.base.server.entity.PersonnelDO;
import com.unis.base.server.service.PersonnelMxService;
import com.unis.common.utils.ResponseWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/personnel")
@Api(value = "人员详细信息", tags = "人员详细信息")
@Slf4j
public class PersonnelMxController {
    @Autowired
    private PersonnelMxService personnelMxService;

    @ApiOperation(value = "企业分页")
    @PostMapping(value = "/pagePersonnel")
    public ResponseWrapper pagePersonnel(@RequestBody PageSDetailsVO pageSDetailsVO) {
        try {
            PageInfo<PagePersonnelVO> pagePersonnelVOS = personnelMxService.pagePersonnel(pageSDetailsVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pagePersonnelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }


    @ApiOperation(value = "根据人员姓名查询")
    @PostMapping(value = "/pagePersonnelDetails")
    public ResponseWrapper pagePersonnelDetails(@RequestParam("ryxm") String ryxm) {
        try {
            List<PersonnelVO> pagePersonnelVOS = personnelMxService.pagePersonnelDetails(ryxm);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pagePersonnelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "查看详情")
    @PostMapping(value = "/catchPersonnelDetails")
    public ResponseWrapper catchPersonnelDetails(@RequestParam("rybh") String rybh) {
        try {
            PersonnelMxVO pagePersonnelVOS = personnelMxService.catchPersonnelDetails(rybh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pagePersonnelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "人员分页")
    @PostMapping(value = "/pageDetails")
    public ResponseWrapper pageDetails(@RequestParam("pageNum") Integer pageNum,
                                       @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam("qymc") String qymc,
                                       @RequestParam(value = "ryxm",required = false) String ryxm,
                                       @RequestParam(value = "personStatus",required = false) String personStatus) {
        try {
            PageInfo<PersonnelVO> pagePersonnelVOS = personnelMxService.pageDetails(pageNum, pageSize, qymc,ryxm,personStatus);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pagePersonnelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "人员状态设置")
    @PostMapping(value = "/updatePersonnel")
    public ResponseWrapper updatePersonnel(@RequestParam("personStatus") Integer personStatus,
                                           @RequestParam("rybh") String rybh) {
        try {
            PersonnelDO personnelDO = personnelMxService.updatePersonnel(personStatus, rybh);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), personnelDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "根据企业名称查询")
    @PostMapping(value = "/pageSDetails")
    public ResponseWrapper pageSDetails(@RequestBody PageSDetailsVO pageSDetailsVO) {
        try {
            List<PagePersonnelVO> pagePersonnelVOS = personnelMxService.pageSDetails(pageSDetailsVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pagePersonnelVOS);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }



    @ApiOperation(value = "判断是否关联人才组")
    @PostMapping(value = "/determine")
    public ResponseWrapper determine(@RequestParam("ryxm") String ryxm) {
        try {
            String ry = personnelMxService.determine(ryxm);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), ry);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

    @ApiOperation(value = "新增人员信息")
    @PostMapping(value = "/getPersonnelDetails")
    public ResponseWrapper getDRyxxsjyList(@RequestBody PersonnelMxVO personnelMxVO) {
        try {
            personnelMxService.insertPersonnelDetails(personnelMxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }


    @ApiOperation(value = "修改人员信息")
    @PostMapping(value = "/upPersonnelDetails")
    public ResponseWrapper upPersonnelDetails(@RequestBody PersonnelMxVO personnelMxVO) {
        try {
            personnelMxService.upPersonnelDetails(personnelMxVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

}
