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

import java.util.List;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.houqin.constant.GlobalConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.device.mapper.DeviceInfoMapper;
import com.bonus.canteen.core.device.domain.DeviceInfo;
import com.bonus.canteen.core.device.service.IDeviceInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 设备资料Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class DeviceInfoServiceImpl implements IDeviceInfoService {
    @Resource
    private DeviceInfoMapper deviceInfoMapper;

    private static final Long DEVICE_TYPE_STALL = 20L;

    /**
     * 查询设备资料
     * 
     * @param deviceId 设备资料主键
     * @return 设备资料
     */
    @Override
    public DeviceInfo selectDeviceInfoByDeviceId(Long deviceId) {
        return deviceInfoMapper.selectDeviceInfoByDeviceId(deviceId);
    }

    /**
     * 查询设备资料列表
     * 
     * @param deviceInfo 设备资料
     * @return 设备资料
     */
    @Override
    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo) {
        return deviceInfoMapper.selectDeviceInfoList(deviceInfo);
    }

    /**
     * 新增设备资料
     * 
     * @param deviceInfo 设备资料
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertDeviceInfo(DeviceInfo deviceInfo) {
        deviceInfo.setCreateTime(DateUtils.getNowDate());
        try {
            checkParam(deviceInfo,false);
            deviceInfoMapper.insertDeviceInfo(deviceInfo);
            deviceInfoMapper.insertDeviceRecipeBinding(deviceInfo);
            return deviceInfoMapper.insertDeviceBinding(deviceInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改设备资料
     * 
     * @param deviceInfo 设备资料
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDeviceInfo(DeviceInfo deviceInfo) {
        deviceInfo.setUpdateTime(DateUtils.getNowDate());
        try {
            checkParam(deviceInfo,true);
            deviceInfoMapper.updateDeviceInfo(deviceInfo);
            deviceInfoMapper.updateDeviceRecipeBinding(deviceInfo);
            return deviceInfoMapper.updateDeviceBinding(deviceInfo);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    private void checkParam(DeviceInfo deviceInfo, boolean isUpdate){
        deviceInfo.setTenantId(GlobalConstants.TENANT_ID);

        if (isUpdate && deviceInfo.getDeviceId() == null) {
            throw new ServiceException("设备ID不能为空");
        }
        if (deviceInfo.getDeviceType() == null) {
            throw new ServiceException("设备类型不能为空");
        }
        if (deviceInfo.getDeviceType().equals(DEVICE_TYPE_STALL)) {
            checkStallRelated(deviceInfo);
        }
        if (StringUtils.isBlank(deviceInfo.getDeviceName())) {
            throw new ServiceException("设备名称不能为空");
        }
        if (StringUtils.isBlank(deviceInfo.getDeviceNumber())) {
            throw new ServiceException("设备编号不能为空");
        }
        checkDeviceUnique(deviceInfo);
        // 查询是否档口已经绑定菜谱
        String recipeId = deviceInfoMapper.selectDeviceRecipeBinding(deviceInfo);
        if (StringUtils.isNotBlank(recipeId)) {
            deviceInfo.setRecipeId(recipeId);
        }
    }

    private void checkStallRelated(DeviceInfo deviceInfo){
        if (StringUtils.isBlank(deviceInfo.getAreaId())) {
            throw new ServiceException("区域不能为空");
        }
        if (StringUtils.isBlank(deviceInfo.getCanteenId())) {
            throw new ServiceException("食堂不能为空");
        }
        if (StringUtils.isBlank(deviceInfo.getStallId())) {
            throw new ServiceException("档口不能为空");
        }
    }

    private void checkDeviceUnique(DeviceInfo deviceInfo){
        if (deviceInfoMapper.checkDeviceNameUnique(deviceInfo) > 0) {
            throw new ServiceException("设备名称已存在");
        }
        if (deviceInfoMapper.checkDeviceNumberUnique(deviceInfo) > 0) {
            throw new ServiceException("设备编号已存在");
        }
        if (deviceInfoMapper.checkDeviceSnUnique(deviceInfo) > 0) {
            throw new ServiceException("设备SN已存在");
        }
    }

    /**
     * 批量删除设备资料
     * 
     * @param deviceIds 需要删除的设备资料主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteDeviceInfoByDeviceIds(Long[] deviceIds) {
        deviceInfoMapper.deleteDeviceBinding(deviceIds);
        deviceInfoMapper.deleteDeviceRecipeBinding(deviceIds);
        return deviceInfoMapper.deleteDeviceInfoByDeviceIds(deviceIds);
    }

    /**
     * 删除设备资料信息
     * 
     * @param deviceId 设备资料主键
     * @return 结果
     */
    @Override
    public int deleteDeviceInfoByDeviceId(Long deviceId) {
        return deviceInfoMapper.deleteDeviceInfoByDeviceId(deviceId);
    }

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

}
