package com.yihu.hos.resource.controller;

import com.yihu.hos.resource.model.RsResourceCategory;
import com.yihu.hos.resource.service.RsCategoryServiceImpl;
import com.yihu.hos.web.framework.model.Result;
import com.yihu.hos.web.framework.util.controller.BaseController;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
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 2015/12/15.
 */
@Controller
@RequestMapping("/resource/rsCategory")
public class RsCategoryController extends BaseController {

    @Resource(name = RsCategoryServiceImpl.BEAN_ID)
    private RsCategoryServiceImpl rsCategoryService;

    //跳转到列表页
    @RequestMapping("initial")
    public String initial(Model model) {

        model.addAttribute("contentPage", "/resource/category/rsCategoryManage");
        return "partView";
    }


    @RequestMapping("/rsCategoryInfo")
    public String rsCategoryInfo(Model model, String id, String mode) {
        RsResourceCategory rrrd;
        try {
            if ("view".equals(mode)) {
                rrrd = rsCategoryService.getCategoryById(id);
            } else if ("modify".equals(mode)) {
                rrrd = rsCategoryService.getCategoryById(id);
            } else {
                rrrd = new RsResourceCategory();
            }
            model.addAttribute("resourceId", id);
            model.addAttribute("mode", mode);

            String s = rrrd.getRemark();
            StringBuilder s1 = new StringBuilder();
            if (s != null) {
                for (int i = 0; i < s.length(); i++) {
                    if (s.charAt(i) == '\'' || s.charAt(i) == '\"') {
                        s1.append("\\");
                        s1.append(s.charAt(i));
                    } else
                        s1.append(s.charAt(i));
                }
            }
           // rrrd.setRemark(s1.toString());
            model.addAttribute("model", rrrd);
            model.addAttribute("contentPage", "resource/category/rsCategoryInfoDialog");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "pageView";
    }

    @RequestMapping("/searchRsCategory")
    @ResponseBody
    public Result getList(String searchNm, String parentid, String status, int page, int rows) {
        Map<String, Object> conditionMap = new HashMap<>();
        conditionMap.put("name", searchNm);
        conditionMap.put("page", page);
        conditionMap.put("rows", rows);
        Result result = null;
        try {
            result = rsCategoryService.getList(conditionMap);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error(e.getMessage());
        }

        return result;
    }


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

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

    }

    @RequestMapping("/deleteCategory")
    @ResponseBody
    public Result deleteCategory(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return rsCategoryService.deleteCategory(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }

    }

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

    }

}
