瀏覽代碼

商品购买支持优惠券

zouxuan 3 年之前
父節點
當前提交
d5f2018067

+ 13 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/dao/SysCouponCodeDao.java

@@ -46,4 +46,17 @@ public interface SysCouponCodeDao extends BaseDAO<Long, SysCouponCode> {
      * @param couponCodeId
      */
     void quit(@Param("couponCodeId") String couponCodeId);
+
+    /**
+     * 根据优惠券编号获取优惠券列表详情
+     * @param couponIdList
+     * @return
+     */
+    List<SysCouponCodeDto> findByIdList(@Param("couponIdList") List<Integer> couponIdList);
+
+    /**
+     * 使用优惠券
+     * @param couponIdList
+     */
+    void use(@Param("couponIdList") List<Integer> couponIdList);
 }

+ 11 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/StudentGoodsSell.java

@@ -36,6 +36,9 @@ public class StudentGoodsSell {
 	@ApiModelProperty(value = "减免费用",required = true)
 	private java.math.BigDecimal marketAmount = BigDecimal.ZERO;
 
+	@ApiModelProperty(value = "优惠券减免费用",required = true)
+	private java.math.BigDecimal couponMarketAmount = BigDecimal.ZERO;
+
 	private Date createTime;
 
 	private Date updateTime;
@@ -64,6 +67,14 @@ public class StudentGoodsSell {
 	@ApiModelProperty(value = "优惠券列表", required = false)
 	private List<Integer> couponIdList;
 
+	public BigDecimal getCouponMarketAmount() {
+		return couponMarketAmount;
+	}
+
+	public void setCouponMarketAmount(BigDecimal couponMarketAmount) {
+		this.couponMarketAmount = couponMarketAmount;
+	}
+
 	public List<Integer> getCouponIdList() {
 		return couponIdList;
 	}

+ 17 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/SysCouponCodeService.java

@@ -2,6 +2,7 @@ package com.ym.mec.biz.service;
 
 import com.ym.mec.biz.dal.dto.HorseRaceLampDto;
 import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
+import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
 import com.ym.mec.biz.dal.entity.SysCouponCode;
 import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
 import com.ym.mec.common.page.PageInfo;
@@ -49,4 +50,20 @@ public interface SysCouponCodeService extends BaseService<Long, SysCouponCode> {
      * @return
      */
     List<SysCouponCode> queryCouponPage(BigDecimal amount, Integer userId);
+
+    /**
+     * 使用优惠券
+     * @param couponIdList
+     * @param amount
+     * @return
+     */
+    StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount);
+
+    List<SysCouponCodeDto> findByIdList(List<Integer> couponIdList);
+
+    /**
+     * 退优惠券
+     * @param couponCodeId
+     */
+    void quit(String couponCodeId);
 }

+ 4 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentPaymentOrderServiceImpl.java

@@ -106,6 +106,8 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
 
     @Autowired
     private CloudTeacherOrderService cloudTeacherOrderService;
+    @Autowired
+    private SysCouponCodeService sysCouponCodeService;
 
     @Override
     public BaseDAO<Long, StudentPaymentOrder> getDAO() {
@@ -348,6 +350,8 @@ public class StudentPaymentOrderServiceImpl extends BaseServiceImpl<Long, Studen
             sysUserCashAccountService.updateBalance(order.getUserId(), order.getActualAmount(),
                     PlatformCashAccountDetailTypeEnum.REFUNDS, memo + ",订单号:" + order.getOrderNo());
 
+            //退还优惠券
+            sysCouponCodeService.quit(order.getCouponCodeId());
             if (StringUtils.isNotBlank(order.getPaymentChannel())
                     && order.getPaymentChannel().equals("ADAPAY")
                     && !rpMap.containsKey("simulation")) {

+ 13 - 9
mec-biz/src/main/java/com/ym/mec/biz/service/impl/StudentRepairServiceImpl.java

@@ -9,6 +9,7 @@ import com.ym.mec.biz.dal.dao.*;
 import com.ym.mec.biz.dal.dto.BasicUserDto;
 import com.ym.mec.biz.dal.dto.GoodsSellDto;
 import com.ym.mec.biz.dal.dto.RepairGoodsDto;
+import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
 import com.ym.mec.biz.dal.entity.*;
 import com.ym.mec.biz.dal.enums.*;
 import com.ym.mec.biz.dal.page.RepairStudentQueryInfo;
@@ -79,7 +80,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
     @Autowired
     private StudentGoodsSellService studentGoodsSellService;
     @Autowired
-    private SysCouponCodeDao sysCouponCodeDao;
+    private SysCouponCodeService sysCouponCodeService;
 
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
 
@@ -147,10 +148,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
                     sysUserCashAccountService.updateBalance(orderByOrderNo.getUserId(), orderByOrderNo.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "关闭订单");
                 }
                 //退优惠券
-                String couponCodeId = orderByOrderNo.getCouponCodeId();
-                if(StringUtils.isNotEmpty(couponCodeId)){
-                    sysCouponCodeDao.quit(couponCodeId);
-                }
+                sysCouponCodeService.quit(orderByOrderNo.getCouponCodeId());
             }
             StudentGoodsSell byOrderNo = studentGoodsSellDao.findByOrderNo(studentGoodsSell.getOrderNo());
             if (byOrderNo != null) {
@@ -209,6 +207,14 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         if (amount.signum() < 0) {
             throw new BizException("操作失败:订单金额异常");
         }
+        //使用优惠券
+        StudentPaymentOrder studentPaymentOrder = sysCouponCodeService.use(studentGoodsSell.getCouponIdList(),amount);
+        amount = studentPaymentOrder.getActualAmount();
+        // 判断金额是否正确
+//        if (studentGoodsSell.getTotalAmount().compareTo(amount) != 0) {
+//            throw new BizException("非法请求");
+//        }
+        studentGoodsSell.setCouponMarketAmount(studentPaymentOrder.getCouponRemitFee());
         studentGoodsSell.setOrganId(student.getOrganId());
         studentGoodsSell.setTotalAmount(amount);
         studentGoodsSell.setGoodsJson(JSONObject.toJSONString(goodsSellDtos));
@@ -222,13 +228,10 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             return repairInfoMap;
         }
 
-        StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
         studentPaymentOrder.setUserId(studentId);
         studentPaymentOrder.setGroupType(GroupType.GOODS_SELL);
         studentPaymentOrder.setOrderNo(orderNo);
         studentPaymentOrder.setType(OrderTypeEnum.GOODS_SELL);
-        studentPaymentOrder.setExpectAmount(amount);
-        studentPaymentOrder.setActualAmount(amount);
         studentPaymentOrder.setStatus(DealStatusEnum.ING);
         studentPaymentOrder.setOrganId(student.getOrganId());
         studentPaymentOrder.setRoutingOrganId(student.getOrganId());
@@ -762,6 +765,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
             if (studentPaymentOrder.getBalancePaymentAmount() != null && studentPaymentOrder.getBalancePaymentAmount().compareTo(BigDecimal.ZERO) > 0) {
                 sysUserCashAccountService.updateBalance(studentPaymentOrder.getUserId(), studentPaymentOrder.getBalancePaymentAmount(), PlatformCashAccountDetailTypeEnum.REFUNDS, "乐器购买支付失败");
             }
+            sysCouponCodeService.quit(studentPaymentOrder.getCouponCodeId());
         }
     }
 
