package com.dcsoft.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.system.domain.SysEmpowerRecord;
import com.dcsoft.system.domain.SysPeople;
import com.dcsoft.system.domain.vo.SysFileVo;
import com.dcsoft.system.uniubi.domain.EwmInfo;
import com.dcsoft.system.uniubi.domain.LadderControlVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dcsoft.system.mapper.SysEquipmentMapper;
import com.dcsoft.system.domain.SysEquipment;
import com.dcsoft.system.service.ISysEquipmentService;
import org.springframework.util.CollectionUtils;

/**
 * 设备信息Service业务层处理
 *
 * @author nichun
 * @date 2023-03-08
 */
@Service
public class SysEquipmentServiceImpl implements ISysEquipmentService
{
    @Autowired
    private SysEquipmentMapper sysEquipmentMapper;

    /**
     * 查询设备信息
     *
     * @param id 设备信息主键
     * @return 设备信息
     */
    @Override
    public SysEquipment selectSysEquipmentById(Long id)
    {
        SysEquipment equipment = sysEquipmentMapper.selectSysEquipmentById(id);
        // 查询访客机图片或视频
        SysFileVo sysFile = new SysFileVo();
        sysFile.setBusinessType("sys_fkj");
        sysFile.setBusinessId(String.valueOf(id));
        List<SysFileVo> list = sysEquipmentMapper.queryFile(sysFile);
        if(!CollectionUtils.isEmpty(list)) {
            equipment.setFileList(list);
        }
        return equipment;
    }

    /**
     * 查询设备信息列表
     *
     * @param sysEquipment 设备信息
     * @return 设备信息
     */
    @Override
    public List<SysEquipment> selectSysEquipmentList(SysEquipment sysEquipment)
    {
        return sysEquipmentMapper.selectSysEquipmentList(sysEquipment);
    }

    /**
     * 新增设备信息
     *
     * @param sysEquipment 设备信息
     * @return 结果
     */
    @Override
    public int insertSysEquipment(SysEquipment sysEquipment)
    {
        sysEquipment.setCreateTime(DateUtils.getNowDate());
        return sysEquipmentMapper.insertSysEquipment(sysEquipment);
    }

    /**
     * 修改设备信息
     *
     * @param sysEquipment 设备信息
     * @return 结果
     */
    @Override
    public int updateSysEquipment(SysEquipment sysEquipment)
    {
        sysEquipment.setUpdateTime(DateUtils.getNowDate());
        return sysEquipmentMapper.updateSysEquipment(sysEquipment);
    }

    /**
     * 批量删除设备信息
     *
     * @param ids 需要删除的设备信息主键
     * @return 结果
     */
    @Override
    public int deleteSysEquipmentByIds(Long[] ids)
    {
        return sysEquipmentMapper.deleteSysEquipmentByIds(ids);
    }

    /**
     * 删除设备信息信息
     *
     * @param id 设备信息主键
     * @return 结果
     */
    @Override
    public int deleteSysEquipmentById(Long id)
    {
        return sysEquipmentMapper.deleteSysEquipmentById(id);
    }

    @Override
    public SysEquipment selectEquipmentBySequence(String deviceNo) {
        return sysEquipmentMapper.selectEquipmentBySequence(deviceNo);
    }

    /**
     * 查询设备信息
     */
    @Override
    public List<SysEquipment> queryEquipmentInfo(List<String> pointIds) {
        return sysEquipmentMapper.queryEquipmentInfo(pointIds);
    }

    /**
     * 查询人员信息
     */
    @Override
    public SysPeople queryPeopleInfo(SysPeople people) {
        return sysEquipmentMapper.queryPeopleInfo(people);
    }

    @Override
    public List<Map<String, Object>> selectSysEquipmentPeopleById(Long id) {
        return sysEquipmentMapper.selectSysEquipmentPeopleById(id);
    }

    /**
     * 根据设备序列号查询产品类型
     * @param deviceKey
     * @return
     */
    @Override
    public String queryDeviceSequence(String deviceKey) {
        return sysEquipmentMapper.queryDeviceSequence(deviceKey);
    }


    /**
     * 查询授权记录规则id
     * @param people
     * @return
     */
    @Override
    public List<SysEmpowerRecord> queryEmpowerRecord(SysPeople people) {
        return sysEquipmentMapper.queryEmpowerRecord(people);
    }

    /**
     * 查询人员信息
     * @param sysPeople
     * @return
     */
    @Override
    public SysPeople queryPeople(SysPeople sysPeople) {
        return sysEquipmentMapper.queryPeople(sysPeople);
    }


    @Override
    public SysEquipment queryEmpower(String sequence) {
        return sysEquipmentMapper.queryEmpower(sequence);
    }

    /**
     * 根据设备序列好查询设备信息
     * @param sysEquipment
     * @return
     */
    @Override
    public SysEquipment queryEquipmentByDeviceKey(SysEquipment sysEquipment) {
        SysEquipment equipment = sysEquipmentMapper.queryEquipmentByDeviceKey(sysEquipment);
        // 查询访客机图片或视频
        SysFileVo sysFile = new SysFileVo();
        sysFile.setBusinessType("sys_fkj");
        sysFile.setBusinessId(String.valueOf(equipment.getId()));
        List<SysFileVo> list = sysEquipmentMapper.queryFile(sysFile);
        if(!CollectionUtils.isEmpty(list)) {
            equipment.setFileList(list);
        }
        return equipment;
    }

    /**
     * 根据设备序列号修改设备密码
     * @param sysEquipment
     */
    @Override
    public void updateEquipmentByDeviceKey(SysEquipment sysEquipment) {
        if(StringUtils.isNotEmpty(sysEquipment.getIp())) {
            sysEquipment.setDeviceIp(sysEquipment.getIp() + ":" + sysEquipment.getPort());
        }
        SysEquipment equipment = sysEquipmentMapper.selectEquipmentBySequence(sysEquipment.getDeviceKey());
        if(equipment != null) {
            sysEquipment.setId(equipment.getId());
            saveFile(sysEquipment);
        }
        sysEquipmentMapper.updateEquipmentByDeviceKey(sysEquipment);
    }

    @Override
    public void saveFile(SysEquipment sysEquipment) {
        SysFileVo sysFile = new SysFileVo();
        sysFile.setBusinessId(String.valueOf(sysEquipment.getId()));
        sysFile.setBusinessType("sys_fkj");
        sysEquipmentMapper.deleteFile(sysFile);
        List<SysFileVo> fileList = sysEquipment.getFileList();
        if(!CollectionUtils.isEmpty(fileList)) {
            List<SysFileVo> sysFiles = new ArrayList<>();
            for (SysFileVo fileVo : fileList) {
                SysFileVo file = new SysFileVo();
                file.setBusinessId(String.valueOf(sysEquipment.getId()));
                file.setFileName(fileVo.getFileName());
                file.setUrl(fileVo.getUrl());
                file.setBusinessType("sys_fkj");
                file.setId(UUID.randomUUID().toString().replace("-", ""));
                sysFiles.add(file);
            }
            sysEquipmentMapper.saveFile(sysFiles);
        }

    }

    /**
     * 根据设备序列号修改设备轮播文件
     * @param sysEquipment
     */
    @Override
    public void updateEquipmentFile(SysEquipment sysEquipment) {
        SysEquipment equipment = sysEquipmentMapper.queryEquipmentByDeviceKey(sysEquipment);
        equipment.setFileList(sysEquipment.getFileList());
        saveFile(equipment);
    }


}
