<?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.fs.store.mapper.FsStorePaymentMapper">

    <resultMap type="FsStorePayment" id="FsStorePaymentResult">
        <result property="paymentId"    column="payment_id"    />
        <result property="payCode"    column="pay_code"    />
        <result property="payTypeCode"    column="pay_type_code"    />
        <result property="payMoney"    column="pay_money"    />
        <result property="payTime"    column="pay_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="tradeNo"    column="trade_no"    />
        <result property="userId"    column="user_id"    />
        <result property="openId"    column="open_id"    />
        <result property="businessType"    column="business_type"    />
        <result property="businessOrderId"    column="business_order_id"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="deptId"    column="dept_id"    />
        <result property="bankTransactionId"    column="bank_transaction_id"    />
        <result property="bankSerialNo"    column="bank_serial_no"    />
        <result property="refundMoney"    column="refund_money"    />
        <result property="refundTime"    column="refund_time"    />
        <result property="orderId"    column="order_id"    />
        <result property="isPayRemain"    column="is_pay_remain"    />
        <result property="payMode"    column="pay_mode"    />
    </resultMap>

    <sql id="selectFsStorePaymentVo">
        select payment_id,pay_mode, pay_code, pay_type_code, pay_money, pay_time, create_time, trade_no, user_id, open_id, business_type, business_order_id, status,remark,company_id,company_user_id,dept_id,bank_transaction_id,bank_serial_no,refund_money,refund_time,order_id,is_pay_remain from fs_store_payment

    </sql>

    <select id="selectFsStorePaymentList" parameterType="FsStorePayment" resultMap="FsStorePaymentResult">
        <include refid="selectFsStorePaymentVo"/>
        <where>
            <if test="payCode != null  and payCode != ''"> and pay_code = #{payCode}</if>
            <if test="payTypeCode != null  and payTypeCode != ''"> and pay_type_code = #{payTypeCode}</if>
            <if test="payMoney != null "> and pay_money = #{payMoney}</if>
            <if test="payTime != null "> and pay_time = #{payTime}</if>
            <if test="tradeNo != null  and tradeNo != ''"> and trade_no = #{tradeNo}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="openId != null  and openId != ''"> and open_id = #{openId}</if>
            <if test="businessType != null "> and business_type = #{businessType}</if>
            <if test="businessOrderId != null  and businessOrderId != ''"> and business_order_id = #{businessOrderId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="remark != null "> and remark = #{remark}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="deptId != null "> and dept_id = #{deptId}</if>
            <if test="bankTransactionId != null "> and bank_transaction_id = #{bankTransactionId}</if>
            <if test="bankSerialNo != null "> and bank_serial_no = #{bankSerialNo}</if>
            <if test="orderId != null "> and order_id = #{orderId}</if>
        </where>
    </select>

    <select id="selectFsStorePaymentById" parameterType="Long" resultMap="FsStorePaymentResult">
        <include refid="selectFsStorePaymentVo"/>
        where payment_id = #{paymentId}
    </select>

    <insert id="insertFsStorePayment" parameterType="FsStorePayment" useGeneratedKeys="true" keyProperty="paymentId">
        insert into fs_store_payment
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="payCode != null">pay_code,</if>
            <if test="payTypeCode != null">pay_type_code,</if>
            <if test="payMoney != null">pay_money,</if>
            <if test="payTime != null">pay_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="tradeNo != null">trade_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="openId != null">open_id,</if>
            <if test="businessType != null">business_type,</if>
            <if test="businessOrderId != null">business_order_id,</if>
            <if test="status != null">status,</if>
            <if test="remark != null">remark,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="deptId != null">dept_id,</if>
            <if test="bankTransactionId != null">bank_transaction_id,</if>
            <if test="bankSerialNo != null">bank_serial_no,</if>
            <if test="refundMoney != null">refund_money,</if>
            <if test="refundTime != null">refund_time,</if>
            <if test="orderId != null">order_id,</if>
            <if test="isPayRemain != null">is_pay_remain,</if>
            <if test="payMode != null">pay_mode,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="payCode != null">#{payCode},</if>
            <if test="payTypeCode != null">#{payTypeCode},</if>
            <if test="payMoney != null">#{payMoney},</if>
            <if test="payTime != null">#{payTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="tradeNo != null">#{tradeNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="openId != null">#{openId},</if>
            <if test="businessType != null">#{businessType},</if>
            <if test="businessOrderId != null">#{businessOrderId},</if>
            <if test="status != null">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="deptId != null">#{deptId},</if>
            <if test="bankTransactionId != null">#{bankTransactionId},</if>
            <if test="bankSerialNo != null">#{bankSerialNo},</if>
            <if test="refundMoney != null">#{refundMoney},</if>
            <if test="refundTime != null">#{refundTime},</if>
            <if test="orderId != null">#{orderId},</if>
            <if test="isPayRemain != null">#{isPayRemain},</if>
            <if test="payMode != null">#{payMode},</if>
        </trim>
    </insert>

    <update id="updateFsStorePayment" parameterType="FsStorePayment">
        update fs_store_payment
        <trim prefix="SET" suffixOverrides=",">
            <if test="payCode != null">pay_code = #{payCode},</if>
            <if test="payTypeCode != null">pay_type_code = #{payTypeCode},</if>
            <if test="payMoney != null">pay_money = #{payMoney},</if>
            <if test="payTime != null">pay_time = #{payTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="tradeNo != null">trade_no = #{tradeNo},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="openId != null">open_id = #{openId},</if>
            <if test="businessType != null">business_type = #{businessType},</if>
            <if test="businessOrderId != null">business_order_id = #{businessOrderId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="deptId != null">dept_id = #{deptId},</if>
            <if test="bankTransactionId != null">bank_transaction_id = #{bankTransactionId},</if>
            <if test="bankSerialNo != null">bank_serial_no = #{bankSerialNo},</if>
            <if test="refundMoney != null">refund_money = #{refundMoney},</if>
            <if test="refundTime != null">refund_time = #{refundTime},</if>
            <if test="orderId != null">order_id = #{orderId},</if>
            <if test="isPayRemain != null">is_pay_remain = #{isPayRemain},</if>
            <if test="payMode != null">pay_mode = #{payMode},</if>
        </trim>
        where payment_id = #{paymentId}
    </update>

    <delete id="deleteFsStorePaymentById" parameterType="Long">
        delete from fs_store_payment where payment_id = #{paymentId}
    </delete>

    <delete id="deleteFsStorePaymentByIds" parameterType="String">
        delete from fs_store_payment where payment_id in
        <foreach item="paymentId" collection="array" open="(" separator="," close=")">
            #{paymentId}
        </foreach>
    </delete>

    <select id="selectFsStorePaymentCounts" resultType="com.alibaba.fastjson.JSONObject">
        SELECT  count(t.payment_id ) AS orderCount,  t.type, sum(IFNULL(t.pay_money,0)) as payMoney
        FROM
        (
        SELECT DATE_FORMAT( o.create_time, #{sqlDateFormat} ) AS type,  o.payment_id,o.pay_money
        FROM
        fs_store_payment  AS o
        <where>
            o.status=1 and o.business_type=1 and
            (DATE_FORMAT(o.create_time,#{sqlDateFormat}) between #{beginTime} and #{finalTime})
            <if test="userIds != null and userIds.length>0">
                AND o.company_user_id IN
                <foreach item="item" collection="userIds" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="companyId != null  ">
                AND o.company_id = #{companyId}
            </if>
        </where>
        ) t
        GROUP BY t.type
    </select>


</mapper>
