<?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.cftech.addresst.dao.AddressMapper">

    <resultMap id="resultMap" type="com.cftech.addresst.model.Address">
        <id column="id" property="id"/>
        <result column="open_id" property="openId"/>
        <result column="address_name" property="addressName"/>
        <result column="address" property="address"/>
        <result column="province_id" property="provinceId"/>
        <result column="city_id" property="cityId"/>
        <result column="area_id" property="areaId"/>
        <result column="sex" property="sex"/>
        <result column="phone" property="phone"/>
        <result column="whether" property="whether"/>
        <result column="accounts_id" property="accountsId"/>
        <result column="del_flag" property="delFlag"/>
        <result column="status" property="status"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
        <result column="description" property="description"/>
        <result column="create_by" property="createBy"/>
        <result column="update_by" property="updateBy"/>
    </resultMap>

    <sql id="sqlWhere">
        <if test="conds!=null">
            <trim prefix="WHERE" prefixOverrides="AND|OR">
                <foreach collection="conds.conds" index="index" item="cond">
                    ${cond.linkType}
                    <if test="cond.condType == 'EQUAL'">${cond.param} = #{cond.value}</if>
                    <if test="cond.condType == 'NOTEQUAL'">${cond.param} &lt;&gt; #{cond.value}</if>
                    <if test="cond.condType == 'GREATEQUAL'">${cond.param} &gt;= #{cond.value}</if>
                    <if test="cond.condType == 'GREATTHAN'">${cond.param} &gt; #{cond.value}</if>
                    <if test="cond.condType == 'LESSEQUAL'">${cond.param} &lt;= #{cond.value}</if>
                    <if test="cond.condType == 'LESSTHAN'">${cond.param} &lt; #{cond.value}</if>
                    <if test="cond.condType == 'BETWEEN'">${cond.param} BETWEEN #{cond.startValue} AND
                        #{cond.endValue}
                    </if>
                    <if test="cond.condType == 'ISNULL'">${cond.param} IS NULL</if>
                    <if test="cond.condType == 'NOTNULL'">${cond.param} IS NOT NULL</if>
                    <if test="cond.condType == 'LIKE'">${cond.param} LIKE #{cond.value}</if>
                    <if test="cond.condType == 'IN'">${cond.param} IN
                        <foreach item="item" index="index" collection="cond.value" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </foreach>
            </trim>
        </if>
    </sql>

    <sql id="sqlColumns">
                                  	t.id,
	t.open_id,
	t.address_name,
	t.address,
	t.province_id,
	t.city_id,
	t.area_id,
	t.sex,
	t.phone,
	t.whether,
	t.accounts_id,
	t.del_flag,
	t.STATUS,
	t.create_time,
	t.update_time,
	t.description,
	t.create_by,
	t.update_by,
	a.areaid,
	a.citytype,
	a.areaname provinceName,
	b.areaname cityName,
	c.areaname countyName
                        </sql>

    <sql id="Columns">
                                  	id,
	open_id,
	address_name,
	address,
	province_id,
	city_id,
	area_id,
	sex,
	phone,
	whether,
	accounts_id,
	del_flag,
	STATUS,
	create_time,
	update_time,
	description,
	create_by,
	update_by

                        </sql>

    <insert id="save" parameterType="com.cftech.addresst.model.Address" useGeneratedKeys="true"
            keyProperty="id">
        insert into t_shipping_address
        (
        <include refid="Columns"/>
        )
        values
        (
        #{id, jdbcType=BIGINT},
        #{openId, jdbcType=BIGINT},
        #{addressName, jdbcType=VARCHAR},
        #{address, jdbcType=VARCHAR},
        #{provinceId, jdbcType=BIGINT},
        #{cityId, jdbcType=BIGINT},
        #{areaId, jdbcType=BIGINT},
        #{sex, jdbcType=BIGINT},
        #{phone, jdbcType=VARCHAR},
        #{whether, jdbcType=VARCHAR},
        #{accountsId, jdbcType=BIGINT},
        #{delFlag, jdbcType=TINYINT},
        #{status, jdbcType=VARCHAR},
        now(),
        now(),
        #{description, jdbcType=VARCHAR},
        #{createBy, jdbcType=BIGINT},
        #{updateBy, jdbcType=BIGINT}
        )
    </insert>
    <select id="addressId" resultType="com.cftech.addresst.model.AddressVO">
      select
        t.id,
        t.open_id as openId,
        t.address_name as addressName,
        t.address,
        t.province_id as provinceId,
        t.city_id as cityId,
        t.area_id as areaId,
        t.sex,
        t.phone,
        t.whether,
        t.accounts_id as accountsId,
        a.areaid,
        a.citytype,
        a.areaname provinceName,
        b.areaname cityName,
        c.areaname areaName
        FROM
        t_shipping_address t
        LEFT JOIN area a ON t.province_id = a.areaid
        LEFT JOIN area b ON t.city_id = b.areaid
        LEFT JOIN area c ON t.area_id = c.areaid
        and t.del_flag=0
        WHERE t.id = #{id}
    </select>
    <select id="addressList" resultType="com.cftech.addresst.model.AddressVO">
        SELECT
        t.id,
        t.open_id as openId,
        t.address_name as addressName,
        t.address,
        t.province_id as provinceId,
        t.city_id as cityId,
        t.area_id as areaId,
        t.sex,
        t.phone,
        t.whether,
        t.accounts_id as accountsId,
        a.areaid,
        a.citytype,
        a.areaname provinceName,
        b.areaname cityName,
        c.areaname areaName
        FROM
        t_shipping_address t
        LEFT JOIN area a ON t.province_id = a.areaid
        LEFT JOIN area b ON t.city_id = b.areaid
        LEFT JOIN area c ON t.area_id = c.areaid
        WHERE t.open_id = #{openId}
            AND t.del_flag=0
        <if test="addressId != null and addressId != '' ">
            AND t.id = #{addressId}
        </if>
    </select>

    <select id="fetchById" parameterType="java.lang.Long" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_shipping_address t
        LEFT JOIN area a ON t.province_id = a.areaid
        LEFT JOIN area b ON t.city_id = b.areaid
        LEFT JOIN area c ON t.area_id = c.areaid
        and t.del_flag=0
        WHERE t.id=#{id}
    </select>

    <select id="count" parameterType="java.util.Map" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM t_shipping_address t
        LEFT JOIN area a ON t.province_id = a.areaid
        LEFT JOIN area b ON t.city_id = b.areaid
        LEFT JOIN area c ON t.area_id = c.areaid
        <include refid="sqlWhere"/>

    </select>

    <select id="fetchSearchByPage" parameterType="java.util.Map" resultMap="resultMap">
        SELECT
        <include refid="sqlColumns"/>
        FROM t_shipping_address t
        LEFT JOIN area a ON t.province_id = a.areaid
        LEFT JOIN area b ON t.city_id = b.areaid
        LEFT JOIN area c ON t.area_id = c.areaid
        <include refid="sqlWhere"/>
        <if test="sort!=null">ORDER BY ${sort.param} ${sort.type}</if>
        <if test="limit>0">limit #{offset},#{limit}</if>
    </select>

    <update id="update" parameterType="com.cftech.addresst.model.Address">
        update t_shipping_address
        <set>
            <if test="id != null">
                id = #{id, jdbcType=BIGINT},
            </if>

            <if test="openId != null">
                open_id = #{openId, jdbcType=BIGINT},
            </if>
            <if test="addressName != null">
                address_name = #{addressName, jdbcType=VARCHAR},
            </if>
            <if test="address != null">
                address = #{address, jdbcType=VARCHAR},
            </if>
            <if test="provinceId != null">
                province_id = #{provinceId, jdbcType=BIGINT},
            </if>
            <if test="cityId != null">
                city_id = #{cityId, jdbcType=TIMESTAMP},
            </if>
            <if test="areaId != null">
                area_id = #{areaId, jdbcType=TIMESTAMP},
            </if>
            <if test="sex != null">
                sex = #{sex, jdbcType=BIGINT},
            </if>
            <if test="phone != null">
                phone = #{phone, jdbcType=VARCHAR},
            </if>
            <if test="whether != null">
                whether = #{whether, jdbcType=VARCHAR},
            </if>
            <if test="accountsId != null">
                accounts_id = #{accountsId, jdbcType=BIGINT},
            </if>
            <if test="delFlag != null">
                del_flag = #{delFlag, jdbcType=TINYINT},
            </if>
            <if test="status != null">
                status = #{status, jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime, jdbcType=TIMESTAMP},
            </if>
            <if test="description != null">
                description = #{description, jdbcType=VARCHAR},
            </if>
            <if test="createBy != null">
                create_by = #{createBy, jdbcType=BIGINT},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy, jdbcType=BIGINT},
            </if>
        </set>
        where id=#{id,jdbcType=BIGINT}
    </update>

    <update id="delete" parameterType="java.lang.Long">
        update t_shipping_address set del_flag=1 where id=#{id,jdbcType=BIGINT}
    </update>
    <!-- 默认地址修改!-->
    <update id="updateWhether" parameterType="java.util.Map">
        update t_shipping_address set whether = 0 where open_id = #{openId} and whether=#{whether}
    </update>

    <select id="listArea" resultType="com.cftech.addresst.model.Area">
        select areaid,areaname,affiliationareaId,citytype from area
    </select>
    <select id="whetherList" resultType="java.lang.Integer">
        select  * from t_shipping_address where  open_id = #{openId} and whether=1 and del_flag=0
    </select>
</mapper>