package com.org.hktshop.service.System;

import com.org.hktshop.Dao.Intface.IGoodsItemDao;
import com.org.hktshop.Interface.system.HktFavoritesService;
import com.org.hktshop.base.BaseService;
import com.org.hktshop.base.UserContext;
import com.org.hktshop.dto.CategoryInfo;
import com.org.hktshop.entity.Page;
import com.org.hktshop.entity.product.HktGoodItem;
import com.org.hktshop.entity.system.HktFavorites;
import com.org.hktshop.entity.user.HktUser;
import com.org.hktshop.utils.ExecSqlLib;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class HktFavoritesServiceImpl extends BaseService<HktFavorites> implements HktFavoritesService {

    @Autowired
    private UserContext userContext;

    @Autowired
    private IGoodsItemDao goodsItemDao;

    @Override
    public void saveHktFavorites(HktFavorites hktFavorites) {
        HktFavorites query = this.baseJdbcDao.query(ExecSqlLib.GetQuerySql(hktFavorites), hktFavorites);
        if(query == null){
            this.updateJdbc(hktFavorites);
            HktGoodItem hktGoodItem = new HktGoodItem();
            hktGoodItem.setId(hktFavorites.getGoodsId());
            hktGoodItem.setScore(4);
            goodsItemDao.updateGoodsScore(hktGoodItem);
        }
    }

    @Override
    public void deleteHktFavorites(HktFavorites hktFavorites) {
        this.baseJdbcDao.execute(ExecSqlLib.GetDeleteSql(hktFavorites));
    }

    @Override
    public void updateHktFavorites(HktFavorites hktFavorites) {
        this.updateJdbc(hktFavorites);
    }

    @Override
    public HktFavorites getHktFavoritesById(String goodId) {
        HktFavorites favorites = new HktFavorites();
        favorites.setGoodsId(goodId);
        String sql = ExecSqlLib.GetQuerySql(favorites);
        HktFavorites query = this.baseJdbcDao.query(sql, favorites);
        return query;
    }

    @Override
    public Set<String> getIdGoodsByUser() {
        Set<String> res = new HashSet<>();
        HktUser user = userContext.getUserInfo();
        if (user == null) {
            return res;
        }
        HktFavorites favorites = new HktFavorites();
        favorites.setUsersId(user.getId());
        String sql = ExecSqlLib.GetQuerySql(favorites);
        List<HktFavorites> hktFavorites = this.baseJdbcDao.queryList(sql, favorites);
        for (HktFavorites hktFavorites1 : hktFavorites) {
            res.add(hktFavorites1.getGoodsId());
        }
        return res;
    }

    @Override
    public Page<HktFavorites> favoritePage(HktFavorites hktFavorites) {
        return this.queryPages(hktFavorites);
    }

    @Override
    public List<CategoryInfo> getCategoryInfo(String userId) {
        HktFavorites favorites = new HktFavorites();
        favorites.setUsersId(userId);
        List<HktFavorites> hktFavorites = queryList(favorites);
        Map<String, Long> map = hktFavorites.stream()
                .collect(Collectors.groupingBy(HktFavorites::getCategoryName, Collectors.counting()));
        List<CategoryInfo> res = new ArrayList<>();
        for (Map.Entry<String, Long> entry : map.entrySet()) {
            CategoryInfo categoryInfo = new CategoryInfo();
            categoryInfo.setName(entry.getKey());
            categoryInfo.setCount(entry.getValue());
            res.add(categoryInfo);
        }
        return res;
    }

    @Override
    public List<HktFavorites> getHktFavoritesByUserId(String userId) {
        HktFavorites favorites = new HktFavorites();
        favorites.setUsersId(userId);
        List<HktFavorites> hktFavoritesList = this.baseJdbcDao.queryList(ExecSqlLib.GetQuerySql(favorites), favorites);
        if (CollectionUtils.isEmpty(hktFavoritesList)) {
            return new ArrayList<>();
        }
        return hktFavoritesList;
    }

}
