package com.sptg.estate.biz.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.IService;
import com.sptg.device.api.entity.Device;
import com.sptg.estate.api.entity.Member;
import com.sptg.estate.api.entity.VideoCallAccount;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import org.springframework.scheduling.annotation.Async;
import org.springframework.util.Assert;

import java.io.Serializable;

/**
 * title: VideoCallAccountServiceInterface
 * package: com.sptg.estate.biz.security
 * description: 视频呼叫账号-CRUD业务接口
 *
 *                  
 *                
 */
public interface VideoCallAccountServiceInterface extends IService<VideoCallAccount> {

    /**
     * 创建视频通话-账号
     * @param userId 账号Id
     * @param roomId 房间Id
     * @return VideoCallAccount
     */
    VideoCallAccount createAccount(Long userId,Long roomId);

    /**
     * 创建视频通话-账号
     * @param id 账号Id
     * @return VideoCallAccount
     */
    VideoCallAccount createAccount(Long id);

    /**
     * 创建视频对讲账号,会员与住户绑定成功时调用
     * @param account 账号信息
     */
    @Async
    default void createAccountByUser(VideoCallAccount account){
        account.getUserList().forEach(user -> {
            //  判断住户是否已绑定视频账号
            Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
            wrapper.eq("user_id",user.getId());
            wrapper.notIn("status",-1);
            if (this.selectCount(wrapper) <= 0){
                //  创建视频账号
                VideoCallAccount entity = this.createAccount(user.getId(),user.getRoomId());
                entity.setAccountType(1);
                //  填充字段
                Member member = account.getMember();
                entity.setCustomerId(member.getId());
                entity.setCustomerName(member.getName());
                entity.setUserName(user.getName());
                entity.setOrgPosition(user.getOrgPosition());
                entity.setXqId(user.getResidentialId());
                entity.setUnitId(user.getUnitId());
                this.insert(entity);
            }
        });
    }

    /**
     * 创建视频对讲账号,设备绑定小区单元成功时调用
     * @param account 账号信息
     */
    @Async
    default void createAccountByDevice(VideoCallAccount account){
        Assert.notNull(account.getDevice(),"设备不能为空");
        Device device = account.getDevice();
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("serial_number",device.getSerialNumber());
        wrapper.notIn("status",-1);
        VideoCallAccount dbAccount = this.selectOne(wrapper);
        if (null != dbAccount){
            this.pushCallAccount(dbAccount);
        }else {
            VideoCallAccount entity = this.createAccount(device.getId());
            entity.setAccountType(2);
            entity.setOrgPosition(device.getOrgPosition());
            entity.setXqId(device.getXqId());
            entity.setUnitId(device.getUnitId());
            entity.setSerialNumber(device.getSerialNumber());
            if (this.insert(entity)){
                this.pushCallAccount(entity);
            }
        }
    }

    /**
     * 创建视频对讲账号,创建管理机账号时调用
     * @param user 账号信息
     */
    @Async
    default void createAccountByAdminUser(DeviceAdminUser user){
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",user.getId());
        wrapper.notIn("status",-1);
        if (this.selectCount(wrapper) <= 0){
            //  创建视频账号
            VideoCallAccount entity = this.createAccount(user.getId(),user.getXqId());
            entity.setUserId(user.getId());
            entity.setAccountType(3);
            //  填充字段
            entity.setUserName(user.getUserName());
            entity.setOrgPosition(user.getOrgPosition());
            entity.setXqId(user.getXqId());
            this.insert(entity);
        }
    }

    /**
     * 下发视频对讲账号到设备
     * @param account 账号信息
     */
    void pushCallAccount(VideoCallAccount account);

    /**
     * 根据会员Id获取对讲账号
     * @param customerId 会员Id
     * @return VideoCallAccount
     */
    default VideoCallAccount selectByCustomerId(Serializable customerId){
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("customer_id",customerId);
        wrapper.eq("account_type",1);
        wrapper.notIn("status",-1);
        return this.selectOne(wrapper);
    }

    /**
     * 根据用户Id删除对讲账号
     * @param userId 用户Id
     */
    void deleteAccountByUserId(Long userId);

    /**
     * 根据房间Id删除对讲账号
     * @param roomId 房间Id
     */
    void deleteAccountByRoomId(Long roomId);
}