<?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.PayListMapper">
  <resultMap id="BaseResultMap" type="com.ssm.manage.entity.PayList">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
      <result column="is_cancel" property="isCancel" jdbcType="INTEGER"/>
      <result column="doc_id" jdbcType="BIGINT" property="docId" />
    <result column="pay_id" jdbcType="VARCHAR" property="payId" />
    <result column="pay_name" jdbcType="VARCHAR" property="payName" />
    <result column="pay_money" jdbcType="DECIMAL" property="payMoney" />
    <result column="type" jdbcType="VARCHAR" property="type" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
      <result column="pay_date" jdbcType="TIMESTAMP" property="payDate"/>
  </resultMap>

  <insert id="addPayList" parameterType="java.util.List">
    insert into pay_list (id,doc_id,pay_id,pay_name,pay_money,type,store_id,pay_date,is_cancel)values
    <foreach collection="payList" item="pay" separator=",">
      (#{pay.id,jdbcType = BIGINT},
       #{pay.docId,jdbcType = VARCHAR},
       #{pay.payId,jdbcType = BIGINT},
       #{pay.payName,jdbcType = VARCHAR},
      #{pay.payMoney,jdbcType = DECIMAL},
       #{pay.type,jdbcType = VARCHAR},
       #{pay.storeId,jdbcType = BIGINT},
       #{pay.payDate,jdbcType=TIMESTAMP},
       #{pay.isCancel,jdbcType=INTEGER}
      )
    </foreach>
  </insert>
    <update id="updateByNum" parameterType="com.ssm.manage.entity.PayList">
        update  pay_list set is_cancel = #{isCancel}<if test="payDate!=null">,pay_date = #{payDate}</if> <if test="storeId!=null">,store_id = #{storeId}</if>
                         where doc_id = #{docId}

    </update>
    <update id="updateIsCancelById">
        update  pay_list set is_cancel = #{isCancel},pay_date = #{payDate}
        where id = #{id}
    </update>

    <resultMap id="selectByDay" type="com.ssm.manage.response.PayListVo">
        <!--
          WARNING - @mbg.generated
        -->
        <result column="pay_id" jdbcType="VARCHAR" property="payId" />
        <result column="pay_name" jdbcType="VARCHAR" property="payName" />
        <result column="pay_money_amount" jdbcType="DECIMAL" property="payMoneyAmount" />
        <result column="store_id" jdbcType="BIGINT" property="storeId" />
        <result column="pay_date" jdbcType="TIMESTAMP" property="payDate"/>
    </resultMap>
<!--    <select id="selectByDay" resultMap="selectByDay">-->
<!--        select store_id,pay_id,pay_name,sum(ifnull(pay_money,0))as pay_money_amount,-->
<!--        date(pay_date) as pay_date-->
<!--        from pay_list WHERE-->
<!--        store_id in-->
<!--        <foreach collection="storeIds" open="(" close=")" item="item" separator=",">-->
<!--            #{item}-->
<!--        </foreach>-->
<!--                and  doc_id in(-->
<!--        SELECT cash_num FROM CASH_LIST_${tab} WHERE is_cancel = 0 AND cash_date between #{startTime} and #{endTime}  UNION ALl-->
<!--        SELECT course_reatment_num FROM COURSE_LIST_${tab} WHERE is_cancel = 0 AND date between #{startTime} and #{endTime} UNION ALl-->
<!--        SELECT recharge_num FROM recharge_list_${tab} WHERE is_cancel = 0 AND recharge_date  between #{startTime} and #{endTime} UNION ALl-->
<!--        SELECT id FROM doc_promotion WHERE is_cancel = 0 AND doc_date between #{startTime} and #{endTime} UNION ALl-->
<!--        SELECT repay_num FROM repay_list WHERE is_cancel=0 AND repay_date between #{startTime} and #{endTime} UNION ALl-->
<!--        SELECT doc_num FROM card_change_list WHERE state = 3 AND doc_date between #{startTime} and #{endTime})-->
<!--        group by store_id,pay_id,pay_name,date(pay_date)-->
<!--    </select>-->
    <select id="selectByDay" resultMap="selectByDay">
        select store_id,pay_id,pay_name,sum(ifnull(pay_money,0))as pay_money_amount,
            date(pay_date) as pay_date
        from pay_list WHERE
        store_id in
                <foreach collection="storeIds" open="(" close=")" item="item" separator=",">
                    #{item}
                </foreach> and
            is_cancel = 0 AND pay_date BETWEEN #{startTime} and #{endTime}
        group by store_id,pay_id,pay_name,date(pay_date)
    </select>
    <select id="selectStoreDailys" resultType="java.math.BigDecimal" parameterType="com.ssm.manage.query.StoreDailyRequest">
        select ifnull(sum(pay_money),0) from pay_list
        where   SUBSTRING(pay_date,1,10) = #{dateStr}
          and is_cancel = 0 and store_id = #{storeId}
    </select>
    <select id="selectList" resultMap="BaseResultMap" parameterType="collection">
        select doc_id,pay_name,pay_money from pay_list where doc_id in
                                                       <foreach collection="courseNums" item="item" separator="," close=")" open="(">
                                                           #{item}
                                                       </foreach>
    </select>
</mapper>