package com.ai.da.service.impl;

import com.ai.da.common.utils.CopyUtil;
import com.ai.da.common.utils.DateUtil;
import com.ai.da.mapper.primary.UserLikeGroupMapper;
import com.ai.da.mapper.primary.UserLikeMapper;
import com.ai.da.mapper.primary.entity.UserLike;
import com.ai.da.mapper.primary.entity.UserLikeGroup;
import com.ai.da.model.vo.UserLikeVO;
import com.ai.da.service.UserLikeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

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

/**
 * 服务实现类
 *
 * @author yanglei
 * @since 2022-09-30
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class UserLikeServiceImpl extends ServiceImpl<UserLikeMapper, UserLike> implements UserLikeService {
    private final UserLikeMapper userLikeMapper;
    private final UserLikeGroupMapper userLikeGroupMapper;

    @Override
    public List<UserLikeVO> getGroupDetail(Long userGroupId) {
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_like_group_id", userGroupId);
        queryWrapper.orderByDesc("create_date");
        List<UserLike> userLikes = userLikeMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(userLikes)) {
            return Lists.newArrayList();
        }
        return CopyUtil.copyList(userLikes, UserLikeVO.class);
    }

    @Override
    public List<UserLikeVO> getGroupDetails(List<Long> userGroupIds) {
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_like_group_id", userGroupIds);
        return CopyUtil.copyList(userLikeMapper.selectList(queryWrapper), UserLikeVO.class);
    }

    public void deleteByUserGroupId(Long userGroupId) {
        QueryWrapper<UserLike> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_like_group_id", userGroupId);
        userLikeMapper.delete(queryWrapper);
    }

    @Override
    public UserLike getByDesignItemId(Long designItemId) {
        QueryWrapper<UserLike> qw = new QueryWrapper<>();
        qw.eq("design_item_id", designItemId);
        List<UserLike> userLikes = baseMapper.selectList(qw);
        if (!userLikes.isEmpty()) {
            return baseMapper.selectList(qw).get(0);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDate(Long designItemId, String timeZone) {
        UpdateWrapper<UserLike> uw = new UpdateWrapper<>();
        uw.eq("design_item_id", designItemId);
        uw.set("update_date", DateUtil.getByTimeZone(timeZone));
        uw.set("converted", 1);

        baseMapper.update(null, uw);
    }

    @Override
    public List<UserLike> getUserLikeList(Long id) {
        QueryWrapper<UserLike> qw = new QueryWrapper<>();
        qw.lambda().eq(UserLike::getUserLikeGroupId, id);
        return userLikeMapper.selectList(qw);
    }

    @Override
    public UserLikeGroup getUserLikeGroupByProjectId(Long id) {
        QueryWrapper<UserLikeGroup> qw = new QueryWrapper<>();
        qw.lambda().eq(UserLikeGroup::getProjectId, id);
        List<UserLikeGroup> userLikeGroupList = userLikeGroupMapper.selectList(qw);
        if (CollectionUtils.isEmpty(userLikeGroupList)) {
            return null;
        }
        return userLikeGroupList.get(0);
    }

    @Override
    public List<UserLike> getByUserLikeGroupId(Long userLikeGroupId) {
        QueryWrapper<UserLike> qw = new QueryWrapper<>();
        qw.lambda().eq(UserLike::getUserLikeGroupId, userLikeGroupId);
        List<UserLike> userLikeList = userLikeMapper.selectList(qw);
        if (CollectionUtils.isEmpty(userLikeList)) {
            return new ArrayList<>();
        }
        return userLikeList;
    }

    @Override
    public UserLike getByDesignOutfitId(Long designOutfitId) {
        QueryWrapper<UserLike> qw = new QueryWrapper<>();
        qw.lambda().eq(UserLike::getDesignOutfitId, designOutfitId);
        // 有脏数据
        List<UserLike> userLikes = baseMapper.selectList(qw);
        if (!userLikes.isEmpty()) {
            return userLikes.get(0);
        }
        return null;
    }

}
