package com.zbkj.service.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.zbkj.common.model.home.HomeItem;
import com.zbkj.common.request.HomeItemRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.vo.HomeItemVo;
import com.zbkj.common.vo.HomeVo;
import com.zbkj.service.dao.HomeItemDao;
import com.zbkj.service.service.CategoryService;
import com.zbkj.service.service.EbHomeItemService;
import com.zbkj.service.service.StoreProductService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class EbHomeItemServiceImpl extends ServiceImpl<HomeItemDao, HomeItem> implements EbHomeItemService {

    @Autowired private HomeItemDao dao;
    @Autowired private CategoryService categoryService;
    @Autowired private StoreProductService productService;

    @Override
    public List<HomeItemVo> getAdminList(HomeItemRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        LambdaQueryWrapper<HomeItem> lambdaQueryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotBlank(request.getName())) {
            lambdaQueryWrapper.and(i -> i
                    .or().like(HomeItem::getName, request.getName())
                    .or().like(HomeItem::getTitle, request.getName()));
        }
        if (null != request.getHomeId()) {
            lambdaQueryWrapper.eq(HomeItem::getHomeId, request.getHomeId());
        }
        lambdaQueryWrapper.orderByDesc(HomeItem::getOrderNo).orderByDesc(HomeItem::getId);
        List<HomeItem> itemList = dao.selectList(lambdaQueryWrapper);
        if (itemList.isEmpty()) {
            return new ArrayList<>();
        }
        Map<Integer, String> cateMap = new HashMap<>();
        Map<Integer, String> prodMap = new HashMap<>();
        List<Integer> cateIds = itemList.stream().filter(i -> i.getJumpType() == 2).map(i -> Integer.parseInt(i.getJumpIds())).collect(Collectors.toList());
        List<Integer> prodIds = itemList.stream().filter(i -> i.getJumpType() == 1 ||  i.getJumpType() == 3).map(HomeItem::getJumpIds).flatMap(scores -> Arrays.stream(scores.split(","))).map(Integer::parseInt).collect(Collectors.toList());
        if (!cateIds.isEmpty()) { cateMap = categoryService.getListInId(cateIds); }
        if (!prodIds.isEmpty()) { prodMap = productService.getName(prodIds); }
        Map<Integer, String> finalCateMap = cateMap;
        Map<Integer, String> finalProdMap = prodMap;
        return itemList.stream().map(i-> {
            HomeItemVo vo = new HomeItemVo();
            BeanUtils.copyProperties(i, vo);
            Map<Integer, String> jumpIdsMap = new HashMap<>();
            if (i.getJumpType() == 1 || i.getJumpType() == 3) {
                String[] pSplit = i.getJumpIds().split(",");
                for (String pId : pSplit) {
                    Integer productId = Integer.parseInt(pId);
                    jumpIdsMap.put(productId, finalProdMap.get(productId));
                }
            } else if (i.getJumpType() == 2) {
                Integer cateId = Integer.parseInt(i.getJumpIds());
                jumpIdsMap.put(cateId, finalCateMap.get(cateId));
            }
            vo.setJumpIdsMap(jumpIdsMap);
            return vo;
        }).collect(Collectors.toList());
    }
}
