优惠券系统反射生成优惠规则

一:先查询领券的中心列表,在查询中心列表的时候是优惠券的信息表联合发布的信息表查询。
因为查询领券中心的信息,最终是要发布的,所以我们需要知道优惠券的信息表的优惠规则,限制规则等,还需要知道领劵中心的定时抢购,领取天数有效,领取多少天之后有效等。
二:实现类的接口
因为在优惠券表的动态属性,本质上是json 不能够拿来用的,所以我们需要实现类的全路径。
所以我们定义一个规则接口,这个接口可以定义一些行为比如下单的金额是否满足优惠的条件(多少个规则就有多个接口)。

<!-- 实体类的映射集合  column数据库的字段  property实体类的的属性-->
    <resultMap id="couponMap" type="com.qf.entity.Coupon">
        <id column="id" property="id"></id>
        <result column="subject" property="subject"></result>
        <result column="subtitle" property="subtitle"></result>
        <result column="content" property="content"></result>
        <result column="rule_info" property="ruleInfo"></result>
        <result column="limit_info" property="limitInfo"></result>
        <result column="utype" property="utype"></result>
        <result column="priority" property="priority"></result>
        <result column="create_time" property="createTime"></result>
        <result column="status" property="status"></result>
        <result column="reccount" property="reccount"></result>

        <!-- 关联查询 - 对一 -->
        <association property="issue" javaType="com.qf.entity.CouponIssue">
            <id column="ciid" property="id"></id>
            <result column="cid" property="cid"></result>
            <result column="method" property="method"></result>
            <result column="type" property="type"></result>
            <result column="begin_time" property="beginTime"></result>
            <result column="end_time" property="endTime"></result>
            <result column="days" property="days"></result>
            <result column="number" property="number"></result>
            <result column="go_start_time" property="goStartTime"></result>
            <result column="go_stop_time" property="goStopTime"></result>
        </association>
    </resultMap>

    <!-- 查询领劵中心的优惠券列表  优惠券的信息和发布的信息 -->
    <select id="getCouponByIssueType" resultMap="couponMap">
        select *, ci.id as ciid, (select count(*) from coupon_recevie where cid = c.id and uid = #{uid}) as reccount from
            coupon c join coupon_issue ci on c.id = ci.cid
            where ci.method = #{method}
    </select>


    <select id="getCouponByUser" resultType="com.qf.entity.Coupon">
        select * from coupon c join coupon_recevie cr on c.id = cr.cid
            where cr.uid = #{uid}
    </select>

1.1:这里就展现其中一个实现类


/**
 * 满减的规则实现类
 */
public class ManJianRule implements IRule {

    private double mustPrice;
    private  double jianPrice;

    /**
     * 下单的金额是否满足优惠的条件
     * @param price
     * @return
     */
    @Override
    public boolean hasPirce(double price) {
        return mustPrice <=price;
    }

    /**
     * 优惠的形式
     * @param price
     * @return
     */
    @Override
    public double youhui(double price) {
        return BigDecimal.valueOf(price).subtract(BigDecimal.valueOf(jianPrice)).doubleValue();
    }

    /**
     * 优惠了多少额度
     * @param price
     * @return
     */
    @Override
    public double youhuiPrice(double price) {
        return jianPrice;
    }
}

三:在查询领券的中心的时候,实体类中需要引入优惠的规则实现对象和优惠的限制对象那我们该去实现他呢?我们就要规则的json -> 实际的规则对象。这里需要动态的实现具体的优惠规则,因为写死了,就不好以后该优惠规则了:比如满100减20

@Data
@Accessors(chain = true)
public class Coupon implements Serializable {

  @TableId(type = IdType.AUTO)
  private Integer id;
  private String subject;
  private String subtitle;
  private Integer utype;
  private Integer priority;
  private String content;
  private String ruleInfo;//优惠规则
  private String limitInfo;//优惠限制
  private Date createTime=new Date();
  private Integer status=0;

  @TableField(exist = false)
  private Integer reccount;
  //优惠的规则实现对象
  @TableField(exist = false)
  private IRule rule;
  //优惠的限制对象
  @TableField(exist = false)
  private ILimit limit;
  /**
   * 发布的信息
   */
  @TableField(exist = false)
  private CouponIssue issue;

  /**
   * 把ruleInfo属性转换成对象
   * @param ruleInfo
   */
  public void setRuleInfo(String ruleInfo) {
    this.ruleInfo = ruleInfo;
    //规则的json -> 实际的规则对象
  }

  public void setLimitInfo(String limitInfo) {
    this.limitInfo = limitInfo;
  }
}

public class CouponUtil {

    /**
     * 将动态的json -> 实现类
     *
     * {
     *     "myclass":"com.qf.coupon.rule.ManJianRule",
     *     "fields":[
     *         {
     *             "type":"0",
     *             "name":"a",
     *             "value":"100"
     *         },
     *         {
     *             "type":"0",
     *             "name":"b",
     *             "value":"20"
     *         }]
     * }
     *
     *
     *
     * 对象: {key:value, key:value ...}
     * 数组:[value,value,value...]
     *
     * {key:[], key:{}, key:[{},{}]}
     *
     *
     * @param <T>
     * @return
     */
    public static <T> T dynamicFied2obj(String dynamicJson){
        try{
            //解析JSON字符串
            JSONObject jsonObject = JSON.parseObject(dynamicJson);
            //当前实现类的类路径
            String myclass = jsonObject.getString("myclass");
            //反射动态创建实现类
            //通过类的全路径限定名获得该类的反射对象
            Class c=Class.forName(myclass);
            //通过反射对象创建对象
            Object obj=c.newInstance();

            //解析类中的动态属性
            JSONArray fields =jsonObject.getJSONArray("fields");
            for (int i = 0; i <fields.size() ; i++) {
                //从数组中解析出属性的json
                JSONObject fieldsjson=fields.getJSONObject(i);
                //获得属性的name
                String name=fieldsjson.getString("name");
                //获得属性的value
                String value=fieldsjson.getString("value");

                //通过反射将动态属性set给指定的对象
                Field field =c.getDeclaredField(name);
                //授权操作私有属性
                field.setAccessible(true);
                //判断属性类型
                //获得属性类型的class对象
                Class typeClass=field.getType();
                if (typeClass==int.class||typeClass==Integer.class){
                      field.setInt(obj,Integer.parseInt(value));
                }else if (typeClass==float.class||typeClass==float.class){
                    field.setFloat(obj,Float.parseFloat(value));
                }else if (typeClass== Date.class){
                    field.set(obj,new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(value));
                }else {
                    field.set(obj,value);
                }
            }
            return (T) obj;
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值