<?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.ruoyi.voluntary.mapper.BusiCardsMapper">

  <resultMap type="BusiCards" id="BusiCardsResult">
    <result property="cardId" column="card_id" />
    <result property="cardNo" column="card_no" />
    <result property="nickName" column="nick_name" />
    <result property="name" column="name" />
    <result property="mobile" column="mobile" />
    <result property="score" column="score" />
    <result property="groupId" column="group_id" />
    <result property="schoolId" column="school_id" />
    <result property="schoolName" column="school_name" />
    <result property="distributeType" column="distribute_type" />
    <result property="distributeLocation" column="distribute_location" />
    <result property="distributeSchoolId" column="distribute_school_id" />
    <result property="distributeSchoolName" column="distribute_school_name" />
    <result property="distributeExamType" column="distribute_exam_type" />
    <result property="customSchoolName" column="custom_school_name" />
    <result property="customClassName" column="custom_class_name" />
    <result property="gradeId" column="grade_id" />
    <result property="gradeName" column="grade_name" />
    <result property="classId" column="class_id" />
    <result property="className" column="class_name" />
    <result property="year" column="year" />
    <result property="endYear" column="end_year" />
    <result property="type" column="type" />
    <result property="agentId" column="agent_id" />
    <result property="agentName" column="agent_name" />
    <result property="password" column="password" />
    <result property="status" column="status" />
    <result property="payStatus" column="pay_status" />
    <result property="isPay" column="is_pay" />
    <result property="isRefund" column="is_refund" />
    <result property="isSettlement" column="is_settlement" />
    <result property="isUsed" column="is_used" />
    <result property="isSend" column="is_send" />
    <result property="closeId" column="close_id" />
    <result property="openId" column="open_id" />
    <result property="remark" column="remark" />
    <result property="distributeTime" column="distribute_time" />
    <result property="outDate" column="out_date" />
    <result property="payTime" column="pay_time" />
    <result property="activeTime" column="active_time" />
    <result property="refundTime" column="refund_time" />
    <result property="closeTime" column="close_time" />
    <result property="createTime" column="create_time" />
    <result property="updateTime" column="update_time" />
  </resultMap>

    <sql id="selectBusiCardsVo">
        select card_id, card_no, nick_name, school_id, distribute_type, distribute_location, distribute_school_id, distribute_exam_type, grade_id, class_id, year, type, agent_id, password, status, is_pay, is_refund, is_settlement, is_used, is_send, close_id, open_id, remark, distribute_time, out_date, pay_time, active_time, refund_time, close_time, create_time, update_time from b_busi_cards
    </sql>

    <select id="selectBusiCardsList" parameterType="BusiCards" resultMap="BusiCardsResult">
        select t1.card_id, t1.card_no, t1.nick_name,t1.school_id,t2.name school_name,
               t7.nick_name name,t7.phonenumber mobile,t7.score score,t7.group_id group_id,
               t1.distribute_type, t1.distribute_location, t1.distribute_school_id, t3.name distribute_school_name, t1.distribute_exam_type ,t1.grade_id, t4.name grade_name, t1.class_id, t5.name class_name,
               t1.year,t1.end_year,t1.type,t1.agent_id,t6.name agent_name,t1.password, t1.status,
               t1.pay_status,t1.is_pay,t1.is_refund, t1.is_settlement, t1.is_used, t1.is_send, t1.close_id, t1.open_id,
               t1.remark, t1.distribute_time,t1.out_date, t1.pay_time,
               t1.active_time, t1.refund_time, t1.close_time, t1.create_time, t1.update_time, t7.custom_school_name, t7.custom_class_name
               from b_busi_cards t1 left join b_busi_schools t2 on t1.school_id = t2.id
               left join b_busi_schools t3 on t1.distribute_school_id = t3.id
               left join b_busi_grade t4 on t1.grade_id = t4.id
               left join b_busi_classes t5 on t1.class_id=t5.class_id
               left join b_busi_agents t6 on t1.agent_id = t6.agent_id
               left join sys_user t7 on t7.card_id = t1.card_id
        <where>
            <if test="cardNo != null  and cardNo != ''"> and t1.card_no like concat('%', #{cardNo}, '%')</if>
            <if test="nickName != null  and nickName != ''"> and t1.nick_name like concat('%', #{nickName}, '%')</if>
            <if test="schoolId != null "> and t1.school_id = #{schoolId}</if>
            <if test="distributeType != null "> and t1.distribute_type = #{distributeType}</if>
            <if test="distributeLocation != null "> and t1.distribute_location = #{distributeLocation}</if>
            <if test="distributeSchoolId != null "> and t1.distribute_school_id = #{distributeSchoolId}</if>
            <if test="distributeExamType != null "> and t1.distribute_exam_type = #{distributeExamType}</if>
            <if test="gradeId != null "> and t1.grade_id = #{gradeId}</if>
            <if test="classId != null "> and t1.class_id = #{classId}</if>
            <if test="year != null "> and t1.year = #{year}</if>
            <if test="endYear != null "> and t1.end_year = #{endYear}</if>
            <if test="type != null "> and t1.type = #{type}</if>
            <if test="agentId != null "> and t1.agent_id = #{agentId}</if>
            <if test="password != null  and password != ''"> and t1.password = #{password}</if>
            <choose>
                <when test="status != null and status == -1">
                    and (t1.status =0 or t1.status = 1)
                </when>
                <otherwise>
                    <if test="status != null "> and t1.status = #{status}</if>
                </otherwise>
            </choose>
            <if test="payStatus != null "> and t1.pay_status = #{payStatus}</if>
            <if test="isPay != null "> and t1.is_pay = #{isPay}</if>
            <if test="isRefund != null "> and t1.is_refund = #{isRefund}</if>
            <if test="isSettlement != null "> and t1.is_settlement = #{isSettlement}</if>
            <if test="isUsed != null "> and t1.is_used = #{isUsed}</if>
            <if test="isSend != null "> and t1.is_send = #{isSend}</if>
            <if test="closeId != null "> and t1.close_id = #{closeId}</if>
            <if test="openId != null "> and t1.open_id = #{openId}</if>
            <if test="distributeTime != null "> and t1.distribute_time = #{distributeTime}</if>
            <if test="outDate != null "> and t1.out_date = #{outDate}</if>
            <if test="payTime != null "> and t1.pay_time = #{payTime}</if>
            <if test="activeTime != null "> and t1.active_time = #{activeTime}</if>
            <if test="refundTime != null "> and t1.refund_time = #{refundTime}</if>
            <if test="closeTime != null "> and t1.close_time = #{closeTime}</if>
            <if test="params.beginDistributeTime != null and params.beginDistributeTime != ''">
                AND date_format(t1.distribute_time,'%y%m%d') &gt;= date_format(#{params.beginDistributeTime},'%y%m%d')
            </if>
            <if test="params.endDistributeTime != null and params.endDistributeTime != ''">
                AND date_format(t1.distribute_time,'%y%m%d') &lt;= date_format(#{params.endDistributeTime},'%y%m%d')
            </if>
            <if test="params.beginPayTime != null and params.beginPayTime != ''">
                AND date_format(t1.pay_time,'%y%m%d') &gt;= date_format(#{params.beginPayTime},'%y%m%d')
            </if>
            <if test="params.endPayTime != null and params.endPayTime != ''">
                AND date_format(t1.pay_time,'%y%m%d') &lt;= date_format(#{params.endPayTime},'%y%m%d')
            </if>
            <if test="pro != null and pro != ''"> and t2.pro = #{pro}</if>
            <if test="city != null and city != ''"> and t2.city = #{city}</if>
            <if test="area != null and area != ''"> and t2.area = #{area}</if>
            <if test="schoolName != null and schoolName != ''"> and t2.name like concat('%', #{schoolName} ,'%')</if>
            <if test="className != null and className != ''"> and t5.name like concat('%', #{className} ,'%')</if>
            <if test="agentName != null and agentName != ''"> and t6.name like concat('%', #{agentName} ,'%')</if>
            <if test="name != null and name != ''"> and t7.nick_name like concat('%', #{name} ,'%')</if>
            <if test="mobile != null and mobile != ''"> and t7.phonenumber like concat('%', #{mobile} ,'%')</if>
            <choose>
                <when test="startCard != null and startCard != '' and endCard != null and endCard != ''">
                    and t1.card_no >= #{startCard} and #{endCard} >= t1.card_no
                </when>
                <when test="startCard != null and startCard != ''">
                    and t1.card_no like concat('%', #{startCard},'%')
                </when>
                <when test="endCard != null and endCard != ''">
                    and t1.card_no  like concat('%', #{endCard} ,'%')
                </when>
                <otherwise>
                </otherwise>
            </choose>

        </where>
        order by t1.card_id desc
    </select>

    <select id="selectBusiCardsClosed"  resultMap="BusiCardsResult">
        <include refid="selectBusiCardsVo"/>
        where status=10
    </select>

    <select id="selectBusiCardsByCardId" parameterType="Long" resultMap="BusiCardsResult">
        <include refid="selectBusiCardsVo"/>
        where card_id = #{cardId}
    </select>

    <select id="selectBusiCardsByCardNo" parameterType="String" resultMap="BusiCardsResult">
        <include refid="selectBusiCardsVo"/>
        where card_no = #{cardNo}
    </select>

    <select id="selectOneECard" resultMap="BusiCardsResult">
        SELECT * from b_busi_cards
        where is_send=0 and card_no like '0%'
        and (LENGTH(card_no)=6 or LENGTH(card_no)=8) limit 1
    </select>

    <select id="selectBusiCardsByCardNos" resultMap="BusiCardsResult">
        <include refid="selectBusiCardsVo"/>
        where 1=1 and card_no in
        <foreach collection="list"  item="item"  open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="getMaxCardNo" parameterType="Integer" resultType="Integer">
        select IFNULL(max(card_no),0) from b_busi_cards where type = #{type}
    </select>

    <select id="selectBusiOutTimeCardsList" parameterType="BusiCards" resultMap="BusiCardsResult">
        select card_id, card_no, nick_name, school_id, distribute_type, distribute_location, distribute_school_id, distribute_exam_type, grade_id, class_id, year, type, agent_id, password, status, is_pay, is_refund, is_settlement, is_used, is_send, close_id, open_id, remark, distribute_time, out_date, pay_time, active_time, refund_time, close_time, create_time, update_time from b_busi_cards
        where 1=1 and (status=2)
        <if test="endOutTime != null and endOutTime != ''">
            and #{endOutTime} > out_date
        </if>
    </select>

    <insert id="updateBusiCardsBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="cardId">
        INSERT INTO b_busi_cards(
        card_id, distribute_type, distribute_location, distribute_school_id, distribute_exam_type, year, agent_id, status, is_pay, is_refund, is_settlement,
        close_id, open_id, distribute_time,
        out_date, pay_time, refund_time, close_time, update_time
        ) VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (#{item.cardId}, #{item.distributeType}, #{item.distributeLocation}, #{item.distributeSchoolId}, #{item.distributeExamType}, #{item.year}, #{item.agentId}, #{item.status},
            #{item.isPay}, #{item.isRefund}, #{item.isSettlement}, #{item.closeId}, #{item.openId}, #{item.distributeTime},
            #{item.outDate}, #{item.payTime}, #{item.refundTime}, #{item.closeTime}, #{item.updateTime})
        </foreach>
        ON DUPLICATE KEY UPDATE distribute_type = VALUES(distribute_type),  distribute_location = VALUES(distribute_location),  distribute_school_id = VALUES(distribute_school_id),  distribute_exam_type = VALUES(distribute_exam_type),  year = VALUES(year), agent_id = VALUES(agent_id),
        status = VALUES(status), is_pay = VALUES(is_pay), is_refund = VALUES(is_refund), is_settlement = VALUES(is_settlement),
        close_id = VALUES(close_id), open_id = VALUES(open_id), distribute_time = VALUES(distribute_time), out_date = VALUES(out_date),
        pay_time = VALUES(pay_time), refund_time = VALUES(refund_time), close_time = VALUES(close_time), update_time = VALUES(update_time)
    </insert>

    <select id="getIdsByCardAndRole" parameterType="java.util.Map" resultMap="BusiCardsResult">
        <include refid="selectBusiCardsVo"/>
        <!-- 莫：初始化时创建的卡status <=1,分配后status=1 0未分配，1已分配，2已激活 -->
        where status &lt;=#{status} and card_no>=#{startCard} and #{endCard} >= card_no
        <!--重新分配卡：只需要查询status=0的； 分配卡：status=0和agentId is null-->
        <if test="againDistributionCard == null or againDistributionCard == 0">
            and agent_id is null
        </if>
    </select>
    <select id="staticByAgent" resultType="java.util.Map">
        select t1.agent_id agentId,t2.name agentName,
        count(t1.distribute_time) opened,
        count(case when t1.pay_status = 1 then 1 end) payed,
        count(case when t1.pay_status = 0 then 1 end) nopay,
        count(case when t1.pay_status = 2 then 1 end) refund,
        count(case when t1.status = 2 then 1 end) active,
        count(case when t1.status = 1  then 1 end) inactive
        from b_busi_cards t1 left join b_busi_agents t2 on t1.agent_id = t2.agent_id
        <where>
          <if test="type != null "> and t1.type = #{type}</if>
          <if test="agentId != null "> and t1.agent_id = #{agentId}</if>
          <if test="params.beginDistributeTime != null and params.beginDistributeTime != ''">
              AND date_format(t1.distribute_time,'%y%m%d') &gt;= date_format(#{params.beginDistributeTime},'%y%m%d')
          </if>
          <if test="params.endDistributeTime != null and params.endDistributeTime != ''">
              AND date_format(t1.distribute_time,'%y%m%d') &lt;= date_format(#{params.endDistributeTime},'%y%m%d')
          </if>
       </where>
       group by t1.agent_id
       order by t1.agent_id
    </select>

    <insert id="insertBatchCardList" parameterType="java.util.List">
        insert into b_busi_cards(type,card_no,password,create_time, update_time)
        VALUES
        <foreach collection ="list" item="item" separator =",">
            (#{item.type},#{item.cardNo},#{item.password},#{item.createTime}, #{item.updateTime})
        </foreach>
    </insert>

    <insert id="insertBusiCards" parameterType="BusiCards" useGeneratedKeys="true" keyProperty="cardId">
        insert into b_busi_cards
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="cardNo != null">card_no,</if>
            <if test="nickName != null and nickName != ''">nick_name,</if>
            <if test="schoolId != null">school_id,</if>
            <if test="distributeType != null">distribute_type,</if>
            <if test="distributeLocation != null">distribute_location,</if>
            <if test="distributeSchoolId != null">distribute_school_id,</if>
            <if test="distributeExamType != null">distribute_exam_type,</if>
            <if test="gradeId != null">grade_id,</if>
            <if test="classId != null">class_id,</if>
            <if test="year != null">year,</if>
            <if test="type != null">type,</if>
            <if test="agentId != null">agent_id,</if>
            <if test="password != null">password,</if>
            <if test="status != null">status,</if>
            <if test="isPay != null">is_pay,</if>
            <if test="isRefund != null">is_refund,</if>
            <if test="isSettlement != null">is_settlement,</if>
            <if test="isUsed != null">is_used,</if>
            <if test="isSend != null">is_send,</if>
            <if test="closeId != null">close_id,</if>
            <if test="openId != null">open_id,</if>
            <if test="remark != null">remark,</if>
            <if test="distributeTime != null">distribute_time,</if>
            <if test="outDate != null">out_date,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="activeTime != null">active_time,</if>
            <if test="refundTime != null">refund_time,</if>
            <if test="closeTime != null">close_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="cardNo != null">#{cardNo},</if>
            <if test="nickName != null and nickName != ''">#{nickName},</if>
            <if test="schoolId != null">#{schoolId},</if>
            <if test="distributeType != null">#{distributeType},</if>
            <if test="distributeLocation != null">#{distributeLocation},</if>
            <if test="distributeSchoolId != null">#{distributeSchoolId},</if>
            <if test="distributeExamType != null">#{distributeExamType},</if>
            <if test="gradeId != null">#{gradeId},</if>
            <if test="classId != null">#{classId},</if>
            <if test="year != null">#{year},</if>
            <if test="type != null">#{type},</if>
            <if test="agentId != null">#{agentId},</if>
            <if test="password != null">#{password},</if>
            <if test="status != null">#{status},</if>
            <if test="isPay != null">#{isPay},</if>
            <if test="isRefund != null">#{isRefund},</if>
            <if test="isSettlement != null">#{isSettlement},</if>
            <if test="isUsed != null">#{isUsed},</if>
            <if test="isSend != null">#{isSend},</if>
            <if test="closeId != null">#{closeId},</if>
            <if test="openId != null">#{openId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="distributeTime != null">#{distributeTime},</if>
            <if test="outDate != null">#{outDate},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="activeTime != null">#{activeTime},</if>
            <if test="refundTime != null">#{refundTime},</if>
            <if test="closeTime != null">#{closeTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateBusiCards" parameterType="BusiCards">
        update b_busi_cards
        <trim prefix="SET" suffixOverrides=",">
            <if test="cardNo != null">card_no = #{cardNo},</if>
            <if test="nickName != null and nickName != ''">nick_name = #{nickName},</if>
            <if test="schoolId != null">school_id = #{schoolId},</if>
            <if test="distributeType != null">distribute_type = #{distributeType},</if>
            <if test="distributeLocation != null">distribute_location = #{distributeLocation},</if>
            <if test="distributeSchoolId != null">distribute_school_id = #{distributeSchoolId},</if>
            <if test="distributeExamType != null">distribute_exam_type = #{distributeExamType},</if>
            <if test="gradeId != null">grade_id = #{gradeId},</if>
            <if test="classId != null">class_id = #{classId},</if>
            <if test="year != null">year = #{year},</if>
            <if test="endYear != null">end_year = #{endYear},</if>
            <if test="type != null">type = #{type},</if>
            <if test="agentId != null">agent_id = #{agentId},</if>
            <if test="password != null">password = #{password},</if>
            <if test="status != null">status = #{status},</if>
            <if test="isPay != null">is_pay = #{isPay},</if>
            <if test="isRefund != null">is_refund = #{isRefund},</if>
            <if test="isSettlement != null">is_settlement = #{isSettlement},</if>
            <if test="isUsed != null">is_used = #{isUsed},</if>
            <if test="isSend != null">is_send = #{isSend},</if>
            <if test="closeId != null">close_id = #{closeId},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="distributeTime != null">distribute_time = #{distributeTime},</if>
            <if test="outDate != null">out_date = #{outDate},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="activeTime != null">active_time = #{activeTime},</if>
            <if test="refundTime != null">refund_time = #{refundTime},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where card_id = #{cardId}
    </update>

    <update id="batchUpdate" parameterType="BusiCards">
         update b_busi_cards
        <trim prefix="SET" suffixOverrides=",">
            -- 已关卡的不能重新关闭
            <if test="params.custSet != null and params.custSet != ''">${params.custSet},</if>
            <if test="status != null">status = #{status},</if>
            <if test="payStatus != null">pay_status = #{payStatus},</if>
            <if test="isPay != null">is_pay = #{isPay},</if>
            <if test="isRefund != null">is_refund = #{isRefund},</if>
            <if test="closeId != null">close_id = #{closeId},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="distributeTime != null">distribute_time = #{distributeTime},</if>
            <if test="outDate != null">out_date = #{outDate},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="refundTime != null">refund_time = #{refundTime},</if>
            <if test="closeTime != null">close_time = #{closeTime},</if>
            <if test="days != null">
                out_date=DATE_ADD(out_date,INTERVAL #{days} day),
            </if>
        </trim>
        where card_id in
          <foreach collection="cardIds"  item="item"  open="(" separator="," close=")">
            #{item}
        </foreach>
        <if test="params.custWhere != null and params.custWhere != ''">${params.custWhere}</if>
    </update>

    <delete id="deleteBusiCardsByCardId" parameterType="Long">
        delete from b_busi_cards where card_id = #{cardId}
    </delete>

    <delete id="deleteBusiCardsByCardIds" parameterType="String">
        delete from b_busi_cards where card_id in
        <foreach item="cardId" collection="array" open="(" separator="," close=")">
            #{cardId}
        </foreach>
    </delete>
</mapper>
