瀏覽代碼

fix:优惠券

liujunchi 3 年之前
父節點
當前提交
22869687c1

+ 12 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/SysCoupon.java

@@ -90,6 +90,18 @@ public class SysCoupon extends BaseEntity implements Serializable{
 	@ApiModelProperty("非数据库字段-可以领取的优惠券数量")
 	private Integer canBeGetNum;
 
+	@ApiModelProperty("领取优惠券条件JSON: {organId:分部id(逗号隔开)}")
+	private String useCondition;
+
+	public String getUseCondition() {
+		return useCondition;
+	}
+
+	public void setUseCondition(String useCondition) {
+		this.useCondition = useCondition;
+	}
+
+
 	public Integer getId() {
 		return id;
 	}

+ 25 - 0
mec-biz/src/main/java/com/ym/mec/biz/dal/entity/UseCondition.java

@@ -0,0 +1,25 @@
+package com.ym.mec.biz.dal.entity;
+
+import io.swagger.annotations.ApiModelProperty;
+
+import java.io.Serializable;
+
+/**
+ * Description
+ *
+ * @author liujunchi
+ * @date 2022-05-24
+ */
+public class UseCondition implements Serializable {
+
+    @ApiModelProperty("分部id(逗号隔开)")
+    private String organId;
+
+    public String getOrganId() {
+        return organId;
+    }
+
+    public void setOrganId(String organId) {
+        this.organId = organId;
+    }
+}

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

@@ -1,5 +1,8 @@
 package com.ym.mec.biz.service.impl;
 
+import com.alibaba.fastjson.JSON;
+import com.ym.mec.auth.api.client.SysUserFeignService;
+import com.ym.mec.auth.api.entity.SysUser;
 import com.ym.mec.biz.dal.dao.StudentPaymentOrderDao;
 import com.ym.mec.biz.dal.dao.SysCouponCodeDao;
 import com.ym.mec.biz.dal.dao.SysCouponDao;
@@ -45,6 +48,9 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
     @Autowired
     private StudentPaymentOrderDao studentPaymentOrderDao;
 
+    @Autowired
+    private SysUserFeignService sysUserFeignService;
+
     @Override
     public BaseDAO<Long, SysCouponCode> getDAO() {
         return sysCouponCodeDao;
@@ -121,6 +127,10 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
         optional.filter(c -> c.getStockCount() == -1
                         || (c.getStockCount() > -1 && c.getStockCount() >= c.getConsumeNum() + exchangeNum))
                 .orElseThrow(() -> new BizException("优惠券库存不足!"));
+
+        // 检测学生能否领取优惠券
+        checkUser(userId,sysCoupon);
+
         //校验兑换的最大次数
         if (sysCoupon.getLimitExchangeNum() != -1) {
             int userUserNum = sysCouponCodeDao.countWithUserAndCoupon(userId, couponId);
@@ -132,6 +142,24 @@ public class SysCouponCodeServiceImpl extends BaseServiceImpl<Long, SysCouponCod
         return sysCoupon;
     }
 
+
+    private void checkUser(Integer userId, SysCoupon sysCoupon) {
+        SysUser sysUser = sysUserFeignService.queryUserById(userId);
+        if (StringUtils.isBlank(sysCoupon.getUseCondition())) {
+            return;
+        }
+        UseCondition useCondition = JSON.parseObject(sysCoupon.getUseCondition(), UseCondition.class);
+        // 检测分部
+        if (StringUtils.isNotBlank(useCondition.getOrganId())) {
+            List<String> organIdList = Arrays.asList(useCondition.getOrganId().split(","));
+            if (!organIdList.contains(sysUser.getOrganId().toString())) {
+                throw new BizException("优惠券不属于当前分部");
+            }
+
+        }
+    }
+
+
     private void opsCoupon(Integer userId, SysCouponCode sysCouponCode, Integer couponId, Integer exchangeNum, SysCoupon sysCoupon) {
         Integer tenantId = TenantContextHolder.getTenantId();
         Date now = new Date();

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

@@ -175,6 +175,8 @@
 			</if>
 			<if test="organId!=null">
 				AND su.organ_id_=#{organId}
+				and (find_in_set(#{organId},JSON_UNQUOTE(json_extract(sc.use_condition_,'$.organId')))
+				or JSON_UNQUOTE(json_extract(sc.use_condition_,'$.organId')) is null)
 			</if>
 			<if test="search!=null and search!=''">
 				AND (scc.user_id_=#{search} OR su.phone_=#{search} OR su.username_ LIKE CONCAT('%', #{search}, '%'))

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

@@ -164,6 +164,8 @@
              WHERE usage_status_  <![CDATA[ <> ]]> 2 AND user_id_ = #{userId}
              GROUP BY coupon_id_) AS b
             ON a.id_ = b.coupon_id_
+        join
+        (select organ_id_ from sys_user where id_ = #{userId}) c
         WHERE issuance_type_ = 0
           AND status_ = 1
           AND tenant_id_ = #{tenantId}
@@ -173,6 +175,8 @@
                     stock_count_ > consume_num_
                 OR stock_count_ = - 1
             )
+        and (find_in_set(c.organ_id_,JSON_UNQUOTE(json_extract(a.use_condition_,'$.organId')))
+        or JSON_UNQUOTE(json_extract(a.use_condition_,'$.organId')) is null)
     </select>
 
 </mapper>

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

@@ -30,6 +30,7 @@
 		<result column="create_time_" property="createTime" />
 		<result column="update_time_" property="updateTime" />
         <result column="tenant_id_" property="tenantId"/>
+		<result column="use_condition_" property="useCondition"/>
 	</resultMap>
 
 	<!-- 根据主键查询一条记录 -->
@@ -46,13 +47,13 @@
 	<insert id="insert" parameterType="com.ym.mec.biz.dal.entity.SysCoupon" useGeneratedKeys="true" keyColumn="id" keyProperty="id">
 		INSERT INTO sys_coupon (id_,name_,description_,status_,issuance_type_,type_,type_detail_,face_value_,full_amount_,limit_exchange_num_,effective_type_,deadline_,
 		                        effective_start_time_, effective_expire_time_,end_date_,start_date_,stock_count_,consume_num_,warning_stock_num_,
-		                        create_time_,update_time_,tenant_id_)
+		                        create_time_,update_time_,tenant_id_,use_condition_)
 		                        VALUES(#{id},#{name},#{description},#{status},#{issuanceType},
 		                               #{type, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
 		                               #{typeDetail},#{faceValue},#{fullAmount},#{limitExchangeNum},
 		                               #{effectiveType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{deadline},
 		                               #{effectiveStartTime},#{effectiveExpireTime},#{endDate},#{startDate},#{stockCount},#{consumeNum},#{warningStockNum},
-		                               NOW(), NOW(),#{tenantId})
+		                               NOW(), NOW(),#{tenantId},#{useCondition})
 	</insert>
 
 	<!-- 根据主键查询一条记录 -->
@@ -102,6 +103,9 @@
 			<if test="warningStatus != null">
 				warning_status_ = #{warningStatus},
 			</if>
+			<if test="useCondition != null">
+				use_condition_ = #{useCondition},
+			</if>
 				update_time_ = NOW()
 		</set>
 		WHERE id_ = #{id} and tenant_id_ = #{tenantId}