package com.unis.detectionnum.server.controller;

import com.github.pagehelper.PageInfo;
import com.unis.common.utils.ResponseWrapper;
import com.unis.detectionnum.common.vo.AnnouncementVO;
import com.unis.detectionnum.server.entity.AnnouncementDO;
import com.unis.detectionnum.server.service.AnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
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.Date;
import java.util.HashMap;
import java.util.Map;

/** 
 * @ClassName: AnnouncementManageController 
 * @Description: 平台管理-公告管理 
 * @author LiuFeng 
 * @date 2019年10月16日 下午4:55:23  
 */
@RestController
@Slf4j
@Api(value = "平台管理-公告管理", tags = "平台管理-公告管理")
public class AnnouncementManageController {

	@Autowired
	private AnnouncementService announcementService;
	
	/**
	 * @Description 平台管理->公告管理管理->根据条件【分页查询】
	 * @param pageNum 条数
	 * @param pageSize 页码
	 * @return 公告信息列表
	 */
	@ApiOperation(value = "平台管理->公告管理管理->根据条件【分页查询】", notes = "参数：开始时间,结束时间")
	@PostMapping(value = "/platformMange/announcement/pageList/{pageNum}/{pageSize}")
	public ResponseWrapper pageList(@PathVariable("pageNum")Integer pageNum,@PathVariable("pageSize") Integer pageSize,
									@RequestBody AnnouncementVO announcementVO) {
		try {
			Map<String,Object> paramMap = new HashMap<String, Object>();
			paramMap.put("startTime", announcementVO.getstartTime());
			paramMap.put("endTime", announcementVO.getendTime());
			
			PageInfo<AnnouncementVO> pageInfos = announcementService.getAnnouncementPageList(paramMap,pageNum,pageSize);
            return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), pageInfos);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}
	
	/**
	 * @Description 平台管理->公告管理管理->【发布公告】
	 * @param announcementVO
	 */
	@ApiOperation(value = "平台管理->公告管理管理->【发布公告】", notes = "参数：条数 ,页码,开始时间,结束时间")
	@PostMapping(value = "/platformMange/announcement")
	public ResponseWrapper saveAnnouncement(@RequestBody AnnouncementVO announcementVO) {
		try {

			announcementService.saveAnnouncement(announcementVO);
            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(),"服务错误，请联系管理员");
	}
	
	/**
	 * @Description 平台管理->公告管理管理->【发布公告】
	 * @param announcementVO
	 */
	@ApiOperation(value = "平台管理->公告管理管理->【修改公告】", notes = "参数：条数 ,页码,开始时间,结束时间")
	@PutMapping(value = "/platformMange/announcement")
	public ResponseWrapper updateAnnouncement(@RequestBody AnnouncementVO announcementVO) {
		try {

			announcementService.updateAnnouncement(announcementVO);
            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(),"服务错误，请联系管理员");
	}
	
	/**
	 * @Description 平台管理->公告管理管理->【删除公告】
	 * @param announcementVO
	 */
	@ApiOperation(value = "平台管理->公告管理管理->【删除公告】", notes = "参数：条数 ,页码,开始时间,结束时间")
	@GetMapping(value = "/platformMange/announcement/{announcementId}")
	public ResponseWrapper deleteAnnouncement(@PathVariable("announcementId")Integer announcementId) {
		try {

			announcementService.deleteAnnouncement(announcementId);
            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(),"服务错误，请联系管理员");
	}

	/**
	 * @Description 平台管理->公告管理管理->【删除公告】
	 * @param announcementId
	 */
	@ApiOperation(value = "平台管理->公告管理管理->【根据id查询公告】", notes = "参数：id")
	@GetMapping(value = "/platformMange/announcement/getByIdAnnouncement/{announcementId}")
	public ResponseWrapper getByIdAnnouncement(@PathVariable("announcementId")Integer announcementId) {
		try {

			AnnouncementDO announcementDO = announcementService.selectByPrimaryKey(announcementId);
			return ResponseWrapper.success(HttpStatus.OK.value(), HttpStatus.OK.getReasonPhrase(), announcementDO);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
		return ResponseWrapper.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.getReasonPhrase(),"服务错误，请联系管理员");
	}

}
