com.wonders.bud.freshmommy.web.openapi.controller.AsqController.java Source code

Java tutorial

Introduction

Here is the source code for com.wonders.bud.freshmommy.web.openapi.controller.AsqController.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.openapi.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.util.EncryptUtil;
import com.wonders.bud.framework.common.util.JsonUtils;
import com.wonders.bud.framework.common.util.RestMsg;
import com.wonders.bud.framework.common.util.TokenTransform;
import com.wonders.bud.freshmommy.service.asq.model.po.AsqHistoryPO;
import com.wonders.bud.freshmommy.service.asq.model.po.AsqQuestionPO;
import com.wonders.bud.freshmommy.service.asq.model.po.AsqResultPO;
import com.wonders.bud.freshmommy.service.asq.model.po.AsqScorePO;
import com.wonders.bud.freshmommy.service.asq.model.po.QuestionPO;
import com.wonders.bud.freshmommy.service.asq.model.po.ScorePO;
import com.wonders.bud.freshmommy.service.asq.service.AsqQuestionService;
import com.wonders.bud.freshmommy.service.asq.service.AsqResultService;
import com.wonders.bud.freshmommy.service.asq.service.AsqScoreService;
import com.wonders.bud.freshmommy.service.asq.service.QuestionService;
import com.wonders.bud.freshmommy.service.asq.service.ScoreService;
import com.wonders.bud.freshmommy.web.openapi.vo.AnwersScoreVO;
import com.wonders.bud.freshmommy.web.openapi.vo.ApiResultVO;
import com.wonders.bud.freshmommy.web.openapi.vo.AsqAnswersVO;
import com.wonders.bud.freshmommy.web.openapi.vo.AsqInfoVO;
import com.wonders.bud.freshmommy.web.openapi.vo.AsqParamsVO;
import com.wonders.bud.freshmommy.web.openapi.vo.asq.AsqHistoryVO;
import com.wonders.bud.freshmommy.web.utils.Constant;

/**
 * <p>
 * Title: freshmommy_[]_[]_[]
 * </p>
 * <p>
 * Description: --API
 * </p>
 * 
 * @author HYH
 * @version $Revision$ 2015114
 * @author (lastest modification by $Author$)
 * @since 20100901
 */
@Controller
@RequestMapping("app/asq")
public class AsqController {
    protected Logger log = Logger.getLogger(AsqController.class);

    @Autowired
    private QuestionService questionService;

    @Autowired
    private AsqScoreService asqScoreService;

    @Autowired
    private ScoreService scoreService;

    @Autowired
    private AsqQuestionService asqQuestionService;

    @Autowired
    private AsqResultService asqResultService;

    public static void main(String[] args) {
        AsqParamsVO asqParams = new AsqParamsVO();
        asqParams.setMonth(2);
        asqParams.setToken("x3q8UfddcQJeE/XMPmtAEQ==");
        asqParams.setbId(1);
        asqParams.setType(new Integer[] { 1, 2 });

        List<AsqAnswersVO> listAsq = new ArrayList<AsqAnswersVO>();
        for (int j = 0; j < 5; j++) {
            for (int i = 0; i < 6; i++) {
                AsqAnswersVO vo = new AsqAnswersVO();
                vo.setType(j + 1);
                vo.setNumber(i + 1);
                vo.setItemNum((int) (Math.random() * 10) % 3);
                listAsq.add(vo);
            }
        }
        try {
            System.out.println(JsonUtils.getJsonData(asqParams));
            System.out.println(EncryptUtil.encode(JsonUtils.getJsonData(asqParams)));
            System.out.println(
                    EncryptUtil.encode("{\"type\":[1,2],\"month\":2,\"token\":\"x3q8UfddcQJeE/XMPmtAEQ==\"}"));
            System.out.println(JsonUtils.getJsonData(listAsq));
            System.out.println(EncryptUtil.encode(JsonUtils.getJsonData(listAsq)));
            System.out.println("{\"type\":[1,2],\"month\":2,\"token\":\"x3q8UfddcQJeE/XMPmtAEQ==\"}");
        } catch (Exception e) {
        }
    }

