package cn.pluss.platform.device.impl;

import cn.pluss.platform.device.DeviceOperateInfoService;
import cn.pluss.platform.entity.DeviceOperateInfo;
import cn.pluss.platform.entity.UserApp;
import cn.pluss.platform.enums.DeviceOperateType;
import cn.pluss.platform.exception.MsgException;
import cn.pluss.platform.mapper.DeviceOperateInfoMapper;
import cn.pluss.platform.vo.DeviceOperateInfoVO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 设备信息流转表 服务实现类
 * </p>
 *
 * @author crystal
 * @since 2022-04-02
 */
@Service
public class DeviceOperateInfoServiceImpl extends ServiceImpl<DeviceOperateInfoMapper, DeviceOperateInfo> implements DeviceOperateInfoService {

    /**
     * 保存设备操作信息
     * @param userApp
     * @param type
     * @param deviceNo
     * @param remark
     */
    @Override
    public void saveDeviceOperateInfo(UserApp userApp, DeviceOperateType type,String deviceNo,String remark) {
        MsgException.checkNull(type,"设备操作类型不能为空");
        MsgException.checkBlank(deviceNo,"设备号不能为空");
        DeviceOperateInfo info = new DeviceOperateInfo(userApp,type.getCode(),deviceNo,remark);
        baseMapper.insert(info);
    }

    /**
     * 批量操作
     * @param userApp
     * @param type
     * @param deviceNoList
     * @param remark
     */
    @Override
    public void saveDeviceOperateListInfo(UserApp userApp, DeviceOperateType type, List<String> deviceNoList, String remark) {
        MsgException.checkNull(type,"设备操作类型不能为空");
        MsgException.check(deviceNoList.size() == 0,"设备号集合不能为空");
        List<DeviceOperateInfo> list = new ArrayList<>(deviceNoList.size());
        for (String deviceNo:deviceNoList) {
            DeviceOperateInfo info = new DeviceOperateInfo(userApp,type.getCode(),deviceNo,remark);
            list.add(info);
        }
        saveBatch(list);
    }

    /**
     * 查询设备号查询指定设备流转详情
     * @param snNo
     * @return
     */
    @Override
    public List<DeviceOperateInfoVO> selectListVoByDeviceNo(String snNo) {
        MsgException.checkBlank(snNo,"缺失设备号！");
        return baseMapper.selectListVo(snNo);
    }

    @Override
    public Page<DeviceOperateInfoVO> pageInfo(Page<DeviceOperateInfoVO> page, DeviceOperateInfo condition) {
        return baseMapper.pageInfo(page,condition);
    }
}
