package com.liquidnet.service.sweet.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.kylin.entity.KylinArtist;
import com.liquidnet.service.kylin.mapper.KylinArtistMapper;
import com.liquidnet.service.sweet.entity.SweetArtists;
import com.liquidnet.service.sweet.entity.SweetArtistsUrl;
import com.liquidnet.service.sweet.mapper.SweetArtistsMapper;
import com.liquidnet.service.sweet.mapper.SweetArtistsUrlMapper;
import com.liquidnet.service.sweet.service.ISweetArtistsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.sweet.vo.SweetArtistsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 艺人表 服务实现类（读 kylin_artist，写已迁移至 admin）
 * </p>
 *
 * @author liquidnet
 * @since 2021-07-23
 */
@Slf4j
@Service
public class SweetArtistsServiceImpl extends ServiceImpl<SweetArtistsMapper, SweetArtists> implements ISweetArtistsService {

    @Autowired
    private KylinArtistMapper kylinArtistMapper;
    @Autowired
    private SweetArtistsUrlMapper sweetArtistsUrlMapper;

    @Override
    public ResponseDto<PageInfo<SweetArtists>> getList(int page, int size, String name) {
        PageInfo<SweetArtists> pageInfo = null;
        try {
            PageHelper.startPage(page, size);
            List<KylinArtist> kylinList;
            if (name != null && !name.isEmpty()) {
                kylinList = kylinArtistMapper.selectList(
                        Wrappers.lambdaQuery(KylinArtist.class)
                                .like(KylinArtist::getArtistName, name)
                                .eq(KylinArtist::getStatus, 1)
                                .orderByDesc(KylinArtist::getSort)
                                .orderByDesc(KylinArtist::getCreatedAt)
                );
            } else {
                kylinList = kylinArtistMapper.selectList(
                        Wrappers.lambdaQuery(KylinArtist.class)
                                .eq(KylinArtist::getStatus, 1)
                                .orderByDesc(KylinArtist::getSort)
                                .orderByDesc(KylinArtist::getCreatedAt)
                );
            }
            List<SweetArtists> data = kylinList.stream().map(this::toSweetArtists).collect(Collectors.toList());
            pageInfo = new PageInfo(data);
            return ResponseDto.success(pageInfo);
        } catch (Exception e) {
            log.error("getList error", e);
            return ResponseDto.success(pageInfo);
        }
    }

    @Override
    public ResponseDto<SweetArtistsVo> detail(String artistsId) {
        try {
            SweetArtistsVo vo = SweetArtistsVo.getNew();
            KylinArtist kylinArtist = kylinArtistMapper.selectOne(
                    Wrappers.lambdaQuery(KylinArtist.class)
                            .eq(KylinArtist::getArtistId, artistsId)
            );
            if (kylinArtist == null) {
                return ResponseDto.failure();
            }
            BeanUtils.copyProperties(toSweetArtists(kylinArtist), vo);
            List<SweetArtistsUrl> sweetArtistsUrl = sweetArtistsUrlMapper.selectList(
                    Wrappers.lambdaQuery(SweetArtistsUrl.class).eq(SweetArtistsUrl::getArtistsId, artistsId)
            );
            vo.setSweetArtistsUrl(sweetArtistsUrl);
            return ResponseDto.success(vo);
        } catch (Exception e) {
            log.error("detail error", e);
            return ResponseDto.failure();
        }
    }

    private SweetArtists toSweetArtists(KylinArtist kylinArtist) {
        SweetArtists sweetArtists = SweetArtists.getNew();
        sweetArtists.setArtistsId(kylinArtist.getArtistId());
        sweetArtists.setName(kylinArtist.getArtistName());
        sweetArtists.setPinyin(kylinArtist.getPinyin());
        sweetArtists.setDescribes(kylinArtist.getIntroduction());
        sweetArtists.setPicUrl(kylinArtist.getAvatarUrl());
        sweetArtists.setStatus(kylinArtist.getStatus());
        sweetArtists.setCreatedAt(kylinArtist.getCreatedAt());
        sweetArtists.setUpdatdAt(kylinArtist.getUpdatedAt());
        return sweetArtists;
    }
}