@@ -814,7 +818,7 @@ public class StudentRepairServiceImpl extends BaseServiceImpl<Integer, StudentRe
         BigDecimal balancePaymentAmount = orderByOrderNo.getBalancePaymentAmount();
         //可用余额
         BigDecimal usableBalance = balancePaymentAmount;
-        BigDecimal marketAmount = studentGoodsSell.getMarketAmount();
+        BigDecimal marketAmount = studentGoodsSell.getMarketAmount().add(studentGoodsSell.getCouponMarketAmount());
         //可用减免金额
         BigDecimal usableMarketAmount = marketAmount;
         //包含减免余额的总金额

+ 67 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/SysCouponCodeServiceImpl.java

@@ -8,6 +8,7 @@ import com.ym.mec.biz.dal.dto.SysCouponCodeDto;
 import com.ym.mec.biz.dal.entity.StudentPaymentOrder;
 import com.ym.mec.biz.dal.entity.SysCoupon;
 import com.ym.mec.biz.dal.entity.SysCouponCode;
+import com.ym.mec.biz.dal.enums.CouponTypeEnum;
 import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
 import com.ym.mec.biz.service.SysCouponCodeService;
 import com.ym.mec.biz.service.SysCouponService;
@@ -19,6 +20,7 @@ import com.ym.mec.common.service.IdGeneratorService;
 import com.ym.mec.common.service.impl.BaseServiceImpl;
 import com.ym.mec.util.collection.MapUtil;
 import com.ym.mec.util.date.DateUtil;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
