package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicNotificationState;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.DataCatelogListModel;
import com.lightinit.hsdataplatform.model.admin.LoginUser;
import com.lightinit.hsdataplatform.model.admin.NotificationListModel;
import com.lightinit.hsdataplatform.model.admin.NotificationSearchModel;
import com.lightinit.hsdataplatform.service.admin.INotificationService;
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;

/**
 * @Author: sunxiang
 * @Date: 2018/7/8 16:05
 * @Description:
 **/
@Controller
@RequestMapping("/admin/notification_mgr")
public class NotificationMgrController {
    @Autowired
    private INotificationService notificationService;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/notification_mgr/index");
        modelAndView.addObject("dicNotificationState", DicNotificationState.getInstance().getDicMap());
        return modelAndView;
    }
    @RequestMapping(value = "list.action")
    public ModelAndView list(NotificationSearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/notification_mgr/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        inputModel.setPage(pager);
        List<NotificationListModel> list=notificationService.getList(inputModel);
        pager.setTotal(notificationService.queryCount(inputModel));
        modelAndView.addObject("outputModel",list);
        modelAndView.addObject("pager",pager);
        return modelAndView;
    }

    @RequestMapping(value = "add.htm")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("admin/notification_mgr/add");
        return modelAndView;
    }

    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState addDo(NotificationSearchModel inputModel, HttpSession session){
        ResultState resultState=null;
        LoginUser loginUser = (LoginUser) session.getAttribute(LoginUser.SESSION_KEY_LOGIN_USER);
        inputModel.setUserId(loginUser.getId());/*loginUser.getId()*/
        if(inputModel.getId()!=null){
            resultState = notificationService.update(inputModel);
        }else{
            resultState = notificationService.add(inputModel);
        }
        return resultState;
    }

    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("admin/notification_mgr/add");
        NotificationSearchModel inputModel = new NotificationSearchModel();
        inputModel.setId(id);
        List<NotificationListModel> list = notificationService.getList(inputModel);
        if(list !=null && list.size()>0){
            modelAndView.addObject("outputModel",list.get(0));
        }
        return modelAndView;
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable Long id){
        return notificationService.delete(id);
    }

    @RequestMapping(value = "detail.htm/{id}")
    public ModelAndView detail(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("admin/notification_mgr/detail");
        NotificationSearchModel inputModel = new NotificationSearchModel();
        inputModel.setId(id);
        List<NotificationListModel> list = notificationService.getList(inputModel);
        if(list !=null && list.size()>0){
            modelAndView.addObject("outputModel",list.get(0));
        }
        return modelAndView;
    }
}
