package com.ruoyi.syzy.service.impl;

import java.util.List;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.syzy.mapper.BWwwNewsVideoMapper;
import com.ruoyi.syzy.domain.BWwwNewsVideo;
import com.ruoyi.syzy.service.IBWwwNewsVideoService;

/**
 * 资讯视频Service业务层处理
 * 
 * @author mingxue
 * @date 2022-07-17
 */
@Service
public class BWwwNewsVideoServiceImpl implements IBWwwNewsVideoService 
{
    private static final Logger log = LoggerFactory.getLogger(BWwwNewsVideoServiceImpl.class);

    @Autowired
    private BWwwNewsVideoMapper bWwwNewsVideoMapper;


    /**
     * 查询资讯视频
     * 
     * @param id 资讯视频ID
     * @return 资讯视频
     */
    @Override
    public BWwwNewsVideo selectBWwwNewsVideoById(Long id)
    {
        return bWwwNewsVideoMapper.selectBWwwNewsVideoById(id);
    }

    @Override
    public BWwwNewsVideo selectBWwwNewsVideoByAliId(String aliId) {
        return bWwwNewsVideoMapper.selectBWwwNewsVideoByAliId(aliId);
    }

    @Override
    public List<String> selectBWwwNewsVideoTypes() {
        return bWwwNewsVideoMapper.selectBWwwNewsVideoTypes();
    }

    @Override
    public List<String> selectBWwwNewsVideoSubTypes(String type) {
        return bWwwNewsVideoMapper.selectBWwwNewsVideoSubTypes(type);
    }

    /**
     * 查询资讯视频列表
     * 
     * @param bWwwNewsVideo 资讯视频
     * @return 资讯视频
     */
    @Override
    public List<BWwwNewsVideo> selectBWwwNewsVideoList(BWwwNewsVideo bWwwNewsVideo)
    {
        return bWwwNewsVideoMapper.selectBWwwNewsVideoList(bWwwNewsVideo);
    }

    /**
     * 新增资讯视频
     * 
     * @param bWwwNewsVideo 资讯视频
     * @return 结果
     */
    @Override
    public int insertBWwwNewsVideo(BWwwNewsVideo bWwwNewsVideo)
    {
        bWwwNewsVideo.setCreateTime(DateUtils.getNowDate());
        return bWwwNewsVideoMapper.insertBWwwNewsVideo(bWwwNewsVideo);
    }

    /**
     * 修改资讯视频
     * 
     * @param bWwwNewsVideo 资讯视频
     * @return 结果
     */
    @Override
    public int updateBWwwNewsVideo(BWwwNewsVideo bWwwNewsVideo)
    {
        return bWwwNewsVideoMapper.updateBWwwNewsVideo(bWwwNewsVideo);
    }

    /**
     * 批量删除资讯视频
     * 
     * @param ids 需要删除的资讯视频ID
     * @return 结果
     */
    @Override
    public int deleteBWwwNewsVideoByIds(Long[] ids)
    {
        return bWwwNewsVideoMapper.deleteBWwwNewsVideoByIds(ids);
    }

    /**
     * 删除资讯视频信息
     * 
     * @param id 资讯视频ID
     * @return 结果
     */
    @Override
    public int deleteBWwwNewsVideoById(Long id)
    {
        return bWwwNewsVideoMapper.deleteBWwwNewsVideoById(id);
    }

    @Override
    public String importData(List<BWwwNewsVideo> videoList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(videoList) || videoList.size() == 0)
        {
            throw new CustomException("导入视频数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BWwwNewsVideo video : videoList)
        {
            //智隆叁加叁的视频需要添加上前缀
            if(StringUtils.isNotEmpty(video.getAliId())){
                video.setAliId(Constants.ZLSJS_PREFIX+video.getAliId());
            }
            try
            {
                // 验证是否存在这个视频
                BWwwNewsVideo v = bWwwNewsVideoMapper.selectBWwwNewsVideoByAliId(video.getAliId());
                if(StringUtils.isNull(v)){
                    //智隆叁加叁的视频
                    v = bWwwNewsVideoMapper.selectBWwwNewsVideoByAliId(Constants.ZLSJS_PREFIX+video.getAliId());
                }
                if (StringUtils.isNull(v))
                {
                    video.setCreateBy(operName);
                    //处理上传文件大小 kb转换为M
//                    if(null!=video.getFileSize()){
//                        video.setFileSize(new BigDecimal(String.valueOf(video.getFileSize()))
//                                .divide(new BigDecimal(1024*1024)).setScale(2,BigDecimal.ROUND_HALF_UP));
//                    }
                    this.insertBWwwNewsVideo(video);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、视频 [" + video.getTitle() + "] 导入成功");
                }
                else if (isUpdateSupport)
                {
                    v.setUpdateBy(operName);

                    v.setTitle(video.getTitle());
                    v.setAliId(video.getAliId());
                    v.setType(video.getType());
                    this.updateBWwwNewsVideo(v);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、视频 [" + video.getTitle() + "] 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、视频 [" + video.getTitle() + "] 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、视频 [" + video.getTitle() + " ]导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum ==videoList.size())
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，部分导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
