package com.mortals.xhx.module.device.service.impl;

import org.springframework.stereotype.Service;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.xhx.module.device.dao.DeviceDao;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import org.springframework.beans.factory.annotation.Autowired;
import com.mortals.xhx.module.device.model.DeviceTopicEntity;
import com.mortals.xhx.module.device.model.DeviceTopicQuery;
import com.mortals.xhx.module.device.service.DeviceTopicService;
import org.springframework.util.ObjectUtils;

import java.util.Date;


/**
 * DeviceService
 * 设备 service实现
 *
 * @author zxfei
 * @date 2021-11-19
 */
@Service("deviceService")
public class DeviceServiceImpl extends AbstractCRUDServiceImpl<DeviceDao, DeviceEntity, Long> implements DeviceService {
    @Autowired
    private DeviceTopicService deviceTopicService;

    @Override
    protected void saveAfter(DeviceEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getDeviceTopicList())) {
            entity.getDeviceTopicList().stream().peek(item -> {
                item.setDeviceId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
            }).count();
            deviceTopicService.save(entity.getDeviceTopicList());
        }
        super.saveAfter(entity, context);
    }

    @Override
    protected void updateAfter(DeviceEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getDeviceTopicList())) {
            Long[] deviceTopicIds = deviceTopicService.find(new DeviceTopicQuery().deviceId(entity.getId())).stream().map(DeviceTopicEntity::getId).toArray(Long[]::new);
            deviceTopicService.remove(deviceTopicIds, context);
            entity.getDeviceTopicList().stream().peek(item -> {
                item.setDeviceId(entity.getId());
                item.setCreateUser(context.getUser().getLoginName());
                item.setCreateUserId(context.getUser().getId());
                item.setCreateTime(new Date());
                item.setUpdateUser(context.getUser().getLoginName());
                item.setUpdateUserId(context.getUser().getId());
                item.setUpdateTime(new Date());
            }).count();
            deviceTopicService.save(entity.getDeviceTopicList());
        }
        super.updateAfter(entity, context);
    }

    @Override
    protected void removeAfter(Long[] ids, Context context, int result) throws AppException {
        for (int i = 0; i < ids.length; i++) {
            Long[] deviceTopicIds = deviceTopicService.find(new DeviceTopicQuery().deviceId(ids[i]), context).stream().map(DeviceTopicEntity::getId).toArray(Long[]::new);
            deviceTopicService.remove(deviceTopicIds, context);
        }
        super.removeAfter(ids, context, result);
    }
}