<?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.pm.enterpriseInfo.mapper.EnterpriseMapper">

    <resultMap type="pd" id="EnterpriseResult">
        <result property="enterpriseId" column="enterprise_id" />
        <result property="enterpriseName" column="enterprise_name" />
        <result property="creditCode" column="credit_code" />
        <result property="legalPerson" column="legal_person" />
        <result property="registeredCapital" column="registered_capital" />
        <result property="establishDate" column="establish_date" />
        <result property="businessScope" column="business_scope" />
        <result property="registeredAddress" column="registered_address" />
        <result property="officeAddress" column="office_address" />
        <result property="contactPhone" column="contact_phone" />
        <result property="email" column="email" />
        <result property="website" column="website" />
        <result property="enterpriseStatus" column="enterprise_status" />
        <result property="certStatus" column="cert_status" />
        <result property="remark" column="remark" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
    </resultMap>

    <sql id="selectEnterpriseVo">
        select enterprise_id, enterprise_name, credit_code, legal_person, registered_capital,
               establish_date, business_scope, registered_address, office_address, contact_phone,
               email, website, enterprise_status, cert_status, remark, create_by, create_time,
               update_by, update_time from sys_enterprise
    </sql>

    <select id="selectEnterpriseList" parameterType="pd" resultMap="EnterpriseResult">
        <include refid="selectEnterpriseVo"/>
        <where>
            <if test="enterpriseName != null and enterpriseName != ''">
                and enterprise_name like concat('%', #{enterpriseName}, '%')
            </if>
            <if test="creditCode != null and creditCode != ''">
                and credit_code = #{creditCode}
            </if>
            <if test="legalPerson != null and legalPerson != ''">
                and legal_person like concat('%', #{legalPerson}, '%')
            </if>
            <if test="enterpriseStatus != null and enterpriseStatus != ''">
                and enterprise_status = #{enterpriseStatus}
            </if>
            <if test="certStatus != null and certStatus != ''">
                and cert_status = #{certStatus}
            </if>
        </where>
    </select>

    <select id="selectEnterpriseByEnterpriseId" parameterType="Long" resultMap="EnterpriseResult">
        <include refid="selectEnterpriseVo"/>
        where enterprise_id = #{enterpriseId}
    </select>

    <insert id="insertEnterprise" parameterType="pd" useGeneratedKeys="true" keyProperty="enterpriseId">
        insert into sys_enterprise
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">enterprise_name,</if>
            <if test="creditCode != null and creditCode != ''">credit_code,</if>
            <if test="legalPerson != null and legalPerson != ''">legal_person,</if>
            <if test="registeredCapital != null and registeredCapital!=''">registered_capital,</if> <!-- 数值类型仅判null -->
            <if test="establishDate != null and establishDate!=''">establish_date,</if> <!-- 日期类型仅判null -->
            <if test="businessScope != null and businessScope != ''">business_scope,</if>
            <if test="registeredAddress != null and registeredAddress != ''">registered_address,</if>
            <if test="officeAddress != null and officeAddress != ''">office_address,</if>
            <if test="contactPhone != null and contactPhone != ''">contact_phone,</if>
            <if test="email != null and email != ''">email,</if>
            <if test="website != null and website != ''">website,</if>
            <if test="enterpriseStatus != null and enterpriseStatus != ''">enterprise_status,</if>
            <if test="certStatus != null and certStatus != ''">cert_status,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">#{enterpriseName},</if>
            <if test="creditCode != null and creditCode != ''">#{creditCode},</if>
            <if test="legalPerson != null and legalPerson != ''">#{legalPerson},</if>
            <if test="registeredCapital != null and registeredCapital!=''">#{registeredCapital},</if>
            <if test="establishDate != null and establishDate!=''">#{establishDate},</if>
            <if test="businessScope != null and businessScope != ''">#{businessScope},</if>
            <if test="registeredAddress != null and registeredAddress != ''">#{registeredAddress},</if>
            <if test="officeAddress != null and officeAddress != ''">#{officeAddress},</if>
            <if test="contactPhone != null and contactPhone != ''">#{contactPhone},</if>
            <if test="email != null and email != ''">#{email},</if>
            <if test="website != null and website != ''">#{website},</if>
            <if test="enterpriseStatus != null and enterpriseStatus != ''">#{enterpriseStatus},</if>
            <if test="certStatus != null and certStatus != ''">#{certStatus},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        </trim>
    </insert>

    <update id="updateEnterprise" parameterType="pd">
        update sys_enterprise
        <trim prefix="SET" suffixOverrides=",">
            <if test="enterpriseName != null and enterpriseName != ''">enterprise_name = #{enterpriseName},</if>
            <if test="creditCode != null and creditCode != ''">credit_code = #{creditCode},</if>
            <if test="legalPerson != null and legalPerson != ''">legal_person = #{legalPerson},</if>
            <if test="registeredCapital != null and registeredCapital!=''">registered_capital = #{registeredCapital},</if>
            <if test="establishDate != null and establishDate!=''">establish_date = #{establishDate},</if>
            <if test="businessScope != null and businessScope != ''">business_scope = #{businessScope},</if>
            <if test="registeredAddress != null and registeredAddress != ''">registered_address = #{registeredAddress},</if>
            <if test="officeAddress != null and officeAddress != ''">office_address = #{officeAddress},</if>
            <if test="contactPhone != null and contactPhone != ''">contact_phone = #{contactPhone},</if>
            <if test="email != null and email != ''">email = #{email},</if>
            <if test="website != null and website != ''">website = #{website},</if>
            <if test="enterpriseStatus != null and enterpriseStatus != ''">enterprise_status = #{enterpriseStatus},</if>
            <if test="certStatus != null and certStatus != ''">cert_status = #{certStatus},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </trim>
        where enterprise_id = #{enterpriseId}
    </update>

    <delete id="deleteEnterpriseByEnterpriseId" parameterType="Long">
        delete from sys_enterprise where enterprise_id = #{enterpriseId}
    </delete>

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

    <select id="selectEnterpriseByCreditCode" parameterType="String" resultMap="EnterpriseResult">
        <include refid="selectEnterpriseVo"/>
        where credit_code = #{creditCode}
    </select>
    <select id="selectEnterpriseCount" resultType="pd">
        SELECT
            COUNT(*) as total,
            COUNT(CASE WHEN cert_status = '1' THEN 1 END) as certified,
            COUNT(CASE WHEN enterprise_status = '0' THEN 1 END) as active,
            COUNT(CASE WHEN enterprise_status = '1' THEN 1 END) as inactive
        FROM sys_enterprise
    </select>
</mapper>
