package org.jeecg.modules.api.service.Impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.api.bean.PageBean;
import org.jeecg.modules.api.service.ActivityService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.studytourActivity.entity.StudytourActivity;
import org.jeecg.modules.studytourActivity.service.IStudytourActivityService;
import org.jeecg.modules.studytourActivityDate.entity.StudytourActivityDate;
import org.jeecg.modules.studytourActivityDate.entity.StudytourDatePrice;
import org.jeecg.modules.studytourActivityDate.service.IStudytourActivityDateService;
import org.jeecg.modules.studytourActivityDate.service.IStudytourDatePriceService;
import org.jeecg.modules.studytourBanner.entity.StudytourBanner;
import org.jeecg.modules.studytourBanner.service.IStudytourBannerService;
import org.jeecg.modules.studytourCollection.entity.StudytourCollection;
import org.jeecg.modules.studytourCollection.service.IStudytourCollectionService;
import org.jeecg.modules.studytourPeriod.entity.StudytourPeriod;
import org.jeecg.modules.studytourPeriod.service.IStudytourPeriodService;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ActivityServiceImpl implements ActivityService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //年龄段信息
    @Resource
    private IStudytourPeriodService studytourPeriodService;
    //研学活动信息
    @Resource
    private IStudytourActivityService studytourActivityService;
    //研学活动团期信息
    @Resource
    private IStudytourActivityDateService studytourActivityDateService;
    //研学活动团期价格
    @Resource
    private IStudytourDatePriceService studytourDatePriceService;
    //研学活动收藏信息
    @Resource
    private IStudytourCollectionService studytourCollectionService;
    /******************************************************************************************************************/

    //首页&分类-查询研学活动列表
    @Override
    public Result<?> queryActivityList(StudytourActivity studytourActivity, PageBean pageBean) {
        log.info("开始查询研学活动列表");
        //返回信息
        String massege = "";
        //分页信息
        Page<StudytourActivity> page = null;
        //查询信息
        LambdaQueryChainWrapper<StudytourActivity> query = null;
        //返回信息
        Page<StudytourActivity> pageList = null;

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

            //组装查询条件
            if(StringUtils.isNotBlank(studytourActivity.getIsDiscount())){
                //限时优惠
                query.eq(StudytourActivity::getIsDiscount, studytourActivity.getIsDiscount());
            }
            if(StringUtils.isNotBlank(studytourActivity.getIsHot())){
                //口碑爆款
                query.eq(StudytourActivity::getIsHot, studytourActivity.getIsHot());
            }
            if(StringUtils.isNotBlank(studytourActivity.getIsNew())){
                //新上线路
                query.eq(StudytourActivity::getIsNew, studytourActivity.getIsNew());
            }
            if(StringUtils.isNotBlank(studytourActivity.getAddressId())){
                //地点
                query.eq(StudytourActivity::getAddressId, studytourActivity.getAddressId());
            }
            if(StringUtils.isNotBlank(studytourActivity.getAgeId())){
                //适合年龄
                query.eq(StudytourActivity::getAgeId, studytourActivity.getAgeId());
            }
            if(StringUtils.isNotBlank(studytourActivity.getTimeId())){
                //活动时长
                query.eq(StudytourActivity::getTimeId, studytourActivity.getTimeId());
            }
            if(StringUtils.isNotBlank(studytourActivity.getCategoryId())){
                //活动分类
                query.eq(StudytourActivity::getCategoryId, studytourActivity.getCategoryId());
            }
            if(StringUtils.isNotBlank(studytourActivity.getTitle())){
                //标题
                query.like(StudytourActivity::getTitle, studytourActivity.getTitle());
            }

            //是否下架
            query.eq(StudytourActivity::getIsDel, "0");

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

            //获取研学活动列表
            pageList = query.page(page);

            log.info("研学活动列表查询结束");
            return Result.OK("研学活动列表", pageList);
        }catch (Exception e){
            log.info("研学活动列表查询失败");
            e.printStackTrace();
            return Result.error("研学活动列表查询失败");
        }
    }

    //首页&分类-查询研学活动详情
    @Override
    public Result<?> queryActivityById(String activityId) {
        log.info("开始查询研学活动详情");
        //返回信息
        String massege = "";

        try{
            StudytourActivity one = studytourActivityService
                    .lambdaQuery()
                    .eq(StudytourActivity::getId, activityId)
                    .one();
            if(null == one){
                log.info("研学活动不存在，请检查活动id：{}", activityId);
                return Result.error("研学活动不存在，请检查活动id："+activityId, one);
            }

            List<StudytourActivityDate> dateList = studytourActivityDateService
                    .lambdaQuery()
                    .eq(StudytourActivityDate::getActivityId, activityId)
                    .list();

            List<StudytourPeriod> periodList = studytourPeriodService.list();
            if(null != dateList){
                //填充活动团期
                one.setDateList(dateList);
                for (StudytourActivityDate studytourActivityDate : dateList) {

                    //填充团期价格
                    List<StudytourDatePrice> priceList = studytourDatePriceService
                            .lambdaQuery()
                            .eq(StudytourDatePrice::getDateId,studytourActivityDate.getId())
                            .list();
                    if(null != priceList){
                        //填充团期价格
                        studytourActivityDate.setPriceList(priceList);

                        //填充价格年龄段名称
                        for (StudytourDatePrice studytourDatePrice : priceList) {
                            for (StudytourPeriod studytourPeriod : periodList) {
                                if(studytourDatePrice.getPeriodId().equals(studytourPeriod.getId())){
                                    studytourDatePrice.setPeriod_dictText(studytourPeriod.getTitle());
                                    break;
                                }
                            }
                        }
                    }
                }
            }

            log.info("研学活动详情查询结束");
            return Result.OK("研学活动详情", one);
        }catch (Exception e){
            log.info("研学活动详情查询失败");
            e.printStackTrace();
            return Result.error("研学活动详情查询失败");
        }
    }

    //首页&分类-查询我收藏的研学活动列表
    @Override
    public Result<?> queryCollectionActivityList(String token, PageBean pageBean) {
        log.info("开始查询活动信息");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";
        //分页信息
        Page<StudytourCollection> page = null;
        //查询信息
        LambdaQueryChainWrapper<StudytourCollection> query = null;
        //返回信息
        Page<StudytourCollection> pageList = null;

        try{
            //1、获取活动列表
            //分页
            page = new Page<StudytourCollection>(pageBean.getPageNo(), pageBean.getPageSize());
            query = studytourCollectionService
                    .lambdaQuery()
                    .eq(StudytourCollection::getUserId, hanHaiMember.getId());

            //组装查询条件

            //按照创建时间降序排列
            query.orderByDesc(StudytourCollection::getCreateTime);

            //获取活动信息
            pageList = query.page(page);

            //2、添加活动信息至收藏内
            for (StudytourCollection record : pageList.getRecords()) {
                StudytourActivity one = studytourActivityService
                        .lambdaQuery()
                        .eq(StudytourActivity::getId, record.getActivityId())
                        .one();
                record.setActivity(one);
            }

            log.info("活动信息查询结束");
            return Result.OK("活动列表", pageList);
        }catch (Exception e){
            log.info("活动信息查询失败");
            e.printStackTrace();
            return Result.error("活动列表查询失败");
        }
    }

    //首页&分类-收藏研学活动
    @Override
    public Result<?> collectionActivity(String token, String activityId) {
        log.info("收藏活动开始");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";

        try{
            //1、校验活动是否存在
            StudytourActivity activity = studytourActivityService.getById(activityId);
            if(null == activity){
                log.info("活动不存在，请检查活动id：{}", activityId);
                return Result.error("活动不存在，请检查活动id：" + activityId);
            }

            //2、校验活动是否已被用户收藏
            StudytourCollection collection = studytourCollectionService
                    .lambdaQuery()
                    .eq(StudytourCollection::getActivityId, activityId)
                    .eq(StudytourCollection::getUserId, hanHaiMember.getId())
                    .one();

            //3、添加/取消收藏
            if(null == collection){
                //3.1、添加收藏
                log.info("执行添加收藏");
                StudytourCollection studytourCollection = new StudytourCollection();
                studytourCollection.setActivityId(activityId);
                studytourCollection.setUserId(hanHaiMember.getId());
                boolean result = studytourCollectionService.save(studytourCollection);

                //判断是否新增成功
                if(result){
                    log.info("收藏成功");
                    return Result.OK("收藏成功");
                }else {
                    log.info("收藏失败");
                    return Result.error("收藏失败");
                }

            }else {
                //3.2、取消收藏
                log.info("执行取消收藏");
                boolean result = studytourCollectionService.removeById(collection.getId());

                //判断是否新增成功
                if(result){
                    log.info("取消收藏成功");
                    return Result.OK("取消收藏成功");
                }else {
                    log.info("取消收藏失败");
                    return Result.error("取消收藏失败");
                }
            }

        }catch (Exception e){
            log.error("操作失败");
            e.printStackTrace();
            return Result.error("操作失败");
        }
    }
}
