package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicResourceType;
import com.lightinit.hsdataplatform.dictionary.DicSortCategory;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.entity.ResourceSort;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.DataRecommenAddModel;
import com.lightinit.hsdataplatform.model.admin.DataRecommendationListModel;
import com.lightinit.hsdataplatform.model.admin.DataRecommendationSearchModel;
import com.lightinit.hsdataplatform.service.admin.IDataRecommendationService;
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.Date;
import java.util.List;

@Controller
@RequestMapping(value="/admin/APIRecommendation")
public class APIRecommendationController extends BaseController {
    @Autowired
    private IDataRecommendationService dataRecommendationService;
    @RequestMapping(value="defaultIndex.htm")
    public ModelAndView defaultIndex() {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/default");
        return modelAndView;
    }


    /**
     * 默认列表
     * @param inputModel
     * @param pageNo
     * @return
     */
    @RequestMapping(value="queryDefaultList.action")
    public ModelAndView queryDefaultList(DataRecommendationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/defaultList");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        //inputModel.setSortCategory(DicSortCategory.DEFAULT_SORT);
        inputModel.setResourceType(DicResourceType.API);
        List<DataRecommendationListModel> outputModel = dataRecommendationService.queryDefaultList(inputModel);
        pager.setTotal(dataRecommendationService.queryDefaultCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("sortCategory",DicSortCategory.DEFAULT_SORT);
        return modelAndView;
    }
    @RequestMapping(value="home.htm")
    public ModelAndView home() {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/home");
        return modelAndView;
    }

    /**
     * 首页列表
     * @param inputModel
     * @param
     * @return
     */
    @RequestMapping(value="queryHomeList.action")
    public ModelAndView queryHomeList(DataRecommendationSearchModel inputModel) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/homeList");
        inputModel.setSortCategory(DicSortCategory.HOME_PAGE_SORT);
        inputModel.setResourceType(DicResourceType.API);
        List<DataRecommendationListModel> outputModel = dataRecommendationService.queryList(inputModel);
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("sortCategory", inputModel.getSortCategory());
        return modelAndView;
    }
    @RequestMapping(value="selection.htm")
    public ModelAndView selection() {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/selection");
        return modelAndView;
    }

    /**
     * 精选列表
     * @param inputModel
     * @param
     * @return
     */
    @RequestMapping(value="querySelectionList.action")
    public ModelAndView querySelectionList(DataRecommendationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/selectionList");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        inputModel.setSortCategory(DicSortCategory.SELECTION_SORTING);
        inputModel.setResourceType(DicResourceType.API);
        List<DataRecommendationListModel> outputModel = dataRecommendationService.queryList(inputModel);
        pager.setTotal(dataRecommendationService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        modelAndView.addObject("sortCategory", inputModel.getSortCategory());
        return modelAndView;
    }
    @RequestMapping(value="add.htm/{sortCategory}")
    public ModelAndView add(@PathVariable String sortCategory) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/add");
        modelAndView.addObject("resourceType", DicResourceType.getInstacne().getDicMap());
        modelAndView.addObject("type", sortCategory);
        return modelAndView;
    }
    @RequestMapping(value="add1.htm/{resourceId}/{sortCategory}")
    public ModelAndView add1(@PathVariable Long resourceId,@PathVariable String sortCategory) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/move");
        ResourceSort model = new  ResourceSort();
        model.setResourceId(resourceId);
        model.setCreatedAt(new Date());
        model.setSortCategory(sortCategory);
        modelAndView.addObject("model", model);
        return modelAndView;
    }

    @RequestMapping(value="addList.htm")
    public ModelAndView addList(DataRecommendationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/addList");
        BaseExample.Page pager = new BaseExample.Page(pageNo, 10);
        inputModel.setPage(pager);
        inputModel.setResourceType(DicResourceType.API);
        List<DataRecommendationListModel> outputModel = dataRecommendationService.queryResourceList(inputModel);
        pager.setTotal(dataRecommendationService.queryResourceCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    @RequestMapping(value="doAdd.action")
    @ResponseBody
    public ResultState doAdd(DataRecommenAddModel inputModel) {
        ResultState resultState = dataRecommendationService.doAdd(inputModel);
        return resultState;
    }
    @RequestMapping(value="move.htm/{id}")
    public ModelAndView move(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/admin/apiRecommendation/move");
        ResourceSort model = dataRecommendationService.selectById(id);
        modelAndView.addObject("model", model);
        return modelAndView;
    }
    @RequestMapping(value="doMove.action")
    @ResponseBody
    public ResultState doMove(DataRecommenAddModel inputModel) {
        ResultState resultState = dataRecommendationService.doMove(inputModel);
        return resultState;
    }
    @RequestMapping(value="delete.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable Long id) {
        ResultState resultState = dataRecommendationService.delete(id);
        return resultState;
    }
}
