<?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.boco.nbd.wios.manage.mapper.def.SupplierMapper">

    <resultMap id="BaseResultMap" type="com.boco.nbd.wios.manage.entity.bo.Supplier">
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="status" jdbcType="INTEGER" property="status"/>
        <result column="organization_code" jdbcType="VARCHAR" property="organizationCode"/>
        <result column="contact_name" jdbcType="VARCHAR" property="contactName"/>
        <result column="contact_phone" jdbcType="VARCHAR" property="contactPhone"/>
        <result column="address" jdbcType="VARCHAR" property="address"/>
        <result column="region_id" jdbcType="VARCHAR" property="regionId"/>
        <result column="file_id" jdbcType="VARCHAR" property="fileId"/>
        <result column="max_order_quantity" jdbcType="INTEGER" property="maxOrderQuantity"/>
        <result column="description" jdbcType="VARCHAR" property="description"/>
        <result column="account_id" jdbcType="INTEGER" property="accountId"/>
        <result column="level" jdbcType="VARCHAR" property="level"/>
        <result column="create_account" jdbcType="VARCHAR" property="createAccount"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="modify_account" jdbcType="VARCHAR" property="modifyAccount"/>
        <result column="modify_time" jdbcType="TIMESTAMP" property="modifyTime"/>
        <result column="check_status" jdbcType="INTEGER" property="checkStatus"/>
        <result column="check_remark" jdbcType="VARCHAR" property="checkRemark"/>
        <result column="business_license" jdbcType="VARCHAR" property="businessLicense"/>
        <result column="install_license" jdbcType="VARCHAR" property="installLicense"/>
        <result column="tuv_cert" jdbcType="VARCHAR" property="tuvCert"/>
        <result column="install_insurance" jdbcType="VARCHAR" property="installInsurance"/>
        <result column="employer_insurance" jdbcType="VARCHAR" property="employerInsurance"/>
    </resultMap>


    <sql id="Base_Column_List">
        id
        , code, name, status, organization_code, contact_name, contact_phone, address,
  region_id, file_id, max_order_quantity, description, account_id, level, create_account,
  create_time, modify_account, modify_time,check_status,check_remark,business_license,
    install_license,tuv_cert,install_insurance,employer_insurance
    </sql>


    <delete id="delete" parameterType="java.lang.Integer">
        delete
        from t_supplier
        where id = #{id,jdbcType=INTEGER}
    </delete>


    <insert id="insert" parameterType="com.boco.nbd.wios.manage.entity.bo.Supplier">
        insert into t_supplier (id, code, name,
                                status, organization_code, contact_name,
                                contact_phone, address, region_id, pack_type, oem_code,
                                file_id, max_order_quantity, description,
                                account_id, level, create_account,
                                create_time, modify_account, modify_time, business_license,
                                install_license, tuv_cert, install_insurance, employer_insurance)
        values (#{id,jdbcType=INTEGER}, #{code,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
                #{status,jdbcType=INTEGER}, #{organizationCode,jdbcType=VARCHAR}, #{contactName,jdbcType=VARCHAR},
                #{contactPhone,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{regionId,jdbcType=VARCHAR},
                #{packType,jdbcType=VARCHAR}, #{oemCode,jdbcType=VARCHAR},
                #{fileId,jdbcType=VARCHAR}, #{maxOrderQuantity,jdbcType=INTEGER}, #{description,jdbcType=VARCHAR},
                #{accountId,jdbcType=INTEGER}, #{level,jdbcType=VARCHAR}, #{createAccount,jdbcType=VARCHAR},
                #{createTime,jdbcType=TIMESTAMP}, #{modifyAccount,jdbcType=VARCHAR}, #{modifyTime,jdbcType=TIMESTAMP},
                #{businessLicense,jdbcType=VARCHAR}, #{installLicense,jdbcType=VARCHAR}, #{tuvCert,jdbcType=VARCHAR},
                #{installInsurance,jdbcType=VARCHAR}, #{employerInsurance,jdbcType=VARCHAR})
    </insert>


    <insert id="insertDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.Supplier">
        insert into t_supplier
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">
                id,
            </if>
            <if test="code != null">
                code,
            </if>
            <if test="name != null">
                name,
            </if>
            <if test="status != null">
                status,
            </if>
            <if test="organizationCode != null">
                organization_code,
            </if>
            <if test="contactName != null">
                contact_name,
            </if>
            <if test="contactPhone != null">
                contact_phone,
            </if>
            <if test="address != null">
                address,
            </if>
            <if test="regionId != null">
                region_id,
            </if>
            <if test="fileId != null">
                file_id,
            </if>
            <if test="maxOrderQuantity != null">
                max_order_quantity,
            </if>
            <if test="description != null">
                description,
            </if>
            <if test="accountId != null">
                account_id,
            </if>
            <if test="level != null">
                level,
            </if>
            <if test="createAccount != null">
                create_account,
            </if>
            <if test="createTime != null">
                create_time,
            </if>
            <if test="modifyAccount != null">
                modify_account,
            </if>
            <if test="modifyTime != null">
                modify_time,
            </if>
            <if test="businessLicense != null">
                business_license,
            </if>
            <if test="installLicense != null">
                install_license,
            </if>
            <if test="tuvCert != null">
                tuv_cert,
            </if>
            <if test="installInsurance != null">
                install_insurance,
            </if>
            <if test="employerInsurance != null">
                employer_insurance,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">
                #{id,jdbcType=INTEGER},
            </if>
            <if test="code != null">
                #{code,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                #{name,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                #{status,jdbcType=INTEGER},
            </if>
            <if test="organizationCode != null">
                #{organizationCode,jdbcType=VARCHAR},
            </if>
            <if test="contactName != null">
                #{contactName,jdbcType=VARCHAR},
            </if>
            <if test="contactPhone != null">
                #{contactPhone,jdbcType=VARCHAR},
            </if>
            <if test="address != null">
                #{address,jdbcType=VARCHAR},
            </if>
            <if test="regionId != null">
                #{regionId,jdbcType=VARCHAR},
            </if>
            <if test="fileId != null">
                #{fileId,jdbcType=VARCHAR},
            </if>
            <if test="maxOrderQuantity != null">
                #{maxOrderQuantity,jdbcType=INTEGER},
            </if>
            <if test="description != null">
                #{description,jdbcType=VARCHAR},
            </if>
            <if test="accountId != null">
                #{accountId,jdbcType=INTEGER},
            </if>
            <if test="level != null">
                #{level,jdbcType=VARCHAR},
            </if>
            <if test="createAccount != null">
                #{createAccount,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="modifyAccount != null">
                #{modifyAccount,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                #{modifyTime,jdbcType=TIMESTAMP},
            </if>
            <if test="businessLicense != null">
                #{businessLicense,jdbcType=VARCHAR},
            </if>
            <if test="installLicense != null">
                #{installLicense,jdbcType=VARCHAR},
            </if>
            <if test="tuvCert != null">
                #{tuvCert,jdbcType=VARCHAR},
            </if>
            <if test="installInsurance != null">
                #{installInsurance,jdbcType=VARCHAR},
            </if>
            <if test="employerInsurance != null">
                #{employerInsurance,jdbcType=VARCHAR},
            </if>
        </trim>
    </insert>


    <update id="updateDynamic" parameterType="com.boco.nbd.wios.manage.entity.bo.Supplier">
        update t_supplier
        <set>
            <if test="code != null">
                code = #{code,jdbcType=VARCHAR},
            </if>
            <if test="name != null">
                name = #{name,jdbcType=VARCHAR},
            </if>
            <if test="status != null">
                status = #{status,jdbcType=INTEGER},
            </if>
            <if test="organizationCode != null">
                organization_code = #{organizationCode,jdbcType=VARCHAR},
            </if>
            <if test="contactName != null">
                contact_name = #{contactName,jdbcType=VARCHAR},
            </if>
            <if test="contactPhone != null">
                contact_phone = #{contactPhone,jdbcType=VARCHAR},
            </if>
            <if test="address != null">
                address = #{address,jdbcType=VARCHAR},
            </if>
            <if test="regionId != null">
                region_id = #{regionId,jdbcType=VARCHAR},
            </if>
            <if test="packType != null">
                pack_type = #{packType,jdbcType=VARCHAR},
            </if>
            <if test="oemCode != null">
                oem_code = #{oemCode,jdbcType=VARCHAR},
            </if>
            <if test="fileId != null">
                file_id = #{fileId,jdbcType=VARCHAR},
            </if>
            <if test="maxOrderQuantity != null">
                max_order_quantity = #{maxOrderQuantity,jdbcType=INTEGER},
            </if>
            <if test="description != null">
                description = #{description,jdbcType=VARCHAR},
            </if>
            <if test="accountId != null">
                account_id = #{accountId,jdbcType=INTEGER},
            </if>
            <if test="level != null">
                level = #{level,jdbcType=VARCHAR},
            </if>
            <if test="createAccount != null">
                create_account = #{createAccount,jdbcType=VARCHAR},
            </if>
            <if test="createTime != null">
                create_time = #{createTime,jdbcType=TIMESTAMP},
            </if>
            <if test="modifyAccount != null">
                modify_account = #{modifyAccount,jdbcType=VARCHAR},
            </if>
            <if test="modifyTime != null">
                modify_time = #{modifyTime,jdbcType=TIMESTAMP},
            </if>
            <if test="checkStatus != null">
                check_status = #{checkStatus,jdbcType=INTEGER},
            </if>
            <if test="checkRemark != null">
                check_remark = #{checkRemark,jdbcType=VARCHAR},
            </if>
            <if test="businessLicense != null">
                business_license = #{businessLicense,jdbcType=VARCHAR},
            </if>
            <if test="installLicense != null">
                install_license = #{installLicense,jdbcType=VARCHAR},
            </if>
            <if test="tuvCert != null">
                tuv_cert = #{tuvCert,jdbcType=VARCHAR},
            </if>
            <if test="installInsurance != null">
                install_insurance = #{installInsurance,jdbcType=VARCHAR},
            </if>
            <if test="employerInsurance != null">
                employer_insurance = #{employerInsurance,jdbcType=VARCHAR},
            </if>
        </set>
        where id = #{id,jdbcType=INTEGER}
    </update>


    <update id="update" parameterType="com.boco.nbd.wios.manage.entity.bo.Supplier">
        update t_supplier
        set code               = #{code,jdbcType=VARCHAR},
            name               = #{name,jdbcType=VARCHAR},
            status             = #{status,jdbcType=INTEGER},
            organization_code  = #{organizationCode,jdbcType=VARCHAR},
            contact_name       = #{contactName,jdbcType=VARCHAR},
            contact_phone      = #{contactPhone,jdbcType=VARCHAR},
            address            = #{address,jdbcType=VARCHAR},
            region_id          = #{regionId,jdbcType=VARCHAR},
            file_id            = #{fileId,jdbcType=VARCHAR},
            max_order_quantity = #{maxOrderQuantity,jdbcType=INTEGER},
            description        = #{description,jdbcType=VARCHAR},
            account_id         = #{accountId,jdbcType=INTEGER},
            level              = #{level,jdbcType=VARCHAR},
            create_account     = #{createAccount,jdbcType=VARCHAR},
            create_time        = #{createTime,jdbcType=TIMESTAMP},
            modify_account     = #{modifyAccount,jdbcType=VARCHAR},
            modify_time        = #{modifyTime,jdbcType=TIMESTAMP},
            business_license   = #{businessLicense,jdbcType=VARCHAR},
            install_license    = #{installLicense,jdbcType=VARCHAR},
            tuv_cert           = #{tuvCert,jdbcType=VARCHAR},
            install_insurance  = #{installInsurance,jdbcType=VARCHAR},
            employer_insurance = #{employerInsurance,jdbcType=VARCHAR}
        where id = #{id,jdbcType=INTEGER}
    </update>


    <select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from t_supplier
        where id = #{id,jdbcType=INTEGER}
    </select>

    <select id="selectByAccountId" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select t.*
        from t_supplier t
        where t.account_id = #{accountId}
    </select>

    <select id="selectByExample" parameterType="com.boco.nbd.wios.manage.entity.bo.SupplierBo"
            resultType="com.boco.nbd.wios.manage.entity.bo.SupplierVo">
        <if test="hasContract != null"><!-- 是否签了合同 -->
            select tt.* from
            (
        </if>
        select t.*,
        <if test="hasContract != null"><!-- 是否签了合同 -->
            sc.id as validContractId,
        </if>
        a.account as accountName from t_supplier t
        left join wb_account a on a.id=t.account_id
        <if test="hasContract != null"><!-- 是否签了合同 -->
            left join T_SUPPLIER_CONTRACT sc on sc.supplier_id=t.id
            <if test="hasContract == 1">
                and sc.status=2
            </if>
        </if>
        <where>
            <if test="id != null">
                and t.id = #{id}
            </if>
            <if test="ids != null">
                and t.id in
                <foreach collection="ids" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
            <if test="status != null">
                and t.status = #{status}
            </if>
            <if test="code != null and code != ''">
                <bind name="codeLike" value="'%' + code + '%'"/>
                and t.code like #{codeLike}
            </if>
            <if test="name != null and name != ''">
                <bind name="nameLike" value="'%' + name + '%'"/>
                and t.name like #{nameLike}
            </if>
            <if test="organizationCode != null and organizationCode != ''">
                <bind name="organizationCodeLike" value="'%' + organizationCode + '%'"/>
                and t.organization_code like #{organizationCodeLike}
            </if>
            <if test="createStartTime != null and createStartTime != ''">
                and t.create_time &gt;= #{createStartTime}
            </if>
            <if test="createEndTime != null and createEndTime != ''">
                and t.create_time &lt;= #{createEndTime}
            </if>
            <if test="checkStatus != null">
                and t.check_status = #{checkStatus}
            </if>
            <if test="level != null and level != ''">
                and t.level = #{level}
            </if>
        </where>
        order by t.create_time desc
        <if test="hasContract != null"><!-- 是否签了合同 -->
            ) as tt
        </if>
        <if test="hasContract != null and hasContract==0"><!-- 未签合同 -->
            where tt.validContractId is null
            group by tt.id
        </if>
        <if test="hasContract != null and hasContract!=0"><!-- 签了合同 -->
            group by tt.id
        </if>
    </select>

    <select id="selectInProcessOrder"
            resultType="com.boco.nbd.wios.manage.entity.bo.SupplierVo">
        select t.*,count(o.id) as order_quantity from t_supplier t
        left join t_order o
        on
        o.install_supplier_id=t.id
        and
        o.status not in
        <foreach collection="orderStatusNotInList" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
        <!-- and
        date_format(o.create_time,'%Y%m%d')=date_format(now(),'%Y%m%d') -->
        where
        t.id in
        <foreach collection="supplierIdList" item="item" index="index" open="(" close=")" separator=",">
            #{item}
        </foreach>
        GROUP BY t.id
    </select>

    <select id="selectOnly" resultType="com.boco.nbd.wios.manage.entity.bo.Supplier">
        select * from t_supplier
        <where>
            <if test="code != null and code !=''">
                or code = #{code}
            </if>
            <if test="name != null and name != ''">
                or name = #{name}
            </if>
        </where>
    </select>

    <select id="selectAll" parameterType="java.lang.Integer" resultMap="BaseResultMap">
        select *
        from t_supplier

        where status = #{status,jdbcType=INTEGER}

    </select>

  <select id="selectByRegionId"  resultMap="BaseResultMap">
  select *
  from  t_supplier
	        where status = 1
            <if test="regionId != null and regionId !=''">
	        and region_id like '%${regionId}%'
            </if>

</select>


  <select id="selectByLevel"  resultMap="BaseResultMap">
    select *
    from  t_supplier
    where status = 1
    <if test="level != null and level !=''">
      and level = #{level}
    </if>

  </select>

    <insert id="batchInsert" parameterType="java.util.List">
        insert into t_supplier (id, code, name,
        status, organization_code, contact_name,
        contact_phone, address, region_id,
        file_id, max_order_quantity, description,
        account_id, level, create_account,
        create_time, modify_account, modify_time
        )
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.id,jdbcType=INTEGER}, #{item.code,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},
            #{item.status,jdbcType=INTEGER}, #{item.organizationCode,jdbcType=VARCHAR},
            #{item.contactName,jdbcType=VARCHAR},
            #{item.contactPhone,jdbcType=VARCHAR}, #{item.address,jdbcType=VARCHAR}, #{item.regionId,jdbcType=VARCHAR},
            #{item.fileId,jdbcType=VARCHAR}, #{item.maxOrderQuantity,jdbcType=INTEGER},
            #{item.description,jdbcType=VARCHAR},
            #{item.accountId,jdbcType=INTEGER}, #{item.level,jdbcType=VARCHAR}, #{item.createAccount,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=TIMESTAMP}, #{item.modifyAccount,jdbcType=VARCHAR},
            #{item.modifyTime,jdbcType=TIMESTAMP}
            )
        </foreach>
    </insert>

</mapper>