<?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.ssm.manage.mapper.CourseAccountMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.CourseAccount">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
      <result column="another_name" jdbcType="VARCHAR" property="anotherName"/>
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="card" jdbcType="VARCHAR" property="card" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="course_num" jdbcType="VARCHAR" property="courseNum" />
    <result column="course_project_id" jdbcType="BIGINT" property="courseProjectId" />
    <result column="course_project_name" jdbcType="VARCHAR" property="courseProjectName" />
    <result column="course_project_money" jdbcType="DECIMAL" property="courseProjectMoney" />
    <result column="course_count" jdbcType="INTEGER" property="courseCount" />
    <result column="course_standard_count" jdbcType="INTEGER" property="courseStandardCount" />
    <result column="course_standard_money" jdbcType="DECIMAL" property="courseStandardMoney" />
    <result column="last_consume_time" jdbcType="TIMESTAMP" property="lastConsumeTime" />
    <result column="course_rest_count" jdbcType="INTEGER" property="courseRestCount" />
    <result column="course_consume_count" jdbcType="INTEGER" property="courseConsumeCount" />
    <result column="course_type" jdbcType="VARCHAR" property="courseType" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_id" jdbcType="BIGINT" property="updateId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />

    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="course_rest_price" jdbcType="DECIMAL" property="courseRestPrice" />
    <result column="course_consume_price" jdbcType="DECIMAL" property="courseConsumePrice" />


    <result column="course_project_moneys" jdbcType="DECIMAL" property="courseProjectMoneys" />
    <result column="course_counts" jdbcType="INTEGER" property="courseCounts" />

      <result column="unrecorded_amount" jdbcType="DECIMAL" property="unrecordedAmount" />
      <result column="unrecorded_number" jdbcType="INTEGER" property="unrecordedNumber" />
      <result column="promotion_doc_id" jdbcType="INTEGER" property="promotionDocId" />

      <result column="cancel_money" jdbcType="DECIMAL" property="cancelMoney" />
      <result column="cancel_num" jdbcType="INTEGER" property="cancelNum" />
      <result column="expenditure_money" jdbcType="DECIMAL" property="expenditureMoney" />
  </resultMap>


  <resultMap id="MemberCourse" type="com.ssm.manage.response.CourseAccountResponse">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
      <result column="another_name" jdbcType="VARCHAR" property="anotherName"/>
    <result column="member_id" jdbcType="BIGINT" property="memberId" />
    <result column="member_name" jdbcType="VARCHAR" property="memberName" />
    <result column="card" jdbcType="VARCHAR" property="card" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="store_name" jdbcType="VARCHAR" property="storeName" />
    <result column="course_num" jdbcType="VARCHAR" property="courseNum" />
    <result column="course_project_id" jdbcType="BIGINT" property="courseProjectId" />
    <result column="course_project_name" jdbcType="VARCHAR" property="courseProjectName" />
    <result column="course_project_money" jdbcType="DECIMAL" property="courseProjectMoney" />
    <result column="course_count" jdbcType="INTEGER" property="courseCount" />
    <result column="course_standard_count" jdbcType="INTEGER" property="courseStandardCount" />
    <result column="course_standard_money" jdbcType="DECIMAL" property="courseStandardMoney" />
    <result column="last_consume_time" jdbcType="TIMESTAMP" property="lastConsumeTime" />
    <result column="course_rest_count" jdbcType="INTEGER" property="courseRestCount" />
    <result column="course_consume_count" jdbcType="INTEGER" property="courseConsumeCount" />
    <result column="course_type" jdbcType="VARCHAR" property="courseType" />
    <result column="state" jdbcType="INTEGER" property="state" />
    <result column="end_time" jdbcType="DATE" property="endTime" />
    <result column="update_by" jdbcType="VARCHAR" property="updateBy" />
    <result column="update_id" jdbcType="BIGINT" property="updateId" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="member_course_money" jdbcType="DECIMAL" property="memberCourseMoney" />
    <result column="price" jdbcType="DECIMAL" property="price" />
    <result column="course_rest_price" jdbcType="DECIMAL" property="courseRestPrice" />
    <result column="course_consume_price" jdbcType="DECIMAL" property="courseConsumePrice" />
      <result column="course_project_moneys" jdbcType="DECIMAL" property="courseProjectMoneys" />
      <result column="course_counts" jdbcType="INTEGER" property="courseCounts" />
      <result column="unrecorded_amount" jdbcType="DECIMAL" property="unrecordedAmount" />
      <result column="unrecorded_number" jdbcType="INTEGER" property="unrecordedNumber" />
      <result column="promotion_doc_id" jdbcType="INTEGER" property="promotionDocId" />
      <result column="cancel_money" jdbcType="DECIMAL" property="cancelMoney" />
      <result column="cancel_num" jdbcType="INTEGER" property="cancelNum" />
      <result column="expenditure_money" jdbcType="DECIMAL" property="expenditureMoney" />
  </resultMap>

  <sql id="baseColumnList">
    id,
    member_id,
    member_name,
    card,
    store_id,
    store_name,
    course_num,
    course_project_id,
    course_project_name,
    course_project_money,
    course_count,
    course_standard_count,
    course_standard_money,
    last_consume_time,
    course_rest_count,
    course_consume_count,
    course_type,
    state,
    another_name,
    end_time,
    update_by,
    update_id,
    update_time,price,course_rest_price,course_consume_price,course_project_moneys,
    course_counts,unrecorded_amount,unrecorded_number,promotion_doc_id,cancel_money,cancel_num
  </sql>
    <sql id="baseColumnList1">
        id,
    member_id,
    member_name,
    card,
    store_id,
    store_name,
    course_num,
    course_project_id,
    course_project_name,
    course_project_money,
    course_count,
    course_standard_count,
    course_standard_money,
    last_consume_time,
    course_rest_count,
    course_consume_count,
    course_type,
    state,
    end_time,
    update_by,
    update_id,
    update_time,price,course_rest_price,course_consume_price,course_project_moneys,
    course_counts,unrecorded_amount,unrecorded_number,promotion_doc_id,cancel_money,cancel_num
    </sql>

  <insert id="insertBatch" parameterType="java.util.ArrayList">
    insert into course_account(<include refid="baseColumnList"/>)values
    <foreach collection="list" index="index" separator="," item="item">
      (
      #{item.id},
      #{item.memberId},
        #{item.memberName},
        #{item.card},
        #{item.storeId},
        #{item.storeName},
        #{item.courseNum},
        #{item.courseProjectId},
        #{item.courseProjectName},
        #{item.courseProjectMoney},
        #{item.courseCount},
        #{item.courseStandardCount},
        #{item.courseStandardMoney},
        #{item.lastConsumeTime},
        #{item.courseRestCount},
        #{item.courseConsumeCount},
        #{item.courseType},
        #{item.state},
        #{item.anotherName},
        #{item.endTime},
      #{item.updateBy},
      #{item.updateId},
      #{item.updateTime},
      #{item.price},
      #{item.courseRestPrice},
      #{item.courseConsumePrice},
        #{item.courseProjectMoneys},
        #{item.courseCounts},
       #{item.unrecordedAmount},
        #{item.unrecordedNumber},
        #{item.promotionDocId},
       #{item.cancelMoney},
        #{item.cancelNum}
      )
    </foreach>
  </insert>

  <select id="memberCourse" parameterType="com.ssm.manage.query.CourseAccountRequest" resultMap="MemberCourse">
    select member_id, member_name, sum(course_project_money) as member_course_money from course_account where store_id = #{storeId, jdbcType = BIGINT}
    <if test="state != null">
      and state = #{state, jdbcType = INTEGER}
    </if>
    GROUP BY member_id
  </select>
