美文网首页金融基础技术与业务
基于CWAP框架的业务基础代码如何写就

基于CWAP框架的业务基础代码如何写就

作者: 李戬plutocracy | 来源:发表于2018-09-30 18:37 被阅读2次

如何写一个REST?

@Controller
@RequestMapping(CbtMarketHelper.PLUGIN_ID)
public class MarketConditionsBondQueryRest extends GenericRest {

    private static final TsLogger logger = TsLogger.getLogger(MarketConditionsBondQueryRest.class);

    protected static Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();

    @Note(note = "行情筛选条件债券列表查询")
    @RequestMapping(value = "/marketConditionsBondGet", method = { RequestMethod.POST })
    @ResponseBody
    public SimpleMessage<?> marketConditionsBondGet(HttpServletRequest req, HttpServletResponse rep,
            @RequestBody String json) {
        // 筛选条件:债劵代码/待偿期/债劵类型/发行时间/到期时间/币种,发行人/发行人是否上市/发行人行业/发行人性质,评级/票面/含权/永续债/特殊
        try {
            logger.info("进入市场行情筛选条件进行查询方法, 参数{}", json);
            String userCd = String.valueOf(req.getAttribute(UserLoginParam.USER_CODE.param()));
            MarketConditionsBondQueryDTO param = gson.fromJson(json, MarketConditionsBondQueryDTO.class);
            logger.info("用户{}筛选条件查询参数为:\n{}", userCd, prettyGson.toJson(param));
            List<MarketBondVo> bondList = CbtMarketContext.get().getMarketConditionsBondQueryApi()
                    .marketConditionsBondGet(param.toMarketConditionsBondQueryReq(userCd));
            if (bondList != null && bondList.size() > 20) {
                // 不要脑子抽风了把所有信息都打印到日志中,这样性能无论如何都无法提高,因为这个结果很可能是几万个
                logger.debug("查询结果,债劵列表{}", prettyGson.toJson(bondList.subList(0, 20)));
            } else {
                logger.debug("查询结果,债劵列表{}", prettyGson.toJson(bondList));
            }
            return SimpleMessage.ok().set("bondList", bondList);
        } catch (GenericTsRuntimeException e) {
            logger.error("行情筛选条件债券列表,查询异常", (Supervise)e);
            return SimpleMessage.error(e.getErrorInfo(req)).repCode(e.getErrorCode());
        } catch (Exception e) {
            logger.error("行情筛选条件债券列表,查询异常", e);
            return SimpleMessage.error("情筛选条件债券列表时发生系统内部查询异常").set("bondList", new ArrayList<>());
        }
    }
}

如何写一个Thin Service For Query & Write ?


/**
 * 行情复杂筛选条件保存服务
 * 
 * @author pluto
 *
 */
public class MarketConditonServiceImpl implements MarketConditonService {

    private static final TsLogger logger = TsLogger.getLogger(MarketConditonServiceImpl.class);

    String deleteSql = "DELETE FROM " + CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL
            + " WHERE USR_CD = ? AND CSTM_FLTR_NM = ? ";
    String insertSql = " INSERT INTO " + CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL
            + " (SR_NO_ID, USR_CD, CSTM_FLTR_NM, TMPLT_NO, WDGT_ID, QTS_FLTR_VL, DATA_SRC, CRTR, CRT_TM, UPDTR, UPD_TM )  VALUES ("
            + CbtMarketHelper.SEQUENCE_CBT_MKT_INFO_FLTR_DTL + ".NEXTVAL,?,?,?,?,?,?,?,?,?,?) ";
    String querySql = " SELECT QTS_FLTR_VL,CSTM_FLTR_NM FROM " + CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL
            + " WHERE USR_CD = ? AND TMPLT_NO = ? ";
    String queryListSql = " SELECT QTS_FLTR_VL,CSTM_FLTR_NM,TMPLT_NO FROM " + CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL
            + " WHERE USR_CD = ?  ";
    String updateSql = " UPDATE " + CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL
            + " SET QTS_FLTR_VL = ?   WHERE USR_CD = ? AND CSTM_FLTR_NM = ? ";

    /**
     * 筛选条件查询
     * 
     * @param param
     */
    @Override
    public List<MarketConditionVO> queryInfoGet(String userCode, MarketConditionsSaveDTO param) {
        Object[] args = new Object[] { userCode, param.getUserScreenConditionsSet() };
        LobHandler lobHandler = new DefaultLobHandler();
        List<MarketConditionVO> list = TsJdbcSingletonProvider.get()
                .getTsJdbcTemplateByTable(CbtMarketHelper.PLUGIN_ID, CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL)
                .query(querySql, args, new RowMapper<MarketConditionVO>() {

                    @Override
                    public MarketConditionVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                        try {
                            MarketConditionVO obj = new MarketConditionVO(userCode);
                            String condition = lobHandler.getClobAsString(rs, "QTS_FLTR_VL");
                            String conditionName = rs.getString("CSTM_FLTR_NM");
                            obj.setTemplateId(param.getTemplateNumber());// 模板号,由后台生成
                            obj.setConditionName(conditionName);// 用户定义的筛选条件名称
                            obj.setQueryCondition(condition);// 用户的筛选条件
                            return obj;
                        } catch (Exception e) {
                            logger.error("行情筛选条件查询MarketConditionVO转换时发生异常", e);
                            return null;
                        }
                    }
                });

