parent
06f6a2e483
commit
96ec101283
@ -0,0 +1,46 @@ |
||||
package com.zbkj.admin.controller.extend; |
||||
|
||||
import cn.hutool.core.collection.CollUtil; |
||||
import com.zbkj.common.model.extend.MallDelivery; |
||||
import com.zbkj.service.service.ExtendService; |
||||
import io.swagger.annotations.Api; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.web.bind.annotation.PostMapping; |
||||
import org.springframework.web.bind.annotation.RequestBody; |
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 对外接口 |
||||
* @author wyq |
||||
*/ |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping("api/extend") |
||||
@Api(tags = "扩展") //配合swagger使用
|
||||
public class ExtendController { |
||||
|
||||
@Resource |
||||
private ExtendService extendService; |
||||
|
||||
/** |
||||
* 多包裹发货 |
||||
* @param mallDeliverys |
||||
* @return |
||||
*/ |
||||
@PostMapping("/delivery/create") |
||||
String createDelivery(@RequestBody List<MallDelivery> mallDeliverys){ |
||||
if (CollUtil.isNotEmpty(mallDeliverys)){ |
||||
mallDeliverys.forEach(mallDelivery->{ |
||||
extendService.createDelivery( mallDelivery); |
||||
}); |
||||
} |
||||
|
||||
return ""; |
||||
} |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,28 @@ |
||||
package com.zbkj.admin.controller.extend; |
||||
|
||||
|
||||
import com.zbkj.service.service.OrderTaskService; |
||||
import com.zbkj.service.service.RetryService; |
||||
import io.swagger.annotations.Api; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping; |
||||
import org.springframework.web.bind.annotation.RestController; |
||||
|
||||
/** |
||||
* 测试接口 |
||||
* @author wyq |
||||
*/ |
||||
@Slf4j |
||||
@RestController |
||||
@RequestMapping("api/extend/test") |
||||
@Api(tags = "测试接口") |
||||
public class TestController { |
||||
@Autowired |
||||
private OrderTaskService orderTaskService; |
||||
|
||||
@Autowired |
||||
private RetryService retryService; |
||||
|
||||
} |
||||
@ -0,0 +1,34 @@ |
||||
package com.zbkj.admin.task.order; |
||||
|
||||
import com.zbkj.common.utils.CrmebDateUtil; |
||||
import com.zbkj.service.service.OrderTaskService; |
||||
import org.slf4j.Logger; |
||||
import org.slf4j.LoggerFactory; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* 新订单自动同步ruoyi |
||||
* @author wyq |
||||
*/ |
||||
@Component("OrderSyncRuoyiTask") |
||||
public class OrderSyncRuoyiTask { |
||||
@Autowired |
||||
private OrderTaskService orderTaskService; |
||||
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(OrderSyncRuoyiTask.class); |
||||
public void autoSync() |
||||
|
||||
{ |
||||
|
||||
logger.info("---OrderSyncRuoyiTask task------produce Data with fixed rate task: Execution Time - {}", CrmebDateUtil.nowDateTime()); |
||||
try { |
||||
orderTaskService.autoSync(); |
||||
|
||||
} catch (Exception e) { |
||||
e.printStackTrace(); |
||||
logger.error("OrderSyncRuoyiTask.task" + " | msg : " + e.getMessage()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,18 @@ |
||||
package com.zbkj.common.model.extend; |
||||
|
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
public class MallDelivery { |
||||
|
||||
private String platformCode; |
||||
|
||||
private List<MallExpressInfoDto> expressList; |
||||
} |
||||
@ -0,0 +1,35 @@ |
||||
package com.zbkj.common.model.extend; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.AllArgsConstructor; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Data |
||||
@AllArgsConstructor |
||||
@ApiModel(value = "发货快递信息") |
||||
public class MallExpressInfoDto { |
||||
|
||||
|
||||
@ApiModelProperty(value = "快递公司名称") |
||||
private String expressName; |
||||
|
||||
/** |
||||
* 快递单号 |
||||
*/ |
||||
private String expressNo; |
||||
|
||||
@ApiModelProperty(value = "快递公司编码") |
||||
private String expressCode; |
||||
|
||||
@ApiModelProperty(value = "商品编码") |
||||
private String skuNo; |
||||
@ApiModelProperty(value = "商品名称") |
||||
private String skuName; |
||||
@ApiModelProperty(value = "商品数量") |
||||
private Integer num; |
||||
|
||||
} |
||||
@ -0,0 +1,85 @@ |
||||
package com.zbkj.common.model.parcel; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
import java.io.Serializable; |
||||
import java.time.LocalDateTime; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Data |
||||
@EqualsAndHashCode(callSuper = false) |
||||
@Accessors(chain = true) |
||||
@TableName("eb_parcel") |
||||
@ApiModel(value="ParcelPO", description="包裹") |
||||
public class ParcelPO implements Serializable { |
||||
|
||||
/** |
||||
* 主键ID |
||||
*/ |
||||
@TableId(type = IdType.AUTO) |
||||
private Long id; |
||||
@ApiModelProperty(value = "订单号") |
||||
private String platformCode; |
||||
@ApiModelProperty(value = "快递单") |
||||
private String expressNo; |
||||
@ApiModelProperty(value = "快递公司编码") |
||||
private String expressName; |
||||
@ApiModelProperty(value = "快递公司名") |
||||
private String expressCode; |
||||
@ApiModelProperty(value = "sku编码") |
||||
private String skuNo; |
||||
@ApiModelProperty(value = "sku名称") |
||||
private String skuName; |
||||
@ApiModelProperty(value = "数量") |
||||
private Integer num; |
||||
@ApiModelProperty(value = "图片") |
||||
private String image; |
||||
|
||||
/** |
||||
* 创建人 |
||||
*/ |
||||
@ApiModelProperty(value = "创建人") |
||||
private String createdBy; |
||||
|
||||
/** |
||||
* 更新人 |
||||
*/ |
||||
@ApiModelProperty(value = "更新人") |
||||
private String updatedBy; |
||||
|
||||
/** |
||||
* 创建时间 |
||||
*/ |
||||
@ApiModelProperty(value = "创建时间") |
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime createdTime; |
||||
|
||||
/** |
||||
* 更新时间 |
||||
*/ |
||||
@ApiModelProperty(value = "更新时间") |
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime updatedTime; |
||||
|
||||
/** |
||||
* 是否逻辑删除:0未删除,1已删除 |
||||
*/ |
||||
private Integer deleted; |
||||
@ApiModelProperty(value = "快递轨迹") |
||||
private String expressInfo; |
||||
@ApiModelProperty(value = "快递状态") |
||||
private String expressStatus; |
||||
@ApiModelProperty(value = "签收时间") |
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime signedTime; |
||||
} |
||||
@ -0,0 +1,65 @@ |
||||
|
||||
package com.zbkj.common.vo; |
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.EqualsAndHashCode; |
||||
import lombok.experimental.Accessors; |
||||
|
||||
/** |
||||
* 调用微信退款所需要的参数 |
||||
* +---------------------------------------------------------------------- |
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ] |
||||
* +---------------------------------------------------------------------- |
||||
* | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
|
||||
* +---------------------------------------------------------------------- |
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权 |
||||
* +---------------------------------------------------------------------- |
||||
* | Author: CRMEB Team <admin@crmeb.com> |
||||
* +---------------------------------------------------------------------- |
||||
*/ |
||||
@Data |
||||
@EqualsAndHashCode(callSuper = false) |
||||
@Accessors(chain = true) |
||||
@ApiModel(value="WxRefundVo", description="调用微信退款所需要的参数") |
||||
public class WxRefundQuery { |
||||
|
||||
@ApiModelProperty(value = "appId,公众号名称,由商户传入", required = true) |
||||
private String appid; |
||||
|
||||
@ApiModelProperty(value = "直连商户的商户号,由微信支付生成并下发", required = true) |
||||
private String mch_id; |
||||
|
||||
@ApiModelProperty(value = "随机字符串,不长于32位", required = true) |
||||
private String nonce_str; |
||||
|
||||
@ApiModelProperty(value = "签名", required = true) |
||||
private String sign; |
||||
|
||||
@ApiModelProperty(value = "签名类型,目前支持HMAC-SHA256和MD5,默认为MD5") |
||||
private String sign_type = "MD5"; |
||||
|
||||
@ApiModelProperty(value = "微信支付订单号:微信生成的订单号,在支付通知中有返回") |
||||
private String transaction_id; |
||||
|
||||
@ApiModelProperty(value = "商户系统内部的订单号,32个字符内、可包含字母, 其他说明见商户订单号", required = true) |
||||
private String out_trade_no; |
||||
|
||||
@ApiModelProperty(value = "商户退款单号,同一退款单号多次请求只退一笔。") |
||||
private String out_refund_no; |
||||
|
||||
@ApiModelProperty(value = "订单总金额,单位为分", required = true) |
||||
private int total_fee; |
||||
|
||||
@ApiModelProperty(value = "退款金额,单位为分", required = true) |
||||
private int refund_fee; |
||||
|
||||
@ApiModelProperty(value = "退款货币种类:符合ISO 4217标准的三位字母代码,默认人民币:CNY,其他值列表详见货币类型") |
||||
private String refund_fee_type = "CNY"; |
||||
|
||||
@ApiModelProperty(value = "退款结果通知url") |
||||
private String notify_url; |
||||
|
||||
} |
||||
|
||||
@ -0,0 +1,21 @@ |
||||
package com.zbkj.service.dao; |
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper; |
||||
import com.zbkj.common.model.parcel.ParcelPO; |
||||
|
||||
/** |
||||
* 订单表 Mapper 接口 |
||||
* +---------------------------------------------------------------------- |
||||
* | CRMEB [ CRMEB赋能开发者,助力企业发展 ] |
||||
* +---------------------------------------------------------------------- |
||||
* | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
|
||||
* +---------------------------------------------------------------------- |
||||
* | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权 |
||||
* +---------------------------------------------------------------------- |
||||
* | Author: CRMEB Team <admin@crmeb.com> |
||||
* +---------------------------------------------------------------------- |
||||
*/ |
||||
public interface ParcelDao extends BaseMapper<ParcelPO> { |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,18 @@ |
||||
package com.zbkj.service.dao; |
||||
|
||||
import com.zbkj.service.pojo.XsSkuInfo; |
||||
import org.apache.ibatis.annotations.Param; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
public interface XsbuyDao { |
||||
List<XsSkuInfo> selectListNew(@Param("content") String content,@Param("pageIndex") long l,@Param("pageSize") Long pageSize); |
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,129 @@ |
||||
package com.zbkj.service.pojo; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat; |
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
import javax.validation.constraints.NotEmpty; |
||||
import javax.validation.constraints.NotNull; |
||||
import javax.validation.constraints.Size; |
||||
import java.math.BigDecimal; |
||||
import java.time.LocalDateTime; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Data |
||||
@ApiModel(description = "创建或更新手工订单请求参数") |
||||
public class TradeOrderCreateReqDTO { |
||||
private Long orderId; |
||||
private String platformCode; |
||||
|
||||
private String erpCode; |
||||
|
||||
|
||||
@ApiModelProperty("渠道 可选值:TO_B,EMPLOYEE_IN_PURCHASE") |
||||
@NotNull(message = "渠道不能为空") |
||||
private String channel; |
||||
@ApiModelProperty("业务归属部门code") |
||||
@NotBlank(message = "业务归属部门code不能为空") |
||||
private String deptCode; |
||||
@ApiModelProperty("业务归属部门name") |
||||
@NotBlank(message = "业务归属部门name不能为空") |
||||
private String deptName; |
||||
@ApiModelProperty("业务员code") |
||||
@NotBlank(message = "业务员不能为空") |
||||
private String businessMan; |
||||
@ApiModelProperty("业务员名称") |
||||
private String businessManName; |
||||
@ApiModelProperty("业务类型 可选值:JETTISON,REPLACE_SEND,IN_PURCHASE") |
||||
@NotNull(message = "业务类型不能为空") |
||||
private String businessType; |
||||
@ApiModelProperty("下单商户/会员") |
||||
@NotBlank(message = "下单商户或购物会员号不能为空") |
||||
private String buyVip; |
||||
@ApiModelProperty("下单时间") |
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime orderTime; |
||||
@ApiModelProperty("付款方式 可选值:FIRST_PRODUCT,FIRST_MONEY") |
||||
@NotNull(message = "付款方式不可为空") |
||||
private String payType; |
||||
@ApiModelProperty("预付款时间") |
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") |
||||
private LocalDateTime perPayTime; |
||||
@ApiModelProperty("凭证文件名 同返利活动中附件名称") |
||||
private String proofName; |
||||
@ApiModelProperty("凭证文件地址 同返利活动中附件地址") |
||||
private String proofPath; |
||||
@Size(max = 100, message = "买家备注长度不能超过100字") |
||||
@ApiModelProperty("买家备注") |
||||
private String userRemark; |
||||
@Size(max = 100, message = "卖家备注长度不能超过100字") |
||||
@ApiModelProperty("卖家备注") |
||||
private String salerRemark; |
||||
@ApiModelProperty("发货仓库code") |
||||
@NotBlank(message = "发货仓库code不能为空") |
||||
private String warehouseCode; |
||||
@ApiModelProperty("发货仓库name") |
||||
@NotBlank(message = "发货仓库name不能为空") |
||||
private String warehouseName; |
||||
@NotEmpty(message = "订单商品不能为空") |
||||
@ApiModelProperty("订单商品") |
||||
private List<TradeOrderItemCreateReqDTO> item; |
||||
@NotBlank(message = "收货人名称不能为空") |
||||
@ApiModelProperty("收货人") |
||||
private String receiverName; |
||||
@NotBlank(message = "收货人手机号不能为空") |
||||
@ApiModelProperty("收货人手机号") |
||||
private String receiverPhone; |
||||
@NotBlank(message = "收货人地址不能为空") |
||||
@ApiModelProperty("收货人地址") |
||||
private String receiverAddress; |
||||
@ApiModelProperty("收货人城市") |
||||
@NotBlank(message = "收货人城市不能为空") |
||||
private String receiverCity; |
||||
@ApiModelProperty("收货人区县") |
||||
@NotBlank(message = "收货人区县不能为空") |
||||
private String receiverDistrict; |
||||
@ApiModelProperty("收货人省份") |
||||
@NotBlank(message = "收货人省份不能为空") |
||||
private String receiverProvince; |
||||
@ApiModelProperty("发票种类 可选值:NORMAL,VAT") |
||||
private String invoiceType; |
||||
@ApiModelProperty("发票类型 可选值:ELECTRONIC,PAPER") |
||||
private String invoiceCatalogue; |
||||
@ApiModelProperty("发票抬头") |
||||
private String invoiceTitle; |
||||
@ApiModelProperty("发票抬头类型 可选值:PERSONAL,COMPANY") |
||||
private String invoiceTitleType; |
||||
|
||||
@ApiModelProperty("纳税人标识号") |
||||
private String ratepayerCode; |
||||
@ApiModelProperty("注册地址") |
||||
private String registerAddress; |
||||
@ApiModelProperty("注册电话") |
||||
private String registerPhone; |
||||
@ApiModelProperty("开户银行") |
||||
private String registerBank; |
||||
@ApiModelProperty("银行账号") |
||||
private String registerAccount; |
||||
@ApiModelProperty("销售总额") |
||||
@NotNull(message = "销售总额不能为空") |
||||
private BigDecimal totalAmount; |
||||
|
||||
|
||||
@ApiModelProperty("毛利") |
||||
@NotNull(message = "毛利不能为空") |
||||
private BigDecimal profit; |
||||
|
||||
@NotNull(message = "是否开票必填") |
||||
private Integer needInvoice; |
||||
|
||||
private String approveCode; |
||||
private Integer payState; |
||||
private BigDecimal payAmount; |
||||
private BigDecimal costAmount; |
||||
} |
||||
@ -0,0 +1,56 @@ |
||||
package com.zbkj.service.pojo; |
||||
|
||||
|
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
|
||||
import javax.validation.constraints.NotBlank; |
||||
import javax.validation.constraints.NotNull; |
||||
import java.math.BigDecimal; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Data |
||||
@ApiModel("创建/更新手工订单请求参数") |
||||
public class TradeOrderItemCreateReqDTO { |
||||
@ApiModelProperty("skuNo") |
||||
@NotNull(message = "skuNo不能为空") |
||||
private String skuNo; |
||||
private String oid; |
||||
|
||||
private String note; |
||||
@ApiModelProperty("品牌 填写中文名称 可选值 非集合,集合") |
||||
private String brandType; |
||||
@ApiModelProperty("价格说明") |
||||
private String priceNote; |
||||
private BigDecimal totalPrice; |
||||
private BigDecimal payPrice; |
||||
private BigDecimal orderPrice; |
||||
//返利
|
||||
private BigDecimal rebate; |
||||
private String activities; |
||||
@ApiModelProperty("skuName") |
||||
@NotNull(message = "skuName不能为空") |
||||
private String skuName; |
||||
@ApiModelProperty("销售价格") |
||||
@NotNull(message = "销售价格不可为空") |
||||
private BigDecimal sellPrice; |
||||
@ApiModelProperty("销售数量") |
||||
@NotBlank(message = "销售数量不能为空") |
||||
private Integer sellQty; |
||||
@ApiModelProperty("成本") |
||||
private BigDecimal cost; |
||||
@ApiModelProperty("毛利") |
||||
private BigDecimal profit; |
||||
@ApiModelProperty("库存") |
||||
private Integer inStock; |
||||
@ApiModelProperty("毛利率") |
||||
private BigDecimal profitRate; |
||||
@ApiModelProperty("市场价") |
||||
private String marketPrice; |
||||
private String marketPriceStr; |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,23 @@ |
||||
package com.zbkj.service.pojo; |
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude; |
||||
import lombok.Data; |
||||
|
||||
/** |
||||
* @param <T> |
||||
* @author broad |
||||
*/ |
||||
@Data |
||||
@JsonInclude(JsonInclude.Include.NON_EMPTY) |
||||
public class WebResponse { |
||||
|
||||
//200正常
|
||||
private String status ; |
||||
|
||||
private String data; |
||||
|
||||
private String msg ; |
||||
|
||||
private Boolean success; |
||||
|
||||
} |
||||
@ -0,0 +1,40 @@ |
||||
package com.zbkj.service.pojo; |
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType; |
||||
import com.baomidou.mybatisplus.annotation.TableId; |
||||
import com.baomidou.mybatisplus.annotation.TableName; |
||||
import io.swagger.annotations.ApiModel; |
||||
import io.swagger.annotations.ApiModelProperty; |
||||
import lombok.Data; |
||||
import lombok.experimental.Accessors; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import java.io.Serializable; |
||||
|
||||
/** |
||||
* @author admin2 |
||||
*/ |
||||
@Data |
||||
@TableName("m_xsbuy_cn.xs_sku") |
||||
@ApiModel(value = "SKU信息对象") |
||||
@Accessors(chain = true) |
||||
public class XsSkuInfo implements Serializable { |
||||
private static final long serialVersionUID = 1L; |
||||
|
||||
@TableId(type = IdType.AUTO) |
||||
private Long id; |
||||
@ApiModelProperty(value = "SKU编号") |
||||
private String skuNo; |
||||
@ApiModelProperty(value = "SKU名称") |
||||
private String skuName; |
||||
@ApiModelProperty(value = "分类名称") |
||||
private String categoryName; |
||||
@ApiModelProperty(value = "分类编号") |
||||
private String categoryCode; |
||||
@ApiModelProperty(value = "品牌编号") |
||||
private String brandCode; |
||||
@ApiModelProperty(value = "品牌名称") |
||||
private String brandName; |
||||
} |
||||
|
||||
@ -0,0 +1,14 @@ |
||||
package com.zbkj.service.service; |
||||
|
||||
import com.zbkj.common.model.extend.MallDelivery; |
||||
|
||||
import java.util.List; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
public interface ExtendService { |
||||
String createDelivery(MallDelivery mallDelivery); |
||||
|
||||
void removeProduct(List<String> skuNos); |
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
package com.zbkj.service.service; |
||||
|
||||
import com.zbkj.common.model.order.StoreOrder; |
||||
import com.zbkj.common.model.user.UserAddress; |
||||
import com.zbkj.service.pojo.TradeOrderItemCreateReqDTO; |
||||
|
||||
import java.util.List; |
||||
|
||||
public interface RetryService { |
||||
|
||||
public void systemErrNc(String msg); |
||||
void createOrder(StoreOrder order, UserAddress userAddress, List<TradeOrderItemCreateReqDTO> detailList); |
||||
|
||||
|
||||
} |
||||
@ -0,0 +1,94 @@ |
||||
package com.zbkj.service.service.impl; |
||||
|
||||
import cn.hutool.core.collection.CollUtil; |
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
||||
|
||||
import com.zbkj.common.model.extend.MallDelivery; |
||||
import com.zbkj.common.model.order.StoreOrder; |
||||
import com.zbkj.common.model.product.StoreProduct; |
||||
import com.zbkj.common.model.product.StoreProductAttrValue; |
||||
import com.zbkj.common.request.StoreOrderSendRequest; |
||||
import com.zbkj.service.dao.StoreOrderDao; |
||||
import com.zbkj.service.dao.StoreProductAttrValueDao; |
||||
import com.zbkj.service.service.ExtendService; |
||||
import com.zbkj.service.service.StoreOrderService; |
||||
import com.zbkj.service.service.StoreProductService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.util.Arrays; |
||||
import java.util.List; |
||||
import java.util.stream.Collectors; |
||||
|
||||
/** |
||||
* @author wyq |
||||
*/ |
||||
@Service |
||||
@Slf4j |
||||
public class ExtendServiceImpl implements ExtendService { |
||||
|
||||
@Resource |
||||
private StoreOrderDao storeOrderDao; |
||||
@Resource |
||||
private StoreProductAttrValueDao storeProductAttrValueDao; |
||||
@Resource |
||||
private StoreProductService storeProductService; |
||||
|
||||
@Resource |
||||
private StoreOrderService storeOrderService; |
||||
@Override |
||||
public String createDelivery(MallDelivery mallDelivery) { |
||||
StoreOrder order =storeOrderDao.selectOne(new LambdaQueryWrapper<StoreOrder>().eq(StoreOrder::getOrderId, mallDelivery.getPlatformCode())); |
||||
if (order==null){ |
||||
if (mallDelivery.getPlatformCode().contains(";" )){ |
||||
List<String> list = Arrays.asList(mallDelivery.getPlatformCode().split(";")); |
||||
if (CollUtil.isNotEmpty(list)){ |
||||
for (String s : list) { |
||||
StoreOrder order1 = storeOrderDao.selectOne(new LambdaQueryWrapper<StoreOrder>().eq(StoreOrder::getOrderId, s)); |
||||
if (order1==null){ |
||||
throw new RuntimeException("合并发货单订单不存在"); |
||||
} |
||||
if (order1.getStatus()>0){ |
||||
log.info("订单已发货不可再次发货"); |
||||
continue; |
||||
} |
||||
StoreOrderSendRequest request=new StoreOrderSendRequest(); |
||||
request.setOrderNo(order1.getOrderId()); |
||||
request.setDeliveryType("express"); |
||||
request.setExpressList(mallDelivery.getExpressList()); |
||||
storeOrderService.sendNew(request); |
||||
} |
||||
return ""; |
||||
} |
||||
} |
||||
throw new RuntimeException("订单不存在"); |
||||
} |
||||
if (order.getStatus()>0){ |
||||
return "订单已发货不可再次发货"; |
||||
} |
||||
StoreOrderSendRequest request=new StoreOrderSendRequest(); |
||||
request.setOrderNo(order.getOrderId()); |
||||
request.setDeliveryType("express"); |
||||
request.setExpressList(mallDelivery.getExpressList()); |
||||
return storeOrderService.sendNew(request); |
||||
|
||||
} |
||||
|
||||
@Override |
||||
public void removeProduct(List<String> skuNos) { |
||||
List<StoreProductAttrValue> attrValues = storeProductAttrValueDao.selectList(new LambdaQueryWrapper<StoreProductAttrValue>().in(StoreProductAttrValue::getBarCode, skuNos).eq(StoreProductAttrValue::getIsDel,false)); |
||||
if (CollUtil.isEmpty(attrValues)){ |
||||
return; |
||||
} |
||||
List<Integer> productIds = attrValues.stream().map(StoreProductAttrValue::getProductId).distinct().collect(Collectors.toList()); |
||||
List<StoreProduct> list = storeProductService.list(new LambdaQueryWrapper<StoreProduct>().in(StoreProduct::getId, productIds).eq(StoreProduct::getIsShow, true)); |
||||
if (!CollUtil.isEmpty(list)){ |
||||
for (StoreProduct storeProduct : list) { |
||||
storeProductService.offShelf(storeProduct.getId()); |
||||
} |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
@ -0,0 +1,154 @@ |
||||
package com.zbkj.service.service.impl; |
||||
|
||||
import com.alibaba.fastjson.JSON; |
||||
import com.zbkj.common.model.order.StoreOrder; |
||||
import com.zbkj.common.model.user.UserAddress; |
||||
import com.zbkj.common.utils.RestTemplateUtil; |
||||
import com.zbkj.service.dao.StoreOrderInfoDao; |
||||
import com.zbkj.service.dao.StoreProductAttrValueDao; |
||||
import com.zbkj.service.dao.StoreProductDao; |
||||
import com.zbkj.service.dao.UserDao; |
||||
import com.zbkj.service.pojo.TradeOrderItemCreateReqDTO; |
||||
import com.zbkj.service.pojo.WebResponse; |
||||
import com.zbkj.service.service.RetryService; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Value; |
||||
import org.springframework.stereotype.Service; |
||||
|
||||
import javax.annotation.Resource; |
||||
import java.math.BigDecimal; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.HashMap; |
||||
import java.util.List; |
||||
import java.util.Map; |
||||
|
||||
/** |
||||
* @author admin2 |
||||
*/ |
||||
@Service |
||||
@Slf4j |
||||
public class RetryServiceImpl implements RetryService { |
||||
|
||||
@Resource |
||||
private RestTemplateUtil restTemplateUtil; |
||||
@Resource |
||||
private StoreOrderInfoDao storeOrderInfoDao; |
||||
@Value("${ruoyi.createOrderUrl}") |
||||
private String createOrderUrl; |
||||
@Value("${ruoyi.applyInvoicingUrl}") |
||||
private String applyInvoicingUrl; |
||||
@Value("${ruoyi.triggerDeliveryCheckUrl}") |
||||
private String triggerDeliveryCheckUrl; |
||||
@Value("${ruoyi.updateOrderRefundStatusUrl}") |
||||
private String updateOrderRefundStatusUrl; |
||||
|
||||
@Value("${systemErrRobot}") |
||||
private String systemErrRobot; |
||||
@Resource |
||||
private UserDao userDao; |
||||
@Resource |
||||
private StoreProductAttrValueDao storeProductAttrValueDao; |
||||
|
||||
@Resource |
||||
private StoreProductDao storeProductDao; |
||||
|
||||
@Override |
||||
public void systemErrNc(String msg) { |
||||
msg = StringUtils.isBlank(msg) ? msg = "空指针" : msg; |
||||
msg = msg.length() > 400 ? msg.substring(0, 400) : msg; |
||||
msg = "时间戳:" + System.currentTimeMillis() + "【微信店铺系统】异常:" + msg; |
||||
Map<String, Object> param = new HashMap<>(); |
||||
Map<String, Object> content = new HashMap<>(); |
||||
content.put("text", msg); |
||||
param.put("content", content); |
||||
param.put("msg_type", "text"); |
||||
try { |
||||
log.error("发送飞书消息入参:{}", msg); |
||||
restTemplateUtil.postMapData(systemErrRobot, param, new HashMap<>()); |
||||
} catch (Exception e) { |
||||
log.error("发送飞书消息失败", e); |
||||
} |
||||
} |
||||
|
||||
|
||||
|
||||
@Override |
||||
public void createOrder(StoreOrder order, UserAddress userAddress, List<TradeOrderItemCreateReqDTO> detailList) { |
||||
|
||||
Map<String, Object> params = new HashMap<>(); |
||||
|
||||
BigDecimal hbdkje=BigDecimal.ZERO; |
||||
BigDecimal hdyhje=BigDecimal.ZERO; |
||||
BigDecimal jfdkje=BigDecimal.ZERO; |
||||
|
||||
// 填充所有属性名
|
||||
params.put("orderId", ""); |
||||
params.put("platformCode", order.getOrderId()); |
||||
params.put("erpCode", System.currentTimeMillis()); |
||||
|
||||
params.put("channelCode", "COMM1"); |
||||
params.put("channelName", "交通银行"); |
||||
params.put("deptCode", ""); |
||||
params.put("deptName", ""); |
||||
params.put("businessMan", "16638693690"); |
||||
params.put("businessManName", "王玉祺"); |
||||
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
||||
params.put("orderTime", sdf.format(order.getPayTime())); |
||||
params.put("buyVip", "交通银行用户"); |
||||
params.put("payType", "FIRST_MONEY"); |
||||
params.put("perPayTime", ""); |
||||
params.put("proofName", ""); |
||||
params.put("proofPath", ""); |
||||
params.put("userRemark", order.getMark()); |
||||
params.put("salerRemark", ""); |
||||
params.put("warehouseCode", "XS-SH-07"); |
||||
params.put("warehouseName", "湘商-湘商-外采主仓"); |
||||
params.put("item", detailList); |
||||
params.put("receiverName", order.getRealName()); |
||||
params.put("receiverPhone", order.getUserPhone()); |
||||
params.put("receiverAddress", order.getUserAddress()); |
||||
params.put("receiverCity", userAddress.getCity()); |
||||
params.put("receiverDistrict", userAddress.getDistrict()); |
||||
params.put("receiverProvince", userAddress.getProvince()); |
||||
//发票种类 可选值:NORMAL,VAT
|
||||
params.put("invoiceType", "NORMAL"); |
||||
//电子发票
|
||||
params.put("invoiceCatalogue", "ELECTRONIC"); |
||||
params.put("invoiceTitle", ""); |
||||
|
||||
//个人发票
|
||||
params.put("invoiceTitleType", "PERSONAL"); |
||||
params.put("ratepayerCode", ""); |
||||
params.put("registerAddress", ""); |
||||
params.put("registerPhone", ""); |
||||
params.put("registerBank", ""); |
||||
params.put("registerAccount", ""); |
||||
params.put("totalAmount", order.getPayPrice()); |
||||
params.put("profit", ""); |
||||
params.put("needInvoice", ""); |
||||
params.put("approveCode", ""); |
||||
//已付款
|
||||
params.put("payState", "1"); |
||||
params.put("payAmount", order.getPayPrice().subtract(hdyhje).subtract(hbdkje).subtract(jfdkje)); |
||||
params.put("costAmount", "0"); |
||||
params.put("installment", "0"); |
||||
params.put("payInfo", ""); |
||||
params.put("pntDsctAmt", jfdkje); |
||||
params.put("platformCouponPrice", hbdkje.add(hdyhje)); |
||||
|
||||
|
||||
params.put("commission", 0); |
||||
params.put("totalPrice", order.getPayPrice().subtract(BigDecimal.ZERO)); |
||||
params.put("installment", 0); |
||||
params.put("payInfo", ""); |
||||
log.info("订单同步参数:{}", JSON.toJSONString(params)); |
||||
String result = restTemplateUtil.postMapData(createOrderUrl, params, new HashMap<>()); |
||||
log.info("订单同步返回:{}", JSON.toJSONString(result)); |
||||
WebResponse webResponse = JSON.parseObject(result, WebResponse.class); |
||||
if (!"200".equals(webResponse.getStatus())) { |
||||
log.info("订单失败"); |
||||
throw new RuntimeException(String.format("订单同步失败订单号%s", order.getOrderId())); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,977 @@ |
||||
package com.zbkj.service.util; |
||||
|
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.apache.commons.lang3.time.DateFormatUtils; |
||||
|
||||
import java.lang.management.ManagementFactory; |
||||
import java.text.ParseException; |
||||
import java.text.SimpleDateFormat; |
||||
import java.util.Arrays; |
||||
import java.util.Calendar; |
||||
import java.util.Date; |
||||
import java.util.List; |
||||
|
||||
/** |
||||
* 时间工具类 |
||||
* |
||||
* @author shuai.fan |
||||
*/ |
||||
public class DateUtils extends org.apache.commons.lang3.time.DateUtils { |
||||
public static final String YYYY = "yyyy"; |
||||
|
||||
public static final String YYYY_MM = "yyyy-MM"; |
||||
|
||||
public static final String YYYY_MM_DD = "yyyy-MM-dd"; |
||||
|
||||
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss"; |
||||
|
||||
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; |
||||
|
||||
private static String[] parsePatterns = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", |
||||
"yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss", "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", |
||||
"yyyy.MM.dd HH:mm", "yyyy.MM" }; |
||||
|
||||
/** |
||||
* 获取当前Date型日期 |
||||
* |
||||
* @return Date() 当前日期 |
||||
*/ |
||||
public static Date getNowDate() { |
||||
return new Date(); |
||||
} |
||||
|
||||
/** |
||||
* 获取当前日期, 默认格式为yyyy-MM-dd |
||||
* |
||||
* @return String |
||||
*/ |
||||
public static String getDate() { |
||||
return dateTimeNow(YYYY_MM_DD); |
||||
} |
||||
|
||||
public static final String getTime() { |
||||
return dateTimeNow(YYYY_MM_DD_HH_MM_SS); |
||||
} |
||||
|
||||
public static final String dateTimeNow() { |
||||
return dateTimeNow(YYYYMMDDHHMMSS); |
||||
} |
||||
|
||||
public static final String dateTimeNow(final String format) { |
||||
return parseDateToStr(format, new Date()); |
||||
} |
||||
|
||||
public static final String dateTime(final Date date) { |
||||
return parseDateToStr(YYYY_MM_DD, date); |
||||
} |
||||
|
||||
public static final String parseDateToStr(final String format, final Date date) { |
||||
return new SimpleDateFormat(format).format(date); |
||||
} |
||||
|
||||
public static final Date dateTime(final String format, final String ts) { |
||||
try { |
||||
return new SimpleDateFormat(format).parse(ts); |
||||
} catch (ParseException e) { |
||||
throw new RuntimeException(e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 日期路径 即年/月/日 如2018/08/08 |
||||
*/ |
||||
public static final String datePath() { |
||||
Date now = new Date(); |
||||
return DateFormatUtils.format(now, "yyyy/MM/dd"); |
||||
} |
||||
|
||||
/** |
||||
* 日期路径 即年/月/日 如20180808 |
||||
*/ |
||||
public static final String dateTime() { |
||||
Date now = new Date(); |
||||
return DateFormatUtils.format(now, "yyyyMMdd"); |
||||
} |
||||
|
||||
/** |
||||
* 日期型字符串转化为日期 格式 |
||||
*/ |
||||
public static Date parseDate(Object str) { |
||||
if (str == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return parseDate(str.toString(), parsePatterns); |
||||
} catch (ParseException e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 获取服务器启动时间 |
||||
*/ |
||||
public static Date getServerStartDate() { |
||||
long time = ManagementFactory.getRuntimeMXBean().getStartTime(); |
||||
return new Date(time); |
||||
} |
||||
|
||||
/** |
||||
* 计算两个时间差 |
||||
*/ |
||||
public static String getDatePoor(Date endDate, Date nowDate) { |
||||
long nd = 1000 * 24 * 60 * 60; |
||||
long nh = 1000 * 60 * 60; |
||||
long nm = 1000 * 60; |
||||
// long ns = 1000;
|
||||
// 获得两个时间的毫秒时间差异
|
||||
long diff = endDate.getTime() - nowDate.getTime(); |
||||
// 计算差多少天
|
||||
long day = diff / nd; |
||||
// 计算差多少小时
|
||||
long hour = diff % nd / nh; |
||||
// 计算差多少分钟
|
||||
long min = diff % nd % nh / nm; |
||||
// 计算差多少秒//输出结果
|
||||
// long sec = diff % nd % nh % nm / ns;
|
||||
return day + "天" + hour + "小时" + min + "分钟"; |
||||
} |
||||
|
||||
/** |
||||
* 把日期字符串格式化成日期类型 |
||||
* |
||||
* @param dateStr |
||||
* @return |
||||
*/ |
||||
public static Date convert2Date(String dateStr) { |
||||
return convert2Date(dateStr, PatternUtils.getTimeFomart(dateStr)); |
||||
} |
||||
|
||||
/** |
||||
* 把日期字符串格式化成日期类型 |
||||
* |
||||
* @param dateStr |
||||
* @param format |
||||
* @return |
||||
*/ |
||||
public static Date convert2Date(String dateStr, String format) { |
||||
SimpleDateFormat simple = new SimpleDateFormat(format); |
||||
try { |
||||
simple.setLenient(false); |
||||
return simple.parse(dateStr); |
||||
} catch (Exception e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 把日期类型格式化成字符串 |
||||
* |
||||
* @param date |
||||
* @return |
||||
*/ |
||||
public static String convert2String(Date date) { |
||||
return convert2String(date, YYYY_MM_DD_HH_MM_SS); |
||||
} |
||||
|
||||
/** |
||||
* 把日期类型格式化成字符串 |
||||
* |
||||
* @param date |
||||
* @param format |
||||
* @return |
||||
*/ |
||||
public static String convert2String(Date date, String format) { |
||||
SimpleDateFormat formater = new SimpleDateFormat(format); |
||||
try { |
||||
return formater.format(date); |
||||
} catch (Exception e) { |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* 获取一个时间(当天)的开始时间 |
||||
* |
||||
* @param date |
||||
* @return |
||||
*/ |
||||
public static Date getDateBegin(Date date) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, 0); |
||||
todayEnd.set(Calendar.MINUTE, 0); |
||||
todayEnd.set(Calendar.SECOND, 0); |
||||
todayEnd.set(Calendar.MILLISECOND, 0); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* 获取一个时间(当天)的结束时间 |
||||
* |
||||
* @param date |
||||
* @return |
||||
*/ |
||||
public static Date getDateEnd(Date date) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, 23); |
||||
todayEnd.set(Calendar.MINUTE, 59); |
||||
todayEnd.set(Calendar.SECOND, 59); |
||||
todayEnd.set(Calendar.MILLISECOND, 999); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 转化为具体消耗时间 |
||||
* </pre> |
||||
* |
||||
* @param mss |
||||
* @return |
||||
*/ |
||||
public static String convert2times(Long mss) { |
||||
if (null == mss) |
||||
return "-"; |
||||
StringBuffer s = new StringBuffer(); |
||||
|
||||
long days = mss / (1000 * 60 * 60 * 24); |
||||
long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); |
||||
long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60); |
||||
long seconds = (mss % (1000 * 60)) / 1000; |
||||
if (days > 0) |
||||
s.append(days + "天"); |
||||
if (hours > 0) |
||||
s.append(hours + "小时"); |
||||
if (minutes > 0) |
||||
s.append(minutes + "分钟"); |
||||
if (seconds > 0) |
||||
s.append(seconds + "秒"); |
||||
|
||||
return s.toString(); |
||||
} |
||||
|
||||
/** |
||||
* 获取 2 个时间的自然年历的时间间隔 |
||||
* |
||||
* @param nextDate 后面的时间,需要大于 previousDate |
||||
* @param previousDate 前面的时间 |
||||
* @return [年, 月, 日, 小时, 分钟, 秒]的数组 |
||||
*/ |
||||
public static String getTimeSpace(Date beginTime, Date endTime) { |
||||
List<Integer> list = getTimeSpaceForList(beginTime, endTime); |
||||
|
||||
StringBuffer s = new StringBuffer(); |
||||
int year = list.get(0); |
||||
int month = list.get(1); |
||||
int day = list.get(2); |
||||
int hour = list.get(3); |
||||
int min = list.get(4); |
||||
int second = list.get(5); |
||||
|
||||
if (year > 0) { |
||||
s.append(year + "年"); |
||||
} |
||||
if (month > 0) { |
||||
s.append(month + "月"); |
||||
} |
||||
if (day > 0) { |
||||
s.append(day + "天"); |
||||
} |
||||
if (hour > 0) { |
||||
s.append(hour + "小时"); |
||||
} |
||||
if (min > 0) { |
||||
s.append(min + "分钟"); |
||||
} |
||||
if (second > 0) { |
||||
s.append(second + "秒"); |
||||
} |
||||
return s.toString(); |
||||
} |
||||
|
||||
public static String getTimeSpaceDayHour(Date beginTime, Date endTime) { |
||||
List<Integer> list = getTimeSpaceForList(beginTime, endTime); |
||||
|
||||
StringBuffer s = new StringBuffer(); |
||||
int year = list.get(0); |
||||
int month = list.get(1); |
||||
int day = list.get(2); |
||||
int hour = list.get(3); |
||||
|
||||
if (year > 0) { |
||||
s.append(year + "年"); |
||||
} |
||||
if (month > 0) { |
||||
s.append(month + "个月"); |
||||
} |
||||
if (day > 0) { |
||||
s.append(day + "天"); |
||||
} |
||||
if (hour > 0) { |
||||
s.append(hour + "小时"); |
||||
} |
||||
return s.toString(); |
||||
} |
||||
|
||||
public static List<Integer> getTimeSpaceForList(Date beginTime, Date endTime) { |
||||
Calendar bigger = Calendar.getInstance(); |
||||
bigger.setTime(beginTime.after(endTime)?beginTime:endTime); |
||||
Calendar smaller = Calendar.getInstance(); |
||||
smaller.setTime(beginTime.after(endTime)?endTime:beginTime); |
||||
|
||||
int year = bigger.get(Calendar.YEAR) - smaller.get(Calendar.YEAR); |
||||
int month = bigger.get(Calendar.MONTH) - smaller.get(Calendar.MONTH); |
||||
int day = bigger.get(Calendar.DAY_OF_MONTH) - smaller.get(Calendar.DAY_OF_MONTH); |
||||
int hour = bigger.get(Calendar.HOUR_OF_DAY) - smaller.get(Calendar.HOUR_OF_DAY);// 24小时制
|
||||
int min = bigger.get(Calendar.MINUTE) - smaller.get(Calendar.MINUTE); |
||||
int second = bigger.get(Calendar.SECOND) - smaller.get(Calendar.SECOND); |
||||
|
||||
boolean hasBorrowDay = false;// "时"是否向"天"借过一位
|
||||
|
||||
if (second < 0) { |
||||
second += 60; |
||||
min--; |
||||
} |
||||
|
||||
if (min < 0) { |
||||
min += 60; |
||||
hour--; |
||||
} |
||||
|
||||
if (hour < 0) { |
||||
hour += 24; |
||||
day--; |
||||
hasBorrowDay = true; |
||||
} |
||||
|
||||
if (day < 0) { |
||||
// 计算截止日期的上一个月有多少天,补上去
|
||||
Calendar tempDate = (Calendar) bigger.clone(); |
||||
tempDate.add(Calendar.MONTH, -1);// 获取截止日期的上一个月
|
||||
day += tempDate.getActualMaximum(Calendar.DAY_OF_MONTH); |
||||
|
||||
// nextDate是月底最后一天,且day=这个月的天数,即是刚好一整个月,比如20160131~20160229,day=29,实则为1个月
|
||||
if (!hasBorrowDay && bigger.get(Calendar.DAY_OF_MONTH) == bigger.getActualMaximum(Calendar.DAY_OF_MONTH)// 日期为月底最后一天
|
||||
&& day >= bigger.getActualMaximum(Calendar.DAY_OF_MONTH)) {// day刚好是nextDate一个月的天数,或大于nextDate月的天数(比如2月可能只有28天)
|
||||
day = 0;// 因为这样判断是相当于刚好是整月了,那么不用向 month 借位,只需将 day 置 0
|
||||
} else {// 向month借一位
|
||||
month--; |
||||
} |
||||
} |
||||
|
||||
if (month < 0) { |
||||
month += 12; |
||||
year--; |
||||
} |
||||
return Arrays.asList(new Integer[] {year,month,day,hour,min,second}); |
||||
} |
||||
|
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取某时间当天的最初时刻 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Date getTimeBegin(Date date) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, 0); |
||||
todayEnd.set(Calendar.MINUTE, 0); |
||||
todayEnd.set(Calendar.SECOND, 0); |
||||
todayEnd.set(Calendar.MILLISECOND, 0); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取当天的最初时刻 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Date getTodayBeginTime() { |
||||
return getTimeBegin(new Date()); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取某时间当天的最后时刻 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Date getTimeEnd(Date date) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, 23); |
||||
todayEnd.set(Calendar.MINUTE, 59); |
||||
todayEnd.set(Calendar.SECOND, 59); |
||||
todayEnd.set(Calendar.MILLISECOND, 999); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取当天的 日信息 |
||||
* </pre> |
||||
* |
||||
* @return |
||||
*/ |
||||
public static Integer getNowDay() { |
||||
return getDay(new Date()); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取指定日期的 日信息 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getDay(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.DATE); |
||||
} |
||||
|
||||
public static Integer getWeek(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.WEEK_OF_YEAR); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取指定时间的 年信息 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getYear(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.YEAR); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取指定时间的月份 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getMonth(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.MONTH); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取指定时间的 小时信息 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getHour(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.HOUR_OF_DAY); |
||||
} |
||||
|
||||
public static Integer getMinute(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.MINUTE); |
||||
} |
||||
|
||||
public static Integer getSecond(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
return cal.get(Calendar.SECOND); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定秒数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param second |
||||
* @return |
||||
*/ |
||||
public static Date addSecond(Date date, Integer second) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.SECOND, todayEnd.get(Calendar.SECOND) + second); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定分钟 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param minute |
||||
* @return |
||||
*/ |
||||
public static Date addMinute(Date date, Integer minute) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.MINUTE, todayEnd.get(Calendar.MINUTE) + minute); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定小时数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param hour |
||||
* @return |
||||
*/ |
||||
public static Date addHour(Date date, Integer hour) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, todayEnd.get(Calendar.HOUR_OF_DAY) + hour); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定天数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param afterDay |
||||
* @return |
||||
*/ |
||||
public static Date addDay(Date date, Integer afterDay) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.DAY_OF_YEAR, todayEnd.get(Calendar.DAY_OF_YEAR) + afterDay); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定月数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param afterDay |
||||
* @return |
||||
*/ |
||||
public static Date addWeek(Date date, Integer afterWeek) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.WEEK_OF_YEAR, todayEnd.get(Calendar.WEEK_OF_YEAR) + afterWeek); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定月数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param afterMonth |
||||
* @return |
||||
*/ |
||||
public static Date addMonth(Date date, Integer afterMonth) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.MONTH, todayEnd.get(Calendar.MONTH) + afterMonth); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 增加指定天数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param afterDay |
||||
* @return |
||||
*/ |
||||
public static Date addYear(Date date, Integer afterYear) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.YEAR, todayEnd.get(Calendar.YEAR) + afterYear); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 减少指定秒 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param second |
||||
* @return |
||||
*/ |
||||
public static Date cutSecond(Date date, Integer second) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.SECOND, todayEnd.get(Calendar.SECOND) - second); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 减少指定分钟数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param minute |
||||
* @return |
||||
*/ |
||||
public static Date cutMinute(Date date, Integer minute) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.MINUTE, todayEnd.get(Calendar.MINUTE) - minute); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 减少指定小时数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param hour |
||||
* @return |
||||
*/ |
||||
public static Date cutHour(Date date, Integer hour) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, todayEnd.get(Calendar.HOUR_OF_DAY) - hour); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 减少指定天数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param beforeDay |
||||
* @return |
||||
*/ |
||||
public static Date cutDay(Date date, Integer beforeDay) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.DAY_OF_YEAR, todayEnd.get(Calendar.DAY_OF_YEAR) - beforeDay); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
public static Date cutMonth(Date date, Integer beforeMonth) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.MONTH, todayEnd.get(Calendar.MONTH) - beforeMonth); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 减少指定年数 |
||||
* </pre> |
||||
* |
||||
* @param date |
||||
* @param beforeYear |
||||
* @return |
||||
*/ |
||||
public static Date cutYear(Date date, Integer beforeYear) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.YEAR, todayEnd.get(Calendar.YEAR) - beforeYear); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取两个时间相隔天数 |
||||
* </pre> |
||||
* |
||||
* @param other |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getDifferenceDays(Date other, Date now) { |
||||
if (null == other || null == now) |
||||
return null; |
||||
|
||||
Calendar cal1 = Calendar.getInstance(); |
||||
cal1.setTime(other); |
||||
|
||||
Calendar cal2 = Calendar.getInstance(); |
||||
cal2.setTime(now); |
||||
Double between_days = (double) ((cal1.getTimeInMillis() - cal2.getTimeInMillis()) / (1000D * 3600D * 24D)); |
||||
return (int) Math.abs(Math.ceil(between_days)); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取两个时间相隔天数 |
||||
* </pre> |
||||
* |
||||
* @param other |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getDifferenceMonths(Date other, Date now) { |
||||
int result = 0; |
||||
Calendar c1 = Calendar.getInstance(); |
||||
Calendar c2 = Calendar.getInstance(); |
||||
|
||||
c1.setTime(other); |
||||
c2.setTime(now); |
||||
|
||||
result = c2.get(Calendar.MONTH) - c1.get(Calendar.MONTH); |
||||
|
||||
return result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取两个时间相隔的秒数 |
||||
* </pre> |
||||
* |
||||
* @param other |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static Integer getDifferenceSecond(Date startTime, Date endTime) { |
||||
if (null == startTime || null == endTime) |
||||
return null; |
||||
|
||||
Calendar cal1 = Calendar.getInstance(); |
||||
cal1.setTime(startTime); |
||||
|
||||
Calendar cal2 = Calendar.getInstance(); |
||||
cal2.setTime(endTime); |
||||
|
||||
Double between_days = (double) ((cal1.getTimeInMillis() - cal2.getTimeInMillis()) / (1000D)); |
||||
|
||||
return (int) Math.abs(Math.ceil(between_days)); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取当前时间到当天结束还剩多少秒 |
||||
* </pre> |
||||
* |
||||
* @return |
||||
*/ |
||||
public static Long getDaySurplusSeconds() { |
||||
Date now = new Date(); |
||||
Calendar cal1 = Calendar.getInstance(); |
||||
cal1.setTime(now); |
||||
|
||||
Calendar cal2 = Calendar.getInstance(); |
||||
cal2.setTime(getDateEnd(now)); |
||||
|
||||
Double between_Seconds = (double) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000D)); |
||||
|
||||
return (long) Math.abs(between_Seconds); |
||||
} |
||||
|
||||
/** |
||||
* 当前时间到后面2个小时还剩下多少分钟 |
||||
* |
||||
* @return |
||||
*/ |
||||
public static Long getHourSurplusSeconds() { |
||||
Date now = new Date(); |
||||
Calendar cal1 = Calendar.getInstance(); |
||||
cal1.setTime(now); |
||||
|
||||
Calendar cal2 = Calendar.getInstance(); |
||||
cal2.setTime(addHour(now, 2)); |
||||
|
||||
Double between_Seconds = (double) ((cal2.getTimeInMillis() - cal1.getTimeInMillis()) / (1000D)); |
||||
|
||||
return (long) Math.abs(between_Seconds); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 判断2个时间是否为同一天 |
||||
* </pre> |
||||
* |
||||
* @param date1 |
||||
* @param date2 |
||||
* @return |
||||
* @throws Exception |
||||
*/ |
||||
public static Boolean compareDateIsSameDay(Date date1, Date date2) { |
||||
Calendar cal1 = Calendar.getInstance(); |
||||
cal1.setTime(date1); |
||||
cal1.set(Calendar.HOUR_OF_DAY, 0); |
||||
cal1.set(Calendar.MINUTE, 0); |
||||
cal1.set(Calendar.SECOND, 0); |
||||
cal1.set(Calendar.MILLISECOND, 0); |
||||
|
||||
Calendar cal2 = Calendar.getInstance(); |
||||
cal2.setTime(date2); |
||||
cal2.set(Calendar.HOUR_OF_DAY, 0); |
||||
cal2.set(Calendar.MINUTE, 0); |
||||
cal2.set(Calendar.SECOND, 0); |
||||
cal2.set(Calendar.MILLISECOND, 0); |
||||
|
||||
return cal1.getTimeInMillis() == cal2.getTimeInMillis(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取出生年 |
||||
* </pre> |
||||
* |
||||
* @param birthday |
||||
* @return |
||||
*/ |
||||
public static Date getBirthday(Date birthday) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(birthday); |
||||
cal.set(Calendar.YEAR, getYear(new Date())); |
||||
return cal.getTime(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* <pre> |
||||
* 获取月日 如2017-8-16 20:45:44 得到 08-16 |
||||
* </pre> |
||||
* |
||||
* @param now |
||||
* @return |
||||
*/ |
||||
public static String getYearMonth(Date now) { |
||||
Calendar cal = Calendar.getInstance(); |
||||
cal.setTime(now); |
||||
int month = cal.get(Calendar.MONTH) + 1; |
||||
int day = cal.get(Calendar.DAY_OF_MONTH); |
||||
return (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day); |
||||
} |
||||
|
||||
public static Integer getAgeWeek(String birthDay) { |
||||
if (StringUtils.isBlank(birthDay)) { |
||||
return null; |
||||
} |
||||
Date tmp = convert2Date(birthDay); |
||||
if (tmp == null) { |
||||
return null; |
||||
} |
||||
return getAgeWeek(tmp); |
||||
} |
||||
|
||||
public static Integer getAgeWeek(Date birthDay) { |
||||
if (null == birthDay) |
||||
return null; |
||||
Integer days = getDifferenceDays(new Date(), birthDay); |
||||
return days / 7 - (days < 0 ? 1 : 0); |
||||
} |
||||
|
||||
public static Integer getAgeDay(Date birthDay) { |
||||
if (null == birthDay) |
||||
return null; |
||||
Integer days = getDifferenceDays(new Date(), birthDay); |
||||
return days; |
||||
} |
||||
|
||||
public static Date get2100year() { |
||||
return convert2Date("2100-01-01 00:00:00", YYYY_MM_DD_HH_MM_SS); |
||||
} |
||||
|
||||
/** |
||||
* 获取某年第一天日期 |
||||
* |
||||
* @param year 年份 |
||||
* @return Date |
||||
*/ |
||||
public static Date getYearBegin(Date date) { |
||||
Calendar calendar = Calendar.getInstance(); |
||||
calendar.clear(); |
||||
calendar.set(Calendar.YEAR, getYear(date)); |
||||
Date currYearFirst = calendar.getTime(); |
||||
return currYearFirst; |
||||
} |
||||
|
||||
/** |
||||
* 获取某年最后一天日期 |
||||
* |
||||
* @param year 年份 |
||||
* @return Date |
||||
*/ |
||||
public static Date getYearEnd(Date date) { |
||||
|
||||
Calendar calendar = Calendar.getInstance(); |
||||
calendar.clear(); |
||||
calendar.set(Calendar.YEAR, getYear(date)); |
||||
calendar.roll(Calendar.DAY_OF_YEAR, -1); |
||||
Date currYearLast = calendar.getTime(); |
||||
|
||||
return currYearLast; |
||||
} |
||||
|
||||
public static Date getMonthBegin(Date date) { |
||||
Calendar todayEnd = Calendar.getInstance(); |
||||
todayEnd.setTime(date); |
||||
todayEnd.set(Calendar.DAY_OF_MONTH, 1); |
||||
todayEnd.set(Calendar.HOUR_OF_DAY, 0); |
||||
todayEnd.set(Calendar.MINUTE, 0); |
||||
todayEnd.set(Calendar.SECOND, 0); |
||||
todayEnd.set(Calendar.MILLISECOND, 0); |
||||
return todayEnd.getTime(); |
||||
} |
||||
|
||||
public static Boolean isMonthBeginning(Date date) { |
||||
Date monthBegin = getMonthBegin(date); |
||||
Date target = getDateBegin(date); |
||||
return monthBegin.equals(target); |
||||
} |
||||
|
||||
public static void main(String[] args) { |
||||
System.out.println(DateUtils.convert2String(cutMonth(new Date(), 1))); |
||||
} |
||||
} |
||||
@ -0,0 +1,796 @@ |
||||
package com.zbkj.service.util; |
||||
|
||||
import com.alibaba.fastjson.JSONObject; |
||||
import lombok.extern.slf4j.Slf4j; |
||||
import org.apache.commons.lang3.StringUtils; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.redis.connection.RedisConnection; |
||||
import org.springframework.data.redis.core.RedisCallback; |
||||
import org.springframework.data.redis.core.RedisTemplate; |
||||
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; |
||||
import org.springframework.stereotype.Component; |
||||
import org.springframework.util.CollectionUtils; |
||||
|
||||
import java.util.*; |
||||
import java.util.concurrent.TimeUnit; |
||||
|
||||
/** |
||||
* redisTemplate封装 |
||||
* |
||||
* @author shuai.fan |
||||
*/ |
||||
@Slf4j |
||||
@Component |
||||
public class RedisLockUtil { |
||||
|
||||
|
||||
@Autowired |
||||
private RedisTemplate<String, Object> redisTemplate; |
||||
|
||||
public RedisLockUtil(RedisTemplate<String, Object> redisTemplate) { |
||||
this.redisTemplate = redisTemplate; |
||||
} |
||||
|
||||
/** |
||||
* 指定缓存失效时间 |
||||
* |
||||
* @param key 键 |
||||
* @param time 时间(秒) |
||||
* @return |
||||
*/ |
||||
public boolean expire(String key, long time) { |
||||
try { |
||||
if (time > 0) { |
||||
redisTemplate.expire(key, time, TimeUnit.SECONDS); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.expire fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 根据key 获取过期时间 |
||||
* |
||||
* @param key 键 不能为null |
||||
* @return 时间(秒) 返回0代表为永久有效 |
||||
*/ |
||||
public long getExpire(String key) { |
||||
try { |
||||
if (StringUtils.isBlank(key)) { |
||||
return 0; |
||||
} |
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.getExpire fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 判断key是否存在 |
||||
* |
||||
* @param key 键 |
||||
* @return true 存在 false不存在 |
||||
*/ |
||||
public boolean hasKey(String key) { |
||||
try { |
||||
return redisTemplate.hasKey(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hasKey fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 删除缓存 |
||||
* |
||||
* @param key 可以传一个值 或多个 |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public void del(String... key) { |
||||
try { |
||||
if (key != null && key.length > 0) { |
||||
if (key.length == 1) { |
||||
redisTemplate.delete(key[0]); |
||||
} else { |
||||
redisTemplate.delete(CollectionUtils.arrayToList(key)); |
||||
} |
||||
} |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.del fail", e); |
||||
} |
||||
} |
||||
|
||||
// ============================String=============================
|
||||
/** |
||||
* 普通缓存获取 |
||||
* |
||||
* @param key 键 |
||||
* @return 值 |
||||
*/ |
||||
public Object get(String key) { |
||||
try { |
||||
return key == null ? null : redisTemplate.opsForValue().get(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.get fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 普通缓存放入 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @return true成功 false失败 |
||||
*/ |
||||
public boolean set(String key, Object value) { |
||||
try { |
||||
redisTemplate.opsForValue().set(key, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.set fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 普通缓存放入并设置时间 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期 |
||||
* @return true成功 false 失败 |
||||
*/ |
||||
public boolean set(String key, Object value, long time) { |
||||
try { |
||||
if (time > 0) { |
||||
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); |
||||
} else { |
||||
set(key, value); |
||||
} |
||||
log.info("{}缓存{}秒,缓存内容:{}",key,DateUtils.convert2times(time*1000),JSONObject.toJSONString(value)); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.set fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
public boolean set(String key, Object value, long time,TimeUnit timeUnit) { |
||||
try { |
||||
if (time > 0) { |
||||
redisTemplate.opsForValue().set(key, value, time, timeUnit); |
||||
} else { |
||||
set(key, value); |
||||
} |
||||
log.info("{}缓存{}秒,缓存内容:{}",key,DateUtils.convert2times(time*1000),JSONObject.toJSONString(value)); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.set fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 递增 |
||||
* |
||||
* @param key 键 |
||||
* @param delta 要增加几(大于0) |
||||
* @return |
||||
*/ |
||||
public long incr(String key, long delta) { |
||||
if (delta < 0) { |
||||
throw new RuntimeException("递增因子必须大于0"); |
||||
} |
||||
return redisTemplate.opsForValue().increment(key, delta); |
||||
} |
||||
|
||||
/** |
||||
* 递增 |
||||
* |
||||
* @param key 键 |
||||
* @return |
||||
*/ |
||||
public long incr(String key) { |
||||
return redisTemplate.opsForValue().increment(key); |
||||
} |
||||
|
||||
/** |
||||
* 递减 |
||||
* |
||||
* @param key 键 |
||||
* @param delta 要减少几(小于0) |
||||
* @return |
||||
*/ |
||||
public long decr(String key, long delta) { |
||||
if (delta < 0) { |
||||
throw new RuntimeException("递减因子必须大于0"); |
||||
} |
||||
return redisTemplate.opsForValue().increment(key, -delta); |
||||
} |
||||
|
||||
// ================================Map=================================
|
||||
/** |
||||
* HashGet |
||||
* |
||||
* @param key 键 不能为null |
||||
* @param item 项 不能为null |
||||
* @return 值 |
||||
*/ |
||||
public Object hget(String key, String item) { |
||||
try { |
||||
return redisTemplate.opsForHash().get(key, item); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hget fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 获取hashKey对应的所有键值 |
||||
* |
||||
* @param key 键 |
||||
* @return 对应的多个键值 |
||||
*/ |
||||
public Map<Object, Object> hmget(String key) { |
||||
try { |
||||
return redisTemplate.opsForHash().entries(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hmget fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* HashSet |
||||
* |
||||
* @param key 键 |
||||
* @param map 对应多个键值 |
||||
* @return true 成功 false 失败 |
||||
*/ |
||||
public boolean hmset(String key, Map<String, Object> map) { |
||||
try { |
||||
redisTemplate.opsForHash().putAll(key, map); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hmset fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* HashSet 并设置时间 |
||||
* |
||||
* @param key 键 |
||||
* @param map 对应多个键值 |
||||
* @param time 时间(秒) |
||||
* @return true成功 false失败 |
||||
*/ |
||||
public boolean hmset(String key, Map<String, Object> map, long time) { |
||||
try { |
||||
redisTemplate.opsForHash().putAll(key, map); |
||||
if (time > 0) { |
||||
expire(key, time); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hmset fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 向一张hash表中放入数据,如果不存在将创建 |
||||
* |
||||
* @param key 键 |
||||
* @param item 项 |
||||
* @param value 值 |
||||
* @return true 成功 false失败 |
||||
*/ |
||||
public boolean hset(String key, String item, Object value) { |
||||
try { |
||||
redisTemplate.opsForHash().put(key, item, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hset fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 向一张hash表中放入数据,如果不存在将创建 |
||||
* |
||||
* @param key 键 |
||||
* @param item 项 |
||||
* @param value 值 |
||||
* @param time 时间(秒) 注意:如果已存在的hash表有时间,这里将会替换原有的时间 |
||||
* @return true 成功 false失败 |
||||
*/ |
||||
public boolean hset(String key, String item, Object value, long time) { |
||||
try { |
||||
redisTemplate.opsForHash().put(key, item, value); |
||||
if (time > 0) { |
||||
expire(key, time); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hset fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 删除hash表中的值 |
||||
* |
||||
* @param key 键 不能为null |
||||
* @param item 项 可以使多个 不能为null |
||||
*/ |
||||
public void hdel(String key, Object... item) { |
||||
try { |
||||
redisTemplate.opsForHash().delete(key, item); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hdel fail", e); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* 判断hash表中是否有该项的值 |
||||
* |
||||
* @param key 键 不能为null |
||||
* @param item 项 不能为null |
||||
* @return true 存在 false不存在 |
||||
*/ |
||||
public boolean hHasKey(String key, String item) { |
||||
try { |
||||
return redisTemplate.opsForHash().hasKey(key, item); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.hHasKey fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* hash递增 如果不存在,就会创建一个 并把新增后的值返回 |
||||
* |
||||
* @param key 键 |
||||
* @param item 项 |
||||
* @param by 要增加几(大于0) |
||||
* @return |
||||
*/ |
||||
public double hincr(String key, String item, double by) { |
||||
return redisTemplate.opsForHash().increment(key, item, by); |
||||
} |
||||
|
||||
/** |
||||
* hash递减 |
||||
* |
||||
* @param key 键 |
||||
* @param item 项 |
||||
* @param by 要减少记(小于0) |
||||
* @return |
||||
*/ |
||||
public double hdecr(String key, String item, double by) { |
||||
return redisTemplate.opsForHash().increment(key, item, -by); |
||||
} |
||||
|
||||
// ============================set=============================
|
||||
/** |
||||
* 根据key获取Set中的所有值 |
||||
* |
||||
* @param key 键 |
||||
* @return |
||||
*/ |
||||
public Set<Object> sGet(String key) { |
||||
try { |
||||
return redisTemplate.opsForSet().members(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.sGet fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 根据value从一个set中查询,是否存在 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @return true 存在 false不存在 |
||||
*/ |
||||
public boolean sHasKey(String key, Object value) { |
||||
try { |
||||
return redisTemplate.opsForSet().isMember(key, value); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.sHasKey fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 将数据放入set缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param values 值 可以是多个 |
||||
* @return 成功个数 |
||||
*/ |
||||
public long sSet(String key, Object... values) { |
||||
try { |
||||
return redisTemplate.opsForSet().add(key, values); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.sSet fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 将set数据放入缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param time 时间(秒) |
||||
* @param values 值 可以是多个 |
||||
* @return 成功个数 |
||||
*/ |
||||
public long sSetAndTime(String key, long time, Object... values) { |
||||
try { |
||||
Long count = redisTemplate.opsForSet().add(key, values); |
||||
if (time > 0) { |
||||
expire(key, time); |
||||
} |
||||
return count; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.sSetAndTime fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 获取set缓存的长度 |
||||
* |
||||
* @param key 键 |
||||
* @return |
||||
*/ |
||||
public long sGetSetSize(String key) { |
||||
try { |
||||
return redisTemplate.opsForSet().size(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.sGetSetSize fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 移除值为value的 |
||||
* |
||||
* @param key 键 |
||||
* @param values 值 可以是多个 |
||||
* @return 移除的个数 |
||||
*/ |
||||
public long setRemove(String key, Object... values) { |
||||
try { |
||||
Long count = redisTemplate.opsForSet().remove(key, values); |
||||
return count; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.setRemove fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
// ===============================list=================================
|
||||
|
||||
/** |
||||
* 获取list缓存的内容 |
||||
* |
||||
* @param key 键 |
||||
* @param start 开始 |
||||
* @param end 结束 0 到 -1代表所有值 |
||||
* @return |
||||
*/ |
||||
public List<Object> lGet(String key, long start, long end) { |
||||
try { |
||||
return redisTemplate.opsForList().range(key, start, end); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lGet fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 获取list缓存的长度 |
||||
* |
||||
* @param key 键 |
||||
* @return |
||||
*/ |
||||
public long lGetListSize(String key) { |
||||
try { |
||||
return redisTemplate.opsForList().size(key); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lGetListSize fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 通过索引 获取list中的值 |
||||
* |
||||
* @param key 键 |
||||
* @param index 索引 index>=0时, 0 表头,1 第二个元素,依次类推;index<0时,-1,表尾,-2倒数第二个元素,依次类推 |
||||
* @return |
||||
*/ |
||||
public Object lGetIndex(String key, long index) { |
||||
try { |
||||
return redisTemplate.opsForList().index(key, index); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lGetIndex fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 将list放入缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @return |
||||
*/ |
||||
public boolean lSet(String key, Object value) { |
||||
try { |
||||
redisTemplate.opsForList().rightPush(key, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lSet fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 将list放入缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @param time 时间(秒) |
||||
* @return |
||||
*/ |
||||
public boolean lSet(String key, Object value, long time) { |
||||
try { |
||||
redisTemplate.opsForList().rightPush(key, value); |
||||
if (time > 0) { |
||||
expire(key, time); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lSet fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 将list放入缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @return |
||||
*/ |
||||
public boolean lSet(String key, List<Object> value) { |
||||
try { |
||||
redisTemplate.opsForList().rightPushAll(key, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lSet fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 将list放入缓存 |
||||
* |
||||
* @param key 键 |
||||
* @param value 值 |
||||
* @param time 时间(秒) |
||||
* @return |
||||
*/ |
||||
public boolean lSet(String key, List<Object> value, long time) { |
||||
try { |
||||
|
||||
redisTemplate.opsForList().rightPushAll(key, value); |
||||
if (time > 0) { |
||||
expire(key, time); |
||||
} |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lSet fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 根据索引修改list中的某条数据 |
||||
* |
||||
* @param key 键 |
||||
* @param index 索引 |
||||
* @param value 值 |
||||
* @return |
||||
*/ |
||||
public boolean lUpdateIndex(String key, long index, Object value) { |
||||
try { |
||||
redisTemplate.opsForList().set(key, index, value); |
||||
return true; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lUpdateIndex fail", e); |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* 移除N个值为value |
||||
* |
||||
* @param key 键 |
||||
* @param count 移除多少个 |
||||
* @param value 值 |
||||
* @return 移除的个数 |
||||
*/ |
||||
public long lRemove(String key, long count, Object value) { |
||||
try { |
||||
Long remove = redisTemplate.opsForList().remove(key, count, value); |
||||
return remove; |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.lRemove fail", e); |
||||
} |
||||
return 0; |
||||
} |
||||
|
||||
/** |
||||
* 模糊查询获取key值 |
||||
* |
||||
* @param pattern |
||||
* @return |
||||
*/ |
||||
public Set<?> keys(String pattern) { |
||||
try { |
||||
return redisTemplate.keys(pattern); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.keys fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* 使用Redis的消息队列 |
||||
* |
||||
* @param channel |
||||
* @param message 消息内容 |
||||
*/ |
||||
public void convertAndSend(String channel, Object message) { |
||||
try { |
||||
redisTemplate.convertAndSend(channel, message); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.convertAndSend fail", e); |
||||
} |
||||
} |
||||
|
||||
public byte[] getBytes(String str) { |
||||
if (str == null) { |
||||
return null; |
||||
} |
||||
try { |
||||
return str.getBytes("UTF-8"); |
||||
} catch (Exception e) { |
||||
return str.getBytes(); |
||||
} |
||||
} |
||||
|
||||
public GenericJackson2JsonRedisSerializer getRedisSerializer() { |
||||
GenericJackson2JsonRedisSerializer redisSerializer = (GenericJackson2JsonRedisSerializer) redisTemplate |
||||
.getDefaultSerializer(); |
||||
return redisSerializer; |
||||
} |
||||
|
||||
public <T> T get(String key, Class<T> clazz, T defaultValue) { |
||||
T data = get(key, clazz); |
||||
if (null == data) { |
||||
data = defaultValue; |
||||
} |
||||
return data; |
||||
} |
||||
|
||||
public <T> T get(String key, Class<T> clazz) { |
||||
if (StringUtils.isBlank(key)) { |
||||
return null; |
||||
} |
||||
try { |
||||
RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); |
||||
byte[] source = connection.get(getBytes(key)); |
||||
if (source == null) { |
||||
return null; |
||||
} |
||||
|
||||
return getRedisSerializer().deserialize(source, clazz); |
||||
} catch (Exception e) { |
||||
log.error("redisUtil.get fail", e); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
|
||||
// 锁前缀
|
||||
public static final String LOCK_PREFIX = "redis_lock"; |
||||
// 加锁失效时间,毫秒
|
||||
public static final int LOCK_EXPIRE = 1; // s
|
||||
|
||||
public boolean lock(String key) { |
||||
return lock(key, LOCK_EXPIRE); |
||||
} |
||||
|
||||
/** |
||||
* 锁方法 |
||||
* |
||||
* @param key 锁key |
||||
* @param lockExpire 锁自动释放时间 |
||||
* @return |
||||
* @author yuwenjie |
||||
* @date 2020-02-25 03:55:52 |
||||
*/ |
||||
public boolean lock(String key, int lockExpire) { |
||||
String lock = LOCK_PREFIX + key; |
||||
return (Boolean) redisTemplate.execute((RedisCallback<?>) connection -> { |
||||
|
||||
long expireAt = System.currentTimeMillis() + lockExpire * 1000 + 1; |
||||
Boolean acquire = connection.setNX(lock.getBytes(), String.valueOf(expireAt).getBytes()); |
||||
|
||||
if (acquire) { |
||||
return true; |
||||
} else { |
||||
byte[] value = connection.get(lock.getBytes()); |
||||
if (Objects.nonNull(value) && value.length > 0) { |
||||
|
||||
long expireTime = Long.parseLong(new String(value)); |
||||
// 如果锁已经过期
|
||||
if (expireTime < System.currentTimeMillis()) { |
||||
// 重新加锁,防止死锁
|
||||
byte[] oldValue = connection.getSet(lock.getBytes(), |
||||
String.valueOf(System.currentTimeMillis() + lockExpire * 1000 + 1).getBytes()); |
||||
return Long.parseLong(new String(oldValue)) < System.currentTimeMillis(); |
||||
} |
||||
} |
||||
} |
||||
return false; |
||||
}); |
||||
} |
||||
public boolean lock(String key, int lockExpire,int sleepTime,int retryCount) { |
||||
boolean lock=false; |
||||
for (int i = 0; i < retryCount; i++) { |
||||
lock = lock(key, lockExpire); |
||||
if (!lock) { |
||||
try { |
||||
log.info("重试锁次数{}",i+1); |
||||
Thread.sleep(sleepTime); |
||||
} catch (InterruptedException e) { |
||||
e.printStackTrace(); |
||||
} |
||||
}else{ |
||||
break; |
||||
} |
||||
} |
||||
return lock; |
||||
} |
||||
/** |
||||
* 删除锁 |
||||
* |
||||
* @param key |
||||
*/ |
||||
public void unLock(String key) { |
||||
redisTemplate.delete(LOCK_PREFIX + key); |
||||
} |
||||
|
||||
|
||||
|
||||
public static void main(String[] args) { |
||||
String s = "abc"; |
||||
|
||||
List<String> list = Arrays.asList(new String[] {"123","abc","435"}); |
||||
|
||||
list.stream().filter(key -> !key.equals(s)).forEach(key -> { |
||||
System.out.println(key); |
||||
}); |
||||
} |
||||
} |
||||
@ -0,0 +1,17 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="com.zbkj.service.dao.XsbuyDao"> |
||||
<resultMap type="com.zbkj.service.pojo.XsSkuInfo" id="BaseMap"> |
||||
<id property="id" column="id"/> |
||||
<result property="skuNo" column="sku_no"/> |
||||
<result property="skuName" column="sku_name"/> |
||||
</resultMap> |
||||
<select id="selectListNew" resultMap="BaseMap"> |
||||
select good_code sku_no,good_name sku_name,brand_code,brand_name,category_name,category_code from m_xsbuy_cn.xs_goods where is_show=1 |
||||
<if test="content !=null and content !=''"> |
||||
and LOWER(good_code) =LOWER(#{content} ) |
||||
</if> |
||||
ORDER BY id limit 20 |
||||
</select> |
||||
|
||||
</mapper> |
||||
@ -1,5 +1,7 @@ |
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
||||
<mapper namespace="com.zbkj.service.dao.UserAddressDao"> |
||||
|
||||
<select id="selectDistinct" resultType="com.zbkj.common.model.user.UserAddress"> |
||||
select * from eb_user_address where uid=#{order.uid} and concat(province,city,district,detail)=#{order.userAddress} limit 1 |
||||
</select> |
||||
</mapper> |
||||
|
||||
Loading…
Reference in new issue