package com.ozs.appshow.controller;

import java.util.List;

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.ChartClassDTO;
import com.ozs.appshow.service.ChartClassService;
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.ApiOperation;

@Api(tags = { "应用展示—>图表分类接口" })
@RestController
public class ChartClassController {

	@Autowired
	private ChartClassService chartClassService;
	@Autowired
	private AccountApi accountApi;

	@ApiOperation("通过菜单名模糊搜索获取菜单树状展示")
	@GetMapping("/appshow/chartClass/tree")
	@ApiImplicitParam(name = "className", value = "分类名（模糊搜索）", required = false, paramType = "query", dataType = "String")
	public R<List<ChartClassDTO>> listChartClassTree(
			@RequestParam(value = "className", required = false) String className) {
		try {
			return chartClassService.listChartClassTree(className);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("通过分类编码获取图表分类信息")
	@GetMapping("/appshow/chartClass/{classCode}")
	@ApiImplicitParam(name = "classCode", value = "分类编码", paramType = "path", dataType = "String")
	public R<ChartClassDTO> getChartClass(@PathVariable(value = "classCode") String classCode) {
		try {
			return chartClassService.getChartClass(classCode);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("新建图表分类接口")
	@PostMapping("/appshow/chartClass")
	public R<String> saveChartClass(@RequestBody ChartClassDTO ChartClassDTO, @RequestHeader String token) {
		try {
			ChartClassDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return chartClassService.saveChartClass(ChartClassDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("菜单信息修改接口")
	@ApiImplicitParam(name = "classCode", value = "分类编码", paramType = "path", dataType = "String")
	@PutMapping("/appshow/chartClass/{classCode}")
	public R<String> updateChartClass(@RequestBody ChartClassDTO ChartClassDTO,
			@PathVariable(value = "classCode") String classCode, @RequestHeader String token) {
		try {
			ChartClassDTO.setClassCode(classCode);
			ChartClassDTO.setUpdateUserId(accountApi.parseTokenId(token));
			return chartClassService.updateChartClass(ChartClassDTO);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

	@ApiOperation("分类删除接口")
	@DeleteMapping("/appshow/chartClass/{classCode}")
	public R<String> removeChartClass(@PathVariable(value = "classCode", required = true) String classCode) {
		try {
			return chartClassService.removeChartClass(classCode);
		} catch (Exception e) {
			e.printStackTrace();
			return new R<>(S.ERROR);
		}
	}

}
