package com.YuyeTech.TPlat.game.gameInfo.service;

import com.YuyeTech.TPlat.game.gameInfo.projo.FcCategoryReference;
import com.YuyeTech.TPlat.game.gameInfo.projo.FcInfo;
import com.YuyeTech.TPlat.dto.FcDTO;
import com.YuyeTech.TPlat.game.gameInfo.repository.FcCategoryReferenceRepository;
import com.YuyeTech.TPlat.game.gameInfo.repository.FcInfoRepository;
import com.YuyeTech.TPlat.utils.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author:slambb
 * @date:2020/1/10
 */
@Service
public class FcInfoServiceImpl implements FcInfoService {
    @Autowired
    private FcInfoRepository fcInfoRepository;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private FavoritesService favoritesService;

    @Autowired
    private FcCategoryReferenceRepository fcCategoryReferenceRepository;

    /**
     * 实现查询游戏类目对应的列表
     *
     * @param categoryType
     * @param pageable
     * @return
     */
    @Override
    public List<FcDTO> findList(String userId, Integer categoryType,String endTime, Pageable pageable) {

        //排序规则和分页
        Specification<FcInfo> specification = new Specification<FcInfo>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                //增加筛选条件
                Predicate predicate = cb.conjunction();
                predicate.getExpressions().add(cb.equal(root.get("fcStatus").as(String.class), 1));

                if (categoryType != 0) {
                    predicate.getExpressions().add(cb.equal(root.get("fcType").as(String.class), categoryType));
                }

                //结束日期
                if (endTime != null && !endTime.trim().equals("")) {
                    predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), endTime));
                }

                return predicate;
            }
        };
        List<FcDTO> gameDTOList = fcInfoRepository.findAll(specification,pageable).stream().map(e -> new FcDTO(
                e.getFcId(),
                e.getFcName(),
                aliyunOSSUtil.addDomainName(e.getFcIcon()),
                e.getFcDescription(),
                aliyunOSSUtil.addDomainName(e.getFcPicture()),
                e.getFcDownloadUrl(),
                //获取收藏信息
                favoritesService.findByUserIdAndFObjectId(userId, e.getFcId().toString()),
                e.getFcRankingId(),
                e.getFcRankingShow().equals(1),
                e.getCreateTime()
        )).collect(Collectors.toList());
        return gameDTOList;
    }

    @Override
    public Page<FcInfo> findPage(String userId, Integer categoryType, Pageable pageable) {
        //假如是0的话，分页查询全部数据
        if (categoryType == 0) {
            Page<FcInfo> gameInfos = fcInfoRepository.findAll(pageable);
            List<FcInfo> gameInfoList = gameInfos.stream().map(e -> {
                e.setFcIcon(aliyunOSSUtil.addDomainName(e.getFcIcon()));
                e.setFcPicture(aliyunOSSUtil.addDomainName(e.getFcPicture()));
                return e;
            }).collect(Collectors.toList());

            return new PageImpl<FcInfo>(gameInfoList,pageable,gameInfos.getTotalElements());
        } else {
            Page<FcInfo> gameInfos = fcInfoRepository.findPageByFcType(categoryType, pageable);
            List<FcInfo> gameInfoList = gameInfos.stream().map(e -> {
                e.setFcIcon(aliyunOSSUtil.addDomainName(e.getFcIcon()));
                e.setFcPicture(aliyunOSSUtil.addDomainName(e.getFcPicture()));
                return e;
            }).collect(Collectors.toList());

            return new PageImpl<FcInfo>(gameInfoList,pageable,gameInfos.getTotalElements());
        }
    }

    @Override
    public List<FcInfo> findAll() {
        return fcInfoRepository.findAll();
    }

    @Override
    public List<FcDTO> findListByType(String userId, Integer categoryNumber, String endTime, Pageable pageable) {
        Page<FcCategoryReference> fcCategoryRefs = fcCategoryReferenceRepository.findByCategoryId(categoryNumber,pageable);
        List<Integer> ids = fcCategoryRefs.stream().map(e->e.getGameId()).collect(Collectors.toList());

        //排序规则和分页
        Specification<FcInfo> specification = new Specification<FcInfo>() {
            @Override
            public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) {
                //增加筛选条件
                Predicate predicate = cb.conjunction();
                predicate.getExpressions().add(cb.equal(root.get("fcStatus").as(String.class), 1));

                //结束日期
                if (endTime != null && !endTime.trim().equals("")) {
                    predicate.getExpressions().add(cb.lessThanOrEqualTo(root.get("createTime").as(String.class), endTime));
                }

                predicate.in(ids);

//                query.orderBy(ids);

                return predicate;
            }
        };

        List<FcDTO> fcDTOList = fcInfoRepository.findAll(specification,pageable).stream().map(e -> new FcDTO(
                e.getFcId(),
                e.getFcName(),
                aliyunOSSUtil.addDomainName(e.getFcIcon()),
                e.getFcDescription(),
                aliyunOSSUtil.addDomainName(e.getFcPicture()),
                e.getFcDownloadUrl(),
                //获取收藏信息
                favoritesService.findByUserIdAndFObjectId(userId, e.getFcId().toString()),
                e.getFcRankingId(),
                e.getFcRankingShow().equals(1),
                e.getCreateTime()
        )).collect(Collectors.toList());
        return fcDTOList;
    }
}
