package com.fs.wxUser.mapper;

import com.fs.wxUser.domain.CompanyWxUser;
import com.fs.wxUser.param.CompanyWxUserParam;
import com.fs.wxUser.param.CompanyWxUserSopParam;
import com.fs.wxUser.vo.CompanyWxUserVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 个微用户Mapper接口
 *
 * @author fs
 * @date 2024-10-22
 */
public interface CompanyWxUserMapper
{
    /**
     * 查询个微用户
     *
     * @param userId 个微用户主键
     * @return 个微用户
     */
    public CompanyWxUser selectCompanyWxUserByUserId(Long userId);

    /**
     * 查询个微用户列表
     *
     * @param companyWxUser 个微用户
     * @return 个微用户集合
     */
    public List<CompanyWxUser> selectCompanyWxUserList(CompanyWxUser companyWxUser);

    @Select("<script> " +
            "select cwu.*,cu.nick_name as companyUserName  from company_wx_user cwu " +
            "left join  company_user cu " +
            "on cwu.company_user_id =cu.user_id " +
            "        <where>  \n" +
            "            <if test=\"map.companyId != null \"> and cwu.company_id = #{map.companyId}</if>\n" +
            "            <if test=\"map.companyUserId != null \"> and cwu.company_user_id = #{map.companyUserId}</if>\n" +
            "            <if test=\"map.nickName != null  and map.nickName != ''\"> and cwu.nick_name like concat(#{map.nickName}, '%')</if>\n" +
            "            <if test=\"map.companyUserName != null  and map.companyUserName != ''\"> and cu.nick_name like concat( #{map.companyUserName}, '%')</if>\n" +
            "            <if test=\"map.weixinId != null  and map.weixinId != ''\"> and cwu.weixin_id = #{map.weixinId}</if>\n" +
            "            <if test=\"map.status != null \"> and cwu.status = #{map.status}</if>\n" +
            "            <if test=\"map.avatar != null  and map.avatar != ''\"> and cwu.avatar = #{map.avatar}</if>\n " +
            "<if test='map.uniqueGroupIds!=null'>\n" +
            "    and (\n" +
            "        <foreach collection='map.uniqueGroupIds' item='item' index='index' separator=' and '>\n" +
            "            find_in_set( #{item}, REGEXP_REPLACE(cwu.group_id, '[\"\\\\[\\\\]]', '') )\n" +
            "        </foreach>\n" +
            "    )\n" +
            "</if>"+
            "        </where> " +
            "</script>")
    public List<CompanyWxUserVO> selectCompanyWxUserListVO(@Param("map") CompanyWxUserParam param);

    /**
     * 新增个微用户
     *
     * @param companyWxUser 个微用户
     * @return 结果
     */
    public int insertCompanyWxUser(CompanyWxUser companyWxUser);

    /**
     * 修改个微用户
     *
     * @param companyWxUser 个微用户
     * @return 结果
     */
    public int updateCompanyWxUser(CompanyWxUser companyWxUser);

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

    /**
     * 批量删除个微用户
     *
     * @param userIds 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteCompanyWxUserByUserIds(Long[] userIds);

    @Select("select * from company_wx_user where weixin_id=#{weixinId}")
    CompanyWxUser selectCompanyWxUserByWeixinId(String weixinId);

    @Select("<script>" +
            "SELECT cxu.*  " +
            "FROM " +
            "company_wx_user cxu " +
            "WHERE cxu.company_id =#{map.companyId} and cxu.company_user_id  in " +
            " <foreach collection='map.userIdsSelectList'  item='item' index='index'  open='(' separator=',' close=')'> #{item} </foreach> " +
            "<if test ='map.filterType==2 and map.tagsIdsSelectList!=null'> " +
            "  and ( \n" +
            "    <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' or '> \n" +
            "        find_in_set( #{item} , REGEXP_REPLACE(cxu.group_id,  '[\"\\\\[\\\\]]', '' ) ) \n" +
            "    </foreach> \n" +
            "    )" +
            "</if> " +
            "<if test='map.filterType == 1 and  map.tagsIdsSelectList!=null'>\n" +
            "    and (\n" +
            "        <foreach collection='map.tagsIdsSelectList' item='item' index='index' separator=' and '>\n" +
            "            find_in_set( #{item}, REGEXP_REPLACE(cxu.group_id, '[\"\\\\[\\\\]]', '') )\n" +
            "        </foreach>\n" +
            "    )\n" +
            "</if>"+
            "</script>")
    List<CompanyWxUser> selectCompanyWxUserByCompanyUserId(@Param("map") CompanyWxUserSopParam wxUserSopParam);

    List<CompanyWxUser> listCompanyIds(@Param("ids") String[] ids);
}
