package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicResourceType;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.Resource;
import com.lightinit.hsdataplatform.entity.ResourceApiInterfaceWithBLOBs;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.AggregationModel;
import com.lightinit.hsdataplatform.model.admin.ClassificationAndAggregationListModel;
import com.lightinit.hsdataplatform.model.admin.ClassificationAndAggregationSearchModel;
import com.lightinit.hsdataplatform.service.admin.IApiPublishService;
import com.lightinit.hsdataplatform.service.admin.IClassificationAndAggregationService;
import com.lightinit.hsdataplatform.service.admin.IResourceMicroappRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
@Controller
@RequestMapping(value = "/admin/classificationAndAggregation")
public class ClassificationAndAggregationController extends BaseController {
    @Autowired
    private IClassificationAndAggregationService classificationAndAggregationService;
    @Autowired
    private IApiPublishService apiPublishService ;
    @RequestMapping(value="index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/classificationandAggregation/index");
        modelAndView.addObject("resourceType",DicResourceType.getInstacne().getDicMap());
        return modelAndView;
    }
    @RequestMapping(value = "list.action")
    public ModelAndView list(ClassificationAndAggregationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/classificationandAggregation/list");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        List<ClassificationAndAggregationListModel> outputModel = classificationAndAggregationService.queryList(inputModel);
        pager.setTotal(classificationAndAggregationService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    @RequestMapping(value = "apiview.htm/{id}/{publishInfoId}")
    public ModelAndView apiview(@PathVariable("id") long id, @PathVariable("publishInfoId") long publishInfoId) {
        ModelAndView modelAndView = new ModelAndView("admin/classificationandAggregation/apiview");
        modelAndView.addObject("resource", apiPublishService.queryOneViewResource(id)) ;
        List<ResourceApiInterfaceWithBLOBs> resourceApiInterfaceWithBLOBsList = apiPublishService.queryOneResourceAPIInterface(id) ;
        modelAndView.addObject("resourceApiInterface", resourceApiInterfaceWithBLOBsList) ;
        modelAndView.addObject("publishInfo", apiPublishService.queryOnePublishInfoById(publishInfoId)) ;
        modelAndView.addObject("publishSettingList", apiPublishService.queryListPublishSettingByInfoId(publishInfoId)) ;
        return modelAndView;
    }

    /**
     * 跳转归类页面
     * @param id
     * @return
     */
    @RequestMapping(value = "aggregation.htm/{id}")
    public ModelAndView aggregation(@PathVariable long id) {
        ModelAndView modelAndView = new ModelAndView("admin/classificationandAggregation/aggregation");
       modelAndView.addObject("resource",classificationAndAggregationService.aggregation(id));
        return modelAndView;
    }


    @RequestMapping(value = "toAggregation.action")
    @ResponseBody
    public ResultState toAggregation(AggregationModel inputModel) {
        ResultState resultState = classificationAndAggregationService.toAggregation(inputModel);
        return resultState;
    }
}
