package com.sf.system.deployment.service.impl;

import java.io.IOException;
import java.util.List;

import com.sf.common.constant.HttpStatus;
import com.sf.common.exception.ServiceException;
import com.sf.common.utils.DateUtils;
import com.sf.common.utils.file.QRCodeUtils;
import com.sf.file.domain.SysOss;
import com.sf.file.service.ISysOssService;
import com.sf.system.deployment.domain.DeploymentServicePublish;
import com.sf.system.deployment.domain.SysApkInfo;
import com.sf.system.deployment.mapper.DeploymentServicePublishMapper;
import com.sf.system.deployment.service.IDeploymentServicePublishService;
import com.sf.system.deployment.service.ISysApkInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 发布列表Service业务层处理
 * 
 * @author ztzh
 * @date 2024-04-19
 */
@Service
public class DeploymentServicePublishServiceImpl implements IDeploymentServicePublishService
{
    @Resource
    private ISysApkInfoService iSysApkInfoService;
    @Resource
    private ISysOssService iSysOssService;
    @Autowired
    private DeploymentServicePublishMapper deploymentServicePublishMapper;

    /**
     * 查询发布列表
     * 
     * @param id 发布列表主键
     * @return 发布列表
     */
    @Override
    public DeploymentServicePublish selectDeploymentServicePublishById(Long id)
    {
        return deploymentServicePublishMapper.selectDeploymentServicePublishById(id);
    }

    /**
     * 查询发布列表列表
     * 
     * @param deploymentServicePublish 发布列表
     * @return 发布列表
     */
    @Override
    public List<DeploymentServicePublish> selectDeploymentServicePublishList(DeploymentServicePublish deploymentServicePublish)
    {
        return deploymentServicePublishMapper.selectDeploymentServicePublishList(deploymentServicePublish);
    }

    /**
     * 新增发布列表
     * 
     * @param deploymentServicePublish 发布列表
     * @return 结果
     */
    @Override
    public int insertDeploymentServicePublish(DeploymentServicePublish deploymentServicePublish)
    {
        deploymentServicePublish.setCreateTime(DateUtils.getNowDate());
        return deploymentServicePublishMapper.insertDeploymentServicePublish(deploymentServicePublish);
    }

    /**
     * 修改发布列表
     * 
     * @param deploymentServicePublish 发布列表
     * @return 结果
     */
    @Override
    public int updateDeploymentServicePublish(DeploymentServicePublish deploymentServicePublish)
    {
        deploymentServicePublish.setUpdateTime(DateUtils.getNowDate());
        return deploymentServicePublishMapper.updateDeploymentServicePublish(deploymentServicePublish);
    }

    /**
     * 批量删除发布列表
     * 
     * @param ids 需要删除的发布列表主键
     * @return 结果
     */
    @Override
    public int deleteDeploymentServicePublishByIds(Long[] ids)
    {
        return deploymentServicePublishMapper.deleteDeploymentServicePublishByIds(ids);
    }

    /**
     * 删除发布列表信息
     * 
     * @param id 发布列表主键
     * @return 结果
     */
    @Override
    public int deleteDeploymentServicePublishById(Long id)
    {
        return deploymentServicePublishMapper.deleteDeploymentServicePublishById(id);
    }

    @Override
    public int publish(DeploymentServicePublish deploymentServicePublish) throws Exception {
        if(1 == deploymentServicePublish.getStatus()){
            throw new ServiceException("安装包已发布", HttpStatus.WARN);
        }
        deploymentServicePublish.setUpdateTime(DateUtils.getNowDate());
        SysApkInfo sysApkInfo = iSysApkInfoService.selectSysApkInfoById(deploymentServicePublish.getApkId());
        SysOss oss = iSysOssService.upload(QRCodeUtils.generateQRCode(sysApkInfo.getSysApk()));
        deploymentServicePublish.setStatus(1L);
        deploymentServicePublish.setQuickMark(oss.getUrl());
        return deploymentServicePublishMapper.updateDeploymentServicePublish(deploymentServicePublish);
    }

    @Override
    public int soldOut(DeploymentServicePublish deploymentServicePublish) {
        if(2 == deploymentServicePublish.getStatus()){
            throw new ServiceException("安装包已下架", HttpStatus.WARN);
        }
        deploymentServicePublish.setStatus(2L);
        return deploymentServicePublishMapper.updateDeploymentServicePublish(deploymentServicePublish);
    }
}
