package com.bonus.canteen.core.supermarket.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.common.utils.MqUtil;
import com.bonus.canteen.core.device.dto.DeviceDTO;
import com.bonus.canteen.core.device.mapper.DoubleScreenMachineMapper;
import com.bonus.canteen.core.device.vo.DeviceFullInfoVO;
import com.bonus.canteen.core.supermarket.mapper.SupermarketDeviceMapper;
import com.bonus.canteen.core.supermarket.service.SupermarketDeviceService;
import com.bonus.canteen.core.user.domain.DeviceMqPersonalUpdateMessageDTO;
import com.bonus.common.houqin.constant.GlobalConstants;
import com.bonus.common.houqin.mq.constant.LeMqConstant;
import com.bonus.common.houqin.utils.id.Id;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author xliu
 * @date 2025/5/8 9:48
 */
@Service
public class SupermarketDeviceServiceImpl implements SupermarketDeviceService {

    @Resource
    private SupermarketDeviceMapper supermarketDeviceMapper;

    @Resource
    private DoubleScreenMachineMapper mapper;


    @Override
    public int insertSupermarketDevice(DeviceDTO dto) {
        if (dto == null){
            throw new RuntimeException("参数为空");
        }
        if (ObjectUtil.isEmpty(dto.getDeviceNum()) || ObjectUtil.isEmpty(dto.getDeviceName())
                || ObjectUtil.isEmpty(dto.getDeviceSn()) || ObjectUtil.isEmpty(dto.getDeviceKey())
                || ObjectUtil.isEmpty(dto.getAreaId()) || ObjectUtil.isEmpty(dto.getCanteenId()) ){
            throw new RuntimeException("请先讲填写所有必填项，再提交相关数据！");
        }
        dto.setTenantId(GlobalConstants.TENANT_ID);
        dto.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        dto.setDeviceType("53");
        dto.setDeviceGateway("");
        //判断设备sn、设备编号、设备名称是否存在
        int count = mapper.checkIsExist(dto.getDeviceNum(),null);
        if (count > 0){
            throw new RuntimeException("设备编号已存在");
        }
        count = mapper.checkIsExist(dto.getDeviceName(),null);
        if (count > 0){
            throw new RuntimeException("设备名称已存在");
        }
        count = mapper.checkIsExist(dto.getDeviceSn(),null);
        if (count > 0){
            throw new RuntimeException("设备sn已存在");
        }
        int code = mapper.insertMachine(dto);
        if (code == 0){
            throw new RuntimeException("设备新增失败");
        }
        code = mapper.insertMachineBind(dto);
        if (code == 0){
            throw new RuntimeException("设备绑定失败");
        }
        dto.setId(Id.next());
        code = mapper.insertDeviceRecipe(dto);
        if (code == 0){
            throw new RuntimeException("添加菜谱关联失败");
        }
        //发送mq
        DeviceMqPersonalUpdateMessageDTO bean = new DeviceMqPersonalUpdateMessageDTO().setUpdatePerson(0,"update");
        MqUtil.pushToSingleDevice(bean, LeMqConstant.Topic.DEVICE_UPDATE_INFO_V4, dto.getDeviceSn());

        return code;
    }


    @Override
    public int updateSupermarketDevice(DeviceDTO dto) {
        if (dto == null){
            throw new RuntimeException("参数为空");
        }
        if (ObjectUtil.isEmpty(dto.getDeviceNum()) || ObjectUtil.isEmpty(dto.getDeviceName())
                || ObjectUtil.isEmpty(dto.getDeviceSn()) || ObjectUtil.isEmpty(dto.getDeviceKey())
                || ObjectUtil.isEmpty(dto.getAreaId()) || ObjectUtil.isEmpty(dto.getCanteenId()) ){
            throw new RuntimeException("请先讲填写所有必填项，再提交相关数据！");
        }
        //判断设备sn、设备编号、设备名称是否存在
        int count = mapper.checkIsExist(dto.getDeviceNum(), dto.getDeviceId());
        if (count > 0){
            throw new RuntimeException("设备编号已存在");
        }
        count = mapper.checkIsExist(dto.getDeviceName(), dto.getDeviceId());
        if (count > 0){
            throw new RuntimeException("设备名称已存在");
        }
        count = mapper.checkIsExist(dto.getDeviceSn(), dto.getDeviceId());
        if (count > 0){
            throw new RuntimeException("设备sn已存在");
        }
        int code = mapper.updateMachine(dto);
        if (code == 0){
            throw new RuntimeException("设备更新失败");
        }
        code = mapper.updateMachineBind(dto);
        if (code == 0){
            throw new RuntimeException("设备绑定更新失败");
        }
        code = mapper.updateDeviceRecipe(dto);

        if (code == 0){
            throw new RuntimeException("修改菜谱关联失败");
        }
        //发送mq
        DeviceMqPersonalUpdateMessageDTO bean = new DeviceMqPersonalUpdateMessageDTO().setUpdatePerson(0,"update");
        MqUtil.pushToSingleDevice(bean, LeMqConstant.Topic.DEVICE_UPDATE_INFO_V4, dto.getDeviceSn());

        return code;
    }

    @Override
    public List<DeviceFullInfoVO> selectSupermarketDevice(DeviceDTO dto) {
        return supermarketDeviceMapper.selectSupermarketDevice(dto);
    }
}
