package com.yihu.hos.resource.controller;

import com.yihu.hos.resource.model.RsDemensionCategory;
import com.yihu.hos.resource.model.RsDimension;
import com.yihu.hos.resource.service.RsDimensionServiceImpl;
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/26.
 */
@Controller
@RequestMapping("/dimension")
public class RsDimensionController {
    @Resource(name = RsDimensionServiceImpl.BEAN_ID)
    private RsDimensionServiceImpl rsDimensionService;

    /**
     * @param model
     * @param mode
     * @return
     */
    @RequestMapping("/dimension")
    public String dimension(Model model, String mode) {
        model.addAttribute("mode", mode);
        model.addAttribute("contentPage", "dimension/dimension/dimension");
        return "partView";
    }

    /**
     * @param model
     * @param mode
     * @return
     */
    @RequestMapping("/dimensioncatetory")
    public String dimensioncatetory(Model model, String mode) {
        model.addAttribute("mode", mode);
        model.addAttribute("contentPage", "dimension/dimensioncatetory/dimensioncatetory");
        return "partView";
    }

    /**
     * 跳转到编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editorDimension")
    public String editorDimension(Model model, String id,String category) {
        try {
            if (!StringUtils.isEmpty(id))
                    model.addAttribute("model", rsDimensionService.getDimensionById(id));
            if(category!=null)
                 model.addAttribute("category",category);

            }
        catch (Exception e) {

            }
        model.addAttribute("contentPage", "dimension/dimension/editorDimension");
        return "pageView";
    }

    /**
     * 跳转到编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editorDimensionCatetory")
    public String editorDimensionCatetory(Model model, String id) {
        if (!StringUtils.isEmpty(id)) {
            try {
                model.addAttribute("model", rsDimensionService.getDimensionCatetoryById(id));
            } catch (Exception e) {

            }
        }
        model.addAttribute("contentPage", "dimension/dimensioncatetory/editorDimensionCatetory");
        return "pageView";
    }

    /**
     * 维度类别列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getDimensionCategoryList")
    @ResponseBody
    public Result getDimensionCategoryList(HttpServletRequest request) {
        try {
            return rsDimensionService.getDimensionCategoryList();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 维度列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getDimensionList")
    @ResponseBody
    public Result getDimensionList(HttpServletRequest request) {
        try {
            Map<String, Object> conditionMap = new HashMap<String, Object>();
            conditionMap.put("category", request.getParameter("dimensionCategoryId"));
            conditionMap.put("name", request.getParameter("name"));
            String page = (String) conditionMap.get("page");
            String rows = (String) conditionMap.get("rows");
            conditionMap.put("page", StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
            conditionMap.put("rows", StringUtils.isEmpty(rows) ? 10 : Integer.valueOf(rows));
            return rsDimensionService.getDimensionList(conditionMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 维度类别列表
     *
     * @param request
     * @return
     */
    @RequestMapping("/getDimensionCatecoryList")
    @ResponseBody
    public Result getDimensionCatecoryList(HttpServletRequest request) {
        try {
            Map<String, Object> conditionMap = new HashMap<String, Object>();
            conditionMap.put("name", request.getParameter("name"));
            String page = (String) conditionMap.get("page");
            String rows = (String) conditionMap.get("rows");
            conditionMap.put("page", StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));
            conditionMap.put("rows", StringUtils.isEmpty(rows) ? 10 : Integer.valueOf(rows));
            return rsDimensionService.getDimensionCatecoryList(conditionMap);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }


    /**
     * 更新维度
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateDimension")
    @ResponseBody
    public Result updateDimension(HttpServletRequest request) {
        try {
            RsDimension rsDimension = new RsDimension();
            BeanUtils.populate(rsDimension, request.getParameterMap());
            return rsDimensionService.updateDimension(rsDimension);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 新增维度
     *
     * @param request
     * @return
     */
    @RequestMapping("/addDimension")
    @ResponseBody
    public Result addDimension(HttpServletRequest request) {
        try {
            RsDimension rsDimension = new RsDimension();
            BeanUtils.populate(rsDimension, request.getParameterMap());
            return rsDimensionService.addDimension(rsDimension);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 删除维度
     *
     * @param request
     * @return
     */
    @RequestMapping("/deleteDimension")
    @ResponseBody
    public Result deleteDimension(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return rsDimensionService.deleteDimensionById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 删除维度类别
     *
     * @param request
     * @return
     */
    @RequestMapping("/deleteDimensionCatetory")
    @ResponseBody
    public Result deleteDimensionCatetory(HttpServletRequest request) {
        try {
            String id = request.getParameter("id");
            return rsDimensionService.deleteDimensionCatetory(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 新增维度类别
     *
     * @param request
     * @return
     */
    @RequestMapping("/addDimensionCatetroy")
    @ResponseBody
    public Result addDimensionCatetroy(HttpServletRequest request) {
        try {
            RsDemensionCategory r = new RsDemensionCategory();
            BeanUtils.populate(r, request.getParameterMap());
            return rsDimensionService.addDimensionCatetroy(r);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }

    /**
     * 新增维度类别
     *
     * @param request
     * @return
     */
    @RequestMapping("/updateDimensionCatetroy")
    @ResponseBody
    public Result updateDimensionCatetroy(HttpServletRequest request) {
        try {
            RsDemensionCategory r = new RsDemensionCategory();
            BeanUtils.populate(r, request.getParameterMap());
            return rsDimensionService.updateDimensionCatetroy(r);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(e.getMessage());
        }

    }
}
