package com.yida.data.device.consume.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.consume.EduConsumeDevice;
import com.yida.data.common.core.entity.system.Dept;
import com.yida.data.common.core.entity.system.enums.SysFunctionEnum;
import com.yida.data.common.core.exception.FebsException;
import com.yida.data.device.consume.mapper.EduConsumeDeviceMapper;
import com.yida.data.device.consume.service.EduConsumeDeviceService;
import com.yida.data.device.vo.consume.ConsumeDeviceVO;
import com.yida.data.system.feign.RemoteDeptService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 消费机设备Service实现
 *
 * @author ZYJ
 * @date 2023-03-14 18:02:27
 */
@Slf4j
@Service
@RequiredArgsConstructor
@Transactional(rollbackFor = Exception.class)
public class EduConsumeDeviceServiceImpl extends ServiceImpl
        <EduConsumeDeviceMapper, EduConsumeDevice> implements EduConsumeDeviceService {

    private final RemoteDeptService remoteDeptService;

    @Override
    public Page<ConsumeDeviceVO> listDevicePage(Integer pageNum, Integer pageSize, String number, Integer state, Long schoolId) {
        List<Long> schoolIds = new ArrayList<>();
        if (Objects.isNull(schoolId)) {
            List<Dept> deptList = remoteDeptService.getSchoolByArea(null, "").getData();
            if (CollUtil.isEmpty(deptList)) {
                schoolIds.add(-1L);
            } else {
                schoolIds = deptList.stream().map(Dept::getDeptId).collect(Collectors.toList());
            }
        } else {
            schoolIds.add(schoolId);
        }
        Page<ConsumeDeviceVO> devicePage = baseMapper.listDevicePage(new Page<>(pageNum, pageSize), schoolIds, number, state);
        devicePage.getRecords().forEach(consumeDeviceVO -> consumeDeviceVO.setSchoolName(remoteDeptService.getInfoById(consumeDeviceVO.getSchoolId()).getData().getDeptName()));
        return devicePage;
    }

    @Override
    public void saveDevice(EduConsumeDevice device) {
        // 查询设备号是否重复
        EduConsumeDevice eduConsumeDevice = getOne(Wrappers.lambdaQuery(new EduConsumeDevice())
                .eq(EduConsumeDevice::getSn, device.getSn())
                .ne(Objects.nonNull(device.getId()), EduConsumeDevice::getId, device.getId())
        );
        if (Objects.nonNull(eduConsumeDevice)) {
            throw new FebsException("该设备号已存在，请勿重复添加");
        }

        if (Objects.isNull(device.getId())) {
            device.setDeviceType(SysFunctionEnum.HAI_PU_TIAN.getValue());
        }
        saveOrUpdate(device);
    }
}
