<?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="cn.ezhizao.project.business.productionCompany.mapper.BizProductionCompanyMapper">

    <resultMap type="cn.ezhizao.project.business.productionCompany.domain.BizProductionCompany" id="BizCompanyResult">
        <id column="id" property="id"/>
        <result column="source_category_id" property="sourceCategoryId" />
        <association property="sourceCategoryName" javaType="java.lang.String" select="getSourceCategoryName" column="source_category_id"/>
        <association property="referrerDataSource" javaType="java.lang.String" select="getReferrerDataSource" column="source_category_id"/>
    </resultMap>

    <select id="getSourceCategoryName" resultType="java.lang.String">
        select title from biz_source where id = #{source_category_id}
    </select>
    <select id="getReferrerDataSource" resultType="java.lang.String">
        select referrer_data_source from biz_source where id = #{source_category_id}
    </select>

    <select id="getList" parameterType="BizCompany" resultMap="BizCompanyResult">
        SELECT
        c.*, s.nick_name as leader_name, a.nick_name as adviser_name
        FROM biz_company c
        LEFT JOIN sys_user s ON c.leader_id=s.user_id
        Left join sys_user a on c.adviser_id = a.user_id
        Left join biz_entrust e on e.company_id = c.id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            c.deleted = 0
            <if test="id != null  and id != ''">AND c.id = #{id}</if>
            <if test="principal != null and principal != ''">
                AND (c.leader_id=#{principal} OR c.adviser_id=#{principal})
            </if>
            <if test="category != null  and category != ''">AND c.category = #{category}</if>
            <if test="code != null  and code != ''">AND c.code = #{code}</if>
            <if test="name != null  and name != ''">AND c.name like concat('%', #{name}, '%')</if>
            <if test="keyword != null  and keyword != ''">AND (c.name like concat('%', #{keyword}, '%') or c.code like concat('%', #{keyword}, '%'))</if>
            <if test="shortName != null  and shortName != ''">AND c.short_name like concat('%', #{shortName}, '%')</if>
            <if test="oldName != null  and oldName != ''">AND c.old_name like concat('%', #{oldName}, '%')</if>
            <if test="owner != null  and owner != ''">AND c.owner = #{owner}</if>
            <if test="phone != null  and phone != ''">AND c.phone = #{phone}</if>
            <if test="email != null  and email != ''">AND c.email = #{email}</if>
            <if test="contactAddress != null  and contactAddress != ''">AND c.contact_address = #{contactAddress}</if>
            <if test="source != null  and source != ''">AND c.source = #{source}</if>
            <if test="type != null  and type != ''">AND c.type = #{type}</if>
            <if test="socialCreditCode != null  and socialCreditCode != ''">AND c.social_credit_code =
                #{socialCreditCode}
            </if>
            <if test="mainBusiness != null  and mainBusiness != ''">AND c.main_business = #{mainBusiness}</if>
            <if test="legalRepresentative != null  and legalRepresentative != ''">AND c.legal_representative =
                #{legalRepresentative}
            </if>
            <if test="foundationDate != null  and foundationDate != ''">AND c.foundation_date = #{foundationDate}</if>
            <if test="licenceDate != null  and licenceDate != ''">AND c.licence_date = #{licenceDate}</if>
            <if test="businessStartDate != null  and businessStartDate != ''">AND c.business_start_date =
                #{businessStartDate}
            </if>
            <if test="businessEndDate != null  and businessEndDate != ''">AND c.business_end_date = #{businessEndDate}
            </if>
            <if test="isPermanentlyEffective != null ">AND c.is_permanently_effective = #{isPermanentlyEffective}</if>
            <if test="registerMoney != null  and registerMoney != ''">AND c.register_money = #{registerMoney}</if>
            <if test="registerMoneyUnit != null  and registerMoneyUnit != ''">AND c.register_money_unit =
                #{registerMoneyUnit}
            </if>
            <if test="provinceCode != null  and provinceCode != ''">AND c.province_code = #{provinceCode}</if>
            <if test="province != null  and province != ''">AND c.province = #{province}</if>
            <if test="cityCode != null  and cityCode != ''">AND c.city_code = #{cityCode}</if>
            <if test="city != null  and city != ''">AND c.city = #{city}</if>
            <if test="districtCode != null  and districtCode != ''">AND c.district_code = #{districtCode}</if>
            <if test="district != null  and district != ''">AND c.district = #{district}</if>
            <if test="address != null  and address != ''">AND c.address = #{address}</if>
            <if test="businessField != null  and businessField != ''">AND c.business_field = #{businessField}</if>
            <if test="taxType != null  and taxType != ''">AND c.tax_type = #{taxType}</if>
            <if test="isZero != null ">AND c.is_zero = #{isZero}</if>
            <if test="competentTaxAuthority != null  and competentTaxAuthority != ''">AND c.competent_tax_authority =
                #{competentTaxAuthority}
            </if>
            <if test="taxCollectorName != null  and taxCollectorName != ''">AND c.tax_collector_name like concat('%',
                #{taxCollectorName}, '%')
            </if>
            <if test="taxCollectorPhone != null  and taxCollectorPhone != ''">AND c.tax_collector_phone =
                #{taxCollectorPhone}
            </if>
            <if test="taxMonth != null ">AND c.tax_month = #{taxMonth}</if>
            <if test="openingBank != null  and openingBank != ''">AND c.opening_bank = #{openingBank}</if>
            <if test="bankAccount != null  and bankAccount != ''">AND c.bank_account = #{bankAccount}</if>
            <if test="annualIncome != null  and annualIncome != ''">AND c.annual_income = #{annualIncome}</if>
            <if test="governmentAccountNo != null  and governmentAccountNo != ''">AND c.government_account_no =
                #{governmentAccountNo}
            </if>
            <if test="governmentPassword != null  and governmentPassword != ''">AND c.government_password =
                #{governmentPassword}
            </if>
            <if test="socialSecurityAccountNo != null  and socialSecurityAccountNo != ''">AND
                c.social_security_account_no = #{socialSecurityAccountNo}
            </if>
            <if test="socialSecurityPassword != null  and socialSecurityPassword != ''">AND c.social_security_password =
                #{socialSecurityPassword}
            </if>
            <if test="employeePassword != null  and employeePassword != ''">AND c.employee_password =
                #{employeePassword}
            </if>
            <if test="housingFundPassword != null  and housingFundPassword != ''">AND c.housing_fund_password =
                #{housingFundPassword}
            </if>
            <if test="housingFundUnitAccount != null  and housingFundUnitAccount != ''">AND c.housing_fund_unit_account
                = #{housingFundUnitAccount}
            </if>
            <if test="housingFundDeductionPassword != null  and housingFundDeductionPassword != ''">AND
                c.housing_fund_deduction_password = #{housingFundDeductionPassword}
            </if>
            <if test="collectionMethod != null  and collectionMethod != ''">AND c.collection_method =
                #{collectionMethod}
            </if>
            <if test="quotaAmount != null and quotaAmount != '' ">AND c.quota_amount = #{quotaAmount}</if>
            <if test="isPayOnWindow != null and  isPayOnWindow != '' ">AND c.is_pay_on_window = #{isPayOnWindow}</if>
            <if test="isFirstSocialSecurity != null and  isFirstSocialSecurity != '' ">AND c.is_first_social_security =
                #{isFirstSocialSecurity}
            </if>
            <if test="isFirstHousingFund != null and  isFirstHousingFund != ''">AND c.is_first_housing_fund =
                #{isFirstHousingFund}
            </if>
            <if test="isFirstHousingFund != null and  isFirstHousingFund != ''">AND c.is_first_housing_fund =
                #{isFirstHousingFund}
            </if>
            <if test="leaderId != null and  leaderId != '' ">AND c.leader_id = #{leaderId}</if>
            <if test="adviserId != null  and  adviserId != ''">AND c.adviser_id = #{adviserId}</if>
            <if test="leaderName != null and  leaderName != '' ">AND s.nick_name LIKE concat('%', #{leaderName}, '%')
            </if>
            <if test="tenantId != null ">
                AND ( c.tenant_id = #{tenantId} or
                exists (select 1 from biz_entrust e where e.company_id = c.id and e.deleted = 0
                and (e.to_tenant_id = #{tenantId} or e.from_tenant_id = #{tenantId}))
                )
            </if>
            <if test="ids != null and ids.size() > 0">
                AND c.id IN
                <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
            </if>
        </trim>
        GROUP BY c.id
    </select>

    <select id="getById" parameterType="Long" resultMap="BizCompanyResult">
        SELECT * FROM biz_company WHERE id = #{id}
    </select>


    <select id="getUserById" resultType="String">
        SELECT nick_name FROM sys_user WHERE user_id = #{id}
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_company
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
            <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>
</mapper>
