package com.fs.company.mapper;

import com.fs.company.domain.CompanyUser;
import com.fs.company.param.CompanyUserAreaParam;
import com.fs.company.param.CompanyUserQwParam;
import com.fs.company.vo.CompanyQwUserByIdsVo;
import com.fs.company.vo.CompanyUserQwListVO;
import com.fs.company.vo.CompanyUserVO;
import com.fs.company.vo.DocCompanyUserVO;
import com.fs.qw.param.QwWatchLogStatisticsListParam;
import com.fs.qw.vo.CompanyUserQwVO;
import com.fs.qw.vo.QwUserVO;
import com.fs.wxUser.domain.CompanyWxUser;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Set;

/**
 * 物业公司管理员信息Mapper接口
 *
 * @author fs
 * @date 2021-05-25
 */
public interface CompanyUserMapper
{
    /**
     * 查询物业公司管理员信息
     *
     * @param userId 物业公司管理员信息ID
     * @return 物业公司管理员信息
     */
    public CompanyUser selectCompanyUserById(Long userId);

    /**
     * 查询物业公司管理员信息列表
     *
     * @param companyUser 物业公司管理员信息
     * @return 物业公司管理员信息集合
     */
    public List<CompanyUser> selectCompanyUserList(CompanyUser companyUser);

    /**
     * 新增物业公司管理员信息
     *
     * @param companyUser 物业公司管理员信息
     * @return 结果
     */
    public int insertCompanyUser(CompanyUser companyUser);

    /**
     * 修改物业公司管理员信息
     *
     * @param companyUser 物业公司管理员信息
     * @return 结果
     */
    public int updateCompanyUser(CompanyUser companyUser);

    @Update("UPDATE company_user SET qw_user_id = NULL,qw_status=0 where user_id=#{userId}")
    public int updateCompanyUserByNullQwUserID(@Param("userId") Long companyUserId);

    public int updateAllCompanyUser(CompanyUser companyUser);

    /**
     * 删除物业公司管理员信息
     *
     * @param userId 物业公司管理员信息ID
     * @return 结果
     */
    public int deleteCompanyUserById(Long userId);

    /**
     * 批量删除物业公司管理员信息
     *
     * @param userIds 需要删除的数据ID
     * @return 结果
     */
    public int deleteCompanyUserByIds(Long[] userIds);

    int updateUserAvatar(@Param("userName")String username, @Param("avatar") String avatar);

    int resetUserPwd(@Param("userName")String userName,  @Param("password")String encryptPassword);


    CompanyUser selectUserByUserName(@Param("userName")String userName);
    @Select("select count(1) from company_user where user_name=#{userName}")
    int checkUserName(String userName);
    @Update("update company_user set password=#{password} where user_id=#{userId}")
    int resetUserPwdByUserId( @Param("userId")Long userId, @Param("password") String encryptPassword);

    List<CompanyUser> selectAllUsersList(CompanyUser user);
//    @Select({"<script> " +
//            "select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.open_id,u.id_card, d.dept_name, d.leader from company_user u\n" +
//            "        left join company_dept d on u.dept_id = d.dept_id\n" +
//            "        where u.del_flag = '0' and u.status=0\n" +
//            "        <if test=\"companyId != null  \">\n" +
//            "            AND u.company_id = #{companyId}\n" +
//            "        </if>\n" +
//            "        <if test=\"nickName != null and nickName != ''\">\n" +
//            "            AND u.nick_name like concat(#{nickName}, '%')\n" +
//            "        </if>\n" +
//            "        <if test=\"phonenumber != null and phonenumber != ''\">\n" +
//            "            AND u.phonenumber like concat('%', #{phonenumber}, '%')\n" +
//            "        </if>\n" +
//            "        <if test=\"beginTime != null and beginTime != ''\"><!-- 开始时间检索 -->\n" +
//            "            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')\n" +
//            "        </if>\n" +
//            "        <if test=\"endTime != null and endTime != ''\"><!-- 结束时间检索 -->\n" +
//            "            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')\n" +
//            "        </if>\n" +
//            "        <if test=\"deptId != null and deptId != 0\">\n" +
//            "            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))\n" +
//            "        </if>"+
//            "</script>"})
    List<CompanyUserVO> selectMyUserList(CompanyUser user);

    @Select("select u.* from company_user u where u.dept_id in ( SELECT dept_id FROM company_dept WHERE dept_id = #{maps.deptId} or find_in_set( #{maps.deptId} , ancestors ) ) ${maps.params.dataScope}")
    List<CompanyUser> getUserListByDeptId(@Param("maps") CompanyUser user);

