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

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSON;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.service.impl.AbstractCRUDServiceImpl;
import com.mortals.framework.util.DateUtils;
import com.mortals.xhx.base.system.message.impl.MessageProducer;
import com.mortals.xhx.common.code.LogTypeEnum;
import com.mortals.xhx.common.key.Constant;
import com.mortals.xhx.common.key.QueueKey;
import com.mortals.xhx.common.model.DefaultTbQueueMsgHeaders;
import com.mortals.xhx.common.model.MessageHeader;
import com.mortals.xhx.module.device.model.DeviceEntity;
import com.mortals.xhx.module.device.model.DeviceLogEntity;
import com.mortals.xhx.module.device.model.DeviceQuery;
import com.mortals.xhx.module.device.service.DeviceLogService;
import com.mortals.xhx.module.device.service.DeviceService;
import com.mortals.xhx.module.product.dao.ProductVersionDao;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.model.ProductVersionEntity;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.module.product.service.ProductVersionService;
import com.mortals.xhx.queue.DefaultTbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsg;
import com.mortals.xhx.queue.TbQueueMsgHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;
import java.util.List;

import static com.mortals.xhx.common.key.Constant.MESSAGETYPE_UPGREAD;

/**
 * ProductVersionService
 * 产品客户端版本 service实现
 *
 * @author zxfei
 * @date 2022-08-22
 */
@Service("productVersionService")
public class ProductVersionServiceImpl extends AbstractCRUDServiceImpl<ProductVersionDao, ProductVersionEntity, Long> implements ProductVersionService {

    @Autowired
    private ProductService productService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private MessageProducer messageProducer;
    @Autowired
    private DeviceLogService deviceLogService;


    @Override
    protected void saveBefore(ProductVersionEntity entity, Context context) throws AppException {
        if (!ObjectUtils.isEmpty(entity.getProductId())) {
            ProductEntity productEntity = productService.get(entity.getProductId(), context);
            if (!ObjectUtils.isEmpty(productEntity)) {
                entity.setProductName(productEntity.getProductName());
                entity.setProductCode(productEntity.getProductCode());
            }
        }
    }


    @Override
    protected void saveAfter(ProductVersionEntity entity, Context context) throws AppException {
        //推送消息
        pushUpgradMsg(entity, context);
        super.saveAfter(entity, context);
    }



    @Override
    protected void updateAfter(ProductVersionEntity entity, Context context) throws AppException {
        pushUpgradMsg(entity, context);
        super.updateAfter(entity, context);
    }


    private void pushUpgradMsg(ProductVersionEntity entity, Context context) {
        if (!ObjectUtils.isEmpty(entity.getProductId())) {
            ProductEntity productEntity = productService.get(entity.getProductId(), context);
            if (!ObjectUtils.isEmpty(productEntity)) {
                List<DeviceEntity> deviceEntities = deviceService.find(new DeviceQuery().productId(entity.getProductId()));
                for (DeviceEntity deviceEntity : deviceEntities) {
                    TbQueueMsgHeaders header = new DefaultTbQueueMsgHeaders();
                    header.put(MessageHeader.MESSAGETYPE, MESSAGETYPE_UPGREAD);
                    header.put(MessageHeader.DEVICECODE, deviceEntity.getDeviceCode());
                    header.put(MessageHeader.TIMESTAMP,  DateUtils.getCurrStrDateTime());
                    TbQueueMsg queueMsg = new DefaultTbQueueMsg(IdUtil.fastUUID(), "W10=", header);
                    messageProducer.sendMsg(QueueKey.DEFAULT_EXCHANGE, Constant.DOWN_TOPIC + deviceEntity.getDeviceCode(), JSON.toJSONString(queueMsg));

                    DeviceLogEntity deviceLogEntity = new DeviceLogEntity();
                    deviceLogEntity.initAttrValue();
                    deviceLogEntity.setTraceID(IdUtil.fastSimpleUUID());
                    deviceLogEntity.setSiteId(deviceEntity.getSiteId());
                    deviceLogEntity.setDeviceId(deviceEntity.getId());
                    deviceLogEntity.setDeviceName(deviceEntity.getDeviceName());
                    deviceLogEntity.setDeviceCode(deviceEntity.getDeviceCode());
                    deviceLogEntity.setMessageHead(MESSAGETYPE_UPGREAD);
                    deviceLogEntity.setContent("W10=");
                    deviceLogEntity.setLogType(LogTypeEnum.下发服务.getValue());
                    deviceLogEntity.setCreateUserId(1L);
                    deviceLogEntity.setCreateTime(new Date());
                    deviceLogService.save(deviceLogEntity,null);
                }
            }
        }
    }
}