| <?xml version="1.0" encoding="UTF-8"?> | 
| <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" | 
|         "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> | 
|   | 
| <mapper namespace="com.matrix.system.shopXcx.dao.ShopCouponDao"> | 
|   | 
|     <!-- 定义ShopCoupon 的复杂关联map --> | 
|     <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="ShopCouponMap"> | 
|         <id property="id" column="id"/> | 
|         <result property="createBy" column="create_by"/> | 
|         <result property="createTime" column="create_time"/> | 
|         <result property="updateBy" column="update_by"/> | 
|         <result property="updateTime" column="update_time"/> | 
|         <result property="cName" column="c_name"/> | 
|         <result property="offsetAmount" column="offset_amount"/> | 
|         <result property="minAmount" column="min_amount"/> | 
|         <result property="beginTime" column="begin_time"/> | 
|         <result property="endTime" column="end_time"/> | 
|         <result property="maxQuantity" column="max_quantity"/> | 
|         <result property="getLimit" column="get_limit"/> | 
|         <result property="quantityReceive" column="quantity_receive"/> | 
|         <result property="couponStatus" column="coupon_status"/> | 
|         <result property="isOpen" column="is_open"/> | 
|         <result property="isAll" column="is_all"/> | 
|         <result property="productIds" column="product_ids"/> | 
|         <result property="attrIds" column="attr_ids"/> | 
|         <result property="tag" column="tag"/> | 
|         <result property="kind" column="kind"/> | 
|         <result property="shopId" column="shop_id"/> | 
|         <result property="shopName" column="shopName"/> | 
|         <result property="companyId" column="company_id" /> | 
|   | 
|   | 
|     </resultMap> | 
|   | 
|   | 
|     <!-- 定义ShopCoupon 的简单map  ,本map不添加其他的关联属性 --> | 
|     <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="ShopCouponSimpleMap"> | 
|         <id property="id" column="id"/> | 
|         <result property="createBy" column="create_by"/> | 
|         <result property="createTime" column="create_time"/> | 
|         <result property="updateBy" column="update_by"/> | 
|         <result property="updateTime" column="update_time"/> | 
|         <result property="cName" column="c_name"/> | 
|         <result property="offsetAmount" column="offset_amount"/> | 
|         <result property="minAmount" column="min_amount"/> | 
|         <result property="beginTime" column="begin_time"/> | 
|         <result property="endTime" column="end_time"/> | 
|         <result property="maxQuantity" column="max_quantity"/> | 
|         <result property="getLimit" column="get_limit"/> | 
|         <result property="quantityReceive" column="quantity_receive"/> | 
|         <result property="couponStatus" column="coupon_status"/> | 
|         <result property="isOpen" column="is_open"/> | 
|         <result property="isAll" column="is_all"/> | 
|         <result property="productIds" column="product_ids"/> | 
|         <result property="attrIds" column="attr_ids"/> | 
|         <result property="tag" column="tag"/> | 
|         <result property="kind" column="kind"/> | 
|         <result property="shopId" column="shop_id"/> | 
|         <result property="companyId" column="company_id" /> | 
|     </resultMap> | 
|   | 
|     <!-- 字段sql --> | 
|     <sql id="columns"> | 
|         create_by, | 
|         create_time, | 
|         update_by, | 
|         update_time, | 
|             id, | 
|             c_name, | 
|             offset_amount, | 
|             min_amount, | 
|             begin_time, | 
|             end_time, | 
|             max_quantity, | 
|             get_limit, | 
|             quantity_receive, | 
|             coupon_status, | 
|             is_open, | 
|             is_all, | 
|             product_ids, | 
|             attr_ids, | 
|             tag, | 
|             kind, | 
|             shop_id, | 
|             company_id | 
|     </sql> | 
|   | 
|     <!-- 属性sql --> | 
|     <sql id="propertys"> | 
|         #{item.createBy}, | 
|         now(), | 
|         #{item.updateBy}, | 
|         now(), | 
|         #{item.id}, | 
|         #{item.cName}, | 
|         #{item.offsetAmount}, | 
|         #{item.minAmount}, | 
|         #{item.beginTime}, | 
|         #{item.endTime}, | 
|         #{item.maxQuantity}, | 
|         #{item.getLimit}, | 
|         #{item.quantityReceive}, | 
|         #{item.couponStatus}, | 
|         #{item.isOpen}, | 
|         #{item.isAll}, | 
|         #{item.productIds}, | 
|         #{item.attrIds}, | 
|         #{item.tag}, | 
|         #{item.kind}, | 
|         #{item.shopId}, | 
|         #{item.companyId} | 
|     </sql> | 
|   | 
|     <!-- where sql --> | 
|     <sql id="where_sql"> | 
|   | 
|         <if test="record!=null"> | 
|             <if test="(record.id!=null and record.id!='') or  (record.id!='' and record.id==0)  "> | 
|                 and id = #{record.id} | 
|             </if> | 
|             <if test="(record.cName!=null and record.cName!='') or  (record.cName!='' and record.cName==0)  "> | 
|                 and c_name = #{record.cName} | 
|             </if> | 
|             <if test="(record.offsetAmount!=null and record.offsetAmount!='') or  (record.offsetAmount!='' and record.offsetAmount==0)  "> | 
|                 and offset_amount = #{record.offsetAmount} | 
|             </if> | 
|             <if test="(record.minAmount!=null and record.minAmount!='') or  (record.minAmount!='' and record.minAmount==0)  "> | 
|                 and min_amount = #{record.minAmount} | 
|             </if> | 
|             <if test="(record.beginTime!=null and record.beginTime!='') or  (record.beginTime!='' and record.beginTime==0)  "> | 
|                 and begin_time = #{record.beginTime} | 
|             </if> | 
|             <if test="(record.endTime!=null and record.endTime!='') or  (record.endTime!='' and record.endTime==0)  "> | 
|                 and end_time = #{record.endTime} | 
|             </if> | 
|             <if test="(record.maxQuantity!=null and record.maxQuantity!='') or  (record.maxQuantity!='' and record.maxQuantity==0)  "> | 
|                 and max_quantity = #{record.maxQuantity} | 
|             </if> | 
|             <if test="(record.getLimit!=null and record.getLimit!='') or  (record.getLimit!='' and record.getLimit==0)  "> | 
|                 and get_limit = #{record.getLimit} | 
|             </if> | 
|             <if test="(record.quantityReceive!=null and record.quantityReceive!='') or  (record.quantityReceive!='' and record.quantityReceive==0)  "> | 
|                 and quantity_receive = #{record.quantityReceive} | 
|             </if> | 
|             <if test="(record.couponStatus!=null and record.couponStatus!='') or  (record.couponStatus!='' and record.couponStatus==0)  "> | 
|                 and coupon_status = #{record.couponStatus} | 
|             </if> | 
|             <if test="(record.isOpen!=null and record.isOpen!='') or  (record.isOpen!='' and record.isOpen==0)  "> | 
|                 and is_open = #{record.isOpen} | 
|             </if> | 
|             <if test="(record.isAll!=null and record.isAll!='') or  (record.isAll!='' and record.isAll==0)  "> | 
|                 and is_all = #{record.isAll} | 
|             </if> | 
|             <if test="(record.productIds!=null and record.productIds!='') or  (record.productIds!='' and record.productIds==0)  "> | 
|                 and product_ids = #{record.productIds} | 
|             </if> | 
|             <if test="(record.attrIds!=null and record.attrIds!='') or  (record.attrIds!='' and record.attrIds==0)  "> | 
|                 and attr_ids = #{record.attrIds} | 
|             </if> | 
|             <if test="(record.tag!=null and record.tag!='') or  (record.tag!='' and record.tag==0)  "> | 
|                 and tag = #{record.tag} | 
|             </if> | 
|             <if test="(record.kind!=null and record.kind!='') or  (record.kind!='' and record.kind==0)  "> | 
|                 and kind = #{record.kind} | 
|             </if> | 
|   | 
|             <if test="(record.shopId!=null and record.shopId!='') or  (record.shopId!='' and record.shopId==0)  "> | 
|                 and shop_id = #{record.shopId} | 
|             </if> | 
|   | 
|             <if test="record.companyId != null and record.companyId !='' "> | 
|                 and company_id = #{record.companyId} | 
|             </if> | 
|   | 
|   | 
|         </if> | 
|   | 
|   | 
|     </sql> | 
|   | 
|     <!-- custom where sql - kingsley  --> | 
|     <sql id="custom_where_sql"> | 
|   | 
|         <if test="record!=null"> | 
|             <if test="(record.id!=null and record.id!='') or  (record.id!='' and record.id==0)  "> | 
|                 and id = #{record.id} | 
|             </if> | 
|             <if test="(record.cName!=null and record.cName!='') or  (record.cName!='' and record.cName==0)  "> | 
|                 and instr(c_name, #{record.cName}) | 
|             </if> | 
|             <if test="(record.offsetAmount!=null and record.offsetAmount!='') or  (record.offsetAmount!='' and record.offsetAmount==0)  "> | 
|                 and offset_amount = #{record.offsetAmount} | 
|             </if> | 
|             <if test="(record.minAmount!=null and record.minAmount!='') or  (record.minAmount!='' and record.minAmount==0)  "> | 
|                 and min_amount = #{record.minAmount} | 
|             </if> | 
|             <if test="(record.couponBeginTime!=null and record.couponBeginTime!='') or  (record.couponBeginTime!='' and record.couponBeginTime==0)  "> | 
|                 and #{record.couponBeginTime} >= date_format(begin_time, '%Y-%m-%d') | 
|             </if> | 
|             <if test="(record.couponEndTime!=null and record.couponEndTime!='') or  (record.couponEndTime!='' and record.couponEndTime==0)  "> | 
|                 and date_format(end_time, '%Y-%m-%d') >= #{record.couponEndTime} | 
|             </if> | 
|             <if test="(record.maxQuantity!=null and record.maxQuantity!='') or  (record.maxQuantity!='' and record.maxQuantity==0)  "> | 
|                 and max_quantity = #{record.maxQuantity} | 
|             </if> | 
|             <if test="(record.getLimit!=null and record.getLimit!='') or  (record.getLimit!='' and record.getLimit==0)  "> | 
|                 and get_limit = #{record.getLimit} | 
|             </if> | 
|             <if test="(record.quantityReceive!=null and record.quantityReceive!='') or  (record.quantityReceive!='' and record.quantityReceive==0)  "> | 
|                 and quantity_receive = #{record.quantityReceive} | 
|             </if> | 
|             <if test="(record.couponStatus!=null and record.couponStatus!='') or  (record.couponStatus!='' and record.couponStatus==0)  "> | 
|                 and coupon_status = #{record.couponStatus} | 
|             </if> | 
|             <if test="(record.isOpen!=null and record.isOpen!='') or  (record.isOpen!='' and record.isOpen==0)  "> | 
|                 and is_open = #{record.isOpen} | 
|             </if> | 
|             <if test="(record.isAll!=null and record.isAll!='') or  (record.isAll!='' and record.isAll==0)  "> | 
|                 and is_all = #{record.isAll} | 
|             </if> | 
|             <if test="(record.productIds!=null and record.productIds!='') or  (record.productIds!='' and record.productIds==0)  "> | 
|                 and product_ids = #{record.productIds} | 
|             </if> | 
|             <if test="(record.attrIds!=null and record.attrIds!='') or  (record.attrIds!='' and record.attrIds==0)  "> | 
|                 and attr_ids = #{record.attrIds} | 
|             </if> | 
|             <if test="(record.tag!=null and record.tag!='') or  (record.tag!='' and record.tag==0)  "> | 
|                 and tag = #{record.tag} | 
|             </if> | 
|             <if test="(record.kind!=null and record.kind!='') or  (record.kind!='' and record.kind==0)  "> | 
|                 and kind = #{record.kind} | 
|             </if> | 
|             <if test="(record.shopId!=null and record.shopId!='') or  (record.shopId!='' and record.shopId==0)  "> | 
|                 and shop_id = #{record.shopId} | 
|             </if> | 
|   | 
|             <if test="record.companyId != null and record.companyId !='' "> | 
|                 and company_id = #{record.companyId} | 
|             </if> | 
|   | 
|   | 
|         </if> | 
|   | 
|     </sql> | 
|   | 
|     <!--  插入方法   --> | 
|     <insert id="insert" parameterType="com.matrix.system.shopXcx.bean.ShopCoupon" | 
|             useGeneratedKeys="true" keyProperty="item.id"> | 
|         INSERT INTO shop_coupon ( | 
|         <include refid="columns"></include> | 
|         ) | 
|         VALUES ( | 
|         <include refid="propertys"></include> | 
|         ) | 
|     </insert> | 
|   | 
|   | 
|     <!--  批量插入   --> | 
|     <insert id="batchInsert" parameterType="java.util.List"> | 
|         INSERT INTO shop_coupon ( | 
|         <include refid="columns"></include> | 
|         ) | 
|         VALUES | 
|         <foreach collection="list" item="item" index="index" separator=",">( | 
|             <include refid="propertys"></include> | 
|             ) | 
|         </foreach> | 
|     </insert> | 
|   | 
|   | 
|     <!--  根据Map更新 部分更新   --> | 
|     <update id="updateByMap" parameterType="java.util.HashMap"> | 
|         UPDATE shop_coupon | 
|         <set> | 
|             <if test="_parameter.containsKey('cName')"> | 
|                 c_name = #{cName}, | 
|             </if> | 
|             <if test="_parameter.containsKey('offsetAmount')"> | 
|                 offset_amount = #{offsetAmount}, | 
|             </if> | 
|             <if test="_parameter.containsKey('minAmount')"> | 
|                 min_amount = #{minAmount}, | 
|             </if> | 
|             <if test="_parameter.containsKey('beginTime')"> | 
|                 begin_time = #{beginTime}, | 
|             </if> | 
|             <if test="_parameter.containsKey('endTime')"> | 
|                 end_time = #{endTime}, | 
|             </if> | 
|             <if test="_parameter.containsKey('maxQuantity')"> | 
|                 max_quantity = #{maxQuantity}, | 
|             </if> | 
|             <if test="_parameter.containsKey('getLimit')"> | 
|                 get_limit = #{getLimit}, | 
|             </if> | 
|             <if test="_parameter.containsKey('quantityReceive')"> | 
|                 quantity_receive = #{quantityReceive}, | 
|             </if> | 
|             <if test="_parameter.containsKey('couponStatus')"> | 
|                 coupon_status = #{couponStatus}, | 
|             </if> | 
|             <if test="_parameter.containsKey('isOpen')"> | 
|                 is_open = #{isOpen}, | 
|             </if> | 
|             <if test="_parameter.containsKey('isAll')"> | 
|                 is_all = #{isAll}, | 
|             </if> | 
|             <if test="_parameter.containsKey('productIds')"> | 
|                 product_ids = #{productIds}, | 
|             </if> | 
|             <if test="_parameter.containsKey('attrIds')"> | 
|                 attr_ids = #{attrIds}, | 
|             </if> | 
|             <if test="_parameter.containsKey('tag')"> | 
|                 tag = #{tag}, | 
|             </if> | 
|             <if test="true"> | 
|                 update_time = now(), | 
|             </if> | 
|             <if test="_parameter.containsKey('kind')"> | 
|                 kind = #{kind}, | 
|             </if> | 
|             <if test="_parameter.containsKey('shopId')"> | 
|                 shop_id = #{shopId}, | 
|             </if> | 
|   | 
|             <if test="_parameter.containsKey('companyId')"> | 
|                 company_id = #{companyId}, | 
|             </if> | 
|         </set> | 
|         WHERE id=#{id} | 
|     </update> | 
|   | 
|   | 
|     <!--  根据对象更新 部分更新   --> | 
|     <update id="updateByModel" parameterType="Integer"> | 
|         UPDATE shop_coupon | 
|         <set> | 
|             <if test="record.cName != null and record.cName != '' "> | 
|                 c_name = #{record.cName}, | 
|             </if> | 
|             <if test="record.offsetAmount != null "> | 
|                 offset_amount = #{record.offsetAmount}, | 
|             </if> | 
|             <if test="record.minAmount != null "> | 
|                 min_amount = #{record.minAmount}, | 
|             </if> | 
|             <if test="record.beginTime != null "> | 
|                 begin_time = #{record.beginTime}, | 
|             </if> | 
|             <if test="record.endTime != null "> | 
|                 end_time = #{record.endTime}, | 
|             </if> | 
|             <if test="record.maxQuantity != null "> | 
|                 max_quantity = #{record.maxQuantity}, | 
|             </if> | 
|             <if test="record.getLimit != null "> | 
|                 get_limit = #{record.getLimit}, | 
|             </if> | 
|             <if test="record.quantityReceive != null "> | 
|                 quantity_receive = #{record.quantityReceive}, | 
|             </if> | 
|             <if test="record.couponStatus != null "> | 
|                 coupon_status = #{record.couponStatus}, | 
|             </if> | 
|             <if test="record.isOpen != null "> | 
|                 is_open = #{record.isOpen}, | 
|             </if> | 
|             <if test="record.isAll != null "> | 
|                 is_all = #{record.isAll}, | 
|             </if> | 
|             <if test="record.productIds != null and record.productIds != '' "> | 
|                 product_ids = #{record.productIds}, | 
|             </if> | 
|             <if test="record.shopId != null and record.shopId != '' "> | 
|                 shop_id = #{record.shopId}, | 
|             </if> | 
|             <if test="record.attrIds != null and record.attrIds != '' "> | 
|                 attr_ids = #{record.attrIds}, | 
|             </if> | 
|             <if test="record.tag != null"> | 
|                 tag = #{record.tag}, | 
|             </if> | 
|             <if test="record.kind != null"> | 
|                 kind = #{record.kind}, | 
|             </if> | 
|             <if test="true"> | 
|                 update_time = now() | 
|             </if> | 
|         </set> | 
|         WHERE id=#{record.id} | 
|     </update> | 
|   | 
|     <!-- 批量删除 --> | 
|     <delete id="deleteByIds" parameterType="java.util.List"> | 
|         delete from shop_coupon where id in | 
|         <foreach collection="list" index="index" item="item" open="(" | 
|                  separator="," close=")"> | 
|             #{item} | 
|         </foreach> | 
|     </delete> | 
|   | 
|     <!-- 根据id删除--> | 
|     <delete id="deleteById" parameterType="Integer"> | 
|         DELETE | 
|         FROM shop_coupon | 
|         where id = #{id} | 
|     </delete> | 
|   | 
|     <!-- 根据对象删除--> | 
|     <delete id="deleteByModel" parameterType="com.matrix.system.shopXcx.bean.ShopCoupon"> | 
|         DELETE FROM shop_coupon | 
|         <where> | 
|             <include refid="where_sql"></include> | 
|         </where> | 
|     </delete> | 
|   | 
|   | 
|     <!-- 分页查询 --> | 
|     <select id="selectInPage" resultMap="ShopCouponMap"> | 
|         select | 
|         ( select shop_name from sys_shop_info where id=shop_id )as shopName, | 
|         <include refid="columns"></include> | 
|         from shop_coupon | 
|         <where> | 
|             <include refid="custom_where_sql"></include> | 
|         </where> | 
|         <if test="pageVo !=null"><!-- 判断pageVo对象是否为空 --> | 
|             <if test="pageVo.sort !=null  and pageVo.order !=null"> | 
|                 order by | 
|                 ${pageVo.sort} ${pageVo.order} | 
|             </if> | 
|             <if test="pageVo.offset >=0  and pageVo.limit >0"> | 
|                 limit | 
|                 #{pageVo.offset},#{pageVo.limit} | 
|             </if> | 
|         </if> | 
|     </select> | 
|   | 
|     <!-- 查询总条数 --> | 
|     <select id="selectTotalRecord" parameterType="long" resultType="java.lang.Integer"> | 
|         select count(*) | 
|         from shop_coupon | 
|         <where> | 
|             <include refid="custom_where_sql"></include> | 
|         </where> | 
|     </select> | 
|   | 
|     <!-- 根据id查询--> | 
|     <select id="selectById" resultMap="ShopCouponMap"> | 
|         select | 
|         <include refid="columns"></include> | 
|         from shop_coupon | 
|         where id=#{id} | 
|     </select> | 
|   | 
|   | 
|     <!-- 根据id 锁表查询--> | 
|     <select id="selectForUpdate" resultMap="ShopCouponMap"> | 
|         select | 
|         <include refid="columns"></include> | 
|         from shop_coupon | 
|         where id=#{id} | 
|         for update | 
|     </select> | 
|   | 
|   | 
|     <!-- 根据对象查询--> | 
|     <select id="selectByModel" resultMap="ShopCouponMap"> | 
|         select | 
|         <include refid="columns"></include> | 
|         from shop_coupon | 
|         <where> | 
|             <include refid="where_sql"></include> | 
|         </where> | 
|     </select> | 
|   | 
|     <select id="selectUsableCoupon" resultMap="ShopCouponMap"> | 
|         select | 
|         <include refid="columns"></include> | 
|         from shop_coupon | 
|         where kind=1 and now() >= begin_time and end_time >= now() | 
|         and is_open = 1 and (max_quantity > quantity_receive or 1 > max_quantity) | 
|         <if test="!isNewPeople"> | 
|             and get_limit = 1 | 
|         </if> | 
|         and id not in ( | 
|         select c_id | 
|         from shop_coupon_record | 
|         where user_id = #{userId} | 
|         ) | 
|         <if test="pageVo != null"><!-- 判断pageVo对象是否为空 --> | 
|             <if test="pageVo.sort != null  and pageVo.order != null"> | 
|                 order by | 
|                 ${pageVo.sort} ${pageVo.order} | 
|             </if> | 
|             <if test="pageVo.offset >= 0  and pageVo.limit > 0"> | 
|                 limit | 
|                 #{pageVo.offset}, #{pageVo.limit} | 
|             </if> | 
|         </if> | 
|     </select> | 
|   | 
|     <!--微信返回map--> | 
|     <resultMap type="com.matrix.system.shopXcx.bean.ShopCoupon" id="WxShopCouponMap"> | 
|         <id property="id" column="id"/> | 
|         <result property="createBy" column="create_by"/> | 
|         <result property="createTime" column="create_time"/> | 
|         <result property="updateBy" column="update_by"/> | 
|         <result property="updateTime" column="update_time"/> | 
|         <result property="cName" column="c_name"/> | 
|         <result property="offsetAmount" column="offset_amount"/> | 
|         <result property="minAmount" column="min_amount"/> | 
|         <result property="beginTime" column="begin_time"/> | 
|         <result property="endTime" column="end_time"/> | 
|         <result property="maxQuantity" column="max_quantity"/> | 
|         <result property="getLimit" column="get_limit"/> | 
|         <result property="quantityReceive" column="quantity_receive"/> | 
|         <result property="couponStatus" column="coupon_status"/> | 
|         <result property="isOpen" column="is_open"/> | 
|         <result property="isAll" column="is_all"/> | 
|         <result property="productIds" column="product_ids"/> | 
|         <result property="attrIds" column="attr_ids"/> | 
|         <result property="canReceive" column="can_receive"/> | 
|         <result property="tag" column="tag"/> | 
|         <result property="isUsing" column="is_using"/> | 
|         <result property="kind" column="kind"/> | 
|     </resultMap> | 
|   | 
|     <select id="selectUsableCouponByProductInfo" resultMap="WxShopCouponMap"> | 
|         select sc.*, | 
|         if(isnull(scr.id), 1, 2) can_receive | 
|         from shop_coupon sc left join shop_coupon_record scr | 
|         on sc.id = scr.c_id and user_id = #{userId} | 
|         where sc.kind=1 and now() >= begin_time and end_time >= now() and is_open = 1 | 
|         and (is_all = 1 or | 
|         ( | 
|         concat('%,', product_ids, ',%') like concat('%,', #{productId}, ',%') | 
|         <if test="attrIds != null and attrIds.size() > 0"> | 
|             or | 
|             <foreach collection="attrIds" item="item" open="(" close=")" separator="or"> | 
|                 concat('%,', attr_ids, ',%') like concat('%,', #{item}, ',%') | 
|             </foreach> | 
|         </if> | 
|         ) | 
|         ) | 
|         and ( | 
|         (if(isnull(scr.id), 1 , 2) = 1 and (max_quantity > quantity_receive or 1 > max_quantity)) | 
|         or (if(isnull(scr.id), 1 , 2) = 2 and user_id = #{userId} and is_using = 2) | 
|         ) | 
|     </select> | 
|   | 
|     <select id="getCouponByTitle" resultMap="ShopCouponMap"> | 
|         select * | 
|         from shop_coupon | 
|         where c_name = #{title} and company_id=#{companyId} | 
|     </select> | 
|   | 
|     <select id="selectHdListByProductId" resultMap="WxShopCouponMap"> | 
|         select * | 
|         from shop_coupon | 
|         where | 
|         kind=2 and now() >= begin_time and end_time >= now() | 
|         and is_open = 1 | 
|         and (is_all = 1 or | 
|         ( | 
|         concat('%,', product_ids, ',%') like concat('%,', #{productId}, ',%') | 
|         <if test="attrIds != null and attrIds.size() > 0"> | 
|             or | 
|             <foreach collection="attrIds" item="item" open="(" close=")" separator="or"> | 
|                 concat('%,', attr_ids, ',%') like concat('%,', #{item}, ',%') | 
|             </foreach> | 
|         </if> | 
|         ) | 
|         ) | 
|     </select> | 
|   | 
|   | 
|     <!--根据优惠券标签查询优惠券列表--> | 
|     <select id="selectCouponListByTag" resultMap="WxShopCouponMap"> | 
|         select sc.*, | 
|         if(isnull(scr.id), 1, 2) can_receive, | 
|         ifnull(scr.is_using, 0) is_using | 
|         from shop_coupon sc left join shop_coupon_record scr | 
|         on sc.id = scr.c_id and user_id = #{userId} | 
|         where sc.shop_id=#{shopId} and sc.kind=1  and now() >= begin_time and end_time >= now() and is_open = 1 | 
|         <!--<if test="!isNewPeople">--> | 
|         <!--and get_limit = 1--> | 
|         <!--</if>--> | 
|         and tag = #{tag} | 
|         and ( | 
|         if(isnull(scr.id), 1 , 2) = 1 | 
|         or (if(isnull(scr.id), 1 , 2) = 2 and user_id = #{userId}) | 
|         ) | 
|     </select> | 
|   | 
|     <!-- 根据状态批量更新 --> | 
|     <update id="updateStateByStateAndIds"> | 
|         update shop_coupon | 
|         <set> | 
|             <if test="state == 1 or state == 2"> | 
|                 is_open = #{state} | 
|             </if> | 
|         </set> | 
|         where id in | 
|         <foreach collection="list" index="index" item="item" open="(" | 
|                  separator="," close=")"> | 
|             #{item} | 
|         </foreach> | 
|     </update> | 
|   | 
|     <!--更新所有优惠券状态(1=未开始,2=进行中,3=已结束)--> | 
|     <update id="updateAllCouponStatus"> | 
|         update shop_coupon | 
|         set coupon_status = if(begin_time > now(), 1, if(now() > end_time, 3, 2)) | 
|     </update> | 
|   | 
|     <!--根据优惠券ID更新优惠券状态(1=未开始,2=进行中,3=已结束)--> | 
|     <update id="updateCouponStatusById"> | 
|         update shop_coupon | 
|         set coupon_status = if(begin_time > now(), 1, if(now() > end_time, 3, 2)) | 
|         where id = #{id} | 
|     </update> | 
|   | 
|     <!-- 根据ID、用户ID查询--> | 
|     <select id="selectByIdAndUserId" resultMap="ShopCouponMap"> | 
|         select coupon.* | 
|         from shop_coupon coupon, | 
|              shop_coupon_record coupon_record | 
|         where coupon.id = coupon_record.c_id | 
|           and coupon_record.user_id = #{userId} | 
|           and now() >= coupon.begin_time | 
|           and coupon.end_time >= now() | 
|           and coupon.is_open = 1 | 
|           and coupon.id = #{id} | 
|           and coupon_record.is_using = 2 | 
|     </select> | 
|   | 
| </mapper> |