package com.controller.admin;


import com.controller.CommonController;
import com.entity.DonationType;
import com.service.DonationTypeService;
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 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 DonationTypeController {
    @Resource
    private DonationTypeService donationTypeService;

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



    public static final String TABLE_NAME = "分类";


    /**
     * ===后台分类管理===
     *
     * @return
     */

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

    // 添加分类
    @RequestMapping("addDonationType.do")
	public String addDonationType(HttpServletRequest request, DonationType donationType, HttpSession session){
        donationTypeService.add(donationType);
        return "redirect:donationTypeList.do";
	}

    /*处理修改分类*/
    @RequestMapping("doUpdateDonationType.do")
    public String doUpdateDonationType(HttpServletRequest request, int id, ModelMap map) {
        map.put("donationType", donationTypeService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"donationType_update";
    }

    /*查看分类*/
    @RequestMapping("doLookDonationType.do")
    public String doLookDonationType(HttpServletRequest request, int id, ModelMap map) {
        map.put("donationType", donationTypeService.getById(id));
        /*通用方法*/
        commonController.commonMethod(map);
        return path+"donationType_look";
    }

    /*保存分类*/
    @RequestMapping("updateDonationTypeType.do")
    public String updateDonationType(HttpServletRequest request,DonationType donationType) {
        donationTypeService.update(donationType);
        return "redirect:donationTypeList.do";
    }


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

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

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

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

    /*删除分类*/
    @RequestMapping("deleteDonationType.do")
    public String deleteDonationType(int id) {
        donationTypeService.delete(id);
        return "redirect:donationTypeList.do";
    }

}

