package com.sptg.estate.biz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.estate.api.entity.Room;
import com.sptg.estate.api.entity.VideoCallAccount;
import com.sptg.estate.api.feigns.VideoCallAccountFeign;
import com.sptg.estate.biz.service.RoomServiceInterface;
import com.sptg.estate.biz.service.VideoCallAccountServiceInterface;
import com.sptg.permissions.api.entity.DeviceAdminUser;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

/**
 * title: VideoCallAccountController
 * package: com.sptg.estate.biz.controller
 * description:
 *
 *                  
 *                
 */
@RestController
@RequiredArgsConstructor
public class VideoCallAccountController implements VideoCallAccountFeign {

    private final VideoCallAccountServiceInterface videoCallAccountServiceInterface;
    private final RoomServiceInterface roomServiceInterface;

    @Override
    @PostMapping(value = "/video/callAccount/addEntityByUser")
    public void addEntityByUser(@RequestBody VideoCallAccount account) {
        this.videoCallAccountServiceInterface.createAccountByUser(account);
    }

    @Override
    @PostMapping(value = "/video/callAccount/addEntityByDevice")
    public void addEntityByDevice(@RequestBody VideoCallAccount account) {
        this.videoCallAccountServiceInterface.createAccountByDevice(account);
    }

    @Override
    @PostMapping(value = "/video/callAccount/addEntityByAdminUser")
    public void addEntityByAdminUser(@RequestBody DeviceAdminUser user) {
        this.videoCallAccountServiceInterface.createAccountByAdminUser(user);
    }

    @Override
    @PostMapping(value = "/video/callAccount/getRoomCallGroupId")
    public ResultBean<Object> getRoomCallGroupId(@RequestBody VideoCallAccount account) {
        Room room = this.getRoom(account);
        Assert.notNull(room,"门牌号不存在");
        //  获取呼叫组Id
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect(" DISTINCT call_group_id ");
        wrapper.eq(" xq_id ",account.getXqId());
        if (null != account.getUnitId()){
            wrapper.eq(" unit_id ", account.getUnitId());
        }
        wrapper.eq(" room_id ",room.getId());
        wrapper.eq("account_type",1);
        wrapper.notIn("status",-1);
        Object result = this.videoCallAccountServiceInterface.selectObj(wrapper);
        Assert.notNull(result,"住户未绑定视频对讲账号");
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/video/callAccount/getManageCallGroupId")
    public ResultBean<Object> getManageCallGroupId(@RequestBody VideoCallAccount account) {
        //  获取呼叫组Id
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect(" DISTINCT call_group_id ");
        wrapper.eq(" xq_id ",account.getXqId());
        wrapper.eq("account_type",3);
        wrapper.notIn("status",-1);
        Object result = this.videoCallAccountServiceInterface.selectObj(wrapper);
        Assert.notNull(result,"小区未绑定管理机");
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    @Override
    @PostMapping(value = "/video/callAccount/getCallAccountIdBySerialNumber")
    public ResultBean<Object> getCallAccountIdBySerialNumber(@RequestBody VideoCallAccount account) {
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.setSqlSelect(" DISTINCT account_id ");
        wrapper.eq("serial_number",account.getSerialNumber());
        wrapper.notIn("status",-1);
        Object result = this.videoCallAccountServiceInterface.selectObj(wrapper);
        Assert.notNull(result,"设备未绑定视频对讲账号");
        return new ResultBean<>(ResultEnum.SUCCESS,result);
    }

    private Room getRoom(VideoCallAccount account){
        Wrapper<Room> wrapper = new EntityWrapper<>();
        wrapper.eq("l_residential_id",account.getXqId());
        wrapper.eq("l_unit_id",account.getUnitId());
        wrapper.eq("s_house_number",account.getRoomNumber());
        //  获取房间信息
        return this.roomServiceInterface.selectOne(wrapper);
    }

    @Override
    @GetMapping(value = "/video/callAccount/getDeviceCallAccount")
    public ResultBean<VideoCallAccount> getDeviceCallAccount(@RequestParam(value = "serialNumber")String serialNumber) {
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("serial_number",serialNumber);
        wrapper.eq("account_type",2);
        wrapper.notIn("status",-1);
        VideoCallAccount account = this.videoCallAccountServiceInterface.selectOne(wrapper);
        Assert.notNull(account,"设备未绑定视频对讲账号");
        return new ResultBean<>(ResultEnum.SUCCESS,account);
    }

    @Override
    @GetMapping(value = "/video/callAccount/getCallAccountByUser")
    public VideoCallAccount getCallAccountByUser(@RequestParam(value = "userId")Long userId) {
        Wrapper<VideoCallAccount> wrapper = new EntityWrapper<>();
        wrapper.eq("user_id",userId);
        wrapper.notIn("status",-1);
        return this.videoCallAccountServiceInterface.selectOne(wrapper);
    }
}
