com.wonders.bud.freshmommy.web.user.controller.BabyController.java Source code

Java tutorial

Introduction

Here is the source code for com.wonders.bud.freshmommy.web.user.controller.BabyController.java

Source

/** 
* 
 * Copyright (c) 1995-2012 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). You shall not disclose such
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.wonders.bud.freshmommy.web.user.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wonders.bud.framework.common.page.Page;
import com.wonders.bud.framework.common.page.PageUtil;
import com.wonders.bud.framework.common.page.PageVO;
import com.wonders.bud.framework.common.util.QueryParam;
import com.wonders.bud.framework.common.util.RestMsg;
import com.wonders.bud.freshmommy.service.user.baby.model.po.BabyPO;
import com.wonders.bud.freshmommy.service.user.baby.service.BabyService;
import com.wonders.bud.freshmommy.web.user.vo.BabyVO;

/**<p>
 * Title: freshmommy_[]_[???]
 * </p>
 * <p>
 * Description: [??Controller]
 * </p>
 * 
 * @author linqin
 * @version $Revision$ 2014-11-20
 * @author (lastest modification by $Author$)
 * @since 20100901
 */
@Controller
@RequestMapping("api/baby")
public class BabyController {

    protected Logger log = Logger.getLogger(BabyController.class);
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Autowired
    private BabyService babyService;

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ResponseBody
    public RestMsg<PageVO<BabyVO>> findBabiesByPage(HttpServletRequest request) {

        RestMsg<PageVO<BabyVO>> rm = new RestMsg<PageVO<BabyVO>>();

        String bName = request.getParameter("bName");
        String bSex = request.getParameter("bSex");
        String starttime = request.getParameter("starttime");//
        String endtime = request.getParameter("endtime");//?
        String row = request.getParameter("row");
        String start = request.getParameter("start");
        String sortorder = request.getParameter("sord"); //?ASC or DESC?DESC
        String sortfield = request.getParameter("sidx");

        try {
            QueryParam param = new QueryParam();
            Map<String, Object> eq = new HashMap<String, Object>();
            if (StringUtils.isNotEmpty(starttime) && StringUtils.isNotEmpty(endtime)) {
                Map<String, Object[]> between = new HashMap<String, Object[]>();
                Object obj[] = new Object[2];
                obj[0] = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(starttime.trim() + " 00:00:00");
                obj[1] = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(endtime.trim() + " 23:59:59");
                between.put("bBirthday", obj);
                param.setBetween(between);
            }
            if (StringUtils.isNotBlank(bSex)) {
                eq.put("bSex", Integer.valueOf(bSex));
                param.setEq(eq);
            }
            Map<String, Object> like = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(bName)) {
                like.put("bName", bName);
                param.setLike(like);
            }
            Page<BabyPO> page = new Page<BabyPO>();
            page.setParam(param);
            PageUtil.pageSplit(page, start, row, sortorder, sortfield, null);
            page = babyService.findBabiesByPage(page);
            PageVO<BabyVO> pageVO = new PageVO<BabyVO>();
            List<BabyVO> data = new ArrayList<BabyVO>();
            List<BabyPO> babyList = page.getResult();
            if (null != babyList && babyList.size() > 0) {
                for (BabyPO po : babyList) {
                    BabyVO vo = new BabyVO();
                    BeanUtils.copyProperties(vo, po);
                    vo.setbBirthday(sdf.format(po.getbBirthday()));
                    data.add(vo);
                }
            }
            pageVO.setData(data);
            PageUtil.pageVOSplit(page, pageVO);
            rm.setResult(pageVO);
            rm = rm.successMsg();
        } catch (Exception e) {
            rm.errorMsg("?");
            log.error(e.getLocalizedMessage());
        }
        return rm;
    }

    /**
     * <p>
     * Description:[?id??]
     * </p>
     * 
     * Created by linqin [2014-11-20]
     * Midified by [] []
     * @param request
     * @return
     */
    @RequestMapping(value = "/babyList", method = RequestMethod.GET)
    @ResponseBody
    public RestMsg<List<BabyVO>> findBabiesByList(HttpServletRequest request) {

        RestMsg<List<BabyVO>> rm = new RestMsg<List<BabyVO>>();

        String uId = request.getParameter("uId");

        try {
            if (StringUtils.isBlank(uId)) {
                rm.errorMsg("?id");
                return rm;
            }
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("uId", Integer.valueOf(uId));
            List<BabyPO> list = new ArrayList<BabyPO>();
            list = babyService.findBabiesByList(paramMap);
            List<BabyVO> babyList = new ArrayList<BabyVO>();
            if (null != list && list.size() > 0) {
                for (BabyPO baby : list) {
                    BabyVO vo = new BabyVO();
                    BeanUtils.copyProperties(vo, baby);
                    babyList.add(vo);
                }
            }
            rm.setResult(babyList);
            rm.successMsg();
        } catch (Exception e) {
            rm.errorMsg(e.getLocalizedMessage());
            log.error(e.getLocalizedMessage());
        }
        return rm;
    }

}