|
|
@@ -0,0 +1,155 @@
|
|
|
+package com.etotem.cfc.service;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.etotem.cfc.common.Result;
|
|
|
+import com.etotem.cfc.entity.ActivityProvision;
|
|
|
+import com.etotem.cfc.entity.ProductOrder;
|
|
|
+import com.etotem.cfc.mapper.ActivityProvisionMapper;
|
|
|
+import com.etotem.cfc.mapper.ProductOrderMapper;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class ActivityProvisionService {
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ActivityProvisionMapper activityProvisionMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private ProductOrderMapper productOrderMapper;
|
|
|
+
|
|
|
+ @Resource
|
|
|
+ private PaymentService paymentService;
|
|
|
+
|
|
|
+ public Result<String> provision(Long orderId, Long auditorId) {
|
|
|
+ ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
+ if (order == null) {
|
|
|
+ return Result.error("订单不存在");
|
|
|
+ }
|
|
|
+ if (!"unprovisioned".equals(order.getProvisionStatus())) {
|
|
|
+ return Result.error("当前状态不允许审核");
|
|
|
+ }
|
|
|
+ ActivityProvision provision = new ActivityProvision();
|
|
|
+ provision.setOrderId(orderId);
|
|
|
+ provision.setOrderNo(order.getOrderNo());
|
|
|
+ provision.setProductId(order.getProductId());
|
|
|
+ provision.setActivityId(order.getActivityId() != null ? order.getActivityId() : order.getProductId());
|
|
|
+ provision.setBuyerId(order.getBuyerId());
|
|
|
+ provision.setFamilyId(order.getFamilyId());
|
|
|
+ provision.setStatus("provisioned");
|
|
|
+ provision.setQrCode(generateQrCode(order.getOrderNo()));
|
|
|
+ provision.setCreatedAt(new Date());
|
|
|
+ provision.setUpdatedAt(new Date());
|
|
|
+ activityProvisionMapper.insert(provision);
|
|
|
+
|
|
|
+ order.setProvisionStatus("provisioned");
|
|
|
+ order.setAuditorId(auditorId);
|
|
|
+ order.setAuditedAt(new Date());
|
|
|
+ productOrderMapper.updateById(order);
|
|
|
+ return Result.success("审核通过,已生成签到码");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<String> cancel(Long orderId, Long auditorId, String reason) {
|
|
|
+ ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
+ if (order == null) {
|
|
|
+ return Result.error("订单不存在");
|
|
|
+ }
|
|
|
+ if (!"unprovisioned".equals(order.getProvisionStatus())) {
|
|
|
+ return Result.error("当前状态不允许取消");
|
|
|
+ }
|
|
|
+ ActivityProvision provision = new ActivityProvision();
|
|
|
+ provision.setOrderId(orderId);
|
|
|
+ provision.setOrderNo(order.getOrderNo());
|
|
|
+ provision.setProductId(order.getProductId());
|
|
|
+ provision.setActivityId(order.getActivityId() != null ? order.getActivityId() : order.getProductId());
|
|
|
+ provision.setBuyerId(order.getBuyerId());
|
|
|
+ provision.setFamilyId(order.getFamilyId());
|
|
|
+ provision.setStatus("cancelled");
|
|
|
+ provision.setRefundAmount(order.getTotalAmount());
|
|
|
+ provision.setRefundAt(new Date());
|
|
|
+ provision.setCreatedAt(new Date());
|
|
|
+ provision.setUpdatedAt(new Date());
|
|
|
+ activityProvisionMapper.insert(provision);
|
|
|
+
|
|
|
+ order.setProvisionStatus("cancelled");
|
|
|
+ order.setStatus("cancelled");
|
|
|
+ order.setAuditorId(auditorId);
|
|
|
+ order.setAuditedAt(new Date());
|
|
|
+ order.setAuditReason(reason);
|
|
|
+ productOrderMapper.updateById(order);
|
|
|
+
|
|
|
+ paymentService.refund(order.getOrderNo(), order.getTotalAmount(), "活动审核不通过:" + reason);
|
|
|
+ return Result.success("已拒绝并退款");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<String> signIn(Long orderId, Long childId) {
|
|
|
+ ActivityProvision provision = activityProvisionMapper.selectOne(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ActivityProvision>()
|
|
|
+ .eq(ActivityProvision::getOrderId, orderId)
|
|
|
+ );
|
|
|
+ if (provision == null) {
|
|
|
+ return Result.error("未找到活动 provision 记录");
|
|
|
+ }
|
|
|
+ if (!"provisioned".equals(provision.getStatus())) {
|
|
|
+ return Result.error("当前状态不允许签到");
|
|
|
+ }
|
|
|
+ provision.setChildId(childId);
|
|
|
+ provision.setCheckedInAt(new Date());
|
|
|
+ provision.setStatus("signed_in");
|
|
|
+ provision.setUpdatedAt(new Date());
|
|
|
+ activityProvisionMapper.updateById(provision);
|
|
|
+
|
|
|
+ ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
+ if (order != null) {
|
|
|
+ order.setProvisionStatus("signed_in");
|
|
|
+ productOrderMapper.updateById(order);
|
|
|
+ }
|
|
|
+ return Result.success("签到成功");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public Result<String> complete(Long orderId) {
|
|
|
+ ActivityProvision provision = activityProvisionMapper.selectOne(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ActivityProvision>()
|
|
|
+ .eq(ActivityProvision::getOrderId, orderId)
|
|
|
+ );
|
|
|
+ if (provision == null) {
|
|
|
+ return Result.error("未找到活动 provision 记录");
|
|
|
+ }
|
|
|
+ if (!"signed_in".equals(provision.getStatus())) {
|
|
|
+ return Result.error("当前状态不允许完成");
|
|
|
+ }
|
|
|
+ provision.setStatus("completed");
|
|
|
+ provision.setCompletedAt(new Date());
|
|
|
+ provision.setUpdatedAt(new Date());
|
|
|
+ activityProvisionMapper.updateById(provision);
|
|
|
+
|
|
|
+ ProductOrder order = productOrderMapper.selectById(orderId);
|
|
|
+ if (order != null) {
|
|
|
+ order.setProvisionStatus("completed");
|
|
|
+ productOrderMapper.updateById(order);
|
|
|
+ }
|
|
|
+ return Result.success("活动完成");
|
|
|
+ }
|
|
|
+
|
|
|
+ public Result<ActivityProvision> getByOrderId(Long orderId) {
|
|
|
+ ActivityProvision provision = activityProvisionMapper.selectOne(
|
|
|
+ new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<ActivityProvision>()
|
|
|
+ .eq(ActivityProvision::getOrderId, orderId)
|
|
|
+ );
|
|
|
+ if (provision == null) {
|
|
|
+ return Result.success(null);
|
|
|
+ }
|
|
|
+ return Result.success(provision);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String generateQrCode(String orderNo) {
|
|
|
+ return "ACT-" + orderNo + "-" + UUID.randomUUID().toString().substring(0, 8).toUpperCase();
|
|
|
+ }
|
|
|
+}
|