发表于: 2017-11-03 22:18:28
1 983
今天完成的事情:
理解代码逻辑,测试接口
package com.ptteng.controller.invest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ptteng.util.code.ProductErrorCode;
import com.ptteng.util.ProductUtil;
import com.qding.common.util.DataUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.ptteng.playboy.invest.model.Product;
import com.ptteng.playboy.invest.service.ProductService;
/**
* Product crud
*
* @author magenm
* @Date 2014-4-16 13:43
*
*/
@Controller
public class ProductController {
private static final Log log = LogFactory.getLog(ProductController.class);
@Autowired
private ProductService productService;
/**
* 条件查询得到产品列表
*
* @param request
* @param response
* @param model
* @param page
* @param size
* @param name
* 产品名称
* @param interestBeginAt
* 起息日
* @param minYearRate
* 最小年利率
* @param maxYearRate
* 最大年利率
* @param minAmount
* 起投额
* @param status
* 产品状态
* @param monthLimit
* 产品期限
* @return
* @author liut
*/
//timeLimitType
@RequestMapping(value = "/a/product/condition/search", method = RequestMethod.GET)
public String getProductList(HttpServletRequest request, HttpServletResponse response,
ModelMap model, Integer page, Integer size, String name,
Integer interestBeginAt, BigDecimal minYearRate,
BigDecimal maxYearRate, BigDecimal minAmount, Integer status,
Integer minMonthLimit, Integer maxMonthLimit, String code,
Integer timeLimitType) {
log.info("method getProductList() begin...");
if (page == null) {
page = 1;
}
if (size == null) {
size = 10;
}
int start = (page - 1) * size;
if (start < 0) {
start = 0;
}
int total = 0, totalPage = 0;
log.info("arguments: name = [" + name + "], interestBeginAt = [" + interestBeginAt
+ "], minYearRate = [" + minYearRate + "], maxYearRate = [" + maxYearRate
+ "], minAmount = [" + minAmount + "], status = [" + status + "], minMonthLimit = ["
+ minMonthLimit + "], maxMonthLimit = [" + maxMonthLimit + "], code = [" + code
+ "], timeLimitType = [" + timeLimitType + "]");
log.info("productList page paramer is: page = " + page + ", size = " + size);
try {
/* 第一步、动态拼接sql,并对参数合法性校验 */
Map<String, Object> params = ProductUtil.getProductListParam(name, interestBeginAt,
minYearRate, maxYearRate, minAmount, status, minMonthLimit, maxMonthLimit, code,
timeLimitType);
log.info("dynamic condition params is: " + params);
//如果map里面的键是error
if (params.containsKey("error")) {
log.info("params contain error key");
//model是一个modelmap
model.addAttribute("code", params.get("error"));
//data/json在web-inf/pages/data中
return "/data/json";
}
/* 第二步、条件查询产品列表 */
//规定是要传这四个值,偏底层,不用管
List<Long> ids = productService.getIdsByDynamicCondition(Product.class, params, start, size);
log.info("product ids is ===" + ids);
List<Product> productList = new ArrayList<>();
if (CollectionUtils.isEmpty(ids)) {
log.info("product ids is empty");
}
//如果非空,打印数组长度
else {
productList = productService.getObjectsByIds(ids);
log.info("product list size is: " + productList.size());
/* 第三步、查询总数 */
total = productService.getIdsByDynamicCondition(Product.class, params, 0, Integer.MAX_VALUE)
.size();
log.info("product total size is: " + total);
totalPage = (total - 1) / size + 1;
}
//这个是对应的接口文档中,返回数据的code状态码
model.addAttribute("code", 0);
//下面是返回数据的message,其中包括页码,每页数据最大值,后面的两个值不理解什么意思,但是原型图上有一列数据,有下一页跳转什么的一定要有这四个
model.addAttribute("page", page);
model.addAttribute("size", size);
model.addAttribute("total", total);
model.addAttribute("totalPage", totalPage);
//返回数据,直接返回整个数据,productlist表
model.addAttribute("productList", productList);
}
catch (Throwable t) {
log.error(t.getMessage());
log.error("get product list error, page is: " + page + " , size is: " + size);
model.addAttribute("code", -1);
}
log.info("method getProductList() end...");
return "/playboy-invest-service/product/json/productListJson";
}
/**
* 产品详情(前)
*
* @param request
* @param response
* @param model
* @param id
* @return
*/
@RequestMapping(value = "/a/product/{id}/detail", method = RequestMethod.GET)
public String getProductJson(HttpServletRequest request, HttpServletResponse response,
ModelMap model, @PathVariable Long id) {
log.info("method getProductJson() begin...");
log.info("get data: id = " + id);
try {
Product product = productService.getObjectById(id);
if (DataUtils.isNullOrEmpty(product)) {
model.addAttribute("code", ProductErrorCode.productDoesNotExist.getValue());
return "/data/json";
}
model.addAttribute("code", 0);
model.addAttribute("product", product);
}
catch (Throwable t) {
log.error(t.getMessage());
log.error("get product error, id is: " + id);
model.addAttribute("code", -1);
}
log.info("method getProductJson() end...");
return "/playboy-invest-service/product/json/productDetailJson";
}
/**
* 编辑产品
*
* @param request
* @param response
* @param model
* @param id
* @param tag
* @param isRecommend
* @param isBuyLimit
* @return
*/
@RequestMapping(value = "/a/u/{id}/product", method = RequestMethod.POST)
public String updateProductJson(HttpServletRequest request, HttpServletResponse response,
ModelMap model, @PathVariable Long id, String tag,
Boolean isRecommend, Boolean isBuyLimit) {
log.info("method updateProductJson() begin...");
log.info("arguments: id = [" + id + "], tag = [" + tag + "], isRecommend = [" + isRecommend
+ "], isBuyLimit = [" + isBuyLimit + "]");
try {
/* 第一步、参数校验 */
Product product = productService.getObjectById(id);
if (DataUtils.isNullOrEmpty(product)) {
model.addAttribute("code", ProductErrorCode.productDoesNotExist.getValue());
return "/data/json";
}
//这三行是角标的意思
product.setTag(tag);
product.setIsRecommend(isRecommend);
product.setIsBuyLimit(isBuyLimit);
/* 校验不能为空的入参 */
//这个方法调用一个类,这个类一个个测试返回值是否为空,如果都不为空,int valiresult就为0
int valiResult = ProductUtil.validateInsertOrUpdateParams(product);
log.info("validate result is: " + valiResult);
if (valiResult == 0) {
/* 第二步、校验通过,更新 */
productService.update(product);
}
model.addAttribute("code", valiResult);
}
catch (Throwable t) {
log.error(t.getMessage());
log.error("update product error, id is: " + id);
model.addAttribute("code", -1);
}
log.info("method updateProductJson() end...");
return "/data/json";
}
/**
* 新建产品
*
* @param request
* @param response
* @param model
* @param product
* @return
*/
//新增这里直接新增一个实体类
@RequestMapping(value = "/a/u/product", method = RequestMethod.POST)
public String addProductJson(HttpServletRequest request, HttpServletResponse response,
ModelMap model, Product product) {
log.info("method addProductJson() begin...");
log.info("add product: product = " + product);
product.setId(null);
try {
/* 第一步、参数合法性校验 */
//与上次相同,一系列判断,合法就赋值0
int valiResult = ProductUtil.validateInsertOrUpdateParams(product);
log.info("validate result is: " + valiResult);
if (valiResult == 0) { // 参数合法
/* 第二步、插入一个产品 */
product.setStatus(Product.SALE_OUT);
Long id = productService.insert(product);
log.info("add product success id = " + id);
}
model.addAttribute("code", valiResult);
}
catch (Throwable t) {
log.error(t.getMessage());
log.error("add product error");
model.addAttribute("code", -1);
}
log.info("method addProductJson() end...");
return "/data/json";
}
/**
* 产品上下架
*
* @param request
* @param response
* @param model
* @param id
* @param status
* @return
*/
@RequestMapping(value = "/a/u/{id}/product/status", method = RequestMethod.POST)
public String updateProductStatusJson(HttpServletRequest request, HttpServletResponse response,
ModelMap model, @PathVariable Long id, Integer status) {
log.info("method updateProductStatusJson() begin...");
log.info("update product: id = " + id + ", status = " + status);
try {
/* 第一步、参数校验 */
if (DataUtils.isNullOrEmpty(status)) {
model.addAttribute("code", ProductErrorCode.statusIsEmpty.getValue());
return "/data/json";
}
Product product = productService.getObjectById(id);
if (DataUtils.isNullOrEmpty(product)) {
model.addAttribute("code", ProductErrorCode.productDoesNotExist.getValue());
return "/data/json";
}
if (status.equals(product.getStatus())) {
model.addAttribute("code", ProductErrorCode.statusIsConflict.getValue());
return "/data/json";
}
/* 第二步、执行上下架操作 */
product.setStatus(status);
productService.update(product);
model.addAttribute("code", 0);
}
catch (Throwable t) {
log.error(t.getMessage());
log.error("update product status error, id is: " + id);
model.addAttribute("code", -1);
}
log.info("method updateProductStatusJson() end...");
return "/data/json";
}
}
工具类(部分):
package com.ptteng.util;
import com.gemantic.common.exception.ServiceException;
import com.gemantic.common.util.StringUtil;
import com.gemantic.dal.config.helper.GroupHelper;
import com.ptteng.common.dao.util.SQLUtil;
import com.ptteng.controller.invest.ProductController;
import com.ptteng.playboy.invest.model.Product;
import com.ptteng.playboy.invest.model.ProductStatistics;
import com.ptteng.util.code.ProductErrorCode;
import com.qding.common.util.DataUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.simple.ParameterizedBeanPropertyRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import javax.sql.DataSource;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
public class ProductUtil {
private static final Log log = LogFactory.getLog(ProductController.class);
public static final String Period_Day = "day";
public static final String Period_Week = "week";
public static final String Period_Month = "month";
private ProductUtil() {
}
/**
* 销量统计,产品列表
*
* @param name
* @param interestBeginAt
* @param minYearRate
* @param maxYearRate
* @param minAmount
* @param status
* @param monthLimit
* @return
* @throws ServiceException
*/
public static Map<String, Object> getProductListParam(String name, Integer interestBeginAt,
BigDecimal minYearRate,
BigDecimal maxYearRate,
BigDecimal minAmount, Integer status,
Integer minMonthLimit,
Integer maxMonthLimit, String code,
Integer timeLimitType)
throws ServiceException {
Map<String, Object> params = new HashMap<>();
/* 第一步、对最小年利率和最大年利率合法性校验 */
if (DataUtils.isNotNullOrEmpty(minYearRate)) {
if (isLegalForYearate(minYearRate)) {
params.put("year_rate & >= ", minYearRate);
}
else {
//参数非法
log.error("param minYearRate illegal, excepted:[0, 1], in fact:" + minYearRate);
params.put("error", ProductErrorCode.illegalRange.getValue());
return params;
}
}
//dateUtils偏底层
if (DataUtils.isNotNullOrEmpty(maxYearRate)) {
if (isLegalForYearate(maxYearRate)) {
//预计年化<=最大年化
params.put("year_rate & <= ", maxYearRate);
}
else {
log.error("param maxYearRate illegal, excepted:[0, 1], in fact:" + maxYearRate);
params.put("error", ProductErrorCode.illegalRange.getValue());
return params;
}
}
//最小和最大是非空,并且最小如果大于最大,就报错
if (DataUtils.isNotNullOrEmpty(minYearRate) && DataUtils.isNotNullOrEmpty(maxYearRate)
&& minYearRate.compareTo(maxYearRate) > 0) {
log.error("param minYearRate can not be greater than maxYearRate, minYearRate: " + minYearRate
+ ", maxYearRate: " + maxYearRate);
params.put("error", ProductErrorCode.minGreaterThanMax.getValue());
return params;
}
/* 第二步、动态拼接sql */
//stringutil偏底层
//对请求参数进行判空
if (StringUtil.isNotEmpty(name)) {
params.put("name & like ", "'%" + name + "%'");
}
if (DataUtils.isNotNullOrEmpty(interestBeginAt)) {
params.put("interest_begin_at", interestBeginAt);
}
if (DataUtils.isNotNullOrEmpty(minAmount)) {
params.put("min_amount", minAmount);
}
if (DataUtils.isNotNullOrEmpty(status)) {
params.put("status", status);
}
if (DataUtils.isNotNullOrEmpty(minMonthLimit)) {
params.put("month_limit &>= ", minMonthLimit);
if (DataUtils.isNotNullOrEmpty(timeLimitType)) {
params.put("time_limit_type", timeLimitType);
}
}
if (DataUtils.isNotNullOrEmpty(maxMonthLimit)) {
params.put("month_limit &<= ", maxMonthLimit);
if (DataUtils.isNotNullOrEmpty(timeLimitType)) {
params.put("time_limit_type", timeLimitType);
}
}
if (StringUtil.isNotEmpty(code)) {
params.put("code & like ", "'%" + code + "%'");
}
//table表,排序
params.put("@table", "product");
params.put("@order", "status desc, is_recommend desc, update_at desc");
return params;
}
public static boolean isLegalForYearate(BigDecimal yearRate) {
if (DataUtils.isNullOrEmpty(yearRate)) {
return false;
}
return yearRate.compareTo(BigDecimal.ZERO) >= 0 && yearRate.compareTo(BigDecimal.ONE) <= 0
? true : false;
}
//取的是请求参数的值,不把角标加上去
public static int validateInsertOrUpdateParams(Product product) {
if (StringUtil.isEmpty(product.getCode())) {
return ProductErrorCode.codeIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getYearRate())) {
return ProductErrorCode.yearRateIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getPayBackType())) {
return ProductErrorCode.payBackTypeIsEmpty.getValue();
}
if (StringUtil.isEmpty(product.getName())) {
return ProductErrorCode.nameIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getMinAmount())) {
return ProductErrorCode.minAmountIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getInterestBeginAt())) {
return ProductErrorCode.interestBeginAtIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getTimeLimitType())) {
return ProductErrorCode.timeLimitTypeIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getMonthLimit())) {
return ProductErrorCode.monthLimitIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getIsBuyLimit())) {
return ProductErrorCode.isBuyLimitIsEmpty.getValue();
}
if (DataUtils.isNullOrEmpty(product.getIsRecommend())) {
return ProductErrorCode.isRecommendIsEmpty.getValue();
}
//判断年化利率和期限类型是否正确
if (!isLegalForYearate(product.getYearRate())) {
return ProductErrorCode.illegalRange.getValue();
}
if (Product.DAY_TYPE == product.getTimeLimitType()
&& Product.PAY_BACE_MULTIPLE == product.getPayBackType()) {
return ProductErrorCode.onlyPayBaceSingleAllow.getValue();
}
return 0;
}
明天计划的事情:
早上重新找一个invest-service的删除例子来搞,其他两个service有bug,一时半会解决不了,问问各个师兄们,不行就问师姐
中午再看一遍视频,了解配置文件.问玉琛关于修改表结构代码生成怎么加入现有项目.
下午小型demo
遇到的问题:
测试postman出现问题
传参报错,明明传入了参数,报状态码说状态值不能为空什么的,之前用的是这种
不管是true和false还是0和1都不行.
之后选用第二个www这个,把值一个个传进去就ok
期间还有出过问题,老规矩clean install,重启server即可,至于为什么我也不知道.
还有一些弱智问题,比如url前缀写错....太粗了
产品接口没问题了,但是产品不能删除,最多就下架,然后我去找关于删除的接口.但是试了几个都不能成功...
早上再试试吧
pub-service里的article居然和接口文档不一样.测试返回-1,调了很久没成功
然后换common里的message
报错Injection of autowired dependencies failed
网上查了有几种说法,我加上去了,初始报错改变了,但是一直显示
04-Nov-2017 03:48:41.406 信息 [http-nio-8080-exec-10] scallop.core.ScallopClient.getInstance delayTime 5 intervalTime 5
04-Nov-2017 03:48:41.413 信息 [http-nio-8080-exec-10] scallop.core.ScallopClient.getResource get resource form registry center. registryCenter:http://scallop.resource.center:8102 registryName:message-playboy-common-service-rmi
04-Nov-2017 03:48:41.602 信息 [http-nio-8080-exec-10] com.noelios.restlet.http.StreamClientHelper.start Starting the HTTP client
04-Nov-2017 03:48:41.763 信息 [http-nio-8080-exec-10] scallop.core.ScallopClient.getResource resource:{"resource":"playboy.common.service:22221","name":"message-playboy-common-service-rmi"}
11-04 03:48:43:ERROR(196)http-nio-8080-exec-10 scallop.sca.binding.rmi.provider.RMIReferenceLoadBalanceInvoker - InvocationTargetException null
java.lang.NullPointerException
空指针异常吗?
这是新加的
注释了@Autowired报错就改了
加上这两个注解报错没能改变
收获:
态度端正,不懂就问
有道是:取乎其上,得乎其中;取乎其中,得乎其下;取乎其下,则无所得矣.
正如老大说的,对自己的要求越高越好.保持紧迫感,加油
评论