        if (list == null) {
            list = new ArrayList<>();
        }
        logger.info("用户{}针对模板{}的行情筛选条件值查询的结果{}", userCode, param.getTemplateNumber(), Arrays.toString(list.toArray()));
        return list;

    }

    /**
     * 筛选条件列表查询
     * 
     * @param param
     */
    @Override
    public List<MarketConditionVO> queryInfoGetList(String userCode, MarketConditionsSaveDTO param) {
        Object[] args = new Object[] { userCode };
        LobHandler lobHandler = new DefaultLobHandler();
        List<MarketConditionVO> list = TsJdbcSingletonProvider.get()
                .getTsJdbcTemplateByTable(CbtMarketHelper.PLUGIN_ID, CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL)
                .query(queryListSql, args, new RowMapper<MarketConditionVO>() {

                    @Override
                    public MarketConditionVO mapRow(ResultSet rs, int rowNum) throws SQLException {
                        try {
                            MarketConditionVO obj = new MarketConditionVO(userCode);
                            String condition = lobHandler.getClobAsString(rs, "QTS_FLTR_VL");
                            String conditionName = rs.getString("CSTM_FLTR_NM");
                            String templateId = rs.getString("TMPLT_NO");
                            obj.setTemplateId(templateId);// 模板号,由后台生成
                            obj.setConditionName(conditionName);// 用户定义的筛选条件名称
                            obj.setQueryCondition(condition);// 用户的筛选条件
                            return obj;
                        } catch (Exception e) {
                            logger.error("行情筛选条件查询MarketConditionVO转换时发生异常", e);
                            return null;
                        }
                    }
                });
        if (list == null) {
            list = new ArrayList<>();
        }
        logger.info("用户{}针对模板{}的行情筛选条件值查询的结果{}", userCode, param.getTemplateNumber(), Arrays.toString(list.toArray()));
        return list;

    }

    /**
     * 筛选条件删除
     * 
     * @param param
     */
    @Override
    public void queryInfoDelete(String userCode, MarketConditionsSaveDTO param) {
        try {
            Object[] args = new Object[] { userCode, param.getUserScreenConditionsSet() };
            // 保存前,删除记录
            TsJdbcSingletonProvider.get()
                    .getTsJdbcTemplateByTable(CbtMarketHelper.PLUGIN_ID, CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL)
                    .update(deleteSql, args);
        } catch (Exception e) {
            logger.error("筛选条件删除时发生异常", e);
        }
        logger.info("用户{}针对模板{}的筛选条件进行删除", userCode, param.getUserScreenConditionsSet());
    }

    /**
     * 筛选条件新增
     * 
     * @param param
     */
    @Override
    public void queryInfoAdd(String userCode, MarketConditionsSaveDTO param) {
        try {
            final Timestamp now = new Timestamp(System.currentTimeMillis());
            TsJdbcSingletonProvider.get()
                    .getTsJdbcTemplateByTable(CbtMarketHelper.PLUGIN_ID, CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL)
                    .update(insertSql, new PreparedStatementSetter() {

                        @Override
                        public void setValues(PreparedStatement ps) throws SQLException {
                            ps.setString(1, userCode);
                            ps.setString(2, param.getUserScreenConditionsSet());
                            ps.setString(3, UUID.randomUUID().toString());
                            ps.setString(4, "-");// TODO:未来支持widgetId的记录
                            ps.setString(5, param.getQueryCondition());
                            ps.setString(6, "TBS");
                            ps.setString(7, "TBS-CREATER");
                            ps.setTimestamp(8, now);
                            ps.setString(9, "TBS-CREATER");
                            ps.setTimestamp(10, now);
                        }

                    });

        } catch (Exception e) {
            logger.error("筛选条件新增时发生异常", e);
        }
        logger.info("用户{}保存自定义模板{}的筛选条件", userCode, param.getUserScreenConditionsSet());
    }

    /**
     * 筛选条件更新
     * 
     * @param param
     */
    @Override
    public void queryInfoUpdate(String userCode, MarketConditionsSaveDTO param) {
        LobHandler lobHandler = new DefaultLobHandler();
        try {
            TsJdbcSingletonProvider.get()
                    .getTsJdbcTemplateByTable(CbtMarketHelper.PLUGIN_ID, CbtMarketHelper.CBT_MKT_INFO_FLTR_DTL)
                    .update(updateSql, new AbstractLobCreatingPreparedStatementCallback(lobHandler) {

                        @Override
                        protected void setValues(PreparedStatement ps, LobCreator lobCreator)
                                throws SQLException, DataAccessException {
                            lobCreator.setClobAsString(ps, 1, param.getQueryCondition());
                            ps.setString(2, userCode);
                            ps.setString(3, param.getUserScreenConditionsSet());
                        }
                    });
        } catch (Exception e) {
            logger.error("筛选条件更新时发生异常", e);
        }
        logger.info("用户{}更新自定义模板{}的筛选条件", userCode, param.getUserScreenConditionsSet());
    }

    /**
     * 筛选条件保存
     * 
     * @param param
     */
    @Override
    public void queryInfoSave(String userCode, MarketConditionsSaveDTO param) {
        List<MarketConditionVO> vo = this.queryInfoGet(userCode, param);
        if (vo != null && vo.size() > 0) {
            this.queryInfoUpdate(userCode, param);
        } else {
            this.queryInfoAdd(userCode, param);
        }
    }

}


相关文章

网友评论

    本文标题:基于CWAP框架的业务基础代码如何写就

    本文链接:https://www.haomeiwen.com/subject/okzwoftx.html