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.victoriatourist.entity.blessing.dto.BlessingCoverSelectDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingInfoDto;
import com.fjhx.victoriatourist.entity.blessing.dto.BlessingInfoSelectDto;
import com.fjhx.victoriatourist.entity.blessing.po.BlessingCover;
import com.fjhx.victoriatourist.entity.blessing.po.BlessingInfo;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingCoverVo;
import com.fjhx.victoriatourist.entity.blessing.vo.BlessingInfoVo;
import com.fjhx.victoriatourist.mapper.blessing.BlessingInfoMapper;
import com.fjhx.victoriatourist.service.blessing.BlessingCoverService;
import com.fjhx.victoriatourist.service.blessing.BlessingInfoService;
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 javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * <p>
 * 祝福留言信息 服务实现类
 * </p>
 *
 * @author LQH
 * @since 2023-12-10
 */
@Service
public class BlessingInfoServiceImpl extends ServiceImpl<BlessingInfoMapper, BlessingInfo> implements BlessingInfoService {

    @Resource
    private BlessingCoverService blessingCoverService;


    @Override
    public List<BlessingInfoVo> getList(BlessingInfoSelectDto dto) {
        IWrapper<BlessingInfo> wrapper = getWrapper();
        wrapper.eq("bi", BlessingInfo::getAcceptPhone, dto.getAcceptPhone());
        wrapper.orderByDesc("bi", BlessingInfo::getCreateTime);
        List<BlessingInfoVo> list = this.baseMapper.getList(wrapper);

        Map<Long, BlessingCoverVo> coverVoMap = blessingCoverService.getList(new BlessingCoverSelectDto()).stream().collect(Collectors.toMap(BlessingCover::getId, Function.identity()));

        list.forEach(x->{
            if (coverVoMap.containsKey(x.getCoverId())){
                BlessingCoverVo coverVo = coverVoMap.get(x.getCoverId());
                x.setCoverName(coverVo.getCoverName());
                x.setAudioList(coverVo.getAudioList());
                x.setCoverList(coverVo.getCoverList());
            }
        });

        return list;
    }

    @Override
    public Page<BlessingInfoVo> getPage(BlessingInfoSelectDto dto) {
        IWrapper<BlessingInfo> wrapper = getWrapper();

        if (StringUtils.isNotEmpty(dto.getAcceptName())){
            wrapper.like("bi", BlessingInfo::getAcceptName, dto.getAcceptName());
        }
        if (StringUtils.isNotEmpty(dto.getAcceptPhone())){
            wrapper.like("bi", BlessingInfo::getAcceptPhone, dto.getAcceptPhone());
        }
        if (StringUtils.isNotEmpty(dto.getGiveName())){
            wrapper.like("bi", BlessingInfo::getGiveName, dto.getGiveName());
        }
        if (StringUtils.isNotEmpty(dto.getGivePhone())){
            wrapper.like("bi", BlessingInfo::getGivePhone, dto.getGivePhone());
        }
        wrapper.keyword(dto, new SqlField("bi", BlessingInfo::getBlessingContent));
        wrapper.orderByDesc("bi", BlessingInfo::getId);
        Page<BlessingInfoVo> page = this.baseMapper.getPage(dto.getPage(), wrapper);

        Map<Long, BlessingCoverVo> coverVoMap = blessingCoverService.getList(new BlessingCoverSelectDto()).stream().collect(Collectors.toMap(BlessingCover::getId, Function.identity()));

        page.getRecords().forEach(x->{
            if (coverVoMap.containsKey(x.getCoverId())){
                BlessingCoverVo coverVo = coverVoMap.get(x.getCoverId());
                x.setCoverName(coverVo.getCoverName());
                x.setAudioList(coverVo.getAudioList());
                x.setCoverList(coverVo.getCoverList());
            }
        });

        return page;
    }

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

    @Override
    public void add(BlessingInfoDto blessingInfoDto) {
        this.save(blessingInfoDto);
    }

    @Override
    public void edit(BlessingInfoDto blessingInfoDto) {
        this.updateById(blessingInfoDto);
    }

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

}
