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

import java.util.List;

import com.bonus.canteen.core.device.domain.ApkDTO;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.canteen.core.device.mapper.DeviceAppVersionMapper;
import com.bonus.canteen.core.device.domain.DeviceAppVersion;
import com.bonus.canteen.core.device.service.IDeviceAppVersionService;

/**
 * 版本Service业务层处理
 * 
 * @author xsheng
 * @date 2025-05-25
 */
@Service
public class DeviceAppVersionServiceImpl implements IDeviceAppVersionService {
    @Autowired
    private DeviceAppVersionMapper deviceAppVersionMapper;

    /**
     * 查询版本
     * 
     * @param id 版本主键
     * @return 版本
     */
    @Override
    public DeviceAppVersion selectDeviceAppVersionById(Integer id) {
        return deviceAppVersionMapper.selectDeviceAppVersionById(id);
    }

    /**
     * 查询版本列表
     * 
     * @param deviceAppVersion 版本
     * @return 版本
     */
    @Override
    public List<DeviceAppVersion> selectDeviceAppVersionList(DeviceAppVersion deviceAppVersion) {
        return deviceAppVersionMapper.selectDeviceAppVersionList(deviceAppVersion);
    }

    /**
     * 新增版本
     * 
     * @param deviceAppVersion 版本
     * @return 结果
     */
    @Override
    public int insertDeviceAppVersion(DeviceAppVersion deviceAppVersion) {
        deviceAppVersion.setCreateTime(DateUtils.getNowDate());
        try {
            return deviceAppVersionMapper.insertDeviceAppVersion(deviceAppVersion);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 修改版本
     * 
     * @param deviceAppVersion 版本
     * @return 结果
     */
    @Override
    public int updateDeviceAppVersion(DeviceAppVersion deviceAppVersion) {
        deviceAppVersion.setUpdateTime(DateUtils.getNowDate());
        try {
            return deviceAppVersionMapper.updateDeviceAppVersion(deviceAppVersion);
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    /**
     * 批量删除版本
     * 
     * @param ids 需要删除的版本主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAppVersionByIds(Integer[] ids) {
        return deviceAppVersionMapper.deleteDeviceAppVersionByIds(ids);
    }

    /**
     * 删除版本信息
     * 
     * @param id 版本主键
     * @return 结果
     */
    @Override
    public int deleteDeviceAppVersionById(Integer id) {
        return deviceAppVersionMapper.deleteDeviceAppVersionById(id);
    }

    @Override
    public AjaxResult uploadApk(ApkDTO dto) {
        try {
            ApkDTO apkDTO = deviceAppVersionMapper.getApkByType(dto.getCanteenType(), dto.getVersion());
            if (apkDTO != null) {
                //检查最新的一条版本是否相同
                ApkDTO lastBean = deviceAppVersionMapper.getApkByType(dto.getCanteenType(), null);
                if (lastBean != null && !lastBean.getVersion().equals(dto.getVersion())){
                    return AjaxResult.error("该版本已存在");
                }
                deviceAppVersionMapper.updateApk(dto);
            } else {
                deviceAppVersionMapper.insertApk(dto);
            }
        } catch (Exception e) {
            return AjaxResult.error("操作失败");
        }
        return AjaxResult.success();
    }
}
