package com.lightinit.hsdataportal.controller.share;
import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.dictionary.DicPublishApiState;
import com.lightinit.hsdataportal.dictionary.DicResourceShareType;
import com.lightinit.hsdataportal.dictionary.DicSortCategory;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.APIShareSearchModel;
import com.lightinit.hsdataportal.model.DataSearchModel;
import com.lightinit.hsdataportal.model.MicroappSearchModel;
import com.lightinit.hsdataportal.service.IAPIservice;
import com.lightinit.hsdataportal.service.IDataService;
import com.lightinit.hsdataportal.service.IMicroappService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by WangYao on 2017/6/6.
 */
@Controller
@RequestMapping(value = {"/share"})
public class ShareHomeController {

    @Autowired
    private IAPIservice apiService ;

    @Autowired
    private IDataService dataService;

    @Autowired
    private IMicroappService microappService;

    @RequestMapping(value = {"/", "home.htm"})
    public String index(ModelMap model, HttpServletRequest request,String searchName) {
        String str = "" ;
        //查找API列表
        APIShareSearchModel apiShareSearchModel = new APIShareSearchModel() ;
        apiShareSearchModel.setNotShareType(DicResourceShareType.NOT_SHARE);
        apiShareSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        apiShareSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        //查找数据列表
        DataSearchModel dataSearchModel = new DataSearchModel() ;
        dataSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        //查找微应用列表
        MicroappSearchModel microappSearchModel = new MicroappSearchModel() ;
        microappSearchModel.setPublishState(DicPublishApiState.PUBLISH);
        microappSearchModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        if (StringUtils.isEmpty(searchName)){
            BaseExample.Page pager = new BaseExample.Page();
            pager.setPageNo(1);
            pager.setPageSize(10);

            apiShareSearchModel.setPage(pager);
            dataSearchModel.setPage(pager);
            microappSearchModel.setPage(pager);
            str = "share/home/index" ;
        }else {
            apiShareSearchModel.setResourceName(searchName);
            microappSearchModel.setResourceName(searchName);
            dataSearchModel.setResourceName(searchName);
            str = "share/home/searchIndex" ;
        }

        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        model.addAttribute("imageContextPath",imageContextPath) ;

        model.addAttribute("apiList",apiService.queryShareList(apiShareSearchModel)) ;


        model.addAttribute("dataList",dataService.getDataIndex(dataSearchModel, 2)) ;


        model.addAttribute("micList",microappService.selectShareList(microappSearchModel)) ;
        return str ;
    }
}
