<?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.yiqi.system.mapper.SysStoreMapper">

    <resultMap type="SysStoreVo" id="SysStoreResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="factoryId" column="factory_id"/>
        <result property="storeType" column="store_type"/>
        <result property="bindAreaId" column="bind_area_id"/>
        <result property="isPrint" column="is_print"/>
        <result property="isSettlement" column="is_settlement"/>
        <result property="isCarCodePrintName" column="is_car_code_print_name"/>
        <result property="printNum" column="print_num"/>
        <result property="isPrintName" column="is_print_name"/>
        <result property="contactName" column="contact_name"/>
        <result property="contactPost" column="contact_post"/>
        <result property="contactIdcard" column="contact_idcard"/>
        <result property="contactWorkTel" column="contact_work_tel"/>
        <result property="contactPhone" column="contact_phone"/>
        <result property="contactFax" column="contact_fax"/>
        <result property="contactZipCode" column="contact_zip_code"/>
        <result property="contactEmail" column="contact_email"/>
        <result property="provinceId" column="province_id"/>
        <result property="provinceName" column="province_name"/>
        <result property="cityId" column="city_id"/>
        <result property="cityName" column="city_name"/>
        <result property="areaId" column="area_id"/>
        <result property="areaName" column="area_name"/>
        <result property="address" column="address"/>
        <result property="addressDetail" column="address_detail"/>
        <result property="latitude" column="latitude"/>
        <result property="longitude" column="longitude"/>
        <result property="officeArea" column="office_area"/>
        <result property="openDate" column="open_date"/>
        <result property="businessStartTime" column="business_start_time"/>
        <result property="businessEndTime" column="business_end_time"/>
        <result property="companyType" column="company_type"/>
        <result property="companyDetail" column="company_detail"/>
        <result property="contactQq" column="contact_qq"/>
        <result property="companyPic" column="company_pic"/>
        <result property="companyLicencePic" column="company_licence_pic"/>
        <result property="status" column="status"/>
        <result property="delFlag" column="del_flag"/>
        <result property="factoryName" column="factory_name"/>
    </resultMap>

    <sql id="selectSysStoreVo">
        select store.*,factory.factory_name from sys_store store join manage_factory factory on store.factory_id = factory.id
    </sql>

    <select id="selectSysStoreList" parameterType="SysStore" resultMap="SysStoreResult">
        <include refid="selectSysStoreVo"/>
        <where>
            and store.del_flag = 0
            <if test="name != null  and name != ''">
                and store.name like concat('%', #{name}, '%')
            </if>
            <if test="factoryId != null ">
                and store.factory_id = #{factoryId}
            </if>
            <if test="storeType != null  and storeType != ''">
                and store.store_type = #{storeType}
            </if>
            <if test="provinceId != null ">
                and store.province_id = #{provinceId}
            </if>
            <if test="cityId != null ">
                and store.city_id = #{cityId}
            </if>
            <if test="status != null  and status != ''">
                and status = #{status}
            </if>
        </where>
    </select>

    <update id="deleteSysStoreById" parameterType="Long">
        update sys_store
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id = #{id}
    </update>

    <update id="deleteSysStoreByIds" parameterType="String">
        update sys_store
        <trim prefix="SET" suffixOverrides=",">
            del_flag = 2 ,
            update_time = now()
        </trim>
        where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="allOrg" resultType="com.yiqi.system.domain.vo.OrgVO" parameterType="String">
        (select id,name as name,'02' as source_type from sys_store where del_flag='0' and status='0'
        union all
        select id,factory_name as name,'01' as source_type from manage_factory where del_flag='0' and status='0') as org
        where 1=1
        <if test="sourceType != null and sourceType != ''">
            and org.source_type=#{sourceType}
        </if>
        order by org.source_type asc
    </select>
</mapper>