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

import java.util.List;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.device.dto.DeviceDTO;
import com.bonus.canteen.core.device.dto.DeviceSearchDTO;
import com.bonus.canteen.core.device.vo.DeviceFullInfoVO;
import com.bonus.common.houqin.constant.GlobalConstants;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.device.mapper.DoubleScreenMachineMapper;
import com.bonus.canteen.core.device.service.IDoubleScreenMachineService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 设备绑定多档口子Service业务层处理
 * 
 * @author xsheng
 * @date 2025-04-03
 */
@Service
public class DoubleScreenMachineServiceImpl implements IDoubleScreenMachineService {
    @Autowired
    private DoubleScreenMachineMapper mapper;

    /**
     * 查询设备绑定多档口子
     *
     * @param dto 查询条件
     * @return 设备绑定多档口子
     */
    @Override
    public List<DeviceFullInfoVO> getDoubleScreenMachineList(DeviceSearchDTO dto) {
        return mapper.getDoubleScreenMachineList(dto);
    }

    /**
     * 新增设备绑定多档口子
     *
     * @param dto 设备绑定多档口子
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(DeviceDTO dto) {
        if (dto == null){
            throw new RuntimeException("参数为空");
        }
        if (ObjectUtil.isEmpty(dto.getDeviceName()) || ObjectUtil.isEmpty(dto.getDeviceSn()) || ObjectUtil.isEmpty(dto.getDeviceName())
        || ObjectUtil.isEmpty(dto.getAreaId()) || ObjectUtil.isEmpty(dto.getCanteenId()) || ObjectUtil.isEmpty(dto.getStallId())){
            throw new RuntimeException("请先讲填写所有必填项，再提交相关数据！");
        }
        dto.setTenantId(GlobalConstants.TENANT_ID);
        dto.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        //判断设备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("设备绑定失败");
        }
    }

    /**
     * 修改设备绑定多档口子
     *
     * @param dto 设备绑定多档口子
     * @return 结果
     */
    @Override
    public void update(DeviceDTO dto) {
        //判断设备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("设备绑定更新失败");
        }
    }

    /**
     * 删除设备绑定多档口子信息
     *
     * @param deviceId 设备绑定多档口子ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String deviceId) {
        int code = mapper.deleteMachine(deviceId);
        if (code == 0){
            throw new RuntimeException("设备删除失败");
        }
        code = mapper.deleteMachineBind(deviceId);
        if (code == 0){
            throw new RuntimeException("设备绑定删除失败");
        }
    }

    /**
     * 根据sn更新时间
     *
     * @param sn
     */
    @Override
    public void updateTimeBySn(String sn) {
        int code = mapper.updateTimeBySn(sn);
        if (code == 0){
            throw new RuntimeException("更新时间失败");
        }
    }
}
