package com.zd.laboratory.service.impl;

import com.zd.common.core.domain.per.PerPrefix;
import com.zd.common.core.exception.ServiceException;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.core.utils.StringUtils;
import com.zd.common.datascope.annotation.DataScope;
import com.zd.common.security.utils.SaveUtil;
import com.zd.laboratory.domain.LabSparseHardware;
import com.zd.laboratory.mapper.LabSparseHardwareMapper;
import com.zd.laboratory.service.ILabSparseHardwareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 疏散硬件Service业务层处理
 *
 * @author liubo
 * @date 2022-01-25
 */
@Service
public class LabSparseHardwareServiceImpl implements ILabSparseHardwareService {
    @Autowired
    private LabSparseHardwareMapper labSparseHardwareMapper;

    /**
     * 查询疏散硬件
     *
     * @param id 疏散硬件主键
     * @return 疏散硬件
     */
    @Override
    public LabSparseHardware selectLabSparseHardwareById(Long id) {
        return labSparseHardwareMapper.selectLabSparseHardwareById(id);
    }

    /**
     * 查询疏散硬件列表
     *
     * @param labSparseHardware 疏散硬件
     * @return 疏散硬件
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "t", permi = PerPrefix.LABORATORY_SPARSEHARDWARE)
    public List<LabSparseHardware> selectLabSparseHardwareList(LabSparseHardware labSparseHardware) {
        return labSparseHardwareMapper.selectLabSparseHardwareList(labSparseHardware);
    }

    /**
     * 新增疏散硬件
     *
     * @param labSparseHardware 疏散硬件
     * @return 结果
     */
    @Override
    public int insertLabSparseHardware(LabSparseHardware labSparseHardware) {
        //设置其他公共字段
        SaveUtil.setCommonAttr(labSparseHardware);

        try {
            labSparseHardwareMapper.insertLabSparseHardware(labSparseHardware);
        }catch (DuplicateKeyException e){
            throw new ServiceException("设备编号已存在，操作失败！");
        }

        return 1;
    }

    /**
     * 修改疏散硬件
     *
     * @param labSparseHardware 疏散硬件
     * @return 结果
     */
    @Override
    public int updateLabSparseHardware(LabSparseHardware labSparseHardware) {
        labSparseHardware.setUpdateTime(DateUtils.getNowDate());
        labSparseHardware.setUpdateBy(SecurityUtils.getUsername());
        if((StringUtils.isNull(labSparseHardware.getBuildId()) && StringUtils.isNull(labSparseHardware.getFloorId())) || StringUtils.isNull(labSparseHardware.getSubjectId())){
            labSparseHardwareMapper.updateLabSparseHardwareByFloor(labSparseHardware);
        }
        return labSparseHardwareMapper.updateLabSparseHardware(labSparseHardware);
    }

    /**
     * 批量删除疏散硬件
     *
     * @param ids 需要删除的疏散硬件主键
     * @return 结果
     */
    @Override
    public int deleteLabSparseHardwareByIds(Long[] ids) {
        return labSparseHardwareMapper.deleteLabSparseHardwareByIds(ids);
    }

    /**
     * 删除疏散硬件信息
     *
     * @param id 疏散硬件主键
     * @return 结果
     */
    @Override
    public int deleteLabSparseHardwareById(Long id) {
        return labSparseHardwareMapper.deleteLabSparseHardwareById(id);
    }

    @Override
    public List<String> selectCameraByFloorId(Long floorId) {
        return labSparseHardwareMapper.selectCameraByFloorId(floorId);
    }

    @Override
    public List<Map<String,Object>> selectSpeakerByFloorId(Long floorId, Long subId) {
        return labSparseHardwareMapper.selectListByFloorId(floorId, subId,2);
    }
}