@@ -157,4 +159,69 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
 
 		return null;
     }
+
+    @Override
+	@Transactional(rollbackFor = Exception.class)
+    public StudentPaymentOrder use(List<Integer> couponIdList, BigDecimal amount) {
+		StudentPaymentOrder studentPaymentOrder = new StudentPaymentOrder();
+		if(couponIdList != null && couponIdList.size() > 0){
+			Date date = new Date();
+			List<SysCouponCodeDto> couponCodeDtoList = sysCouponCodeDao.findByIdList(couponIdList);
+			if(couponIdList.size() != couponCodeDtoList.size()){
+				throw new BizException("操作失败:优惠券数据异常");
+			}
+			BigDecimal fullAmount = BigDecimal.ZERO;
+			BigDecimal faceAmount = BigDecimal.ZERO;
+
+			for (SysCouponCodeDto sysCouponCodeDto : couponCodeDtoList) {
+				if(sysCouponCodeDto.getUsageStatus() == 1){
+					throw new BizException("操作失败:优惠券已使用");
+				}
+				if(sysCouponCodeDto.getUseStartDate().after(date)){
+					throw new BizException("操作失败:不在优惠券使用期限");
+				}
+				if(date.after(sysCouponCodeDto.getUseDeadlineDate())){
+					throw new BizException("操作失败:优惠券已过期");
+				}
+				CouponTypeEnum couponType = sysCouponCodeDto.getCouponType();
+				switch (couponType){
+					case DISCOUNT:
+						throw new BizException("操作失败:暂不支持折扣劵");
+					case FULL_REDUCTION:
+						fullAmount = fullAmount.add(sysCouponCodeDto.getFullAmount());
+						faceAmount = faceAmount.add(sysCouponCodeDto.getFaceValue());
+						break;
+					default:
+						throw new BizException("操作失败:错误的优惠券类型");
+				}
+			}
+			if(amount.compareTo(fullAmount) < 0){
+				throw new BizException("操作失败:当前消费金额不满足优惠券满减条件");
+			}
+			amount = amount.subtract(faceAmount);
+			if (amount.signum() < 0) {
+				amount = BigDecimal.ZERO;
+			}
+			//使用优惠券
+			sysCouponCodeDao.use(couponIdList);
+			studentPaymentOrder.setCouponCodeId(StringUtils.join(couponIdList,","));
+			studentPaymentOrder.setCouponRemitFee(faceAmount);
+		}
+		studentPaymentOrder.setExpectAmount(amount);
+		studentPaymentOrder.setActualAmount(amount);
+		return studentPaymentOrder;
+    }
+
+	@Override
+	public List<SysCouponCodeDto> findByIdList(List<Integer> couponIdList) {
+		return sysCouponCodeDao.findByIdList(couponIdList);
+	}
+
+	@Override
+	public void quit(String couponCodeId) {
+		if(StringUtils.isEmpty(couponCodeId)){
+			return;
+		}
+		sysCouponCodeDao.quit(couponCodeId);
+	}
 }

+ 6 - 2
mec-biz/src/main/resources/config/mybatis/StudentGoodsSellMapper.xml

@@ -13,6 +13,7 @@
 		<result column="goods_json_" property="goodsJson" />
 		<result column="total_amount_" property="totalAmount" />
 		<result column="market_amount_" property="marketAmount" />
+		<result column="coupon_market_amount_" property="couponMarketAmount" />
 		<result column="order_no_" property="orderNo" />
 		<result column="username_" property="userName" />
 		<result column="organ_id_" property="organId" />
