package com.ozs.kpim.controller.topic;

import com.ozs.kpim.controller.common.IBaseController;
import com.ozs.kpim.dto.topic.TopicDTO;
import com.ozs.kpim.service.kpiconfig.AccountApi;
import com.ozs.kpim.service.topic.TopicManagerService;
import com.ozs.kpim.util.R;
import com.ozs.kpim.util.S;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * @ClassName TopicController
 * @Desc 主题管理Controller
 * @Author jade
 * @Date 2019/2/19
 * @Version 1.0
 */
@RestController
@Api(tags = { "主题管理接口" })
public class TopicManagerController  implements IBaseController<TopicDTO> {

    @Resource
    private TopicManagerService topicManagerService;

    @Resource
    private AccountApi accountApi;


    @ApiOperation("创建主题")
    @PostMapping("/kpim/topic/save")
    @Override
    public R<?> save(@RequestBody TopicDTO topicDTO,@RequestHeader("token") String token) {
        try {
            topicDTO.setUpdateUserId(accountApi.parseTokenId(token));
            return topicManagerService.save(topicDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }
    }

    @ApiOperation("修改主题")
    @PutMapping("/kpim/topic/update")
    @Override
    public R<?> update(@RequestBody TopicDTO topicDTO,@RequestHeader("token") String token) {
        try {
            topicDTO.setUpdateUserId(accountApi.parseTokenId(token));
            return topicManagerService.update(topicDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }
    }

    @ApiOperation("删除主题")
    @DeleteMapping ("/kpim/topic/delete/{id}")
    @Override
    public R<?> delete(@PathVariable String id) {
        try {
            return topicManagerService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }
    }

    @ApiOperation("删除主题")
    @DeleteMapping ("/kpim/topic/deleteByTopicCode/{topicCode}")
    public R<?> deleteByTopicCode(@PathVariable String topicCode) {
        try {
            return topicManagerService.deleteByTopicCode(topicCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }
    }

    @ApiOperation("查询主题详情")
    @GetMapping ("/kpim/topic/getById/{id}")
    @Override
    public R<TopicDTO> getById(@PathVariable String id) {

        try {
            return topicManagerService.getById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }

    }

    @ApiOperation("查询主题详情，根据主题编码")
    @GetMapping ("/kpim/topic/getByTopicCode/{topicCode}")
    public R<?> getByTopicCode(@PathVariable String topicCode) {

        try {
            return topicManagerService.getByTopicCode(topicCode);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }

    }

    @ApiOperation("查询主题树(Ztree格式带检索)")
    @ApiImplicitParam(name = "name", value = "主题名称", required = false, paramType = "query", dataType = "String")
    @GetMapping ("/kpim/topic/topicForZtree")
    public R<?>  topicForZtree(@RequestParam(required = false) String name) throws Exception {
        try{
            return topicManagerService.topicForZtree(name);
        } catch (Exception e) {
            e.printStackTrace();
            return new R<>(S.ERROR,e.getMessage());
        }
    }


}
