package com.yihu.hos.resource.controller;

import com.yihu.hos.resource.model.RsResourceRest;
import com.yihu.hos.resource.model.RsResourceRestDetail;
import com.yihu.hos.resource.service.RsResourceRestServiceImpl;
import com.yihu.hos.web.framework.model.DataGridResult;
import com.yihu.hos.web.framework.model.Result;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * Created by chenweida on 2016/1/30.
 */
@RequestMapping("/resourceRest")
@Controller
public class RsResourceRestController {
    @Resource(name = RsResourceRestServiceImpl.BEAN_ID)
    private RsResourceRestServiceImpl resourceRestService;

    @RequestMapping("/initial")
    public String initial(Model model) {
        model.addAttribute("contentPage", "resource/resourcerest/resourceRest");
        return "partView";
    }

    @RequestMapping("/resourceRestDialog")
    public String resourceRestDialog(Model model, String id, String mode) {
        RsResourceRest rrr;
        try {
            if ("view".equals(mode)) {
                rrr = resourceRestService.getResourceRestById(id);
            } else if ("modify".equals(mode)) {
                rrr = resourceRestService.getResourceRestById(id);
            } else {
                rrr = new RsResourceRest();
            }
            model.addAttribute("resourceId", id);
            model.addAttribute("mode", mode);
            model.addAttribute("model", rrr);
            model.addAttribute("contentPage", "resource/resourcerest/resourceRestDialog");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("/resourceRestDetailDialog")
    public String resourceRestDetailDialog(Model model, String id, String mode) {
        RsResourceRestDetail rrrd;
        try {
            if ("view".equals(mode)) {
                rrrd = resourceRestService.getResourceRestDetail(id);
            } else if ("modify".equals(mode)) {
                rrrd = resourceRestService.getResourceRestDetail(id);
            } else {
                rrrd= new RsResourceRestDetail();
            }
            model.addAttribute("resourceId", id);
            model.addAttribute("mode", mode);
            model.addAttribute("model", rrrd);
            model.addAttribute("contentPage", "resource/resourcerest/resourceRestDetailDialog");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("/getResourceRestDetilList")
    @ResponseBody
    public Result getResourceRestDetilList(HttpServletRequest request) {
        try {
            String page = StringUtils.isEmpty(request.getParameter("page"))?"1":request.getParameter("page");
            String rows = StringUtils.isEmpty(request.getParameter("rows"))?"10":request.getParameter("rows");
            String resourceRestId = request.getParameter("resourceRestId");
            String id = request.getParameter("id");
            String resourceRestName = request.getParameter("resourceRestName");
            DataGridResult result = new DataGridResult(Integer.valueOf(page), Integer.valueOf(rows));
            Map<String, Object> conditionMap = new HashMap<>();
            conditionMap.put("resourceRestId",resourceRestId);
            conditionMap.put("id",id);
            conditionMap.put("resourceRestName",resourceRestName);
            result = resourceRestService.getResourceRestDetilList(conditionMap, Integer.valueOf(page), Integer.valueOf(rows));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    @RequestMapping("/getResourceRestList")
    @ResponseBody
    public Result getResourceRestList(HttpServletRequest request) {
        try {
            String page = request.getParameter("page");
            String rows = request.getParameter("rows");
            String categoryId = request.getParameter("categoryId");
            DataGridResult result = new DataGridResult(Integer.valueOf(page), Integer.valueOf(rows));
            Map<String, Object> conditionMap = new HashMap<>();
            conditionMap.put("categoryId",categoryId);
            result = resourceRestService.getResourceRestList(conditionMap, Integer.valueOf(page), Integer.valueOf(rows));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    @RequestMapping("/deleteResourceRest")
    @ResponseBody
    public Result deleteResourceRest(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return  resourceRestService.deleteResourceRestById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

    @RequestMapping("/deleteResourceRestDetail")
    @ResponseBody
    public Result deleteResourceRestDetail(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return resourceRestService.deleteResourceRestDetail(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }

    }

    @RequestMapping("/addResourceRest")
    @ResponseBody
    public Result addResourceRest(HttpServletRequest request) {
        try {
            RsResourceRest rr=new RsResourceRest();
            BeanUtils.populate(rr, request.getParameterMap());
            return resourceRestService.addResourceRest(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }

    }

    @RequestMapping("/updateResourceRest")
    @ResponseBody
    public Result updateResourceRest(HttpServletRequest request) {
        try {
            RsResourceRest rr=new RsResourceRest();
            BeanUtils.populate(rr, request.getParameterMap());
            return resourceRestService.updateResourceRest(rr);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }

    @RequestMapping("/addResourceRestDetail")
    @ResponseBody
    public Result addResourceRestDetail(HttpServletRequest request) {
        try {
            RsResourceRestDetail rrd=new RsResourceRestDetail();
            BeanUtils.populate(rrd, request.getParameterMap());
            return resourceRestService.addResourceRestDetail(rrd);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("新增失败");
        }

    }

    @RequestMapping("/updateResourceRestDetail")
    @ResponseBody
    public Result updateResourceRestDetail(HttpServletRequest request) {
        try {
            RsResourceRestDetail rrd=new RsResourceRestDetail();
            BeanUtils.populate(rrd, request.getParameterMap());
            return resourceRestService.updateResourceRestDetail(rrd);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }
}
