package com.controller.admin;


import com.controller.CommonController;
import com.entity.Capital;
import com.service.CapitalService;
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 CapitalController {
    @Resource
    private CapitalService capitalService;


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



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



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

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

    // 添加应急资金
    @RequestMapping("addCapital.do")
	public String addCapital(@RequestParam(value="file",required=false)MultipartFile file,String img,
        HttpServletRequest request,Capital news, HttpSession session){
             img=Info.fileUpload(file, request, img);
             news.setImg(img);
        capitalService.add(news);
        return "redirect:capitalList.do";
	}




    /*处理修改应急资金*/
    @RequestMapping("doUpdateCapitals.do")
    public String doUpdateCapitals(HttpServletRequest request, int id, ModelMap map) {
        map.put("capitals", capitalService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"capital_update";
    }

    /*处理修改应急资金*/
    @RequestMapping("doUpdateCapital.do")
    public String doUpdateCapital(HttpServletRequest request, int id, ModelMap map) {
        map.put("capitals", capitalService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"capital_update";
    }

    /*查看应急资金*/
    @RequestMapping("doLookCapitals.do")
    public String doLookCapitals(HttpServletRequest request, int id, ModelMap map) {
        map.put("capitals", capitalService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"capital_look";
    }

    /*保存应急资金*/
    @RequestMapping("updateCapitals.do")
    public String updateCapitals(@RequestParam(value="file",required=false)MultipartFile file,String img,
    HttpServletRequest request,Capital capital) {
            img=Info.fileUpload(file, request, img);
            if(img!=null&&!img.equals("")){
                capital.setImg(img);
            }
        capitalService.update(capital);
        return "redirect:capitalList.do";
    }


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

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

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

        if (capital.getName() != null && !capital.getName().equals("")) {
            bMap.put("name",capital.getName());
        }
        if (capital.getPrice() != 0) {
            bMap.put("price",capital.getPrice());
        }
        List<Capital> list = capitalService.getAll(bMap);
        map.put("list", list);
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"capital_list";
    }

    /*删除应急资金*/
    @RequestMapping("deleteCapitals.do")
    public String deleteCapitals(int id) {
        capitalService.delete(id);
        return "redirect:capitalList.do";
    }

}

