package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.BannerService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.teambuyBanner.entity.TeambuyBanner;
import org.jeecg.modules.teambuyBanner.service.ITeambuyBannerService;
import org.jeecg.modules.teambuyGoods.entity.TeambuyGoods;
import org.jeecg.modules.teambuyGoods.service.ITeambuyGoodsService;
import org.jeecg.modules.teambuyInfo.entity.TeambuyInfo;
import org.jeecg.modules.teambuyInfo.service.ITeambuyInfoService;
import org.jeecg.modules.teambuyOrderGoods.entity.TeambuyOrderGoods;
import org.jeecg.modules.teambuyOrderGoods.service.ITeambuyOrderGoodsService;
import org.jeecg.modules.teambuyOrderLeader.entity.TeambuyOrderLeader;
import org.jeecg.modules.teambuyOrderLeader.service.ITeambuyOrderLeaderService;
import org.jeecg.modules.teambuyOrderMember.entity.TeambuyOrderMember;
import org.jeecg.modules.teambuyOrderMember.service.ITeambuyOrderMemberService;
import org.jeecg.modules.teambuyTeamLeader.entity.TeambuyTeamLeader;
import org.jeecg.modules.teambuyTeamLeader.service.ITeambuyTeamLeaderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class BannerServiceImpl implements BannerService {

    /******************************************************************************************************************/
    //轮播图信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //轮播图信息
    @Resource
    private ITeambuyBannerService teambuyBannerService;
    //展示信息
    @Resource
    private ITeambuyInfoService teambuyInfoService;
    //团长信息
    @Resource
    private ITeambuyTeamLeaderService teambuyTeamLeaderService;
    //团餐订单信息
    @Resource
    private ITeambuyOrderLeaderService teambuyOrderLeaderService;
    //团员订单信息
    @Resource
    private ITeambuyOrderMemberService teambuyOrderMemberService;
    //团员订单商品信息
    @Resource
    private ITeambuyOrderGoodsService teambuyOrderGoodsService;
    //商品信息信息
    @Resource
    private ITeambuyGoodsService teambuyGoodsService;
    /******************************************************************************************************************/

    //查看轮播图列表
    @Override
    public Result<?> queryBannerList(String type, PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyBanner> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyBanner> query = null;
        //返回信息
        Page<TeambuyBanner> pageList = null;

        try{
            //分页
            page = new Page<TeambuyBanner>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyBannerService
                    .lambdaQuery();

            //组装查询条件
            if(StringUtils.isNotBlank(type)){
                query.eq(TeambuyBanner::getType, type);
            }

            //按照排序编号升序排列
            query.orderByAsc(TeambuyBanner::getOrderNo);

            //获取轮播图信息
           pageList = query.page(page);

            return Result.OK("轮播图列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("轮播图列表查询失败");
        }
    }

    //查看展示信息列表
    @Override
    public Result<?> queryInfoList(PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyInfo> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyInfo> query = null;
        //返回信息
        Page<TeambuyInfo> pageList = null;

        try{
            //分页
            page = new Page<TeambuyInfo>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyInfoService
                    .lambdaQuery();

            //按照排序编号升序排列
            query.orderByDesc(TeambuyInfo::getCreateTime);

            //获取展示信息
            pageList = query.page(page);

            return Result.OK("首页展示信息列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("首页展示信息列表查询失败");
        }
    }

    //查看团员订单列表
    @Override
    public Result<?> rollList(PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyOrderMember> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyOrderMember> query = null;
        //返回信息
        Page<TeambuyOrderMember> pageList = null;

        try{
            //分页
            page = new Page<TeambuyOrderMember>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyOrderMemberService
                    .lambdaQuery();
            //未取消订单
            query.eq(TeambuyOrderMember::getIsDel, "0");
            //已完成订单
            query.eq(TeambuyOrderMember::getStatus, "4");
            //按照创建时间降序排列
            query.orderByDesc(TeambuyOrderMember::getCreateTime);

            //获取团员订单信息列表
            pageList = query.page(page);

            //查询团员订单商品信息列表列表
            for (TeambuyOrderMember record : pageList.getRecords()) {
                //订单所属团长信息
                TeambuyOrderLeader orderLeader = teambuyOrderLeaderService.getById(record.getOrderLeaderId());
                TeambuyTeamLeader leader = teambuyTeamLeaderService.getById(orderLeader.getUserId());
                record.setTeamLeader(leader);

                //订单商品列表
                List<TeambuyOrderGoods> goodsList = teambuyOrderGoodsService
                        .lambdaQuery()
                        .eq(TeambuyOrderGoods::getOrderId, record.getId())
                        .list();
                //商品详情
                for (TeambuyOrderGoods teambuyOrderGoods : goodsList) {
                    TeambuyGoods goods = teambuyGoodsService.getById(teambuyOrderGoods.getGoodsId());
                    teambuyOrderGoods.setGoods(goods);
                }
                record.setGoodsList(goodsList);
            }

            //查询订单所属用户信息
            for (TeambuyOrderMember record : pageList.getRecords()) {
                HanHaiMember member = hanHaiMemberService.getById(record.getUserId());
                record.setHanHaiMember(member);
            }

            return Result.OK("订单列表", pageList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("订单列表查询失败");
        }
    }

}
