package com.yc.education.controller.admin;

import com.github.pagehelper.PageInfo;
import com.yc.education.model.CaseType;
import com.yc.education.service.ICaseTypeService;
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-26 11:19
 */
@Controller
public class caseTypeManageController {

    @Autowired
    private ICaseTypeService iCaseTypeService;


    /**
     * 后台
     * 查询所有类别信息 并分页
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/admin/caseTypeManage.html")
    public ModelAndView showAllCaseType(@RequestParam(defaultValue = "1") Integer pageNum,
                                        @RequestParam(defaultValue = "3") Integer pageSize){

        ModelAndView mav = new ModelAndView();

        List<CaseType>  allCaseType =   iCaseTypeService.listAllCaseType(pageNum,pageSize);

        mav.addObject("pageinfo",new PageInfo<CaseType>(allCaseType));

        mav.addObject("pageNum",pageNum);

        mav.addObject("pageSize",pageSize);

        return  mav;
    }

    /**
     * 后台
     * 根据id查询该条案例类别信息
     * @param id
     * @return
     */
    @RequestMapping("/admin/updateCaseType.html")
    public ModelAndView updateCaseType(Integer id){

        ModelAndView mav  = new ModelAndView();

        CaseType  caseType = iCaseTypeService.selectByKey(id);

        mav.addObject("caseType",caseType);

        return  mav;
    }

    /**
     *后台 类别 修改
     * @param caseType
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/updateDetailCaseType.html")
    public AjaxMessage<Object> updateDetailCaseType(CaseType caseType){

        AjaxMessage  am   =  new AjaxMessage();

        int rows  = iCaseTypeService.updateNotNull(caseType);

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