    @Select("select count(1) from company_user  where company_id=#{companyId} and del_flag=0  ")
    Integer selectCompanyUserCountByCompanyId(Long companyId);

    @Select("select count(1) from company_user  where user_name=#{userName} and del_flag=0  ")
    Integer checkUserNameUnique(String userName);

    @Select("select u.*,d.dept_name from company_user u left join company_dept d on d.dept_id=u.dept_id  where u.user_id in (select )  ")
    List<DocCompanyUserVO> selectDocCompanyUserList(Long docId);

    @Select("select u.*,d.dept_name from company_user u left join company_dept d on d.dept_id=u.dept_id  where find_in_set(u.user_id,#{companyUserIds})  ")
    List<DocCompanyUserVO> selectDocCompanyUserListByIds(String companyUserIds);

    @Select("select u.* from company_user u   where u.company_id=#{companyId} and u.nick_name=#{nickName} limit 1 ")
    CompanyUser selectCompanyUserByCompanyIdAndNickName(@Param("companyId")long companyId,@Param("nickName") String nickName);

//    @Select({"<script> " +
//            "select u.user_id, u.dept_id, u.nick_name, u.user_name, u.email, u.avatar, u.phonenumber, u.password, u.sex, u.status, u.del_flag, u.login_ip, u.login_date, u.create_by, u.create_time, u.remark,u.open_id,u.id_card, d.dept_name, d.leader ,\n" +
//            "        (select count(1) from crm_customer cc where u.user_id= cc.receive_user_id and date_format(cc.receive_time,'%y%m%d') = date_format(now(),'%y%m%d')  ) as now_day_customer_count\n" +
//            "        from company_user u\n" +
//            "        left join company_dept d on u.dept_id = d.dept_id\n" +
//            "\n" +
//            "        where u.del_flag = '0' and u.status=0\n" +
//            "        <if test=\"companyId != null  \">\n" +
//            "            AND u.company_id = #{companyId}\n" +
//            "        </if>\n" +
//            "        <if test=\"nickName != null and nickName != ''\">\n" +
//            "            AND u.nick_name like concat( #{nickName}, '%')\n" +
//            "        </if>\n" +
//            "        <if test=\"phonenumber != null and phonenumber != ''\">\n" +
//            "            AND u.phonenumber like concat('%', #{phonenumber}, '%')\n" +
//            "        </if>\n" +
//            "        <if test=\"beginTime != null and beginTime != ''\"><!-- 开始时间检索 -->\n" +
//            "            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')\n" +
//            "        </if>\n" +
//            "        <if test=\"endTime != null and endTime != ''\"><!-- 结束时间检索 -->\n" +
//            "            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')\n" +
//            "        </if>\n" +
//            "        <if test=\"deptId != null and deptId != 0\">\n" +
//            "            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))\n" +
//            "        </if>"+
//            "</script>"})
    List<CompanyUserVO> selecCompanyAllUsersList(CompanyUser map);

    @Select("select user_id from company_user where company_id=#{companyId} and user_type = '00'")
    Long selectCompanyAdminByCompanyId(Long companyId);

    @Select("select * from company_user where company_id=#{companyId} and user_type = '00'")
    CompanyUser selectCompanyUserAdminByCompanyId(Long companyId);


    @Select("SELECT cu.*, qu.qw_user_id AS qw_id, qu.id AS qw_user_id_actual " +
            "FROM company_user cu " +
            "RIGHT JOIN qw_user qu ON qu.company_user_id = cu.user_id  and qu.is_del=0 " +
            "WHERE cu.company_id = #{maps.companyId} " +
            "AND cu.del_flag = 0 " +
            "AND cu.qw_user_id IS NOT NULL " +
            "AND cu.dept_id IN ( " +
            "    SELECT cd.dept_id FROM company_dept cd " +
            "    WHERE cd.dept_id = #{maps.deptId} " +
            "    OR FIND_IN_SET(#{maps.deptId}, cd.ancestors))")
    List<CompanyUserQwVO> getUserListByDeptIdToQwUserid(@Param("maps") CompanyUser user);

    @Select("select * from  qw_user  where corp_id=#{corpId} and company_id=#{companyId}")
    List<QwUserVO> selectCompanyQwUserList(@Param("corpId") String corpId,@Param("companyId")Long companyId);

