package com.fjhx.victoriatourist.service.blessing.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjhx.file.entity.FileInfoVo;
import com.fjhx.file.utils.ObsFileUtil;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingCoverDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingCoverSelectDto;
import com.fjhx.victoriatourist.entity.blessing.po.BlessingCover;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingCoverVo;
import com.fjhx.victoriatourist.mapper.blessing.BlessingCoverMapper;
import com.fjhx.victoriatourist.service.blessing.BlessingCoverService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.wrapper.IWrapper;
import com.ruoyi.common.utils.wrapper.SqlField;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 祝福封面 服务实现类
 * </p>
 *
 * @author lqh
 * @since 2023-12-09
 */
@Service
public class BlessingCoverServiceImpl extends ServiceImpl<BlessingCoverMapper, BlessingCover> implements BlessingCoverService {

    @Override
    public List<BlessingCoverVo> getList(BlessingCoverSelectDto dto) {
        IWrapper<BlessingCover> wrapper = getWrapper();
        wrapper.orderByDesc("bc", BlessingCover::getId);
        wrapper.keyword(dto, new SqlField("bc", BlessingCover::getCoverName));
        List<BlessingCoverVo> list = this.baseMapper.getList(wrapper);
        list.forEach(x->{
            //获取封面图片
            List<Long> businessIdList = new ArrayList<>();
            businessIdList.add(x.getId());
            Map<Long, List<FileInfoVo>> fileMap = ObsFileUtil.getFileMap(businessIdList, 1);
            x.setCoverList(fileMap.get(x.getId()));
            Map<Long, List<FileInfoVo>> fileMap2 = ObsFileUtil.getFileMap(businessIdList, 2);
            x.setAudioList(fileMap2.get(x.getId()));
        });
        return list;
    }

    @Override
    public Page<BlessingCoverVo> getPage(BlessingCoverSelectDto dto) {
        IWrapper<BlessingCover> wrapper = getWrapper();
        if (StringUtils.isNotEmpty(dto.getCoverType())){
            wrapper.eq("bc", BlessingCover::getCoverType, dto.getCoverType());
        }
        wrapper.keyword(dto, new SqlField("bc", BlessingCover::getCoverName));
        wrapper.orderByDesc("bc", BlessingCover::getId);
        Page<BlessingCoverVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        page.getRecords().forEach(x->{
            //获取封面图片
            List<Long> businessIdList = new ArrayList<>();
            businessIdList.add(x.getId());
            Map<Long, List<FileInfoVo>> fileMap = ObsFileUtil.getFileMap(businessIdList, 1);
            x.setCoverList(fileMap.get(x.getId()));
            Map<Long, List<FileInfoVo>> fileMap2 = ObsFileUtil.getFileMap(businessIdList, 2);
            x.setAudioList(fileMap2.get(x.getId()));
        });

        return page;
    }

    @Override
    public BlessingCoverVo detail(Long id) {
        BlessingCover BlessingCover = this.getById(id);
        BlessingCoverVo result = BeanUtil.toBean(BlessingCover, BlessingCoverVo.class);
        return result;
    }

    @Override
    public void add(BlessingCoverDto blessingCoverDto) {
        this.save(blessingCoverDto);

        // 封面
        ObsFileUtil.saveFile(blessingCoverDto.getCoverList(), blessingCoverDto.getId(), 1);
        ObsFileUtil.saveFile(blessingCoverDto.getAudioList(), blessingCoverDto.getId(), 2);

    }

    @Override
    public void edit(BlessingCoverDto blessingCoverDto) {
        this.updateById(blessingCoverDto);

        // 封面
        ObsFileUtil.editFile(blessingCoverDto.getCoverList(), blessingCoverDto.getId(), 1);
        ObsFileUtil.editFile(blessingCoverDto.getAudioList(), blessingCoverDto.getId(), 2);

    }

    @Override
    public void delete(Long id) {
        this.removeById(id);
    }

}
