<?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.th.demo.mapping.finance.VerifyAccountMapper" >
    <resultMap id="BaseResultMap" type="com.th.demo.model.finance.VerifyAccount" >
        <id column="uuid" property="uuid" jdbcType="VARCHAR" />
        <result column="acc_date" property="accDate" jdbcType="VARCHAR" />
        <result column="acc_wgt" property="accWgt" jdbcType="DOUBLE" />
        <result column="invoice_amt" property="invoiceAmt" jdbcType="DOUBLE" />
        <result column="crt_time" property="crtTime" jdbcType="TIMESTAMP" />
        <result column="acc_reminder" property="accReminder" jdbcType="DOUBLE" />
        <result column="acc_amt" property="accAmt" jdbcType="DOUBLE" />
        <result column="fk_componey" property="fkComponey" jdbcType="VARCHAR" />
        <result column="crt_user_id" property="crtUserId" jdbcType="VARCHAR" />
        <result column="invoice_id" property="invoiceId" jdbcType="VARCHAR" />

        <result column="invoice_company" property="invoiceCompany" jdbcType="VARCHAR" />
        <result column="invoice_date" property="invoiceDate" jdbcType="VARCHAR" />
        <result column="write_invoice_amt" property="writeInvoiceAmt" jdbcType="DOUBLE" />
        <result column="invoice_crt_time" property="invoiceCrtTime" jdbcType="VARCHAR" />

        <result column="out_rcd_id" property="outRcdId" jdbcType="VARCHAR" />
        <result column="plate_no" property="plateNo" jdbcType="VARCHAR" />
        <result column="hq_componey" property="hqComponey" jdbcType="VARCHAR" />
        <result column="out_car_no" property="outCarNo" jdbcType="VARCHAR" />
        <result column="rcv_address" property="rcvAddress" jdbcType="VARCHAR" />
        <result column="out_date" property="outDate" jdbcType="TIMESTAMP" />

        <result column="rcv_pay_mth" property="rcvPayMth" jdbcType="VARCHAR" />
        <result column="rcv_pay_date" property="rcvPayDate" jdbcType="VARCHAR" />
        <result column="rcv_pay_amt" property="rcvPayAmt" jdbcType="DOUBLE" />
        <result column="rcv_pay_crt_time" property="rcvPayCrtTime" jdbcType="TIMESTAMP" />

        <result column="not_acc_amt" property="notAccAmt" jdbcType="VARCHAR" />
        <result column="wait_pay_amt" property="waitPayAmt" jdbcType="VARCHAR" />
        <result column="remain_amt" property="remainAmt" jdbcType="VARCHAR" />
    </resultMap>
    <resultMap id="BaseResultMap2" type="com.th.demo.model.finance.VerifyAccountProgress" >
        <id column="uuid" property="uuid" jdbcType="VARCHAR" />
        <result column="acc_date" property="accDate" jdbcType="VARCHAR" />
        <result column="acc_wgt" property="accWgt" jdbcType="DOUBLE" />
        <result column="invoice_amt" property="invoiceAmt" jdbcType="DOUBLE" />
        <result column="crt_time" property="crtTime" jdbcType="TIMESTAMP" />
        <result column="acc_reminder" property="accReminder" jdbcType="DOUBLE" />
        <result column="acc_amt" property="accAmt" jdbcType="VARCHAR" />
        <result column="fk_componey" property="fkComponey" jdbcType="VARCHAR" />
        <result column="crt_user_id" property="crtUserId" jdbcType="VARCHAR" />
        <result column="invoice_id" property="invoiceId" jdbcType="VARCHAR" />

        <result column="invoice_company" property="invoiceCompany" jdbcType="VARCHAR" />
        <result column="invoice_date" property="invoiceDate" jdbcType="VARCHAR" />
        <result column="write_invoice_amt" property="writeInvoiceAmt" jdbcType="VARCHAR" />
        <result column="invoice_crt_time" property="invoiceCrtTime" jdbcType="VARCHAR" />

        <result column="out_rcd_id" property="outRcdId" jdbcType="VARCHAR" />
        <result column="plate_no" property="plateNo" jdbcType="VARCHAR" />
        <result column="hq_componey" property="hqComponey" jdbcType="VARCHAR" />
        <result column="out_car_no" property="outCarNo" jdbcType="VARCHAR" />
        <result column="rcv_address" property="rcvAddress" jdbcType="VARCHAR" />
        <result column="out_date" property="outDate" jdbcType="TIMESTAMP" />

        <result column="rcv_pay_mth" property="rcvPayMth" jdbcType="VARCHAR" />
        <result column="rcv_pay_date" property="rcvPayDate" jdbcType="VARCHAR" />
        <result column="rcv_pay_amt" property="rcvPayAmt" jdbcType="VARCHAR" />
        <result column="rcv_pay_crt_time" property="rcvPayCrtTime" jdbcType="TIMESTAMP" />

        <result column="not_acc_amt" property="notAccAmt" jdbcType="VARCHAR" />
        <result column="wait_pay_amt" property="waitPayAmt" jdbcType="VARCHAR" />
        <result column="remain_amt" property="remainAmt" jdbcType="VARCHAR" />
    </resultMap>
    <select id="selectAccount" resultMap="BaseResultMap" >
        select  t.uuid,t.acc_date,t.acc_wgt,t.invoice_amt,t.crt_time,t.acc_reminder,t.acc_amt ,t.fk_componey,a.user_desc  as crt_user_id
                ,t.invoice_company,t.invoice_date,t.write_invoice_amt,t.invoice_crt_time,t.invoice_id,t.out_rcd_id,t.plate_no ,t.out_date
                ,t.hq_componey,t.out_car_no,t.rcv_address,t.rcv_pay_mth,t.rcv_pay_date,t.rcv_pay_amt,t.rcv_pay_crt_time
                from t_verify_account t ,sys_user a
                 where t.crt_user_id = a.id
                 and t.acc_date between  #{param1,jdbcType=VARCHAR} and #{param2,jdbcType=VARCHAR}
                 and t.fk_componey like concat('%',#{param3,jdbcType=VARCHAR},'%')
                 and t.acc_date is not null
                 and   t.invoice_date is null
                 and   t.rcv_pay_date is null
                 order by t.crt_time desc
    </select>
    <select id="selectInvocie" resultMap="BaseResultMap" >
        select  t.uuid,t.acc_date,t.acc_wgt,t.invoice_amt,t.crt_time,t.acc_reminder,t.acc_amt ,t.fk_componey,a.user_desc  as crt_user_id
                ,t.invoice_company,t.invoice_date,t.write_invoice_amt,t.invoice_crt_time,t.invoice_id,t.out_rcd_id,t.plate_no ,t.out_date
                ,t.hq_componey,t.out_car_no,t.rcv_address,t.rcv_pay_mth,t.rcv_pay_date,t.rcv_pay_amt,t.rcv_pay_crt_time
                from t_verify_account t ,sys_user a
                 where t.crt_user_id = a.id
                 and t.invoice_date between  #{param1,jdbcType=VARCHAR} and #{param2,jdbcType=VARCHAR}
                 and t.fk_componey like concat('%',#{param3,jdbcType=VARCHAR},'%')
                 and t.acc_date is not null
                 and   t.invoice_date is not null
                 and   t.rcv_pay_date is null
                 order by t.crt_time desc
    </select>

    <select id="queryInvoiceRslt" resultMap="BaseResultMap" >
        select  t.uuid,t.acc_date,t.acc_wgt,t.invoice_amt,t.crt_time,t.acc_reminder,t.acc_amt ,t.fk_componey,a.user_desc  as crt_user_id
                ,t.invoice_company,t.invoice_date,t.write_invoice_amt,t.invoice_crt_time,t.invoice_id,t.out_rcd_id,t.plate_no ,t.out_date
                ,t.hq_componey,t.out_car_no,t.rcv_address,t.rcv_pay_mth,t.rcv_pay_date,t.rcv_pay_amt,t.rcv_pay_crt_time
                from t_verify_account t ,sys_user a
                 where t.crt_user_id = a.id
                 and t.acc_date between  #{param1,jdbcType=VARCHAR} and #{param2,jdbcType=VARCHAR}
                 and t.fk_componey like concat('%',#{param3,jdbcType=VARCHAR},'%')
                 and t.acc_date is not null
                 and   t.invoice_date is not null
                 and   t.rcv_pay_date is not null
                 order by t.crt_time desc
    </select>

    <select id="queryProgress" resultMap="BaseResultMap2" >
                 select a.plate_no,a.weight as acc_wgt,a.add_time as out_date,c.truck_no as out_car_no,a.receive_address as rcv_address
                 ,e.name as fk_componey,a.customer_name as hq_componey,ifnull(b.acc_date,'-') as acc_date  ,ifnull(0+b.invoice_amt,'-') as invoice_amt
								,ifnull(0+b.acc_reminder,'-') as acc_reminder ,ifnull(0+b.acc_amt,'-') as acc_amt  ,ifnull(b.crt_user_id,'-')  as crt_user_id
								,ifnull(b.invoice_company,'-') as invoice_company ,ifnull(b.invoice_date,'-') as invoice_date ,ifnull(0+b.write_invoice_amt,'-') as write_invoice_amt
								,ifnull(b.invoice_id,'-') as invoice_id ,ifnull(b.out_rcd_id,'-') as out_rcd_id ,ifnull(b.plate_no,'-') as plate_no
								,ifnull(b.rcv_pay_mth,'-') as rcv_pay_mth
							  ,ifnull(b.rcv_pay_date,'-') as rcv_pay_date ,ifnull(0+b.rcv_pay_amt,'-') as rcv_pay_amt
							  ,ifnull(0+b.not_acc_amt,'-') as not_acc_amt,ifnull(0+b.wait_pay_amt,'-') as wait_pay_amt,ifnull(0+b.remain_amt,'-')  as remain_amt
				  from t_ware_out_record a left join
                 (select  t.uuid,ifnull(t.acc_date,'-') as acc_date  ,ifnull(t.invoice_amt,'-') as invoice_amt
				,ifnull(t.acc_reminder,'-') as acc_reminder ,ifnull(t.acc_amt,'-') as acc_amt  ,a.user_desc  as crt_user_id
                ,ifnull(t.invoice_company,'-') as invoice_company ,ifnull(t.invoice_date,'-') as invoice_date ,ifnull(t.write_invoice_amt,'-') as write_invoice_amt
								 ,ifnull(t.invoice_id,'-') as invoice_id ,ifnull(t.out_rcd_id,'-') as out_rcd_id ,ifnull(t.plate_no,'-') as plate_no

                  ,ifnull(t.rcv_pay_mth,'-') as rcv_pay_mth
                 ,ifnull(t.rcv_pay_date,'-') as rcv_pay_date ,ifnull(t.rcv_pay_amt,'-') as rcv_pay_amt
                 ,ifnull(ifnull(t.invoice_amt,0) - ifnull(t.write_invoice_amt,0),'-') as not_acc_amt,ifnull(ifnull(t.invoice_amt,0) - ifnull(t.rcv_pay_amt,0),'-') as wait_pay_amt
                 ,ifnull(ifnull(t.invoice_amt,0) - ifnull(t.rcv_pay_amt,0),'-') as remain_amt
                from t_verify_account t ,sys_user a
                 where t.crt_user_id = a.id
                 order by t.crt_time desc
                 ) b
		 on a.id = b.out_rcd_id
		 left join t_ware_distribution c  on a.distribution_id = c.id
		 left join t_ware_in_record d on a.in_id = d.id
		 left join t_maint_customer e on d.fk_compony_name = e.id
        where a.cancel_flag = '0'
            and   date_format(a.add_time,'%Y%m%d%H%i%S') between #{param1,jdbcType=VARCHAR}  and #{param2,jdbcType=VARCHAR}
            and ifnull(a.fk_compony_name,'*') like concat('%',#{param3,jdbcType=VARCHAR},'%')
    </select>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" >
        select  *
                from t_verify_account t  where t.uuid = #{param1,jdbcType=VARCHAR}
    </select>

    <insert id="insert" parameterType="com.th.demo.model.finance.VerifyAccount" >
        <selectKey keyProperty="uuid" order="BEFORE" resultType="java.lang.String">
            SELECT  REPLACE(UUID(),'-','') as uuid from dual
        </selectKey>
        insert into t_verify_account(uuid,acc_date,acc_wgt,invoice_amt,crt_time,acc_reminder,acc_amt,fk_componey,crt_user_id,out_rcd_id,plate_no,out_date,hq_componey
                                      ,out_car_no,rcv_address)
        values (#{uuid,jdbcType=VARCHAR},#{accDate,jdbcType=VARCHAR}, #{accWgt,jdbcType=DOUBLE}, #{invoiceAmt,jdbcType=DOUBLE}, #{crtTime,jdbcType=TIMESTAMP}
        , #{accReminder,jdbcType=DOUBLE}, #{accAmt,jdbcType=DOUBLE}, #{fkComponey,jdbcType=VARCHAR}, #{crtUserId,jdbcType=VARCHAR},#{outRcdId,jdbcType=VARCHAR}
        ,#{plateNo,jdbcType=VARCHAR},#{outDate,jdbcType=VARCHAR},#{hqComponey,jdbcType=VARCHAR},#{outCarNo,jdbcType=VARCHAR},#{rcvAddress,jdbcType=VARCHAR})
    </insert>

    <insert id="insertNew" parameterType="com.th.demo.model.finance.InvoiceNew" >
        <selectKey keyProperty="uuid" order="BEFORE" resultType="java.lang.String">
            SELECT  REPLACE(UUID(),'-','') as uuid from dual
        </selectKey>
        insert into t_invoice(uuid,acc_date,fk_componey,acc_wgt,invoice_amt,acc_reminder,acc_amt,crt_time,crt_user_id,invoice_company,invoice_date,write_invoice_amt
                                ,)
        values (#{uuid,jdbcType=VARCHAR},#{accDate,jdbcType=VARCHAR},#{fkComponey,jdbcType=VARCHAR},#{accWgt,jdbcType=VARCHAR},#{invoiceAmt,jdbcType=VARCHAR}
        ,#{accReminder,jdbcType=VARCHAR},#{accAmt,jdbcType=VARCHAR},#{crtTime,jdbcType=VARCHAR},#{crtUserId,jdbcType=VARCHAR},#{invoiceCompany,jdbcType=VARCHAR}
        ,#{invoiceDate,jdbcType=VARCHAR},#{writeInvoiceAmt,jdbcType=VARCHAR})
    </insert>
    <update id="updateWriteInvocieInfo" parameterType="com.th.demo.model.finance.VerifyAccount" >
        update t_verify_account  t
				set t.write_invoice_amt = #{writeInvoiceAmt,jdbcType=DOUBLE}
						,t.invoice_date = #{invoiceDate,jdbcType=VARCHAR}
						,t.invoice_company = #{invoiceCompany,jdbcType=VARCHAR}
						,t.invoice_crt_time =  #{invoiceCrtTime,jdbcType=TIMESTAMP}
						,t.invoice_id = #{invoiceId,jdbcType=VARCHAR}
	    where t.uuid = #{uuid,jdbcType=VARCHAR}
    </update>

    <update id="updateRcvPayInfo" parameterType="com.th.demo.model.finance.VerifyAccount" >
        update t_verify_account  t
				set t.rcv_pay_mth = #{rcvPayMth,jdbcType=VARCHAR}
						,t.rcv_pay_date = #{rcvPayDate,jdbcType=VARCHAR}
						,t.rcv_pay_amt = #{rcvPayAmt,jdbcType=VARCHAR}
						,t.rcv_pay_crt_time =  #{rcvPayCrtTime,jdbcType=TIMESTAMP}
	    where t.uuid = #{uuid,jdbcType=VARCHAR}
    </update>

    <delete id="backVerifyAcc" parameterType="com.th.demo.model.finance.VerifyAccount" >
        delete from  t_verify_account
	    where uuid = #{uuid,jdbcType=VARCHAR}
    </delete>
    <update id="backInvoice" parameterType="com.th.demo.model.finance.VerifyAccount" >
        update t_verify_account  t
				set t.write_invoice_amt = null
						,t.invoice_date = null
						,t.invoice_company = null
						,t.invoice_crt_time =  null
						,t.invoice_id = null
	    where t.uuid = #{uuid,jdbcType=VARCHAR}
    </update>
    <update id="backRcvPay" parameterType="com.th.demo.model.finance.VerifyAccount" >
        update t_verify_account  t
				set t.rcv_pay_mth = null
						,t.rcv_pay_date = null
						,t.rcv_pay_amt = null
						,t.rcv_pay_crt_time =  null
	    where t.uuid = #{uuid,jdbcType=VARCHAR}
    </update>


</mapper>