    @Select({"<script> " +
            "select u.*, d.dept_name, d.leader from company_user u\n" +
            "        left join company_dept d on u.dept_id = d.dept_id\n" +
            "        where u.del_flag = '0'\n" +
            "        <if test=\"userName != null and userName != ''\">\n" +
            "            AND u.user_name like concat('%', #{userName}, '%')\n" +
            "        </if>\n" +
            "\n" +
            "        <if test=\"nickName != null and nickName != ''\">\n" +
            "            AND u.nick_name like concat( #{nickName}, '%')\n" +
            "        </if>\n" +
            "        <if test=\"companyId != null and companyId != ''\">\n" +
            "            AND u.company_id = #{companyId}\n" +
            "        </if>\n" +
            "        <if test=\"status != null and status != ''\">\n" +
            "            AND u.status = #{status}\n" +
            "        </if>\n" +
            "        <if test=\"qwStatus != null \">\n" +
            "            AND u.qw_status = #{qwStatus} \n" +
            "        </if>\n" +
            "        <if test=\"phonenumber != null and phonenumber != ''\">\n" +
            "            AND u.phonenumber like concat('%', #{phonenumber}, '%')\n" +
            "        </if>\n" +
            "        <if test=\"beginTime != null and beginTime != ''\"><!-- 开始时间检索 -->\n" +
            "            AND date_format(u.create_time,'%y%m%d') &gt;= date_format(#{beginTime},'%y%m%d')\n" +
            "        </if>\n" +
            "        <if test=\"endTime != null and endTime != ''\"><!-- 结束时间检索 -->\n" +
            "            AND date_format(u.create_time,'%y%m%d') &lt;= date_format(#{endTime},'%y%m%d')\n" +
            "        </if>\n" +
            "        <if test=\"deptId != null and deptId != 0\">\n" +
            "            AND (u.dept_id = #{deptId} OR u.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{deptId}, ancestors) ))\n" +
            "        </if>\n" +
            "        <!-- 数据范围过滤 -->\n" +
            "        ${params.dataScope}"+
            "</script>"})
    List<CompanyUserQwListVO> selectCompanyUserQwListVO(CompanyUserQwParam user);


    @Update({
            "<script>",
            "  UPDATE company_user",
            "  SET address_id = #{data.addressId}",
            "  WHERE user_id IN",
            "  <foreach collection='data.userIds' item='userId' open='(' separator=',' close=')'>",
            "    #{userId}",
            "  </foreach>",
            "</script>"
    })
    public int updateCompanyUserAreaList(@Param("data") CompanyUserAreaParam param);

    @Select("select * from  company_wx_user  where company_user_id=#{companyUserId} and nick_name=#{nickName}")
    CompanyWxUser selectCompanyUserByNickName(@Param("companyUserId") Long companyUserId, @Param("nickName") String nickName);

    List<CompanyQwUserByIdsVo> selectCompanyQwUserByIds(@Param("ids") List<Long> ids);
    @Select("select qw_user_id FROM company_user where user_id=#{companyUserId}")
    String selectQwUserIdsByCompany(Long companyUserId);

    @Select("select * from company_user where user_id=#{userId}")
    CompanyUser selectCompanyUserByUserId(Long userId);


    @Select("select * from company_user where company_id=#{companyId} and del_flag=0")
    List<CompanyUser> selectCompanyUserByCompanyId(Long companyId);

    /**
     * 查询公司销售列表
     * @param ids 销售ID集合
     * @return  list
     */
    List<CompanyUser> selectCompanyUserByIds(@Param("ids") Set<Long> ids);

    List<CompanyUser> selectAllCompanyUserByParentId(Long parentId);

    List<CompanyUser> selectAllCompanyUserAndSelf(Long userId);

    /**
     * 批量审核用户
     * @param userIds 用户ID集合
     */
    void auditUsers(@Param("userIds") List<Long> userIds);

    List<CompanyUser> getAllUserListLimit(@Param("companyId") Long companyId,
                                          @Param("keywords") String keywords);

    @Select("select domain from company_user where user_id = #{userId}")
    String selectDomainByUserId(Long userId);

    Long selectQwWatchLogStatisticsListVONewCount(QwWatchLogStatisticsListParam param);

    @Select("select * from company_user where ma_open_id = #{maOpenId}")
    CompanyUser getCompanyUserByOpenId(String openId);

    @Select("select * from company_user where phonenumber = #{phoneNumber}")
    CompanyUser getCompanyUserByPhone(String phoneNumber);

}
