package com.ruoyi.basicData.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.basicData.domain.TCustomerContact;
import com.ruoyi.basicData.mapper.TCustomerContactMapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.basicData.mapper.TWarehouseWebcamMapper;
import com.ruoyi.basicData.domain.TWarehouseWebcam;
import com.ruoyi.basicData.service.ITWarehouseWebcamService;

/**
 * 仓库摄像头Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-21
 */
@Service
public class TWarehouseWebcamServiceImpl implements ITWarehouseWebcamService {
    @Autowired
    private TWarehouseWebcamMapper tWarehouseWebcamMapper;

    @Autowired
    private TCustomerContactMapper customerContactMapper;

    /**
     * 查询仓库摄像头
     *
     * @param fId 仓库摄像头ID
     * @return 仓库摄像头
     */
    @Override
    public TWarehouseWebcam selectTWarehouseWebcamById(Long fId) {
        return tWarehouseWebcamMapper.selectTWarehouseWebcamById(fId);
    }

    /**
     * 查询仓库摄像头列表
     *
     * @param tWarehouseWebcam 仓库摄像头
     * @return 仓库摄像头
     */
    @Override
    public List<TWarehouseWebcam> selectTWarehouseWebcamList(TWarehouseWebcam tWarehouseWebcam) {
        return tWarehouseWebcamMapper.selectTWarehouseWebcamList(tWarehouseWebcam);
    }

    /**
     * 查询仓库摄像头
     *
     * @param deviceSerial 设备序列号
     * @return 仓库摄像头
     */
    @Override
    public TWarehouseWebcam selectTWarehouseWebcamByDeviceSerial(String deviceSerial) {
        return tWarehouseWebcamMapper.selectTWarehouseWebcamByDeviceSerial(deviceSerial);
    }

    /**
     * APP查询仓库摄像头列表
     *
     * @param tWarehouseWebcam 仓库摄像头
     * @return 仓库摄像头
     */
    @Override
    public List<TWarehouseWebcam> selectAppTWarehouseWebcamList(TWarehouseWebcam tWarehouseWebcam) {
        if (StringUtils.isNotNull(tWarehouseWebcam.getfWarehouseId())) {
            return tWarehouseWebcamMapper.selectAppTWarehouseWebcamList(tWarehouseWebcam.getfWarehouseId(), null);
        }

        TCustomerContact customerContact = new TCustomerContact();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        Long corpId = null;
        if ("外部用户".equals(user.getDept().getDeptName())) {
            customerContact.setfTel(SecurityUtils.getLoginUser().getUser().getUserName());
            List<TCustomerContact> tCustomerContacts = customerContactMapper.selectTCustomerContactList(customerContact);
            if (CollectionUtils.isNotEmpty(tCustomerContacts)) {
                corpId = tCustomerContacts.get(0).getfPid();
            }
        }

        return tWarehouseWebcamMapper.selectAppTWarehouseWebcamList(null, corpId);
    }

    /**
     * 新增仓库摄像头
     *
     * @param tWarehouseWebcam 仓库摄像头
     * @return 结果
     */
    @Override
    public int insertTWarehouseWebcam(TWarehouseWebcam tWarehouseWebcam) {
        tWarehouseWebcam.setCreateTime(DateUtils.getNowDate());
        return tWarehouseWebcamMapper.insertTWarehouseWebcam(tWarehouseWebcam);
    }

    /**
     * 修改仓库摄像头
     *
     * @param tWarehouseWebcam 仓库摄像头
     * @return 结果
     */
    @Override
    public int updateTWarehouseWebcam(TWarehouseWebcam tWarehouseWebcam) {
        tWarehouseWebcam.setUpdateTime(DateUtils.getNowDate());
        return tWarehouseWebcamMapper.updateTWarehouseWebcam(tWarehouseWebcam);
    }

    /**
     * 批量删除仓库摄像头
     *
     * @param fIds 需要删除的仓库摄像头ID
     * @return 结果
     */
    @Override
    public int deleteTWarehouseWebcamByIds(Long[] fIds) {
        return tWarehouseWebcamMapper.deleteTWarehouseWebcamByIds(fIds);
    }

    /**
     * 删除仓库摄像头信息
     *
     * @param fId 仓库摄像头ID
     * @return 结果
     */
    @Override
    public int deleteTWarehouseWebcamById(Long fId) {
        return tWarehouseWebcamMapper.deleteTWarehouseWebcamById(fId);
    }
}
