标签:insert 扣减 String 积分 update duplicate points time
基本需求描述:积分扣减/增加操作,记录每条积分明细的同时需要更新一下积分总数,积分不足扣减的时候需要返回异常提示信息。
初始方案:
1 @Modifying 2 @Query(value = "insert ignore into points_total(customer_id, phone_no, pin, points, create_time, update_time) values(?2,?3,?4,?1,?5,?5) on duplicate key update points =if( points + ?1 >= 0,points + ?1,points),update_time= if(points + ?1 >= 0,?6,update_time) ", nativeQuery = true) 3 int saveOrUpdate(Integer newPoints, String customerId, String phoneNo, String pin, Date createTime, Date updateTime);
上面是使用if语法判断积分是否足够扣减,积分不足扣减的话就保持原值;
实践中在初次插入或者积分不足来update的时候都返回1,而积分足够更新的时候返回2,所以我们没办法区别初次插入和积分不足扣减这两种情况,而业务上需要知道什么时候发生了积分不足扣减。
改进方案:
1 @Modifying 2 @Query(value = "insert ignore into points_total(customer_id, phone_no, pin, points, create_time, update_time) values(?2,?3,?4,?1,?5,?5) on duplicate key update points = points + ?1,update_time= ?6 ", nativeQuery = true) 3 int saveOrUpdate(Integer newPoints, String customerId, String phoneNo, String pin, Date createTime, Date updateTime) throws DataIntegrityViolationException;
定义积分总数字段points为无符号整数(满足积分总数不能为负数的要求),当积分扣减为负数时会抛出DataIntegrityViolationException异常,代码逻辑通多细致识别此异常可以判定是发生了积分不足扣减。
1 catch (DataIntegrityViolationException e) { 2 Throwable cause1 = e.getCause(); 3 if (cause1 != null && cause1 instanceof DataException) { 4 Throwable cause2 = cause1.getCause(); 5 if (cause2 != null && cause2 instanceof MysqlDataTruncation) { 6 //积分不足扣减 7 if (cause2.getMessage() != null && cause2.getMessage().startsWith("Data truncation: BIGINT UNSIGNED value is out of range")) { 8 return new Result<>(0, null, new PointChangeResult(false, RetCode.point_change_ret_code.E03.name())); 9 } 10 } 11 } 12 throw new RuntimeException(e); 13 }
标签:insert,扣减,String,积分,update,duplicate,points,time 来源: https://www.cnblogs.com/mylittlecabin/p/15031770.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。