<?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.github.wxiaoqi.security.admin.mapper.AppShareholderDetailMapper">
    <resultMap id="appShareholderDetailMap" type="com.github.wxiaoqi.security.admin.entity.AppShareholderDetail">
        <result property="id" column="id"/>
        <result property="companyName" column="company_name"/>
        <result property="userId" column="user_id"/>
        <result property="phone" column="phone"/>
        <result property="companyId" column="company_id"/>
        <result property="name" column="name"/>
        <result property="crtTime" column="crt_time"/>
        <result property="isQuit" column="is_quit"/>
        <result property="updTime" column="upd_time"/>
        <result property="positionId" column="position_id"/>
    </resultMap>

    <select id="selectMaxShareHolderByUserIdOrPhone" resultType="java.lang.Integer">
        select max(`position_id`) from `app_shareholder_detail` where `is_quit`=0
        <choose>
            <when test="userId != null and (phone == null or phone != '')">
                and `user_id`=#{userId}
            </when>
            <when test="phone != null and phone != '' and userId == null">
                and `phone`=#{phone}
            </when>
            <otherwise>
                and `user_id`=#{userId}
            </otherwise>
        </choose>
    </select>

    <select id="updShareHolderIsQuit" resultType="com.github.wxiaoqi.security.admin.entity.AppShareholderDetail">
        UPDATE `app_shareholder_detail`
        SET
        <if test="isQuit!=null">
            `is_quit`=#{isQuit}
        </if>
        WHERE
        1 = 1
        <if test="phone!=null">
            and `phone`=#{phone}
        </if>
        <if test="companyId!=null">
            and `company_id`=#{companyId}
        </if>
    </select>


    <select id="selectShareHolderByUserIdOrPhone" resultType="java.lang.Integer">
        select min(`position_id`) from `app_shareholder_detail` where `is_quit`=0
        <choose>
            <when test="userId != null and (phone == null or phone != '')">
                and `user_id`=#{userId}
            </when>
            <when test="phone != null and phone != '' and userId == null">
                and `phone`=#{phone}
            </when>
            <otherwise>
                and `user_id`=#{userId}
            </otherwise>
        </choose>
    </select>
    <select id="findPage" resultType="com.github.wxiaoqi.security.admin.entity.AppShareholderDetail">

        select * from (
        select max(id) as id,max(name) as `name`,max(is_quit) as `isQuit`,max(position_id) as `positionId`,phone as
        `phone`,group_concat(company_name) as `companyName`,group_concat(company_id) as `companyIdArray` from
        app_shareholder_detail
        where is_quit=0 and
        position_id=1 group by phone
        union ALL
        select max(id) as id,max(name) as `name`,max(is_quit) as `isQuit`,max(position_id) as `positionId`,phone as
        `phone`,group_concat(company_name) as `companyName` ,group_concat(company_id) as `companyIdArray` from
        app_shareholder_detail
        where is_quit=0 and
        position_id=2 group by phone
        union ALL
        select max(id) as id,max(name) as `name`,max(is_quit) as `isQuit`,max(position_id) as `positionId`,phone as
        `phone`,group_concat(company_name) as `companyName` ,group_concat(company_id) as `companyIdArray`from
        app_shareholder_detail
        where is_quit=1 and position_id
        =1 group by phone
        union all
        select max(id) as id,max(name) as `name`,max(is_quit) as `isQuit`,max(position_id) as `positionId`,phone as
        `phone`,group_concat(company_name) as `companyName` ,group_concat(company_id) as `companyIdArray`from
        app_shareholder_detail
        where is_quit=1 and position_id
        =2 group by phone
        ) a
        where 1=1
        <if test="name !=null and name !='' ">
            and a.name like CONCAT('%',#{name},'%')
        </if>
        <if test="positionId!=null">
            and a.positionId=#{positionId}
        </if>
        <if test="phone!=null">
            and a.phone=#{phone}
        </if>
        <!--<if test="companyId!=null">
            and a.companyIdArray=#{companyId}
        </if>-->
        <if test="companyId!=null">
            and find_in_set(#{companyId}, a.companyIdArray)
        </if>
    </select>

    <resultMap id="detail" type="com.github.wxiaoqi.security.admin.entity.AppShareholderDetail">
        <result property="id" column="id"/>
        <result property="companyName" column="companyName"/>
        <result property="userId" column="user_id"/>
        <result property="phone" column="phone"/>
        <result property="companyId" column="company_id"/>
        <result property="name" column="name"/>
        <result property="crtTime" column="crt_time"/>
        <result property="isQuit" column="is_quit"/>
        <result property="updTime" column="upd_time"/>
        <result property="positionId" column="positionId"/>
    </resultMap>
</mapper>