package com.gree.mall.miniapp.logic.renovation;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.miniapp.bean.goods.GoodsNewBean;
import com.gree.mall.miniapp.bean.user.CurrentCompanyWechat;
import com.gree.mall.miniapp.commonmapper.AppMapper;
import com.gree.mall.miniapp.commonmapper.SecActivityStageMapper;
import com.gree.mall.miniapp.enums.GoodsTypeEnum;
import com.gree.mall.miniapp.enums.OrderShareStatusEnum;
import com.gree.mall.miniapp.logic.common.CommonLogic;
import com.gree.mall.miniapp.logic.goods.GoodsLogic;
import com.gree.mall.miniapp.plus.entity.*;
import com.gree.mall.miniapp.plus.service.*;
import com.gree.mall.miniapp.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RenovationLogic {

    @Autowired
    CommonLogic commonLogic;
    @Autowired
    GoodsLogic goodsLogic;
    @Autowired
    AppMapper appMapper;
    @Autowired
    AdminCompanyWechatTemplateService adminCompanyWechatTemplateService;
    @Autowired
    GoodsNewsCategoryService goodsNewsCategoryService;
    @Autowired
    GoodsNewsCategoryGoodsService goodsNewsCategoryGoodsService;
    @Autowired
    GoodsTagRelaService goodsTagRelaService;
    @Autowired
    SecActivityStageMapper secActivityStageMapper;
    @Autowired
    OrderShareService orderShareService;

    /**
     * 小程序首页装修模板
     *
     * @param request
     * @return
     */
    public AdminCompanyWechatTemplate detail(HttpServletRequest request) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);

        AdminCompanyWechatTemplate adminCompanyWechatTemplate = adminCompanyWechatTemplateService.lambdaQuery()
                .eq(AdminCompanyWechatTemplate::getTemplateType, currentCompanyWechat.getUseTemplate())
                .eq(AdminCompanyWechatTemplate::getCompanyWechatId, currentCompanyWechat.getCompanyWechatId()).one();
        return adminCompanyWechatTemplate;
    }

    /**
     * 精选类别列表
     */
    public IPage<GoodsNewsCategory> categoryList(HttpServletRequest request, Integer pageNo, Integer pageSize) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        IPage<GoodsNewsCategory> page = goodsNewsCategoryService.lambdaQuery()
                .eq(GoodsNewsCategory::getStatus, 1)
                .eq(GoodsNewsCategory::getCompanyWechatId, currentCompanyWechat.getCompanyWechatId())
                .orderByAsc(GoodsNewsCategory::getSortNum)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }

    public List<OrderShare> settleList(HttpServletRequest request){
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        List<OrderShare> list = orderShareService.lambdaQuery()
                .eq(OrderShare::getCompanyWechatId,currentCompanyWechat.getCompanyWechatId())
                .eq(OrderShare::getStatus, OrderShareStatusEnum.OVER.toString())
                .gt(OrderShare::getAmount, 0)
                .orderByDesc(OrderShare::getSettleTime)
                .page(new Page<>(1,10)).getRecords();
        return list;
    }


    /**
     * 精选分类商品列表
     */
    public IPage<GoodsNewBean> queryNewGoods(HttpServletRequest request, String objId, String type, Integer pageNo, Integer pageSize, Integer sort) {
        CurrentCompanyWechat currentCompanyWechat = commonLogic.getCurrentCompanyWechat(request);
        IPage<GoodsNewBean> page = appMapper.queryNewGoods(new Page(pageNo, pageSize), objId, type, currentCompanyWechat.getCompanyWechatId(), sort);

        goodsLogic.supplyGoodsNewBean(page,currentCompanyWechat);
        return page;
    }


}
