package com.fs.qw.mapper;

import java.util.List;
import com.fs.qw.domain.QwAccount;
import com.fs.qw.domain.QwAccountAudit;
import com.fs.qw.domain.QwForbiddenMessage;
import com.fs.qw.dto.QwOnlineStatusDTO;
import com.fs.qw.param.QwAccountParam;
import com.fs.qw.vo.QwAccountListVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 企微功能账号管理Mapper接口
 *
 * @author fs
 * @date 2023-08-23
 */
public interface QwAccountMapper
{
    /**
     * 查询企微功能账号管理
     *
     * @param id 企微功能账号管理ID
     * @return 企微功能账号管理
     */
    public QwAccount selectQwAccountById(Long id);

    /**
     * 查询企微功能账号管理列表
     *
     * @param qwAccount 企微功能账号管理
     * @return 企微功能账号管理集合
     */
    public List<QwAccount> selectQwAccountList(QwAccount qwAccount);

    /**
     * 新增企微功能账号管理
     *
     * @param qwAccount 企微功能账号管理
     * @return 结果
     */
    public int insertQwAccount(QwAccount qwAccount);

    /**
     * 修改企微功能账号管理
     *
     * @param qwAccount 企微功能账号管理
     * @return 结果
     */
    public int updateQwAccount(QwAccount qwAccount);

    /**
     * 删除企微功能账号管理
     *
     * @param id 企微功能账号管理ID
     * @return 结果
     */
    public int deleteQwAccountById(Long id);

    /**
     * 批量删除企微功能账号管理
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    public int deleteQwAccountByIds(Long[] ids);

    @Select({"<script> " +
            "select q.*,u.avatar,u.nick_name,u.corp_name,u.user_id as qwUserId, u.avatar,u.corp_name from qw_account q left join qw_user u on u.device_id = q.device_id  " +
            "left join company c on c.company_id = q.company_id left join company_user cu on cu.user_id = q.company_user_id  " +
            "where (q.is_audit = 1 or q.is_audit = -1) " +
            "<if test = 'maps.companyUserId != null      '> " +
            "and q.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test = 'maps.companyId != null      '> " +
            "and q.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test= 'maps.account != null  and maps.account !=\"\"    '> " +
            "and q.account like concat('%', #{maps.account}, '%')  " +
            "</if>" +
            "<if test= 'maps.companyUserName != null  and maps.companyUserName !=\"\"    '> " +
            "and cu.nick_name like concat('%', #{maps.companyUserName}, '%')  " +
            "</if>" +
            "<if test = 'maps.authCheck != null      '> " +
            "and q.auth_check =#{maps.authCheck} " +
            "</if>" +
            "<if test = 'maps.offlineType != null      '> " +
            "and q.offline_type =#{maps.offlineType} " +
            "</if>" +
            " order by q.id desc " +
            "</script>"})
    List<QwAccountListVO> selectQwAccounts(@Param("maps")QwAccountParam param);

    @Select({"<script> " +
            "select q.*,c.company_name as companyName,cu.nick_name as companyUserName,u.user_id as qwUserId, u.avatar,u.nick_name,u.corp_name,u.device_id from qw_account q " +
            "left join company c on c.company_id = q.company_id left join company_user cu on cu.user_id = q.company_user_id left join qw_user u on u.device_id = q.device_id " +
            "where 1=1 " +
            "<if test = 'maps.companyId != null      '> " +
            "and q.company_id =#{maps.companyId} " +
            "</if>" +
            "<if test = 'maps.deptId != null    '> " +
            "  AND (q.dept_id = #{maps.deptId} OR q.dept_id IN ( SELECT t.dept_id FROM company_dept t WHERE find_in_set(#{maps.deptId}, ancestors) )) " +
            "</if>" +
            "<if test = 'maps.companyUserId != null      '> " +
            "and q.company_user_id =#{maps.companyUserId} " +
            "</if>" +
            "<if test= 'maps.account != null  and maps.account !=\"\"    '> " +
            "and q.account like concat('%', #{maps.account}, '%')  " +
            "</if>" +
            "<if test= 'maps.companyUserName != null  and maps.companyUserName !=\"\"    '> " +
            "and cu.nick_name like concat('%', #{maps.companyUserName}, '%')  " +
            "</if>" +
            "<if test = 'maps.authCheck != null      '> " +
            "and q.auth_check =#{maps.authCheck} " +
            "</if>" +
            "<if test = 'maps.offlineType != null      '> " +
            "and q.offline_type =#{maps.offlineType} " +
            "</if>" +
            "<if test = 'maps.isAudit != null      '> " +
            "and q.is_audit =#{maps.isAudit} " +
            "</if>" +
            " order by q.id desc " +
            "</script>"})
    List<QwAccountListVO> selectAllQwAccounts(@Param("maps")QwAccountParam param);

    @Update("update qw_account set is_audit=#{isAudit}, device_id=#{deviceId}, remark=#{remark} where id = #{id}")
    int auditQwAccount(QwAccountAudit audit);

    @Select("select device_id from qw_account where account = #{account} ")
    String getDeviceIdByAccount(String account);

    @Select("select count(*) from qw_account where account = #{account}")
    int selectAccountCount(String account);

    @Update("update qw_account set available_device = #{availableDevice},device_id = #{deviceId} where id = #{id}")
    int updateDevice(QwAccount qwAccount);

    @Update("update qw_account set user_id = #{userId} where device_id = #{deviceId}")
    int setUserIdByDeviceId(@Param("deviceId") String deviceId,@Param("userId")Long userId);

    @Update("update qw_account set auth_check = #{authCheck} ,online_time = #{onlineTime} where device_id = #{deviceId}")
    void  setAuthCheckByDeviceId(QwOnlineStatusDTO qwOnlineStatusDTO);

    @Update("update qw_account set auth_check = #{authCheck} ,offline_time = #{offlineTime},offline_type = #{offlineType} where device_id = #{deviceId}")
    void setOfflineByDeviceId(QwOnlineStatusDTO qwOnlineStatusDTO);

    @Select("select q.account,q.company_id,q.company_user_id,c.company_name as companyName,cu.nick_name as companyUserName from qw_account " +
            " q left join company c on c.company_id = q.company_id left join company_user cu " +
            " on cu.user_id = q.company_user_id where device_id = #{deviceId} ")
    QwAccountListVO selectCompany(String deviceId);

}
