<?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.cool.store.mapper.ShopAccountMapper">
    <resultMap id="BaseResultMap" type="com.cool.store.entity.ShopAccountDO">
        <!--
          WARNING - @mbg.generated
        -->
        <id column="id" jdbcType="INTEGER" property="id"/>
        <result column="shop_id" jdbcType="INTEGER" property="shopId"/>
        <result column="system_name" jdbcType="VARCHAR" property="systemName"/>
        <result column="account" jdbcType="VARCHAR" property="account"/>
        <result column="bound_phone" jdbcType="VARCHAR" property="boundPhone"/>
        <result column="password_salt" jdbcType="VARCHAR" property="passwordSalt"/>
        <result column="password" jdbcType="VARCHAR" property="password"/>
        <result column="status" jdbcType="BIT" property="status"/>
        <result column="entry_status" jdbcType="TINYINT" property="entryStatus"/>
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="last_sync_time" jdbcType="TIMESTAMP" property="lastSyncTime"/>
        <result column="secondary_password" jdbcType="VARCHAR" property="secondaryPassword"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
    </resultMap>

    <!-- 批量新增 -->
    <insert id="batchInsert">
        INSERT INTO xfsg_shop_account (
        shop_id,
        system_name,
        account,
        bound_phone,
        password_salt,
        password,
        status,
        entry_status,
        last_sync_time,
        secondary_password,
        remark
        ) VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.shopId},
            #{item.systemName},
            #{item.account},
            #{item.boundPhone},
            #{item.passwordSalt},
            #{item.password},
            #{item.status},
            #{item.entryStatus},
            #{item.lastSyncTime},
            #{item.secondaryPassword},
            #{item.remark}
            )
        </foreach>
    </insert>

    <!-- 根据shopId查询多条数据 -->
    <select id="selectByShopId" resultMap="BaseResultMap">
        SELECT
        id,
        shop_id,
        system_name,
        account,
        bound_phone,
        password_salt,
        password,
        status,
        entry_status,
        create_time,
        last_sync_time,
        secondary_password,
        remark
        FROM
        xfsg_shop_account
        WHERE
        shop_id = #{shopId}
        order by id
    </select>

    <select id="selectByShopIdAndSystemName" resultMap="BaseResultMap">
        SELECT
        id,
        shop_id,
        system_name,
        account,
        bound_phone,
        password_salt,
        password,
        status,
        entry_status,
        create_time,
        last_sync_time,
        secondary_password,
        remark
        FROM
        xfsg_shop_account
        WHERE
        shop_id = #{shopId}
        and system_name = #{systemName}
    </select>

    <!-- 根据shopId与system_name修改status -->
    <update id="updateStatusByShopIdAndSystemName">
        UPDATE
        xfsg_shop_account
        <set>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="syncTime != null">
                last_sync_time = #{syncTime},
            </if>
            <if test="ylsCode != null">
                account = #{ylsCode},
            </if>
        </set>
        WHERE
        shop_id = #{shopId}
        and system_name in
        <foreach close=")" collection="systemNameList" item="systemName" open="(" separator=",">
            #{systemName}
        </foreach>
    </update>

    <update id="updateEntryStatusByShopIdAndSystemName">
        UPDATE
        xfsg_shop_account
        SET
        entry_status = #{entryStatus,jdbcType=BIT}
        WHERE
        shop_id = #{shopId}
        and system_name in
        <foreach close=")" collection="systemNameList" item="systemName" open="(" separator=",">
            #{systemName}
        </foreach>
    </update>


    <update id="updateAccountByShopIdAndSystemName">
        UPDATE
        xfsg_shop_account
        SET
        account = #{account},
        password = #{password},
        status = #{status}
        WHERE
        shop_id = #{shopId}
        AND system_name = #{systemName}
    </update>


    <!-- 批量修改密码和密码盐 -->
    <update id="batchUpdatePasswordByShopIds">
        UPDATE
        xfsg_shop_account
        SET
        password = #{password},
        secondary_password = #{secondaryPassword},
        password_salt = #{passwordSalt},
        last_sync_time = #{lastSyncTime,jdbcType=TIMESTAMP}
        WHERE
        system_name in ('火码POS','云流水','新掌柜')
        and shop_id IN
        <foreach close=")" collection="shopIds" item="shopId" open="(" separator=",">
            #{shopId}
        </foreach>
    </update>
    <update id="dateHandle">
        <foreach collection="list" item="item" separator=";">
            update xfsg_shop_account
            <set>
                <if test="item.passwordSalt != null">
                    password_salt = #{item.passwordSalt},
                </if>
                <if test="item.password != null">
                    password = #{item.password},
                </if>
                <if test="item.secondaryPassword != null">
                    secondary_password = #{item.secondaryPassword},
                </if>
                remark = #{item.remark}
            </set>
            where id = #{item.id}

        </foreach>
    </update>
    <update id="updateStatusDataHandle">

        update xfsg_shop_account
        <set>
            status = 5
        </set>
        where
        shop_id in
        <foreach collection="list" item="shopId" open="(" close=")" separator=",">
            #{shopId}
        </foreach>
        and system_name in ('火码POS','云流水','新掌柜') and status !=5
    </update>


    <select id="getALlFail">
        select * from xfsg_shop_account WHERE
        system_name in ('火码POS')
        and status = 6
    </select>
    <select id="getSpecificByShopIds" resultType="com.cool.store.entity.ShopAccountDO">
        select shop_id as shopId,system_name as systemName,status from xfsg_shop_account
        where shop_id in
        <foreach collection="shopIds" item="shopId" open="(" close=")" separator=",">
            #{shopId}
        </foreach>
    </select>
    <select id="getPasswordIsNull" resultType="java.lang.Long">
        SELECT shop_id as shopId FROM xfsg_shop_account
        WHERE `password` is null and `system_name` = '火码POS'

    </select>


    <select id="getEntryPendingList" resultMap="BaseResultMap">
        select
        *
        from xfsg_shop_account a
        left join xfsg_shop_info si on a.shop_id = si.id
        <if test="request.signTypes != null and request.signTypes.size() >0">
            left join xfsg_sign_franchise sign on a.shop_id = sign.shop_id
        </if>
        <where>
            si.deleted = 0 and si.shop_status !=2 and a.`entry_status` in (2,3,4,6)
            <if test="systemName!=null and systemName!=''">
                and a.system_name = #{systemName}
            </if>
            <if test="request.shopKeyword !=null and request.shopKeyword !=''">
                and (si.shop_name like concat('%',#{request.shopKeyword},'%') or si.shop_code like concat('%',#{request.shopKeyword},'%'))
            </if>
            <if test="request.regionIds !=null and request.regionIds.size()>0">
                and si.region_id in
                <foreach collection="request.regionIds" item="regionId" index="index" open="(" separator="," close=")">
                    #{regionId}
                </foreach>
            </if>
            <if test="request.storeTypes !=null and request.storeTypes.size()>0">
                and si.store_type in
                <foreach collection="request.storeTypes" item="storeType" index="index" open="(" separator="," close=")">
                    #{storeType}
                </foreach>
            </if>
            <if test="request.signTypes !=null and request.signTypes.size()>0">
                and sign.sign_type in
                <foreach collection="request.signTypes" item="signType" index="index" open="(" separator="," close=")">
                    #{signType}
                </foreach>
            </if>
            <if test="request.supervisorId != null and request.supervisorId!= ''">
                and si.investment_manager = #{request.supervisorId}
            </if>
            <if test="request.joinModes !=null and request.joinModes.size()>0">
                and si.join_mode in
                <foreach collection="request.joinModes" item="joinMode" index="index" open="(" separator="," close=")">
                    #{joinMode}
                </foreach>
            </if>
            order by a.create_time desc
        </where>
    </select>
</mapper>