<?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.dgtly.system.mapper.SysCompanyMapper">
    
    <resultMap type="SysCompany" id="SysCompanyResult">
        <result property="id"    column="id"    />
        <result property="status"    column="status"    />
        <result property="companyName"    column="company_name"    />
        <result property="companyType"    column="company_type"    />
        <result property="detailAddress"    column="detail_address"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="legalPerson"    column="legal_person"    />
        <result property="contactNumber"    column="contact_number"    />
        <result property="establishDate"    column="establish_date"    />
        <result property="unifiedSocialCreditCode"    column="unified_social_credit_code"    />
        <result property="businessLicenseUrl"    column="business_license_url"    />
        <result property="defaultImgUrl"    column="default_img_url"    />
        <result property="propagandaImgUrl"    column="propaganda_img_url"    />
        <result property="companyIntroduce"    column="company_introduce"    />
        <result property="versionNum"    column="version_num"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="remark"    column="remark"    />
        <result property="primaryBusiness"    column="primary_business"    />
        <result property="sort"    column="sort"    />
    </resultMap>


    <sql id="selectSysCompanyVo">
        select sc.id, sc.company_name,sc.company_type,sc.detail_address,sc.longitude,sc.latitude,sc.legal_person,sc.contact_number,sc.establish_date,
        sc.unified_social_credit_code,sc.business_license_url,sc.default_img_url,sc.propaganda_img_url,sc.company_introduce,sc.version_num,
        sc.create_by,sc.update_by,sc.update_time,sc.remark,sc.status,sc.create_time,sc.primary_business,sc.sort
        from sys_company sc
    </sql>

    <select id="selectSysCompanyList" parameterType="SysCompany" resultMap="SysCompanyResult">
        <include refid="selectSysCompanyVo"/>
        <where>  
            <if test="companyName != null  and companyName != ''"> and sc.company_name like concat('%', #{companyName},'%')</if>
            <if test="status != null  and status != ''">and sc.status =#{status}</if>
            <if test="companyType != null  and companyType != ''">and sc.companyType =#{company_type}</if>
        </where>
        order by sort desc
    </select>
    
    <select id="selectSysCompanyById" parameterType="Long" resultMap="SysCompanyResult">
        <include refid="selectSysCompanyVo"/>
        where sc.id = #{id}
    </select>
        
    <insert id="insertSysCompany" parameterType="SysCompany" useGeneratedKeys="true" keyProperty="id">
        insert into sys_company
        <trim prefix="(" suffix=")" suffixOverrides=",">
            create_time,
            <if test="status != null  and status != ''">status,</if>
            <if test="companyName != null  and companyName != ''">company_name,</if>
            <if test="companyType != null  and companyType != ''">company_type,</if>
            <if test="detailAddress != null  and detailAddress != ''">detail_address,</if>
            <if test="longitude != null  and longitude != ''">longitude,</if>
            <if test="latitude != null  and latitude != ''">latitude,</if>
            <if test="legalPerson != null  and legalPerson != ''">legal_person,</if>
            <if test="contactNumber != null  and contactNumber != ''">contact_number,</if>
            <if test="establishDate != null ">establish_date,</if>
            <if test="unifiedSocialCreditCode != null  and unifiedSocialCreditCode != ''">unified_social_credit_code,</if>
            <if test="businessLicenseUrl != null  and businessLicenseUrl != ''">business_license_url,</if>
            <if test="defaultImgUrl != null  and defaultImgUrl != ''">default_img_url,</if>
            <if test="propagandaImgUrl != null  and propagandaImgUrl != ''">propaganda_img_url,</if>
            <if test="companyIntroduce != null  and companyIntroduce != ''">company_introduce,</if>
            <if test="versionNum != null  and versionNum != ''">version_num,</if>
            <if test="createBy != null  and createBy != ''">create_by,</if>
            <if test="updateBy != null  and updateBy != ''">update_by,</if>
            <if test="remark != null  and remark != ''">remark,</if>
            <if test="primaryBusiness != null  and primaryBusiness != ''">primary_business,</if>
            <if test="sort != null  and sort != ''">sort,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            now(),
            <if test="status != null  and status != ''">#{status},</if>
            <if test="companyName != null  and companyName != ''">#{companyName},</if>
            <if test="companyNameEn != null  and companyNameEn != ''">#{companyNameEn},</if>
            <if test="companyType != null  and companyType != ''">#{companyType},</if>
            <if test="detailAddress != null  and detailAddress != ''">#{detailAddress},</if>
            <if test="detailAddressEn != null  and detailAddressEn != ''">#{detailAddressEn},</if>
            <if test="longitude != null  and longitude != ''">#{longitude},</if>
            <if test="latitude != null  and latitude != ''">#{latitude},</if>
            <if test="legalPerson != null  and legalPerson != ''">#{legalPerson},</if>
            <if test="legalPersonEn != null  and legalPersonEn != ''">#{legalPersonEn},</if>
            <if test="contactNumber != null  and contactNumber != ''">#{contactNumber},</if>
            <if test="establishDate != null ">#{establishDate},</if>
            <if test="unifiedSocialCreditCode != null  and unifiedSocialCreditCode != ''">#{unifiedSocialCreditCode},</if>
            <if test="businessLicenseUrl != null  and businessLicenseUrl != ''">#{businessLicenseUrl},</if>
            <if test="defaultImgUrl != null  and defaultImgUrl != ''">#{defaultImgUrl},</if>
            <if test="propagandaImgUrl != null  and propagandaImgUrl != ''">#{propagandaImgUrl},</if>
            <if test="companyIntroduce != null  and companyIntroduce != ''">#{companyIntroduce},</if>
            <if test="companyIntroduceEn != null  and companyIntroduceEn != ''">#{companyIntroduceEn},</if>
            <if test="versionNum != null  and versionNum != ''">#{versionNum},</if>
            <if test="createBy != null  and createBy != ''">#{createBy},</if>
            <if test="updateBy != null  and updateBy != ''">#{updateBy},</if>
            <if test="remark != null  and remark != ''">#{remark},</if>
            <if test="primaryBusiness != null  and primaryBusiness != ''">#{primaryBusiness},</if>
            <if test="primaryBusinessEn != null  and primaryBusinessEn != ''">#{primaryBusinessEn},</if>
            <if test="sort != null  and sort != ''">#{sort},</if>
         </trim>
    </insert>

    <update id="updateSysCompany" parameterType="SysCompany">
        update sys_company
        <trim prefix="SET" suffixOverrides=",">
            update_time = now(),
            version_num=version_num+1,
            <if test="status != null">status = #{status},</if>
            <if test="companyName != null  and companyName != ''">company_name= #{companyName},</if>
            <if test="companyType != null  and companyType != ''">company_type= #{companyType},</if>
            <if test="detailAddress != null  and detailAddress != ''">detail_address= #{detailAddress},</if>
            <if test="longitude != null  and longitude != ''">longitude= #{longitude},</if>
            <if test="latitude != null  and latitude != ''">latitude= #{latitude},</if>
            <if test="legalPerson != null  and legalPerson != ''">legal_person= #{legalPerson},</if>
            <if test="contactNumber != null  and contactNumber != ''">contact_number= #{contactNumber},</if>
            <if test="establishDate != null ">establish_date= #{establishDate},</if>
            <if test="unifiedSocialCreditCode != null  and unifiedSocialCreditCode != ''">unified_social_credit_code= #{unifiedSocialCreditCode},</if>
            <if test="businessLicenseUrl != null  and businessLicenseUrl != ''">business_license_url= #{businessLicenseUrl},</if>
            <if test="defaultImgUrl != null  and defaultImgUrl != ''">default_img_url= #{defaultImgUrl},</if>
            <if test="propagandaImgUrl != null  and propagandaImgUrl != ''">propaganda_img_url= #{propagandaImgUrl},</if>
            <if test="companyIntroduce != null  and companyIntroduce != ''">company_introduce= #{companyIntroduce},</if>
            <if test="createBy != null  and createBy != ''">create_by= #{createBy},</if>
            <if test="updateBy != null  and updateBy != ''">update_by= #{updateBy},</if>
            <if test="remark != null  and remark != ''">remark= #{remark},</if>
            <if test="primaryBusiness != null  and primaryBusiness != ''">primary_business=#{primaryBusiness},</if>
            <if test="sort != null  and sort != ''">sort=#{sort},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSysCompanyById" parameterType="Long">
        delete from sys_company where id = #{id}
    </delete>

    <delete id="deleteSysCompanyByIds" parameterType="String">
        delete from sys_company where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>


    <select id="selectCompanyHotList"  resultMap="SysCompanyResult">
        select sc.id, sc.company_name,sc.establish_date,sc.default_img_url,sc.primary_business,sc.propaganda_img_url
        from sys_company sc
        where sc.id != 1 and sc.status = 0
        order by sort desc
        limit 0,10
    </select>
    <select id="selectSysCompanyListExRoot" parameterType="SysCompany" resultMap="SysCompanyResult">
        <include refid="selectSysCompanyVo"/>
        <where>
            sc.id !=1
            <if test="companyName != null  and companyName != ''"> and (sc.company_name like concat('%',#{companyName},'%') </if>
        </where>
        order by sc.sort desc
    </select>


    <select id="countCompany" resultType="java.lang.Integer">

        select count(company_name) from sys_company where del_flag =0
    </select>
    
</mapper>