package com.lightinit.hsdataplatform.controller.admin;

import com.lightinit.hsdataplatform.dictionary.DicActivityState;
import com.lightinit.hsdataplatform.dictionary.DicActivityType;
import com.lightinit.hsdataplatform.entity.Activity;
import com.lightinit.hsdataplatform.entity.ActivityWithBLOBs;
import com.lightinit.hsdataplatform.entity.BaseExample;
import com.lightinit.hsdataplatform.model.ResultState;
import com.lightinit.hsdataplatform.model.admin.ActivitySearchModel;
import com.lightinit.hsdataplatform.service.admin.IActivityMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/admin/activity_mgr")
public class ActivityMgrController {

    @Autowired
    private IActivityMgrService activityMgrService;
    @RequestMapping(value = "index.htm")
    public ModelAndView index(){
        ModelAndView modelAndView = new ModelAndView("admin/activity_mgr/index");
        modelAndView.addObject("dicActivityType", DicActivityType.getInstacne().getDicMap());
        modelAndView.addObject("dicActivityState", DicActivityState.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "list.action")
    public ModelAndView list(ActivitySearchModel inputModel, @RequestParam(defaultValue = "1") int pageNo){
        ModelAndView modelAndView = new ModelAndView("admin/activity_mgr/list");
        BaseExample.Page pager = new BaseExample.Page();
        pager.setPageNo(pageNo);
        List<Activity> outputModel = activityMgrService.queryList(inputModel, pager);
        pager.setTotal(activityMgrService.queryCount(inputModel));
        modelAndView.addObject("list", outputModel);
        modelAndView.addObject("pager", pager);
        return modelAndView;
    }

    @RequestMapping(value = "add.htm")
    public ModelAndView add(){
        ModelAndView modelAndView = new ModelAndView("admin/activity_mgr/add");
        modelAndView.addObject("dicActivityType", DicActivityType.getInstacne().getDicMap());
        modelAndView.addObject("dicActivityState", DicActivityState.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "add.action")
    @ResponseBody
    public ResultState doAdd(ActivityWithBLOBs inputModel){
        ResultState resultState = activityMgrService.add(inputModel);
        return resultState;
    }

    @RequestMapping(value = "edit.htm/{id}")
    public ModelAndView edit(@PathVariable long id){
        ModelAndView modelAndView = new ModelAndView("admin/activity_mgr/edit");
        modelAndView.addObject("busiModel", activityMgrService.queryOne(id));
        modelAndView.addObject("dicActivityType", DicActivityType.getInstacne().getDicMap());
        modelAndView.addObject("dicActivityState", DicActivityState.getInstacne().getDicMap());
        return modelAndView;
    }

    @RequestMapping(value = "edit.action")
    @ResponseBody
    public ResultState doEdit(ActivityWithBLOBs inputModel){
        ResultState resultState = activityMgrService.edit(inputModel);
        return resultState;
    }

    @RequestMapping(value = "delete.action/{id}")
    @ResponseBody
    public ResultState delete(@PathVariable long id){
        ResultState<Long> resultState = activityMgrService.delete(id);
        return resultState;
    }

}
