package com.yc.education.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.ServiceSupport;
import com.yc.education.service.IServiceSupportService;
import com.yc.education.util.AjaxMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
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.List;

/**
 * 后台 服务支持
 * @author Hickey
 * @create 2019-07-29 14:15
 */
@Controller
public class ServiceManageController {

    @Autowired
    private IServiceSupportService iServiceSupportService;


    /**
     * 后台  服务支持   查询所有服务信息 分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/admin/serviceManage.html")
    public ModelAndView listAllService(@RequestParam(defaultValue = "1")Integer pageNum,
                                       @RequestParam(defaultValue = "3")Integer pageSize){

        ModelAndView mav  =  new ModelAndView();

        List<ServiceSupport>  listAllService  =  iServiceSupportService.listAllService(pageNum,pageSize);//查询服务信息 分页
        mav.addObject("pageinfo",new PageInfo<ServiceSupport>(listAllService));

        mav.addObject("pageNum",pageNum);//页码

        mav.addObject("pageSize",pageSize);//每页条数

        return  mav;
    }

    /**
     * 后台  根据id查询该条服务信息到修改界面
     * @param id
     * @return
     */
    @RequestMapping("/admin/updateService.html")
    public ModelAndView updateService(Integer id){

        ModelAndView   mav  =  new ModelAndView();

        ServiceSupport findServiceById  = iServiceSupportService.selectByKey(id);//根据id查询当前服务信息
        mav.addObject("findServiceById",findServiceById);

        return mav;
    }

    /**
     * 后台  修改服务信息
     * @param serviceSupport
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/updateDetailService.html")
    public  AjaxMessage<Object> updateDetailService(ServiceSupport serviceSupport){

        AjaxMessage  am = new AjaxMessage();

        int rows  =  iServiceSupportService.updateNotNull(serviceSupport);

        if (rows > 0 ){
            am.setIs(true);
            am.setMsg("修改成功");
        }else {
            am.setIs(false);
            am.setMsg("信息修改失败");
        }
        return  am;
    }
}
