package com.yc.education.controller.admin;

import com.baidu.ueditor.ActionEnter;
import com.baidu.ueditor.define.ActionMap;
import com.baidu.ueditor.define.BaseState;
import com.github.pagehelper.PageInfo;
import com.yc.education.model.CaseType;
import com.yc.education.model.SuccessCase;
import com.yc.education.service.ICaseTypeService;
import com.yc.education.service.ISuccessCaseService;
import com.yc.education.util.AjaxMessage;
import com.yc.education.util.ViewUtil;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 后台 案例管理控制层
 * @author Hickey
 * @create 2019-07-25 15:45
 */
@Controller
public class CaseManageController {

    @Autowired
    private ISuccessCaseService iSuccessCaseService;

    @Autowired
    private ICaseTypeService iCaseTypeService;

    /**
     * 后台
     * 查询所有案例信息 分页  多条件查询
     * @param id
     * @param name
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/admin/caseManage.html")
    public ModelAndView listAllCase(Integer id, String name,
                                    @RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "6") Integer pageSize){

        ModelAndView mav = new ModelAndView();

        List<CaseType> listCaseType  = iCaseTypeService.listAllCaseType();//查询案例类别

        mav.addObject("listCaseType",listCaseType);

        List<SuccessCase>  listAllCase = iSuccessCaseService.manageAllCase(id,name,pageNum,pageSize);//查询所有案例

        mav.addObject("pageinfo",new PageInfo<SuccessCase>(listAllCase));//案例信息 和 分页信息

        mav.addObject("id",id);//类别id

        mav.addObject("name",name);//根据案例名模糊搜索

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

        mav.addObject("pageSize",pageSize);//显示条数

        return  mav;
    }

    /**
     * 后台
     * 跳转到添加页面 并查询所有案例类别
     * @return
     */
    @RequestMapping("/admin/addCase.html")
    public  ModelAndView addCase(){
        ModelAndView mav = new ModelAndView();


        List<CaseType> listAllType  =  iCaseTypeService.listAllCaseType();

        mav.addObject("listAllType",listAllType);
        return  mav;
    }

    /**
     * 后台 添加案例
     * @param successCase
     * @param request
     * @return
     */
    @RequestMapping("/admin/addCaseDetail.html")
    public ModelAndView addCaseDetail(SuccessCase successCase, HttpServletRequest request){

        String rootPath = request.getSession().getServletContext().getRealPath("/");
        String contextPath = request.getSession().getServletContext().getContextPath();
        BaseState state = (BaseState) new ActionEnter(request, rootPath, contextPath, "admin")
                .invoke(ActionMap.UPLOAD_FILE);
        successCase.setCaseimage(state.getInfo("url"));//successCase:参数名  Caseimage:图片字段

        int rows  = iSuccessCaseService.save(successCase);//添加案例信息 返回受影响的行数

        return ViewUtil.returnview(rows,"caseManage.html","案例列表");//案例添加成功 跳转到caseManage.html
    }


    /**
     * 后台  跳转到修改界面
     * 根据案例id查询要修改的该条案例并展示的修改界面
     * @param id
     * @return
     */
    @RequestMapping("/admin/updateCase.html")
    public  ModelAndView  findCaseById(Integer id){

        ModelAndView  mav  =  new ModelAndView();

        SuccessCase  findCaseById  =  iSuccessCaseService.selectByKey(id);//根据列表中修改案例id  查询出要修改的该条案例

        mav.addObject("findCaseById",findCaseById);

        return  mav;
    }

    /**
     * 后台
     * 修改案例
     * @param successCase
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/updateCaseDetail.html")
    public AjaxMessage<Object> updateCase(SuccessCase successCase){

        AjaxMessage  am  = new AjaxMessage();

        int rows   =  iSuccessCaseService.updateNotNull(successCase);//调用通用接口修改案例

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

    /**
     * 后台
     * 根据案例id删除该条案例
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/deleteCase.html")
    public  AjaxMessage<Object> deleteCase(Integer id){

        AjaxMessage  am  =  new AjaxMessage();

        int rows  =  iSuccessCaseService.delete(id);//根据id删除该条案例信息

        if (rows > 0) {
            am.setIs(true);
            am.setMsg("删除成功");
        }else{
            am.setIs(false);
            am.setMsg("案例删除失败");
        }
        return am;
    }

    @ResponseBody
    @RequestMapping("/admin/deleteCaseArray.html")
    public  AjaxMessage deleteCaseArray(int [] caseArray){

        AjaxMessage am  = new AjaxMessage();

        int rows  =  iSuccessCaseService.deleteCaseArray(caseArray);

        if( rows > 1){
            am.setIs(true);
            am.setMsg("删除成功");
        }else {
            am.setIs(false);
            am.setMsg("批量删除失败");
        }
        return am;
    }


}
