package com.mzl.flower.service.impl.productOrders;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mzl.flower.component.SequenceNo;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.dto.request.productOrders.ProductOrdersCreateDTO;
import com.mzl.flower.entity.customer.Customer;
import com.mzl.flower.entity.productOrders.ProductOrdersDO;
import com.mzl.flower.enums.PayStatusEnum;
import com.mzl.flower.enums.ProductTypeEnum;
import com.mzl.flower.enums.alipay.AlipayResultEnum;
import com.mzl.flower.mapper.productOrders.ProductOrdersMapper;
import com.mzl.flower.service.customer.CustomerService;
import com.mzl.flower.service.productOrders.ProductOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mzl.flower.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
/**
*
* 服务实现类
*
*
* @author generator@TaoJie
* @since 2025-03-31
*/
@Service
public class ProductOrdersServiceImpl extends ServiceImpl implements ProductOrdersService {
@Autowired
private ProductOrdersMapper productOrdersMapper;
@Autowired
private SequenceNo sequenceNo;
@Autowired
private CustomerService customerService;
@Transactional
@Override
public ProductOrdersDO createProductOrders(ProductOrdersDO productOrdersDO) {
String userId = SecurityUtils.getUserId();
// ProductOrdersDO productOrdersDO = new ProductOrdersDO();
productOrdersDO.setId(IdUtil.simpleUUID());
productOrdersDO.setUserId(userId);
// 待支付
productOrdersDO.setPaymentStatus(PayStatusEnum.PAYING.getName());
productOrdersDO.setOrderNo(getSalesNo());
productOrdersDO.create(userId);
int insert = productOrdersMapper.insert(productOrdersDO);
return productOrdersMapper.selectById(productOrdersDO.getId());
}
@Transactional
@Override
public Boolean updateProductOrderStatus(Map params) {
String orderNo = params.get("out_trade_no"); // 本系统对应的订单好
String tradeNo = params.get("trade_no"); //支付宝交易号。支付宝交易凭证号。
String tradeStatus = params.get("trade_status"); //交易状态。咨询目前所处的状态。
String gmtPayment = params.get("gmt_payment"); // 交易 付款时间。该笔交易的买家付款时间。格式为 yyyy-MM-dd HH:mm:ss。
String gmtRefund = params.get("gmt_refund"); // 交易退款时间。该笔交易的退款时间。格式 为 yyyy-MM-dd HH:mm:ss.SS。
String gmtClose = params.get("gmt_close"); // 交易退款时间。该笔交易的退款时间。格式 为 yyyy-MM-dd HH:mm:ss.SS。
// 根据orderNo 查找订单
QueryWrapper queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(ProductOrdersDO::getDeleted, false)
.eq(ProductOrdersDO::getOrderNo, orderNo);
List productOrdersDOS = productOrdersMapper.selectList(queryWrapper);
if(!CollectionUtils.isEmpty(productOrdersDOS)){
ProductOrdersDO productOrdersDO = productOrdersDOS.get(0);
if(tradeStatus.equals(AlipayResultEnum.TRADE_CLOSED.getName())){
// 未付款交易超时关闭,或支付完成后全额退款
productOrdersDO.setPaymentStatus(PayStatusEnum.CANCEL.getName());
}
if(tradeStatus.equals(AlipayResultEnum.TRADE_SUCCESS.getName())){
// 交易支付成功
productOrdersDO.setPaymentStatus(PayStatusEnum.PAID.getName());
}
if(tradeStatus.equals(AlipayResultEnum.TRADE_FINISHED.getName())){
// 交易完成(交易结束,不可退款)
productOrdersDO.setPaymentStatus(PayStatusEnum.FINISHED.getName());
}
// 将yyyy-MM-dd HH:mm:ss转换成LocalDateTime
if(gmtPayment != null){
productOrdersDO.setPaymentTime(DateUtils.dateTimeStringToLocalDateTime(gmtPayment));
}
// 更新用户到期时间
if(!StringUtils.isEmpty(productOrdersDO.getUserId())){
Customer customer = customerService.getByUserId(productOrdersDO.getUserId());
if(customer != null){
LocalDateTime overTime = null;
// 根据年卡,月卡、月卡持续计算到期时间
if(productOrdersDO.getOrderType().equals(ProductTypeEnum.MONTH_CONTINUED.getName())){
if(null!=customer.getMemberOvertime()){
// 在当前时间上增加一个月
overTime = customer.getMemberOvertime().plusMonths(1);
}else{
// 当前时间上增加一个月
overTime =LocalDateTime.now().plusMonths(1);
}
}
if(productOrdersDO.getOrderType().equals(ProductTypeEnum.MONTH.getName())){
if(null!=customer.getMemberOvertime()){
// 在当前时间上增加一个月
overTime = customer.getMemberOvertime().plusMonths(1);
}else{
// 当前时间上增加一个月
overTime =LocalDateTime.now().plusMonths(1);
}
}
if(productOrdersDO.getOrderType().equals(ProductTypeEnum.YEAR.getName())){
if(null!=customer.getMemberOvertime()){
// 在当前时间上增加一个月
overTime = customer.getMemberOvertime().plusYears(1);
}else{
// 当前时间上增加一个月
overTime = customer.getMemberOvertime().plusYears(1);
}
}
// 设置过期时间
customer.setMemberOvertime(overTime);
customer.setIsMember(true); // 会员
}
// 更新会员信息
customerService.updateMemberInfo(customer);
}
return productOrdersMapper.updateById(productOrdersDO)>0;
}
return false;
}
private String getSalesNo(){
String seq = sequenceNo.getSeqNo(SequenceNo.ORDER_PRODUCT);
return "PO" + DateUtils.format(LocalDateTime.now(), "yyyyMMddHHmmss") + seq;
}
}