package com.lightinit.hsdataportal.controller.open;

import com.lightinit.hsdataportal.common.ConfigUtils;
import com.lightinit.hsdataportal.common.SysContants;
import com.lightinit.hsdataportal.controller.BaseController;
import com.lightinit.hsdataportal.dictionary.DicNewsCategory;
import com.lightinit.hsdataportal.dictionary.DicNewsState;
import com.lightinit.hsdataportal.entity.BaseExample;
import com.lightinit.hsdataportal.model.*;
import com.lightinit.hsdataportal.service.IEntrepreneurshipTutoringService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
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 javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping(value = "/open/EntrepreneurshipTutoring")
public class EntrepreneurshipTutoringController extends BaseController {
    @Autowired
    private IEntrepreneurshipTutoringService entrepreneurshipTutoring;
    @RequestMapping(value = "index.action")
    public ModelAndView index(HttpSession session, EntrepreneurshipTutoringSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("open/EntrepreneurshipTutoring/index");
        EntrepreneurshipTutoringController aa = new EntrepreneurshipTutoringController();
        BaseExample.Page pager = new BaseExample.Page(pageNo,3);
        inputModel.setPage(pager);
        inputModel.setNewsCategory(DicNewsCategory.INFORMAT);
        inputModel.setNewsState(DicNewsState.PUBLISHED);
        List<EntrepreneurshipTutoringListModel> informatList = entrepreneurshipTutoring.selectList(inputModel);
        inputModel.setNewsCategory(DicNewsCategory.OPENCLASS);
        List<EntrepreneurshipTutoringListModel> openClassList = entrepreneurshipTutoring.selectList(inputModel);
        inputModel.setNewsCategory(DicNewsCategory.UNSCRAMBLE);
        List<EntrepreneurshipTutoringListModel> unscrambleList = entrepreneurshipTutoring.selectList(inputModel);
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        modelAndView.addObject("informatList", informatList);
        modelAndView.addObject("openClassList", openClassList);
        modelAndView.addObject("unscrambleList", unscrambleList);
            modelAndView.addObject("imageContextPath", imageContextPath);
        return modelAndView;
    }

    /**
     * 创业辅导
     * @param session
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "informatList.action")
    @ResponseBody
    public ModelAndView informatList(HttpSession session, EntrepreneurshipTutoringSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("open/EntrepreneurshipTutoring/information");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        inputModel.setNewsCategory(DicNewsCategory.INFORMAT);
        inputModel.setNewsState(DicNewsState.PUBLISHED);
        List<EntrepreneurshipTutoringListModel> outputModel = entrepreneurshipTutoring.selectList(inputModel);
        pager.setTotal(entrepreneurshipTutoring.selectCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    /**
     * 政策解读
     * @param session
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "unscrambleList.action")
    @ResponseBody
    public ModelAndView unscrambleList(HttpSession session,EntrepreneurshipTutoringSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("open/EntrepreneurshipTutoring/unscramble");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        inputModel.setNewsCategory(DicNewsCategory.UNSCRAMBLE);
        inputModel.setNewsState(DicNewsState.PUBLISHED);
        List<EntrepreneurshipTutoringListModel> outputModel = entrepreneurshipTutoring.selectList(inputModel);
        pager.setTotal(entrepreneurshipTutoring.selectCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    /**
     * 咨询服务
     * @param session
     * @param pageNo
     * @return
     */
    @RequestMapping(value = "openClassList.action")
    @ResponseBody
    public ModelAndView openClassList(HttpSession session,EntrepreneurshipTutoringSearchModel inputModel,@RequestParam(defaultValue = "1") int pageNo) {
        ModelAndView modelAndView = new ModelAndView("open/EntrepreneurshipTutoring/openClass");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        inputModel.setNewsCategory(DicNewsCategory.OPENCLASS);
        inputModel.setNewsState(DicNewsState.PUBLISHED);
        List<EntrepreneurshipTutoringListModel> outputModel = entrepreneurshipTutoring.selectList(inputModel);
        pager.setTotal(entrepreneurshipTutoring.selectCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }
    @RequestMapping(value = "view.htm/{id}")
    public ModelAndView view(@PathVariable long id) {
        ModelAndView modelAndView = new ModelAndView("open/EntrepreneurshipTutoring/view");
        EntrepreneurshipTutoringViewModel model = entrepreneurshipTutoring.view(id);
        String newsContent = model.getNewsContent();
        String imageContextPath = ConfigUtils.getConfiguration(SysContants.PropertyFilePath.CONFIG).getString(SysContants.PropertyKey.IMAGE_CONTEXT_PATH);
        String imgTag = "<img src=\"";
        String consult = newsContent.replaceAll(imgTag, imgTag + imageContextPath);
        model.setNewsContent(consult);
        modelAndView.addObject("model", model);
        return modelAndView;
    }
}
