发表于: 2017-10-27 22:35:30

1 591


今天完成的事情:

写完两个交易接口



明天计划的事情:

测试接口并写消息列表接口



遇到的问题:

测试接口报400



收获:

控制器

@Controller
public class TransactController {
private static final Log log = LogFactory.getLog(TransactController.class);

   @Autowired
   private TransactService transactService;


   @Autowired
   private UserService userService;




   @RequestMapping(value="/a/u/transact/search",method = RequestMethod.GET)
public String getTransactSearch(HttpServletRequest request, HttpServletResponse response,
                                   Model model, @PathVariable Long id)throws Exception{
log.info("get transact : id="+id);
       try{
transactService.getObjectById(id);

           log.info("select transact success");
           model.addAttribute("code",0);
       }catch(Throwable t){
t.printStackTrace();
           log.error(t.getMessage());
           log.error("select transact error,id is"+id);
           model.addAttribute("code",-1);
       }
return "/data/json";
   }




/**
    * @param
    * @return
    * @throws ServiceException
    * @throws ServiceDaoException
    */
   //交易记录接口
   //根据接口文档定义参数
   @RequestMapping(value = "/a/transact/{uid}/search", method = RequestMethod.GET)
public String getTransact(HttpServletRequest request,
                             HttpServletResponse response, ModelMap model, Integer page, Integer size,
                             @PathVariable Long uid, String mobile, String productName, Long startAt, Long endAt,
                             String name, Long scene, Long status) throws Exception {

//打印日志输出这些数据
       log.info("get transact list page=" + page + "size=" + size + "uid=" + uid + "mobile=" + mobile + "productName=" + productName +
"startAt=" + startAt + "endAt=" + endAt + "name=" + name + "scene=" + scene + "status=" + status);

       //页数和每页数值判空
       if (page == null) {
page = 1;
       }
if (size == null) {
size = 10;
       }
int start = (page - 1) * size;
       if (start < 0) {
start = 0;
       }


//输出共有多少条信息和每一页的个数
       log.info("pageList : page =" + start + ",size=" + size);

       try {
Map<String, Object> map = TransactUtil.getTransactUtil(mobile, productName, startAt, endAt,
                   name, scene, status, false);
           log.info("get transact list map=" + map);
           List<Long> transactIds = transactService.getIdsByDynamicCondition(Transact.class, map, start, size);
           List<Transact> transacts = transactService.getObjectsByIds(transactIds);
           Map<Long, User> userMap = new HashMap<>();


           if (CollectionUtils.isEmpty(transactIds)) {
log.info("transactIds size=0");
           } else {
log.info("transactIds size=" + transactIds.size());
               transacts = transactService.getObjectsByIds(transactIds);
               List<Long> userIds = new ArrayList<>();
               for (Transact transact : transacts) {
userIds.add(transact.getUserId());
               }

List<User> users = userService.getObjectsByIds(userIds);
               for (User user : users) {
userMap.put(user.getId(), user);
               }
}
map = TransactUtil.getTransactUtil(mobile, productName, startAt, endAt, name, scene,
                   status, true);
           BigInteger total = (BigInteger) transactService.getObjectByDynamicCondition(Transact.class, map
, 0, Integer.MAX_VALUE);
           log.info("total=" + total);
           int totalPage = 1;
           if (total.intValue() > 0) {
totalPage = (((total.intValue() - 1)) / (size)) + 1;
           }
log.info("totalPage=" + totalPage);

           model.addAttribute("code", 0);
           model.addAttribute("page", page);
           model.addAttribute("size", size);
           model.addAttribute("total", total);//
           model.addAttribute("transactList", transacts);
           model.addAttribute("userMap", userMap);

       } catch (Throwable t) {
t.printStackTrace();
           log.error(t.getMessage());
           log.error("get feedBack list error,page is" + start + ",size" + size);
           model.addAttribute("code", -1);
       }

return "/playboy-common-service/transact/json/transactListJson";
   }

}

工具类:

public class TransactUtil {

public static Map<String,Object> getTransactUtil( String mobile, String productName, Long startAt, Long endAt, String name, Long scene, Long status,boolean count) {

//此处HashedMap是一个类名
       Map<String, Object> params = new HashedMap();

       //now获取当前时间
       Long now = System.currentTimeMillis();


       Set<String> tables = new HashSet();
       tables.add("transact");

       //DataUtils是底层的包,直接导包就能用
       if (DataUtils.isNotNullOrEmpty(startAt)) {
params.put("transact.create_at & >= ", "'" + startAt + "'");
       }
if (DataUtils.isNotNullOrEmpty(endAt)) {
params.put("transact.create_at & >= ", "'" + startAt + "'");
       }


if (StringUtil.isNotEmpty(productName)
|| DataUtils.isNotNullOrEmpty(scene)
|| DataUtils.isNotNullOrEmpty(status)) {
if (StringUtil.isNotEmpty(productName)) {
params.put(" user.productName & like ", "'%" + productName + "%'");

           }
if (DataUtils.isNotNullOrEmpty(scene)) {
params.put(" user.scene & like ", "'%" + scene + "%'");

           }
if (DataUtils.isNotNullOrEmpty(status)) {
params.put(" user.status & like ", "'%" + status + "%'");

           }

params.put("transact.uid", "user.id");
           tables.add("jinxin_invest.user");

       }
if (count) {
params.put("@query", " count(transact.id) ");
       } else {
params.put("@order", "  transact.update_at desc ");
           params.put("@query", " ftransact.id");
       }
String table = convertTable(tables);
       params.put("@table", table);

       return params;
}
public static String convertTable(Set<String> tables) {
StringBuffer tableBuffer = new StringBuffer();
       int index = 0;
       Iterator var4 = tables.iterator();

       while(var4.hasNext()) {
String table = (String)var4.next();
           ++index;
           tableBuffer = tableBuffer.append(table);
           if(index < tables.size()) {
tableBuffer = tableBuffer.append(",");
           }
}

return tableBuffer.toString();
   }
}



返回列表 返回列表
评论

    分享到