    /**
     * <p>
     * Description:
     * </p>
     * 
     * @param request
     * @return RestMsg<Object>
     */
    @RequestMapping(value = "/subjects", method = RequestMethod.POST)
    @ResponseBody
    public RestMsg<List<AsqInfoVO>> AsqSubject(HttpServletRequest request) {
        RestMsg<List<AsqInfoVO>> restMsg = new RestMsg<List<AsqInfoVO>>();
        String params = request.getParameter("params");// ?
        try {
            params = EncryptUtil.decode(params);
            AsqParamsVO asqParams = JsonUtils.jsonStringToBean(params, AsqParamsVO.class);

            // ???
            String token = asqParams.getToken();
            if (!TokenTransform.authorization(token)) {
                return restMsg.loginMsg();
            }
            int month = asqParams.getMonth();
            if (month == 0) {
                restMsg.errorMsg("?,?");
            }
            List<AsqInfoVO> data = new ArrayList<AsqInfoVO>();
            List<QuestionPO> list = questionService.getQuestionsByMonth(month);
            if (list != null && list.size() > 0) {
                for (QuestionPO po : list) {
                    AsqInfoVO vo = new AsqInfoVO();
                    vo.setSid(po.getQid());
                    vo.setExt(po.getqExt());
                    vo.setNumber(po.getqOrder());
                    vo.setSubject(po.getqInfo());
                    vo.setType(po.getqType());
                    vo.setImage(po.getqImage());
                    data.add(vo);
                }

            }
            restMsg.successMsg();
            restMsg.setResult(data);
        } catch (Exception e) {
            log.error(e.getMessage());
            restMsg.errorMsg("?");
        }
        return restMsg;
    }

    /**
     * <p>
     * Description:ASQ???
     * </p>
     * Created by [Administrator] [2015116] Midified by [] []
     * 
     * @param request
     * @return
     */
    @RequestMapping(value = "/scores", method = RequestMethod.POST)
    @ResponseBody
    public RestMsg<List<AnwersScoreVO>> babyScore(HttpServletRequest request) {
        RestMsg<List<AnwersScoreVO>> restMsg = new RestMsg<List<AnwersScoreVO>>();

        String params = request.getParameter("params");// ?
        String answers = request.getParameter("answers");// ??

        try {
            params = EncryptUtil.decode(params);
            AsqParamsVO asqParams = JsonUtils.jsonStringToBean(params, AsqParamsVO.class);
            // ???
            String token = asqParams.getToken();
            if (!TokenTransform.authorization(token)) {
                return restMsg.loginMsg();
            }
            int month = asqParams.getMonth();
            int bId = asqParams.getbId();
            if (month == 0 || month < 2 || month > 37) {
                restMsg.errorMsg("?,?");
            }
            if (bId == 0) {
                restMsg.errorMsg("?,????");
            }

            List<AnwersScoreVO> data = new ArrayList<AnwersScoreVO>();
            List<AsqScorePO> asqScores = null;
            if (StringUtils.isNotBlank(answers)) {
                answers = EncryptUtil.decode(answers);
                List<AsqAnswersVO> listAsq = JsonUtils.jsonStringToList(answers, AsqAnswersVO.class);
                if (answers != null && listAsq != null && listAsq.size() > 0) {
                    for (AsqAnswersVO asq : listAsq) {
                        String qid = "";
                        if (month < 10) {
                            qid = "0";
                        }
                        int type = asq.getType();
                        int number = asq.getNumber();
                        int itemNum = asq.getItemNum();
                        if (type < 1 || type > 5) {
                            return restMsg.errorMsg("");
                        }
                        if (number < 1 || number > 6) {
                            return restMsg.errorMsg("?");
                        }
                        if (itemNum < 0 || itemNum > 2) {
                            return restMsg.errorMsg("");
                        }
                        qid = qid + month + "_" + type + "_" + number;
                        AsqQuestionPO po = asqQuestionService.getScore(bId, qid);

                        int score = itemNum * 5;// 
                        if (po != null) {// ?
                            po.setScore(score);
                            asqQuestionService.update(po);
                        } else {
                            po = new AsqQuestionPO();
                            po.setBid(bId);
                            po.setScore(score);
                            po.setStatus(Constant.ASQ_FINISHED);
                            po.setQid(qid);
                            asqQuestionService.save(po);
                        }
                    }
                    asqScores = asqScoreService.countMonthScore(bId, month);
                }
            } else {
                asqScores = asqScoreService.getMonthScore(bId, month);
            }
            if (asqScores != null && asqScores.size() > 0) {
                for (AsqScorePO asq : asqScores) {
                    ScorePO spo = scoreService.getById(asq.getSid());
                    if (spo != null) {
                        AnwersScoreVO vo = new AnwersScoreVO();
                        vo.setTop(spo.getsMax());
                        vo.setButtom(spo.getsMin());
                        vo.setType(spo.getqType());
                        vo.setTitle(Constant.qTypeMap.get(spo.getqType()));
                        vo.setScore(asq.getSource());
                        data.add(vo);
                    }
                }
            }
            restMsg.successMsg();
            restMsg.setResult(data);
        } catch (Exception e) {
            log.error(e.getMessage());
            restMsg.errorMsg("??");
        }
        return restMsg;
    }

