package com.unis.base.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.base.common.vo.PageSystemVO;
import com.unis.base.common.vo.PersonSystemVO;
import com.unis.base.common.vo.SavePersonVO;
import com.unis.base.server.service.PersonSystemService;
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.*;

@RestController
@RequestMapping("/personnelSystem")
@Api(value = "人员制度", tags = "人员制度")
@Slf4j
public class PersonSystemController {
    @Autowired
    private PersonSystemService personSystemService;

    @ApiOperation(value = "文件分页")
    @PostMapping(value = "/pageSystem")
    public ResponseWrapper pageSystem(@RequestBody PageSystemVO pageSystemVO) {
        try {
            PageInfo<PersonSystemVO> pagePersonnelVOS = personSystemService.pageSystem(pageSystemVO);
            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 = "/saveSystem")
    public ResponseWrapper saveSystem(@RequestBody SavePersonVO savePersonVO) {
        try {
            personSystemService.saveSystem(savePersonVO);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            if (e.getMessage().equals("文件名称不可重复!")){
                return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),e.getMessage());
            }

        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(),HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
    }

    @ApiOperation(value = "发布文件")
    @PostMapping(value = "/sentSystem")
    public ResponseWrapper sentSystem(@RequestParam("fileCode") String fileCode,
                                        @RequestParam("status") Integer status
                                      ) {
        try {
            personSystemService.sentSystem(fileCode,status);
            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 = "/updateSystem")
    public ResponseWrapper updateSystem(@RequestBody SavePersonVO savePersonVO) {
        try {
            personSystemService.updateSystem(savePersonVO);
            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 = "/deleteSystem")
    public ResponseWrapper deleteSystem(@RequestParam("id") String id) {
        try {
            personSystemService.deleteSystem(id);
            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 = "/selectSystem")
    public ResponseWrapper selectSystem(@RequestParam("id") String id) {
        try {
            PersonSystemVO personSystemVO = personSystemService.selectSystem(id);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), personSystemVO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
        return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(), "服务错误，请联系管理员");
    }

}
