package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicResourceType;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.ResourceApiInterfaceWithBLOBs;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.*;
import com.lightinit.hsdataplatform.service.admin.IApiPublishService;
import com.lightinit.hsdataplatform.service.admin.IRelationshipManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
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;

@RequestMapping(value = "/admin/RelationshipManage")
@Controller
public class RelationshipManageController extends BaseController {
    @Autowired
    private IRelationshipManageService relationshipManageService;
    @Autowired
    private IApiPublishService apiPublishService ;
    @RequestMapping(value="index.htm")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/index");
        modelAndView.addObject("resourceType",DicResourceType.getInstacne().getDicMap());
        return modelAndView;
    }
    @RequestMapping(value = "list.action")
    @ResponseBody
    public ModelAndView list(RelationshipManageSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/list");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        List<RelationshipManageListModel> outputModel = relationshipManageService.queryRelationshipManageList(inputModel);
        pager.setTotal(relationshipManageService.queryRelationshipManageCount(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/relationshipManage/detal");
        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;
    }
    @RequestMapping(value="edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id,RelationshipManageSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/edit");
        modelAndView.addObject("resourceType",DicResourceType.getInstacne().getDicMap());
        //调用
        //List<RelationshipManageListModel> calleeList = relationshipManageService.calleeList(inputModel);
        List<RelationshipManageListModel> callerList = relationshipManageService.callerList(id);
        modelAndView.addObject("callerList", callerList);
        //modelAndView.addObject("calleeList", calleeList);
        modelAndView.addObject("resourceId", id);
        return modelAndView;
    }
    @RequestMapping(value="calleeList.action")
    public ModelAndView calleeList(RelationshipManageSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/callee");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        //被调用
        List<RelationshipManageListModel> calleeList = relationshipManageService.calleeList(inputModel);
        pager.setTotal(relationshipManageService.calleeCount(inputModel));
        modelAndView.addObject("calleeList", calleeList);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    @RequestMapping(value="caller.action/{id}")
    public ModelAndView caller(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/callerList");
        //调用
        List<RelationshipManageListModel> callerList = relationshipManageService.caller(id);
        modelAndView.addObject("callerList", callerList);
        return modelAndView;
    }
    @RequestMapping(value="callee.action/{id}")
    public ModelAndView callee(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/calleeList");
        //被调用
        List<RelationshipManageListModel> calleeList = relationshipManageService.callee(id);
        modelAndView.addObject("calleeList", calleeList);
        return modelAndView;
    }
    @RequestMapping(value="doEdit.action")
    @ResponseBody
    public ResultState doEdit(AtlasRelationshipModel inputModel) {

        ResultState resultSet =   relationshipManageService.doEdit(inputModel);
        return resultSet;

    }
    @RequestMapping(value="detal.htm/{id}/{resourceType}")
    public ModelAndView detal(@PathVariable("id") long id, @PathVariable("resourceType") String resourceType) {
        ModelAndView modelAndView = new ModelAndView("admin/relationshipManage/detal");
        RelationshipManageViewModel model  =   relationshipManageService.detal(id,resourceType);
//        if(!ObjectUtils.isEmpty(model.getInterfaceId())) {
        if(model != null) {
            List<ResourceAPIRegisterAddAPIInterfaceInputModel> listIn =relationshipManageService.queryInterfaceIn(model.getInterfaceId());
            List<ResourceAPIRegisterAddAPIInterfaceOutputModel> listOut =relationshipManageService.queryInterfaceOut(model.getInterfaceId());
            modelAndView.addObject("listIn",listIn);
            modelAndView.addObject("listOut",listOut);
        }
        modelAndView.addObject("model",model);

        return modelAndView;

    }


}
