<?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.maint.CargoMapper" >
    <resultMap id="BaseResultMap" type="com.th.demo.model.maint.Cargo" >
        <id column="id" property="id" jdbcType="VARCHAR" />
        <result column="sum_weight" property="sumWeight" jdbcType="DOUBLE" />
        <result column="use_weight" property="useWeight" jdbcType="DOUBLE" />
        <result column="belong_id" property="belongId" jdbcType="DOUBLE" />

        <association  column="uper_customer_id" property="uperCustomer" select="com.th.demo.mapping.maint.CustomerMapper.selectByPrimaryKey" />
        <association  column="customer_id" property="Customer" select="com.th.demo.mapping.maint.CustomerMapper.selectByPrimaryKey" />
    </resultMap>

    <sql id="Base_Column_List" >
        id, uper_customer_id, customer_id, sum_weight,use_weight,belong_id
    </sql>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
        select
        <include refid="Base_Column_List" />
        from t_maint_cargo
        where id = #{param1,jdbcType=VARCHAR}
    </select>

    <update id="updateByPrimaryKey" parameterType="com.th.demo.model.maint.Cargo" >
        update t_maint_cargo
        set uper_customer_id = #{uperCustomer.id ,jdbcType=VARCHAR},
            customer_id = #{Customer.id ,jdbcType=VARCHAR},
            sum_weight = #{sumWeight,jdbcType=DOUBLE},
            use_weight = #{useWeight,jdbcType=DOUBLE},
            belong_id = #{belongId,jdbcType=VARCHAR}
        where id = #{id,jdbcType=VARCHAR}
    </update>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
        delete from t_maint_cargo
        where id = #{param1,jdbcType=VARCHAR}
    </delete>

    <insert id="insert" parameterType="com.th.demo.model.maint.Cargo" >
        <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
            SELECT  REPLACE(UUID(),'-','') as id from dual
        </selectKey>


        insert into t_maint_cargo (id, uper_customer_id, customer_id,
        sum_weight, belong_id)
        values (#{id,jdbcType=VARCHAR}, #{uperCustomer.id,jdbcType=VARCHAR}, #{Customer.id,jdbcType=VARCHAR},
        #{sumWeight,jdbcType=DOUBLE}, #{belongId,jdbcType=VARCHAR})
    </insert>

    <select id="selectAll" resultMap="BaseResultMap" >
        select *
        from t_maint_cargo
        where 1=1
        <if test="param1 != null and param1 != ''" >
            and uper_customer_id = #{param1,jdbcType=VARCHAR}
        </if>
        <if test="param2 != null and param2 != ''" >
            and customer_id = #{param2,jdbcType=VARCHAR}
        </if>
          and belong_id = #{param3,jdbcType=VARCHAR}
    </select>

    <select id="selectByByFkAndCustomer" resultMap="BaseResultMap" >
        select *
        from t_maint_cargo
        where 1=1
          and uper_customer_id = #{param1,jdbcType=VARCHAR}
          and customer_id = #{param2,jdbcType=VARCHAR}
          and belong_id = #{param3,jdbcType=VARCHAR}
    </select>

</mapper>