<?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.jeeplus.test.wdt.merchantCompany.mapper.MerchantCompanyMapper">

    <resultMap id="merchantCompanyResult" type="com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO">
        <id property="id" column="id"/>
        <result property="companyName" column="companyName"/>
        <result property="companyType" column="companyType"/>
        <result property="linkmanName" column="linkmanName"/>
        <result property="linkmanPhone" column="linkmanPhone"/>
        <result property="practitionerCount" column="practitionerCount"/>
        <result property="annualSales" column="annualSales"/>
        <result property="commerceShop" column="commerceShop"/>
        <result property="onlineRetailSales" column="onlineRetailSales"/>
        <collection property="settledPlatformDTOList" javaType="java.util.List" ofType="com.jeeplus.test.wdt.merchantCompany.service.dto.SettledPlatformDTO">
            <id property="id" column="settledPlatformDTO.id"/>
            <result property="name" column="settledPlatformDTO.name"/>
            <result property="sort" column="settledPlatformDTO.sort"/>
        </collection>
        <collection property="merchantProductDTOList" javaType="java.util.List" ofType="com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantProductDTO">
            <id property="id" column="merchantProductDTO.id"/>
            <result property="name" column="merchantProductDTO.name"/>
            <result property="sort" column="merchantProductDTO.sort"/>
        </collection>
    </resultMap>

    <sql id="merchantCompanyColumns">
		distinct a.id AS "id",
		a.create_by_id AS "createBy.id",
		a.create_time AS "createTime",
		a.update_by_id AS "updateBy.id",
		a.update_time AS "updateTime",
		a.del_flag AS "delFlag",
		a.remarks AS "remarks",
        a.company_name AS "companyName",
        a.company_type AS "companyType",
        a.linkman_name AS "linkmanName",
        a.linkman_phone AS "linkmanPhone",
        a.practitioner_count AS "practitionerCount",
        a.annual_sales AS "annualSales",
        a.commerce_shop AS "commerceShop",
        a.online_retail_sales AS "onlineRetailSales"
	</sql>

    <sql id="merchantCompanyJoins">
		LEFT JOIN sys_user user ON user.id = a.create_by_id
	</sql>



    <select id="findById" resultMap="merchantCompanyResult">
        SELECT
        <include refid="merchantCompanyColumns"/>
        ,msp.id AS "settledPlatformDTO.id",
        msp.sort AS "settledPlatformDTO.sort",
        msp.name AS "settledPlatformDTO.name",
        mp.id AS "merchantProductDTO.id",
        mp.sort AS "merchantProductDTO.sort",
        mp.name AS "merchantProductDTO.name"
        FROM merchant_company_info a
        <include refid="merchantCompanyJoins"/>
        left join merchant_company_occupancy_relation_info mcori on mcori.company_id = a.id
        left join merchant_settled_platform msp on msp.id = mcori.occupancy_id

        left join merchant_company_product_relation_info mcpri on mcpri.company_id = a.id
        left join merchant_product mp on mp.id = mcpri.product_id
        WHERE a.id = #{id} and a.del_flag = 0
    </select>

    <select id="getByCompanyName" resultType="com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO">
        SELECT
        <include refid="merchantCompanyColumns"/>
        FROM merchant_company_info a
        <include refid="merchantCompanyJoins"/>
        WHERE a.company_name = #{companyName} and a.del_flag = 0
    </select>

    <select id="findList" resultType="com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO">
        SELECT
        <include refid="merchantCompanyColumns"/>
        ,(select group_concat(msp.name) from merchant_settled_platform msp
        LEFT JOIN merchant_company_occupancy_relation_info mcori ON msp.id = mcori.occupancy_id where mcori.company_id = a.id
        ) as "settledPlatformStr"
        FROM merchant_company_info a
        <include refid="merchantCompanyJoins"/>
        left join merchant_company_occupancy_relation_info mcori on mcori.company_id = a.id
        left join merchant_settled_platform msp on msp.id = mcori.occupancy_id
        ${ew.customSqlSegment}
    </select>

    <select id="getListJson" resultType="com.jeeplus.test.wdt.merchantCompany.service.dto.MerchantCompanyDTO">
        SELECT
        <include refid="merchantCompanyColumns"/>
        FROM merchant_company_info a
        <include refid="merchantCompanyJoins"/>
        ${ew.customSqlSegment}
    </select>

    <delete id="deleteSettledPlatform">
        delete from merchant_company_occupancy_relation_info where company_id = #{id}
    </delete>

    <insert id="insertSettledPlatform">
        INSERT INTO merchant_company_occupancy_relation_info(company_id, occupancy_id)
        VALUES (#{id}, #{settledPlatformId})
    </insert>

    <delete id="deleteMerchantProduct">
        delete from merchant_company_product_relation_info where company_id = #{id}
    </delete>

    <insert id="insertMerchantProduct">
        INSERT INTO merchant_company_product_relation_info(company_id, product_id)
        VALUES (#{id}, #{merchantProductId})
    </insert>

</mapper>
