package com.fs.his.mapper;

import java.util.List;
import com.fs.his.domain.FsUser;
import com.fs.his.param.FsUserParam;
import com.fs.his.vo.FsUserExportListVO;
import com.fs.his.vo.FsUserVO;
import com.fs.qw.param.QwFsUserParam;
import com.fs.qw.vo.QwFsUserVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 用户Mapper接口
 *
 * @author fs
 * @date 2023-06-07
 */
public interface FsUserMapper
{
    /**
     * 查询用户
     *
     * @param userId 用户主键
     * @return 用户
     */
    public FsUser selectFsUserByUserId(Long userId);

    /**
     * 查询用户列表
     *
     * @param fsUser 用户
     * @return 用户集合
     */
    public List<FsUser> selectFsUserList(FsUser fsUser);

    /**
     * 新增用户
     *
     * @param fsUser 用户
     * @return 结果
     */
    public int insertFsUser(FsUser fsUser);

    /**
     * 修改用户
     *
     * @param fsUser 用户
     * @return 结果
     */
    public int updateFsUser(FsUser fsUser);

    /**
     * 删除用户
     *
     * @param userId 用户主键
     * @return 结果
     */
    public int deleteFsUserByUserId(Long userId);

    /**
     * 批量删除用户
     *
     * @param userIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteFsUserByUserIds(Long[] userIds);
    @Select({"<script> " +
                "select f1.*,f2.nick_name tui_name,f2.phone tui_phone FROM fs_user f1 LEFT JOIN fs_user f2 ON f1.tui_user_id =f2.user_id "+
            " where f1.is_del=0 "+
            "  <if test=\"nickName != null  and nickName != ''\"> and f1.nick_name like concat( #{nickName}, '%')</if>\n" +
            "            <if test=\"avatar != null  and avatar != ''\"> and f1.avatar = #{avatar}</if>\n" +
            "            <if test=\"phone != null  and phone != ''\"> and f1.phone like concat('%', #{phone}, '%')</if>\n" +
            "            <if test=\"status != null \"> and f1.status = #{status}</if>\n" +
            "            <if test=\"isBuy != null \"> and f1.is_buy = #{isBuy}</if>\n" +
            "            <if test=\"userId != null \"> and f1.user_id = #{userId}</if>\n" +
            "            <if test=\"tuiUserId != null  and tuiUserId != ''\"> and f1.tui_user_id = #{tuiUserId}</if>\n" +
            "            <if test=\"tuiTime != null \"> and f1.tui_time = #{tuiTime}</if>\n" +
            "            <if test=\"userCode != null  and userCode != ''\"> and f1.user_code = #{userCode}</if>\n" +
            "            <if test=\"lastIp != null  and lastIp != ''\"> and f1.last_ip = #{lastIp}</if>\n" +
            "            <if test=\"source != null  and source != ''\"> and f1.source = #{source}</if>\n" +
            "            <if test=\"loginDevice != null  and loginDevice != ''\"> and f1.login_device = #{loginDevice}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(f1.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(f1.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "order by f1.user_id desc"+
            "</script>"})
    List<FsUserVO> selectFsUserListVO(FsUserParam fsUser);
    @Update("update fs_user set is_del=1 where user_id=#{userId}")
    int updateFsUserByUserId(Long userId);

    @Select("select f1.*,f2.nick_name tui_name,f2.phone tui_phone FROM fs_user f1 LEFT JOIN fs_user f2 ON f1.tui_user_id =f2.user_id where f1.user_id=#{userId} ")
    FsUserVO selectFsUserVoByUserId(Long userId);

    @Select("select * from fs_user where ma_open_id=#{openId}")
    FsUser selectFsUserByOpenId(String openId);
    @Select("select * from fs_user where phone=#{phone}")
    FsUser selectFsUserByPhone(String phone);

    @Select("select * from fs_user where phone=#{phone} limit 1")
    FsUser selectFsUserByPhoneLimitOne(String phone);
    @Select({"<script> " +
            "select distinct f1.*,f2.nick_name tui_name,f2.phone tui_phone FROM company_user_user cuu LEFT JOIN fs_user f1 ON cuu.user_id =f1.user_id left JOIN fs_user f2 ON f1.tui_user_id =f2.user_id"+
            " where f1.is_del=0 "+
            "            <if test=\"companyUserId != null \"> and cuu.company_user_id=#{companyUserId}</if>\n" +
            "            <if test=\"companyId != null \"> and cuu.company_id=#{companyId}</if>\n" +
            "            <if test=\"nickName != null  and nickName != ''\"> and f1.nick_name like concat( #{nickName}, '%')</if>\n" +
            "            <if test=\"avatar != null  and avatar != ''\"> and f1.avatar = #{avatar}</if>\n" +
            "            <if test=\"phone != null  and phone != ''\"> and f1.phone like concat('%', #{phone}, '%')</if>\n" +
            "            <if test=\"status != null \"> and f1.status = #{status}</if>\n" +
            "            <if test=\"isBuy != null \"> and f1.is_buy = #{isBuy}</if>\n" +
            "            <if test=\"userId != null \"> and f1.user_id = #{userId}</if>\n" +
            "            <if test=\"tuiUserId != null  and tuiUserId != ''\"> and f1.tui_user_id = #{tuiUserId}</if>\n" +
            "            <if test=\"tuiTime != null \"> and f1.tui_time = #{tuiTime}</if>\n" +
            "            <if test=\"userCode != null  and userCode != ''\"> and f1.user_code = #{userCode}</if>\n" +
            "            <if test=\"lastIp != null  and lastIp != ''\"> and f1.last_ip = #{lastIp}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(f1.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(f1.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "order by f1.user_id desc" +
            "</script>"})
    List<FsUserVO> selectFsUserListVOByComponentsUser(FsUserParam fsUser);

    @Select({"<script> " +
            "SELECT  \n" +
            "    fp.create_time ,\n" +
            "    cu.nick_name ,\n" +
            "    fu.user_id ,\n" +
            "    fp.patient_name ,\n" +
            "    fp.mobile ,\n" +
            "    fu.is_buy \n" +
            "FROM fs_user fu\n" +
            "LEFT JOIN company_user_user cuu ON fu.user_id = cuu.user_id\n" +
            "LEFT JOIN company_user cu ON cuu.company_user_id = cu.user_id\n" +
            "LEFT JOIN fs_patient fp ON fu.user_id = fp.user_id\n" +
            "WHERE fp.create_time IS NOT NULL and fu.is_del = 0 \n" +
            "  <if test=\"companyUserId != null \"> and cuu.company_user_id=#{companyUserId} </if>\n" +
            "  <if test=\"companyId != null \"> and cuu.company_id=#{companyId} </if>\n" +
            "  <if test=\"nickName != null  and nickName != ''\"> and fu.nick_name like concat( #{nickName}, '%')</if>\n" +
            "   <if test=\"phone != null  and phone != ''\"> and fu.phone like concat( #{phone}, '%')</if>\n" +
            "   <if test=\"status != null \"> and fu.status = #{status}</if>\n" +
            "            <if test=\"isBuy != null \"> and fu.is_buy = #{isBuy}</if>\n" +
            "            <if test=\"userId != null \"> and fu.user_id = #{userId}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(fu.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(fu.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "ORDER BY fu.user_id DESC "+
            "</script>"})
    List<FsUserExportListVO> selectFsUserExportListVO(FsUserParam fsUser);

    @Select("SELECT user_id\n" +
            "FROM fs_user_integral_logs\n" +
            "WHERE business_type = 2\n" +
            "GROUP BY user_id\n" +
            "HAVING COUNT(user_id) >= 10;")
    List<Long> selectUserIds();
    @Select("select user_id from fs_store_order where `status`>1 or `status`<-1 GROUP BY user_id ")
    List<Long> selectUserByStoreOrder();
    @Select({"<script> " +
            "select f1.*,f2.nick_name tui_name,f2.phone tui_phone FROM fs_user f1 LEFT JOIN fs_user f2 ON f1.tui_user_id =f2.user_id "+
            " where f1.is_del=0 "+
            "  <if test=\"nickName != null  and nickName != ''\"> and f1.nick_name like concat(#{nickName}, '%')</if>\n" +
            "            <if test=\"avatar != null  and avatar != ''\"> and f1.avatar = #{avatar}</if>\n" +
            "            <if test=\"phone != null  and phone != ''\"> and f1.phone = #{phone}</if>\n" +
            "            <if test=\"status != null \"> and f1.status = #{status}</if>\n" +
            "            <if test=\"isBuy != null \"> and f1.is_buy = #{isBuy}</if>\n" +
            "            <if test=\"userId != null \"> and f1.user_id = #{userId}</if>\n" +
            "            <if test=\"tuiUserId != null  and tuiUserId != ''\"> and f1.tui_user_id = #{tuiUserId}</if>\n" +
            "            <if test=\"tuiTime != null \"> and f1.tui_time = #{tuiTime}</if>\n" +
            "            <if test=\"userCode != null  and userCode != ''\"> and f1.user_code = #{userCode}</if>\n" +
            "            <if test=\"lastIp != null  and lastIp != ''\"> and f1.last_ip = #{lastIp}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(f1.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(f1.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "order by f1.user_id desc " +
            "</script>"})
    List<FsUserVO> selectFsUserListVOByComponent(FsUserParam fsUser);


    @Select("select * from fs_user where union_id=#{unionid} limit 1")
    FsUser selectFsUserByUnionid(String unionid);

    @Select("<script>" +
            "SELECT * FROM fs_user WHERE user_id IN " +
            "<foreach item='id' collection='userIds' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<FsUser> findUsersByIds(@Param("userIds") List<Long> userIds);

    @Select({"<script> " +
            "select user_id, nick_name, phone FROM fs_user where is_del=0 "+
            "  <if test=\"nickName != null  and nickName != ''\"> and nick_name like concat( #{nickName}, '%')</if>\n" +
            "            <if test=\"phone != null  and phone != ''\"> and phone =#{phone}</if>\n" +
            "order by user_id desc"+
            "</script>"})
    List<QwFsUserVO> selectQwFsUserListVO(QwFsUserParam qwFsUserParam);

    @Select("select * from fs_user where user_id=#{userId} and TIMESTAMPDIFF(DAY, create_time, NOW()) <= 7 ")
    FsUser selectFsUserIsNew(@Param("userId")Long userId);

    @Select("select * from fs_user where is_vip = 1 ")
    List<FsUser> selectFsUserIsVip();
    @Select("select user_id,nick_name,phone from fs_user where create_time > '2024-06-01' and  phone is not null ")
    List<FsUser> selectUserPhoneAndId();

    @Select({"<script> " +
            "select user_id, nick_name, phone FROM fs_user where is_del=0 "+
            "  <if test=\"nickName != null  and nickName != ''\"> and nick_name like concat( #{nickName}, '%')</if>\n" +
            "            <if test=\"phone != null  and phone != ''\"> and phone like concat( #{phone}, '%')</if>\n" +
            "order by user_id desc"+
            "</script>"})
    List<QwFsUserVO> selecQwFsUserListVO(QwFsUserParam qwFsUserParam);

    @Select({"<script> " +
            "SELECT count(1) FROM fs_user fu\n" +
            "LEFT JOIN company_user_user cuu ON fu.user_id = cuu.user_id\n" +
            "LEFT JOIN company_user cu ON cuu.company_user_id = cu.user_id\n" +
            "LEFT JOIN fs_patient fp ON fu.user_id = fp.user_id\n" +
            "WHERE fp.create_time IS NOT NULL and fu.is_del = 0 \n" +
            "  <if test=\"companyUserId != null \"> and cuu.company_user_id=#{companyUserId} </if>\n" +
            "  <if test=\"companyId != null \"> and cuu.company_id=#{companyId} </if>\n" +
            "  <if test=\"nickName != null  and nickName != ''\"> and fu.nick_name like concat( #{nickName}, '%')</if>\n" +
            "   <if test=\"phone != null  and phone != ''\"> and fu.phone like concat( #{phone}, '%')</if>\n" +
            "   <if test=\"status != null \"> and fu.status = #{status}</if>\n" +
            "            <if test=\"isBuy != null \"> and fu.is_buy = #{isBuy}</if>\n" +
            "            <if test=\"userId != null \"> and fu.user_id = #{userId}</if>\n" +
            "            <if test=\"sTime != null \">  and DATE(fu.create_time) &gt;= DATE(#{sTime})</if>\n" +
            "            <if test=\"eTime != null \">  and DATE(fu.create_time) &lt;= DATE(#{eTime})</if>\n" +
            "ORDER BY fu.user_id DESC "+
            "</script>"})
    Long selectFsUserExportListVOCount(FsUserParam fsUser);

    @Select("select * from fs_user where phone=#{phone}")
    FsUser selectFsUserByMpOpenId(@Param("phone") String phone);

    @Select("select * from fs_user where chhy_ma_open_id=#{openId}")
    FsUser selectFsUserByChhyOpenId(String openId);
}
