package com.ozs.appshow.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ozs.appshow.dto.ListSubjectDTO;
import com.ozs.appshow.dto.RelSubjectChartParam;
import com.ozs.appshow.dto.SubjectInfoDTO;
import com.ozs.appshow.util.AccountApi;
import com.ozs.appshow.util.R;
import com.ozs.appshow.util.S;

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

/**
 * 
 * @ClassName: SubjectManageController
 * @Description: 应用展示--专题管理相关接口
 * @author: Zhao You Kai
 * @date: Apr 18, 2019 7:12:58 PM
 */

@Api(tags = { "应用展示—>专题管理接口" })
@RestController
public class SubjectManageController {

	@Autowired
	private com.ozs.appshow.service.SubjectManageService SubjectManageService;
	@Autowired
	private AccountApi accountApi;

	@ApiOperation("专题列表信息接口，分上下线展示")
	@GetMapping("/appshow/subjectManage/list")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "subjectName", value = "专题名称，模糊搜索", required = false, paramType = "query", dataType = "String") })
	public R<ListSubjectDTO> listSubject(@RequestParam(value = "subjectName", required = false) String subjectName) {
		try {
			return SubjectManageService.listSubject(subjectName);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("保存专题接口")
	@PostMapping("/appshow/subjectManage")
	public R<String> saveSubject(@RequestBody SubjectInfoDTO subjectInfo, @RequestHeader("token") String token) {
		subjectInfo.setUpdateUserId(accountApi.parseTokenId(token));
		try {
			return SubjectManageService.saveSubject(subjectInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("修改专题接口")
	@PutMapping("/appshow/subjectManage/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "专题ID", required = false, paramType = "path", dataType = "String") })
	public R<String> updateSubject(@PathVariable String id, @RequestBody SubjectInfoDTO subjectInfo,
			@RequestHeader("token") String token) {
		try {
			subjectInfo.setId(id);
			subjectInfo.setUpdateUserId(accountApi.parseTokenId(token));
			return SubjectManageService.updateSubject(subjectInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("删除专题接口")
	@DeleteMapping("/appshow/subjectManage/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "{id}", value = "专题ID", required = false, paramType = "path", dataType = "String") })
	public R<String> removeSubject(@PathVariable String id) {
		try {
			return SubjectManageService.removeSubject(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("修改专题状态接口")
	@PutMapping("/appshow/subjectManage/state/{id}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "专题ID", required = false, paramType = "path", dataType = "String") })
	public R<String> updateSubjectState(@PathVariable String id, @RequestBody SubjectInfoDTO subjectInfo,
			@RequestHeader("token") String token) {
		try {
			subjectInfo.setId(id);
			subjectInfo.setUpdateUserId(accountApi.parseTokenId(token));
			return SubjectManageService.updateSubjectState(subjectInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("主题配置接口，关联图表")
	@PostMapping("/appshow/subjectManage/relSubjectChart")
	public R<String> saveRelSubjectChart(@RequestBody RelSubjectChartParam param) {
		try {
			return SubjectManageService.saveRelSubjectChart(param);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("获取主题信息")
	@GetMapping("/appshow/subjectManage/{id}")
	@ApiImplicitParam(name = "id", value = "专题ID", required = false, paramType = "path", dataType = "String")
	public R<RelSubjectChartParam> getSubject(@PathVariable String id) {
		try {
			return SubjectManageService.getSubject(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

}