    /**
     * <p>
     * Description:??
     * </p>
     * 
     * @param request
     * @return RestMsg<Object>
     */
    @RequestMapping(value = "/history", method = RequestMethod.POST)
    @ResponseBody
    public RestMsg<List<AsqHistoryVO>> Asqhistory(HttpServletRequest request) {
        RestMsg<List<AsqHistoryVO>> restMsg = new RestMsg<List<AsqHistoryVO>>();
        String params = request.getParameter("params");// ?
        try {
            params = EncryptUtil.decode(params);
            AsqParamsVO asqParams = JsonUtils.jsonStringToBean(params, AsqParamsVO.class);

            // ???
            String token = asqParams.getToken();
            if (!TokenTransform.authorization(token)) {
                return restMsg.loginMsg();
            }
            int bId = asqParams.getbId();
            if (bId == 0) {
                restMsg.errorMsg("?,????");
            }
            List<AsqHistoryPO> poList = asqScoreService.getAsqHistory(bId);
            List<AsqHistoryVO> result = new ArrayList<AsqHistoryVO>();
            if (poList != null && poList.size() > 0) {
                for (AsqHistoryPO po : poList) {
                    AsqHistoryVO vo = new AsqHistoryVO();
                    vo.setDate(po.getLogDate());
                    vo.setMonth(po.getMonth());
                    result.add(vo);
                }
            }
            restMsg.successMsg();
            restMsg.setResult(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            restMsg.errorMsg("???");
        }
        return restMsg;
    }

    /**
     * <p>
     * Description:??
     * </p>
     * 
     * @param request
     * @return RestMsg<Object>
     */
    @RequestMapping(value = "/results", method = RequestMethod.POST)
    @ResponseBody
    public RestMsg<List<ApiResultVO>> AsqResults(HttpServletRequest request) {
        RestMsg<List<ApiResultVO>> restMsg = new RestMsg<List<ApiResultVO>>();
        String params = request.getParameter("params");// ?
        try {
            params = EncryptUtil.decode(params);
            AsqParamsVO asqParams = JsonUtils.jsonStringToBean(params, AsqParamsVO.class);

            // ???
            String token = asqParams.getToken();
            if (!TokenTransform.authorization(token)) {
                return restMsg.loginMsg();
            }
            int month = asqParams.getMonth();
            if (month == 0) {
                restMsg.errorMsg("??");
            }
            Integer[] types = asqParams.getType();
            if (types == null || types.length == 0) {
                restMsg.errorMsg("?");
            }
            List<ApiResultVO> result = new ArrayList<ApiResultVO>();
            for (int type : types) {
                ApiResultVO vo = new ApiResultVO();
                String prefix = "";
                if (month < 10) {
                    prefix = "0" + month;
                } else {
                    prefix = "" + month;
                }
                AsqResultPO po = asqResultService.getById(type + "_" + prefix);
                if (po != null) {
                    vo.setDesc(po.getrDesc());
                    vo.setMonth(month);
                    vo.setType(type);
                    result.add(vo);
                }
            }
            restMsg.successMsg();
            restMsg.setResult(result);
        } catch (Exception e) {
            log.error(e.getMessage());
            restMsg.errorMsg("???");
        }
        return restMsg;
    }
}