package com.mortals.xhx.module.one.web;

import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.web.BaseCRUDJsonMappingController;
import com.mortals.xhx.module.one.model.OneTopicalEntity;
import com.mortals.xhx.module.one.service.OneTopicalService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


/**
* <p>Title: 主题表</p>
* <p>Description: OneTopicalController  </p>
* <p>Copyright: Copyright &reg;  </p>
* <p>Company: </p>
* @author zxfei
* @version 1.0.0
*/
@RestController
@RequestMapping("one/topical")
public class OneTopicalController extends BaseCRUDJsonMappingController<OneTopicalService,OneTopicalForm,OneTopicalEntity,Long> {

    public OneTopicalController(){
        super.setFormClass(OneTopicalForm.class);
        super.setModuleDesc("主题表");
    }


    /**
     * 查询主题与主题包（二级结构）
     */
    @PostMapping(value = "getTopicList")
    public String getListByParentId(OneTopicalForm form) {
        JSONObject ret = new JSONObject();
        ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        try {
            List<Map<String, Object>> treeList = this.service.getTreeList(form.getQuery().getTopicalType());
            ret.put(KEY_RESULT_DATA,treeList);

        } catch (Exception e) {
            log.error("查询信息错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }

    /**
     * 更换排序
     */
    @PostMapping(value = "upOrDownTopicList")
    public String upOrDownTopicList(@RequestParam(value = "id") Long id,@RequestParam(value = "type") Integer type) {
        JSONObject ret = new JSONObject();
        ret.put(KEY_RESULT_CODE, VALUE_RESULT_SUCCESS);
        try {
            this.service.upOrDownTopicList(id,type);
        } catch (Exception e) {
            log.error("更新错误", e);
            ret.put(KEY_RESULT_CODE, VALUE_RESULT_FAILURE);
            ret.put(KEY_RESULT_MSG, e.getMessage());
        }
        return ret.toJSONString();
    }

}