<?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.company.mapper.CompanyMoneyLogsMapper">

    <resultMap type="CompanyMoneyLogs" id="CompanyMoneyLogsResult">
        <result property="logsId"    column="logs_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="money"    column="money"    />
        <result property="balance"    column="balance"    />
        <result property="remark"    column="remark"    />
        <result property="createTime"    column="create_time"    />
        <result property="logsType"    column="logs_type"    />
        <result property="businessId"    column="business_id"    />
    </resultMap>

    <sql id="selectCompanyMoneyLogsVo">
        select logs_id, company_id, money,balance, remark, create_time,logs_type,business_id from company_money_logs
    </sql>

    <select id="selectCompanyMoneyLogsList" parameterType="CompanyMoneyLogs" resultMap="CompanyMoneyLogsResult">
        <include refid="selectCompanyMoneyLogsVo"/>
        <where>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="money != null "> and money = #{money}</if>
        </where>
    </select>

    <select id="selectCompanyMoneyLogsById" parameterType="Long" resultMap="CompanyMoneyLogsResult">
        <include refid="selectCompanyMoneyLogsVo"/>
        where logs_id = #{logsId}
    </select>
    <select id="selectCompanyMoneyLogsExportVOList" resultType="com.fs.company.vo.CompanyMoneyLogsExportVO">
        select l.*,c.company_name,o.order_code,deliver.deliver_id as delivery_id,p.pay_code,p.pay_type_code
        from company_money_logs l
            left join company c on c.company_id=l.company_id
            left join fs_store_order o on (o.id=l.business_id and (l.logs_type=4 or  l.logs_type=5))
            left join fs_store_payment p on (p.payment_id=l.business_id and (l.logs_type=8 or  l.logs_type=9))
            left join fs_store_delivers deliver on deliver.order_id=o.id
        <where>
            <if test = 'maps.companyId != null  '>
                and l.company_id = #{maps.companyId}
            </if>
            <if test = 'maps.logsType != null  '>
                and l.logs_type = #{maps.logsType}
            </if>
            <if test = 'maps.beginTime != null and maps.beginTime != "" '>
                and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')
            </if>
            <if test = 'maps.endTime != null and maps.endTime != "" '>
                and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')
            </if>
        </where>
        order by l.logs_id desc
    </select>
    <select id="selectCompanyMoneyLogsExport1VOList" resultType="com.fs.company.vo.CompanyMoneyLogsExport1VO">
        select DISTINCT l.*,c.company_name,p.pay_type_code,o.order_code,o.real_name,o.user_phone,o.user_address,o.total_price,o.pay_money,o.pay_delivery,o.status,o.delivery_pay_status,
        o.delivery_time,o.delivery_pay_time,deliver.deliver_id as delivery_id
        ,(select group_concat(dd.dept_name separator  ',') from company_dept dd where o.dept_id=dd.dept_id or find_in_set(dd.dept_id,d.ancestors)) as dept_name
        from company_money_logs l left join company c on c.company_id=l.company_id left join fs_store_order o on o.id=l.business_id
        left join (select * from fs_store_payment where status != 0) p on o.id=p.order_id left join company_dept d on d.dept_id=o.dept_id
        left join fs_store_delivers deliver on deliver.order_id=o.id
        where (l.logs_type=4||l.logs_type=5)
        <if test = 'maps.companyId != null  '>
            and l.company_id = #{maps.companyId}
        </if>
        <if test = 'maps.beginTime != null and maps.beginTime !="" '>
            and date_format(l.create_time,'%y%m%d') &gt;= date_format(#{maps.beginTime},'%y%m%d')
        </if>
        <if test = 'maps.endTime != null and maps.endTime != "" '>
            and date_format(l.create_time,'%y%m%d') &lt;= date_format(#{maps.endTime},'%y%m%d')
        </if>
        order by l.logs_id desc LIMIT 30000
    </select>

    <insert id="insertCompanyMoneyLogs" parameterType="CompanyMoneyLogs" useGeneratedKeys="true" keyProperty="logsId">
        insert into company_money_logs
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="companyId != null">company_id,</if>
            <if test="money != null">money,</if>
            <if test="balance != null">balance,</if>
            <if test="remark != null">remark,</if>
            <if test="createTime != null">create_time,</if>
            <if test="logsType != null">logs_type,</if>
            <if test="businessId != null">business_id,</if>
            <if test="type != null">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="companyId != null">#{companyId},</if>
            <if test="money != null">#{money},</if>
            <if test="balance != null">#{balance},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="logsType != null">#{logsType},</if>
            <if test="businessId != null">#{businessId},</if>
            <if test="type != null">#{type},</if>
         </trim>
    </insert>

    <update id="updateCompanyMoneyLogs" parameterType="CompanyMoneyLogs">
        update company_money_logs
        <trim prefix="SET" suffixOverrides=",">
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="money != null">money = #{money},</if>
            <if test="balance != null">balance = #{balance},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="logsType != null">logs_type = #{logsType},</if>
            <if test="businessId != null">business_id = #{businessId},</if>
        </trim>
        where logs_id = #{logsId}
    </update>

    <delete id="deleteCompanyMoneyLogsById" parameterType="Long">
        delete from company_money_logs where logs_id = #{logsId}
    </delete>

    <delete id="deleteCompanyMoneyLogsByIds" parameterType="String">
        delete from company_money_logs where logs_id in
        <foreach item="logsId" collection="array" open="(" separator="," close=")">
            #{logsId}
        </foreach>
    </delete>

</mapper>