@@ -37,14 +38,17 @@
 	<!-- 向数据库增加一条记录 -->
 	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentGoodsSell" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
 		INSERT INTO student_goods_sell (user_id_,teacher_id_,goods_json_,total_amount_,market_amount_,
-		create_time_,update_time_,order_no_,organ_id_,author_user_,cooperation_organ_id_)
+		create_time_,update_time_,order_no_,organ_id_,author_user_,cooperation_organ_id_,coupon_market_amount_)
 		VALUES(#{userId},#{teacherId},#{goodsJson},#{totalAmount},#{marketAmount},
-		NOW(),NOW(),#{orderNo},#{organId},#{authorUser},#{cooperationOrganId})
+		NOW(),NOW(),#{orderNo},#{organId},#{authorUser},#{cooperationOrganId},#{couponMarketAmount})
 	</insert>
 	
 	<!-- 根据主键查询一条记录 -->
 	<update id="update" parameterType="com.ym.mec.biz.dal.entity.StudentGoodsSell">
 		UPDATE student_goods_sell <set>
+		<if test="couponMarketAmount != null">
+			coupon_market_amount_ = #{couponMarketAmount},
+		</if>
 		<if test="cooperationOrganId != null">
 			cooperation_organ_id_ = #{cooperationOrganId},
 		</if>

+ 4 - 4
mec-biz/src/main/resources/config/mybatis/StudentPaymentOrderMapper.xml

@@ -101,12 +101,12 @@
     <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.StudentPaymentOrder" useGeneratedKeys="true"
             keyColumn="id" keyProperty="id">
         INSERT INTO student_payment_order
-        (id_, group_type_, user_id_, organ_id_, routing_organ_id_, type_, expect_amount_, actual_amount_, com_amount_,
+        (group_type_, user_id_, organ_id_, routing_organ_id_, type_, expect_amount_, actual_amount_, com_amount_,
          per_amount_,
          balance_payment_amount_, remit_fee_, course_remit_fee_, trans_no_,
          status_, memo_, create_time_, update_time_, payment_channel_, payment_business_channel_,
-         payment_account_no_, mer_nos_, order_no_, music_group_id_, class_group_id_, pay_time_,batch_no_)
-        VALUES (#{id}, #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
+         payment_account_no_, mer_nos_, order_no_, music_group_id_, class_group_id_, pay_time_,batch_no_,coupon_code_id_,coupon_remit_fee_)
+        VALUES (#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                 #{userId}, #{organId}, #{routingOrganId},
                 #{type,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
                 #{expectAmount}, #{actualAmount}, #{comAmount}, #{perAmount}, #{balancePaymentAmount},
@@ -114,7 +114,7 @@
                 #{status,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}, #{memo}, now(), now(),
                 #{paymentChannel}, #{paymentBusinessChannel}, #{paymentAccountNo}, #{merNos}, #{orderNo},
                 #{musicGroupId},
-                #{classGroupId}, #{payTime},#{batchNo})
+                #{classGroupId}, #{payTime},#{batchNo},#{couponCodeId},#{couponRemitFee})
     </insert>
 
 

+ 31 - 0
mec-biz/src/main/resources/config/mybatis/SysCouponCodeMapper.xml

@@ -109,6 +109,13 @@
 	<update id="quit">
 		UPDATE sys_coupon_code SET usage_status_ = 0,used_time_ = NULL,update_time_ = NOW() WHERE FIND_IN_SET(id_,#{couponCodeId})
 	</update>
+	<update id="use">
+		UPDATE sys_coupon_code SET usage_status_ = 1,used_time_ = NOW(),update_time_ = NOW()
+		WHERE id_ IN
+		<foreach collection="couponIdList" open="(" close=")" item="item" separator=",">
+			#{item}
+		</foreach>
+	</update>
 
 	<!-- 根据主键删除一条记录 -->
 	<delete id="delete" >
@@ -218,4 +225,28 @@
 		LEFT JOIN sys_coupon sc ON sc.id_ = suc.coupon_id_
 		GROUP BY suc.user_id_,suc.payment_order_id_,suc.coupon_id_)t
 	</select>
+	<select id="findByIdList" resultMap="SysCouponCodeDto">
+		SELECT
+		scc.user_id_,
+		scc.id_ coupon_code_id_,
+		scc.coupon_id_,
+		sc.type_ coupon_type_,
+		sc.name_ coupon_name_,
+		sc.face_value_,
+		sc.full_amount_,
+		sc.description_ coupon_description_,
+		scc.code_,
+		scc.payment_order_id_,
+		scc.use_start_date_,
+		scc.use_deadline_date_,
+		scc.used_time_,
+		IF(scc.usage_status_ = 0 AND scc.use_deadline_date_ &lt; NOW(),2,scc.usage_status_) usage_status_
+		FROM sys_coupon_code scc
+		LEFT JOIN sys_coupon sc ON scc.coupon_id_=sc.id_
+		WHERE scc.id_ IN
+		<foreach collection="couponIdList" open="(" close=")" item="item" separator=",">
+			#{item}
+		</foreach>
+		ORDER BY scc.id_ DESC
+	</select>
 </mapper>

+ 0 - 4
mec-student/src/main/java/com/ym/mec/student/controller/MusicGroupController.java

@@ -80,15 +80,11 @@ public class MusicGroupController extends BaseController {
     private MusicGroupPaymentCalenderDetailDao musicGroupPaymentCalenderDetailDao;
     @Autowired
     private MusicGroupPaymentCalenderDao musicGroupPaymentCalenderDao;
-
     @Autowired
     private StudentPreRegistrationDao studentPreRegistrationDao;
     @Autowired
     private OrganizationService organizationService;
 
-    @Autowired
-    private CourseScheduleDao courseScheduleDao;
-
     @ApiOperation("预报名")
     @PostMapping(value = "/preRegister")
     public Object preRegister(@RequestBody StudentPreRegistration studentPreRegistration) {

+ 4 - 2
mec-web/src/main/java/com/ym/mec/web/controller/education/EduRepairController.java

@@ -25,8 +25,10 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.*;
-import java.util.stream.Collectors;
+import java.util.Date;
+import java.util.List;
+import java.util.Map;
+import java.util.Objects;
 
 @RequestMapping("eduRepair")
 @Api(tags = "教务维修服务")

+ 30 - 0
mec-web/src/main/java/com/ym/mec/web/controller/education/EduSysCouponCodeController.java

@@ -0,0 +1,30 @@
+package com.ym.mec.web.controller.education;
+
+import com.ym.mec.biz.dal.page.SysCouponCodeQueryInfo;
+import com.ym.mec.biz.service.SysCouponCodeService;
+import com.ym.mec.common.controller.BaseController;
+import com.ym.mec.common.entity.HttpResponseResult;
+import com.ym.mec.common.exception.BizException;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RequestMapping("eduSysCouponCode")
+@Api(tags = "优惠券明细")
+@RestController
+public class EduSysCouponCodeController extends BaseController {
+
+	@Autowired
+	private SysCouponCodeService sysCouponCodeService;
+
+	@ApiOperation("分页查询")
+	@RequestMapping(value = "queryPage")
+	public HttpResponseResult queryPage(SysCouponCodeQueryInfo queryInfo) {
+		if(queryInfo.getUserId() == null){
+			throw new BizException("请指定学员");
+		}
+		return succeed(sysCouponCodeService.querySysCouponUseList(queryInfo));
+	}
+}

+ 2 - 2
mec-web/src/main/resources/logback-spring.xml

@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <configuration scan="true" scanPeriod="10 seconds">
 
-	<property name="LOG_HOME" value="/Users/chenxiaoyu/Documents/logs/web-%d{yyyy-MM-dd_HH}-%i.log" />
+	<property name="LOG_HOME" value="/mdata/logs/web-%d{yyyy-MM-dd_HH}-%i.log" />
 	<property name="CONSOLE_LOG_PATTERN"
 			  value="[%X{username} %X{ip} %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36}] : %msg%n" />
 
@@ -30,7 +30,7 @@
 	<appender name="messagefile"
 			  class="ch.qos.logback.core.rolling.RollingFileAppender">
 		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
-			<FileNamePattern>/Users/chenxiaoyu/Documents/logs/web-message-%d{yyyy-MM-dd_HH}-%i.log</FileNamePattern>
+			<FileNamePattern>/mdata/logs/web-message-%d{yyyy-MM-dd_HH}-%i.log</FileNamePattern>
 			<MaxHistory>90</MaxHistory>
 			<TimeBasedFileNamingAndTriggeringPolicy
 					class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">