package com.controller.admin;


import com.controller.CommonController;
import com.entity.Place;
import com.service.PlaceService;
import com.util.Info;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  应急资金控制类
 * @ignore
 * @Author:Alcedo QQ:815657032
 * @Date 2020/4/15 11:05
 */
@Controller
@RequestMapping("/admin/")
public class PlaceController {
    @Resource
    private PlaceService placeService;


    @Resource
    private CommonController commonController;
    /*设置访问路径*/
    private String path="admin/views/place/";



    public static final String TABLE_NAME = "应急资金";



    /**
     * ===后台应急资金管理===
     *
     * @return
     */

    /*处理添加*/
    @RequestMapping("doAddPlace.do")
    public String doAddPlace(HttpServletRequest request, ModelMap map) {
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_add";
    }

    // 添加
    @RequestMapping("addPlace.do")
	public String addPlace(@RequestParam(value="file",required=false)MultipartFile file, String img,
                           HttpServletRequest request, Place place, HttpSession session){
             img=Info.fileUpload(file, request, img);
        place.setImg(img);
        placeService.add(place);
        return "redirect:placeList.do";
	}




    /*处理修改*/
    @RequestMapping("doUpdatePlaces.do")
    public String doUpdatePlaces(HttpServletRequest request, int id, ModelMap map) {
        map.put("places", placeService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_update";
    }

    /*处理修改*/
    @RequestMapping("doUpdatePlace.do")
    public String doUpdatePlace(HttpServletRequest request, int id, ModelMap map) {
        map.put("places", placeService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_update";
    }

    /*查看*/
    @RequestMapping("doLookPlaces.do")
    public String doLookPlaces(HttpServletRequest request, int id, ModelMap map) {
        map.put("places", placeService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_look";
    }

    /*保存*/
    @RequestMapping("updatePlaces.do")
    public String updatePlaces(@RequestParam(value="file",required=false)MultipartFile file,String img,
    HttpServletRequest request,Place place) {
            img=Info.fileUpload(file, request, img);
            if(img!=null&&!img.equals("")){
                place.setImg(img);
            }
        placeService.update(place);
        return "redirect:placeList.do";
    }


    // 分页查询信息的列表
    @RequestMapping("placeList.do")
    public String placeList(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                            HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();

        List<Place> list = placeService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_list";
    }

    // 分页查询信息的列表
    @RequestMapping("place.do")
    public String place(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                            HttpSession session) {
        Map<String, Object> bMap = new HashMap<>();

        List<Place> list = placeService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"scens";
    }




    // 分页查询信息的列表--模糊查询
    @RequestMapping("placesListSearch.do")
    public String placesListSearch(@RequestParam(value = "page", defaultValue = "0") Integer page, ModelMap map,
                                  HttpSession session, Place place) {
        Map<String, Object> bMap = new HashMap<>();

        if (place.getName() != null && !place.getName().equals("")) {
            bMap.put("name",place.getName());
        }
        List<Place> list = placeService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"place_list";
    }

    /*删除*/
    @RequestMapping("deletePlaces.do")
    public String deletePlaces(int id) {
        placeService.delete(id);
        return "redirect:placeList.do";
    }

}