<!--    and course_type not in (1,2)-->
  <select id="selectRefundList" parameterType="com.ssm.manage.query.CourseAccountRequest" resultMap="BaseResultMap">
   select <include refid="baseColumnList1"/> from course_account where
   store_id in
   <foreach collection="storeIds" item="id" index="index" open="(" close=")" separator=",">
    #{id}
  </foreach>
 and member_id = #{memberId, jdbcType = BIGINT} and course_rest_count > 0
   union
    select
    a.id,
    a.member_id,
    a.member_name,
    a.card,
    a.store_id,
    a.store_name,
    a.course_num,
    a.course_project_id,
    a.course_project_name,
    a.course_project_money,
    a.course_count,
    a.course_standard_count,
    a.course_standard_money,
    a.last_consume_time,
    a.course_rest_count,
    a.course_consume_count,
    a.course_type,
    a.state,
    a.end_time,
    a.update_by,
    a.update_id,
    a.update_time,
    a.price,
    a.course_rest_price,
    a.course_consume_price,
    a.course_project_moneys,
    a.course_counts,a.unrecorded_amount,a.unrecorded_number,a.promotion_doc_id,a.cancel_money,a.cancel_num from course_account a, debt_list b
    where a.id = b.account_id and b.state = 0
    and a.store_id  in
    <foreach collection="storeIds" item="id" index="index" open="(" close=")" separator=",">
      #{id}
    </foreach> and a.member_id = #{memberId, jdbcType = BIGINT} and a.course_rest_count = 0 and course_type not in (1,2)
  </select>

    <select id="selectMessage" parameterType="com.ssm.manage.query.CourseAccountRequest" resultMap="MemberCourse">
        select<include refid="baseColumnList"/> from course_account where course_project_id = #{courseProjectId, jdbcType = BIGINT}
    </select>
    <select id="selectByExport" resultMap="BaseResultMap">
        select * from course_account <where> 1=1
        <if test="memberId != null">and member_id = #{memberId}</if>
        <if test="storeIds != null and storeIds.size()>0">and store_id in <foreach collection="storeIds" open="(" close=")" separator="," item="item">#{item}</foreach>
        </if>
        <if test="courseRestCount != null and courseRestCount==1">and course_rest_count > 0</if>
        <if test="courseRestCount != null and courseRestCount==0">and course_rest_count = 0</if>
    </where>
    </select>
</mapper>