package org.jeecg.modules.apiService.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.*;
import org.jeecg.modules.apiService.IndexApiService;
import org.jeecg.modules.apiUtils.TxtToSoundUtils;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.travelAddress.entity.TravelAddress;
import org.jeecg.modules.travelAddress.service.ITravelAddressService;
import org.jeecg.modules.travelAdvance.entity.TravelAdvance;
import org.jeecg.modules.travelAdvance.service.ITravelAdvanceService;
import org.jeecg.modules.travelAmusement.entity.TravelAmusement;
import org.jeecg.modules.travelAmusement.service.ITravelAmusementService;
import org.jeecg.modules.travelArea.entity.TravelArea;
import org.jeecg.modules.travelArea.service.ITravelAreaService;
import org.jeecg.modules.travelArticle.entity.TravelArticle;
import org.jeecg.modules.travelArticle.service.ITravelArticleService;
import org.jeecg.modules.travelCategory.service.ITravelCategoryService;
import org.jeecg.modules.travelCertification.service.ITravelCertificationService;
import org.jeecg.modules.travelCollection.entity.TravelCollection;
import org.jeecg.modules.travelCollection.service.ITravelCollectionService;
import org.jeecg.modules.travelExperience.entity.TravelExperience;
import org.jeecg.modules.travelExperience.service.ITravelExperienceService;
import org.jeecg.modules.travelImage.entity.TravelImage;
import org.jeecg.modules.travelImage.service.ITravelImageService;
import org.jeecg.modules.travelOrder.entity.TravelOrder;
import org.jeecg.modules.travelOrder.service.ITravelOrderService;
import org.jeecg.modules.travelRole.service.ITravelRoleService;
import org.jeecg.modules.travelRoleInfo.entity.TravelRoleInfo;
import org.jeecg.modules.travelRoleInfo.service.ITravelRoleInfoService;
import org.jeecg.modules.travelShopcar.entity.TravelShopcar;
import org.jeecg.modules.travelShopcar.service.ITravelShopcarService;
import org.jeecg.modules.travelSpot.entity.TravelSpot;
import org.jeecg.modules.travelSpot.service.ITravelSpotService;
import org.jeecg.modules.travelThumpup.entity.TravelThumpup;
import org.jeecg.modules.travelThumpup.service.ITravelThumpupService;
import org.jeecg.modules.travelTime.entity.TravelTime;
import org.jeecg.modules.travelTime.service.ITravelTimeService;
import org.jeecg.modules.travelVideo.entity.TravelVideo;
import org.jeecg.modules.travelVideo.service.ITravelVideoService;
import org.jeecg.modules.travelVolunteer.entity.TravelVolunteer;
import org.jeecg.modules.travelVolunteer.service.ITravelVolunteerService;
import org.jeecg.modules.travelWares.entity.TravelWares;
import org.jeecg.modules.travelWares.service.ITravelWaresService;
import org.jeecg.modules.travelWaresOrder.entity.TravelWaresOrder;
import org.jeecg.modules.travelWaresOrder.service.ITravelWaresOrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class IndexApiServiceImpl implements IndexApiService {

    //获取小程序任一文章或服务


    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户表
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //角色表
    @Resource
    private ITravelRoleService travelRoleService;
    //角色信息表
    @Resource
    private ITravelRoleInfoService travelRoleInfoService;
    //景区表
    @Resource
    private ITravelAreaService travelAreaService;
    //景点表
    @Resource
    private ITravelSpotService travelSpotService;
    //分类表
    @Resource
    private ITravelCategoryService travelCategoryService;
    //图片表
    @Resource
    private ITravelImageService travelImageService;
    //文章表
    @Resource
    private ITravelArticleService travelArticleService;
    //视频表
    @Resource
    private ITravelVideoService travelVideoService;
    //视频点赞表
    @Resource
    private ITravelThumpupService travelThumpupService;
    //游玩项目表
    @Resource
    private ITravelAmusementService travelAmusementService;

    //非遗体验表
    @Resource
    private ITravelExperienceService travelExperienceService;

    //收货地址表
    @Resource
    private ITravelAddressService travelAddressService;

    //预约时段表
    @Resource
    private ITravelTimeService travelTimeService;

    //预约订单表
    @Resource
    private ITravelOrderService travelOrderService;

    //商品表
    @Resource
    private ITravelWaresService travelWaresService;

    //商品订单表
    @Resource
    private ITravelWaresOrderService travelWaresOrderService;

    //收藏表
    @Resource
    private ITravelCollectionService travelCollectionService;

    //购物车
    @Resource
    private ITravelShopcarService travelShopcarService;

    //建议表
    @Resource
    private ITravelAdvanceService travelAdvanceService;

    //志愿者表
    @Resource
    private ITravelVolunteerService travelVolunteerService;

    //证件表
    @Resource
    private ITravelCertificationService travelCertificationService;

    /*****************************************************************************************/

    /***************************首页**************************/
    //获取banner图列表
    @Override
    public Result<?> queryBannerList(String bannerCategoryId) {

        //0-首页 1-遗产路径 2-我要跟拍 3-非遗体验 4-无忧服务 5-申遗历程
        switch (bannerCategoryId){
            case "0":
                bannerCategoryId = "1836222983394902017";
                break;
            case "1":
                bannerCategoryId = "1838473006392385538";
                break;
            case "2":
                bannerCategoryId = "1838473106556559361";
                break;
            case "3":
                bannerCategoryId = "1838473306209624065";
                break;
            case "4":
                bannerCategoryId = "1838473405736263682";
                break;
            case "5":
                bannerCategoryId = "1843170621184884738";
                break;
            default:
                return Result.OK("参数输入不合法，请重新输入：0-首页 1-遗产路径 2-我要跟拍 3-非遗体验 4-无忧服务 5-申遗历程");

        }

        List<TravelImage> list = null;
        if(null != bannerCategoryId){
            list = travelImageService
                    .lambdaQuery()
                    .eq(TravelImage::getCategoryId, bannerCategoryId)
                    .orderByAsc(TravelImage::getOrderId)
                    .list();
        }else {
            list = travelImageService
                    .lambdaQuery()
                    .orderByAsc(TravelImage::getOrderId)
                    .list();
        }

        return Result.OK("轮播图列表", list);
    }

    //获取小程序任一文章或服务
    @Override
    public Result<?> queryArticleListByTitle(String articleTitle, PageBean pageBean) {

        //0-申遗历程 1-申遗缘由 2-遗产价值

        Page<TravelArticle> page = new Page<TravelArticle>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelArticle> pageList =null;

        if(null != articleTitle){

            pageList = travelArticleService
                    .lambdaQuery()
                    .like(TravelArticle::getArticleTitle, articleTitle)
                    .orderByDesc(TravelArticle::getCreateTime)
                    .page(page);
        }else {
            pageList = travelArticleService
                    .lambdaQuery()
                    .orderByDesc(TravelArticle::getCreateTime)
                    .page(page);
        }

        return Result.OK("文章列表", pageList);
    }

    //获取视频列表带分页
    @Override
    public Result<?> queryVideoList(PageBean pageBean) {
        Page<TravelVideo> page = new Page<TravelVideo>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelVideo> pageList = travelVideoService
                .lambdaQuery()
                .orderByDesc(TravelVideo::getCreateTime)
                .page(page);

        return Result.OK("视频列表", pageList);
    }

    //根据id给视频点赞
    @Override
    public Result<?> addThumpup(String token, String videoId) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //视频表点赞标识
        boolean flag = false;
        //视频关联表点赞标识
        boolean thumpupFlag = false;
        //返回信息
        String successMessage = "视频点赞/取消点赞失败！";
        String errorMessage = "视频点赞/取消点赞失败！";

        //视频表增减点赞数量
        TravelVideo video = queryVideoById(videoId);
        int newThumpup = video.getVideoThumpup();

        //视频点赞表增减数据
        //查询该用户是否给该视频点过赞
        TravelThumpup thumpup = travelThumpupService
                .lambdaQuery()
                .eq(TravelThumpup::getUserId, hanHaiMember.getId())
                .eq(TravelThumpup::getItemId, videoId)
                .one();

        //判断是点赞还是取消点赞
        if(null != thumpup){
            //取消点赞
            newThumpup -= 1;
            //视频点赞表取消关联
            thumpupFlag = travelThumpupService.removeById(thumpup.getId());
            successMessage = "取消点赞成功";
            errorMessage = "取消点赞失败";
        }else {
            //点赞
            newThumpup += 1;
            //视频点赞表添加关联
            TravelThumpup entity = new TravelThumpup();
            entity.setUserId(hanHaiMember.getId());
            entity.setItemId(videoId);
            thumpupFlag = travelThumpupService.save(entity);
            successMessage = "点赞成功";
            errorMessage = "点赞失败";
        }

        video.setVideoThumpup(newThumpup);
        flag = travelVideoService.saveOrUpdate(video);

        if(flag && thumpupFlag){
            return Result.OK(successMessage);
        }else {
            return Result.OK(errorMessage);
        }

    }

    //根据id查询视频信息
    public TravelVideo queryVideoById(String videoId) {

        TravelVideo video = travelVideoService
                .lambdaQuery()
                .eq(TravelVideo::getId, videoId)
                .one();

        return video;
    }

    /***************************申遗历程**************************/
    //获取文章列表带分页
    @Override
    public Result<?> queryArticleList(PageBean pageBean) {
        Page<TravelArticle> page = new Page<TravelArticle>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelArticle> pageList = travelArticleService
                .lambdaQuery()
                .orderByDesc(TravelArticle::getCreateTime)
                .page(page);

        return Result.OK("文章列表", pageList);
    }

    //根据文章类型获取文章列表带分页
    @Override
    public Result<?> queryArticleListByType(String articleType, PageBean pageBean) {

        //0-申遗历程 1-申遗缘由 2-遗产价值

        Page<TravelArticle> page = new Page<TravelArticle>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelArticle> pageList =null;

        if(null != articleType){

            pageList = travelArticleService
                    .lambdaQuery()
                    .eq(TravelArticle::getCategoryId, articleType)
                    .orderByDesc(TravelArticle::getCreateTime)
                    .page(page);
        }else {
            pageList = travelArticleService
                    .lambdaQuery()
                    .orderByDesc(TravelArticle::getCreateTime)
                    .page(page);
        }

        return Result.OK("文章列表", pageList);
    }

    //根据文章id获取文章详情
    @Override
    public Result<?> queryArticleById(String articleId) {
        TravelArticle one = travelArticleService
                .lambdaQuery()
                .eq(TravelArticle::getId, articleId)
                .one();

        return Result.OK("文章详情", one);
    }

    /**************遗产概况*********************/
    //获取景区列表带分页
    @Override
    public Result<?> queryAreaList(PageBean pageBean) {

        Page<TravelArea> page = new Page<TravelArea>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelArea> pageList = travelAreaService
                .lambdaQuery()
                .page(page);

        return Result.OK("景区列表", pageList);
    }

    //根据id获取景区详情
    @Override
    public Result<?> queryAreaListById(String areaId) {
        TravelArea one = travelAreaService
                .lambdaQuery()
                .eq(TravelArea::getId, areaId)
                .one();

        return Result.OK("景区详情", one);
    }

    //根据景区id获取该景区下的地点列表-景点/厕所/美食店铺/民宿
    @Override
    public Result<?> querySpotList(String areaId, String categoryId, PageBean pageBean) {

        //areaId：0-瓷都镇区 1-湖田片区 2-高岭片区 3-瑶里片区 4-蛟潭片区
        //categoryId：0-景点 1-美食店铺 2-民宿 3-厕所

        if(null != areaId){
            switch (areaId){
                case "0": areaId = "1835255148875296770";
                    break;
                case "1": areaId = "1835296270515376130";
                    break;
                case "2": areaId = "1835296379189792769";
                    break;
                case "3": areaId = "1835296552271941634";
                    break;
                case "4": areaId = "1835296680814776322";
                    break;
                default:
                    return Result.OK("所属区域参数输入错误，请重新输入：areaId：0-瓷都镇区 1-湖田片区 2-高岭片区 3-瑶里片区 4-蛟潭片区");
            }
        }


        Page<TravelSpot> page = new Page<TravelSpot>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelSpot> pageList = null;
        if(null != areaId && null != categoryId){
            pageList = travelSpotService
                    .lambdaQuery()
                    .eq(TravelSpot::getAreaId, areaId)
                    .eq(TravelSpot::getCategoryId, categoryId)
                    .page(page);
        }else if(null != areaId){
            pageList = travelSpotService
                    .lambdaQuery()
                    .eq(TravelSpot::getAreaId, areaId)
                    .page(page);
        }else if(null != categoryId){
            pageList = travelSpotService
                    .lambdaQuery()
                    .eq(TravelSpot::getCategoryId, categoryId)
                    .page(page);
        }else {
            pageList = travelSpotService
                    .lambdaQuery()
                    .page(page);
        }

        return Result.OK("景点列表", pageList);
    }

    //根据id获取地点详情
    @Override
    public Result<?> querySpotById(String spotId) {

        TravelSpot spot = travelSpotService
                .lambdaQuery()
                .eq(TravelSpot::getId, spotId)
                .one();

        return Result.OK("地点详情", spot);
    }


    //根据角色类型获取角色信息列表
    @Override
    public Result<?> queryRoleInfoList(String roleId, String level, String spot, PageBean pageBean) {

        //level: 1-金牌讲解员，1-专业讲解员
        //roleId: 0-讲解员，1-达人，2-摄影师

        Page<TravelRoleInfo> page = new Page<TravelRoleInfo>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelRoleInfo> pageList = null;

        if(null != roleId &&null != level && null != spot){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleId, roleId)
                    .eq(TravelRoleInfo::getRoleLevel, level)
                    .like(TravelRoleInfo::getSkill, spot)
                    .page(page);
        }else if(null != roleId && null != level){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleId, roleId)
                    .eq(TravelRoleInfo::getRoleLevel, level)
                    .page(page);
        }else if(null != roleId && null != spot){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleId, roleId)
                    .like(TravelRoleInfo::getSkill, spot)
                    .page(page);
        }else if(null != level && null != spot){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleLevel, level)
                    .like(TravelRoleInfo::getSkill, spot)
                    .page(page);
        }else if(null != roleId){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleId, roleId)
                    .page(page);
        }else if(null != level){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .eq(TravelRoleInfo::getRoleLevel, level)
                    .page(page);
        }else if(null != spot){
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .like(TravelRoleInfo::getSkill, spot)
                    .page(page);
        }else{
            pageList = travelRoleInfoService
                    .lambdaQuery()
                    .page(page);
        }

        return Result.OK("角色列表", pageList);
    }

    //根据角色id获取角色详情
    @Override
    public Result<?> queryRoleInfoById(String token, String roleInfoId) {

        //获取角色详情
        TravelRoleInfo roleInfo = travelRoleInfoService
                .lambdaQuery()
                .eq(TravelRoleInfo::getId, roleInfoId)
                .one();

        //如果用户是登录状态，查询该用户的收藏列表
        List<TravelCollection> collectionList = null;
        if(null != token){
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
            collectionList = travelCollectionService
                    .lambdaQuery()
                    .eq(TravelCollection::getUserId, hanHaiMember.getId())
                    .list();
        }

        //判断该项目是否被用户收藏
        boolean collectionFlag = false;
        if(null != collectionList){
            for (TravelCollection collection : collectionList) {
                if(roleInfo.getId() .equals(collection.getItemId())){
                    collectionFlag = true;
                    break;
                }
            }
        }

        RoleInfoApiBean roleInfoApiBean = new RoleInfoApiBean();
        roleInfoApiBean.setCollectonFlag(collectionFlag);
        roleInfoApiBean.setTravelRoleInfo(roleInfo);


        return Result.OK("角色详情", roleInfoApiBean);
    }

    //获取视频列表
    @Override
    public Result<?> queryVedioById(String token, String roleInfoId, PageBean pageBean) {
        //如果用户是登录状态，查询该用户的点赞列表
        List<TravelThumpup> thumpupList = null;
        if(null != token){
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
            thumpupList = travelThumpupService
                    .lambdaQuery()
                    .eq(TravelThumpup::getUserId, hanHaiMember.getId())
                    .list();
        }

//        Page<TravelVideo> page = new Page<TravelVideo>(pageBean.getPageNo(), pageBean.getPageSize());
//        Page<TravelVideo> pageList;
//
//        if(null != roleInfoId){
//            pageList = travelVideoService
//                    .lambdaQuery()
//                    .eq(TravelVideo::getCreateBy, roleInfoId)
//                    .orderByDesc(TravelVideo::getCreateTime)
//                    .page(page);
//        }else {
//            pageList = travelVideoService
//                    .lambdaQuery()
//                    .orderByDesc(TravelVideo::getCreateTime)
//                    .page(page);
//        }

        //查询视频列表
        List<TravelVideo> videoList;
        if(null != roleInfoId){
            videoList = travelVideoService
                    .lambdaQuery()
                    .eq(TravelVideo::getCreateBy, roleInfoId)
                    .orderByDesc(TravelVideo::getCreateTime)
                    .list();
        }else {
            videoList = travelVideoService
                    .lambdaQuery()
                    .orderByDesc(TravelVideo::getCreateTime)
                    .list();
        }

        List<VideoApiBean> videoApiBeanList = new ArrayList<>();
        //判断视频是否被用户点赞(时间复杂度比较大，需优化)
        for (TravelVideo video : videoList) {
            VideoApiBean videoApiBean = new VideoApiBean();
            boolean thumpupFlag = false;
            if(null != thumpupList){
                for (TravelThumpup thumpup : thumpupList) {
                    if(video.getId().equals(thumpup.getItemId())){
                        thumpupFlag = true;
                        break;
                    }
                }
            }
            videoApiBean.setThumpupFlag(thumpupFlag);
            videoApiBean.setTravelVideo(video);
            videoApiBeanList.add(videoApiBean);
        }

        //分页
        //页码
        int pageNo = pageBean.getPageNo();
        //每一页展示数量
        int pageSize = pageBean.getPageSize();
        //数据总量
        int listCount = videoApiBeanList.size();
        if(listCount < 1){
            return Result.OK("没有视频数据");
        }
        //总页数
        int pageCount = listCount%pageSize==0?listCount/pageSize:listCount/pageSize+1;
        //边界判断
        if(pageNo < 1){
            pageNo = 1;
        }else if(pageNo > pageCount){
            pageNo = pageCount;
        }

        //起始下标
        int startIndex = (pageNo-1)*pageBean.getPageSize();
        //结束下标
        int endIndex = startIndex + pageBean.getPageSize();
        if(endIndex > listCount){
            endIndex = listCount;
        }

        List<VideoApiBean> videoApiBeanListPage = videoApiBeanList.subList(startIndex,endIndex);

        return Result.OK("视频列表", videoApiBeanListPage);
    }

    //获取游玩项目列表
    @Override
    public Result<?> queryAmusementList(String amusementType, String isPay, PageBean pageBean) {
        Page<TravelAmusement> page = new Page<TravelAmusement>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelAmusement> pageList;

        if(null != amusementType && null != isPay){
            pageList = travelAmusementService
                    .lambdaQuery()
                    .eq(TravelAmusement::getAmusementType, amusementType)
                    .eq(TravelAmusement::getIsPay, isPay)
                    .page(page);
        }else if(null != amusementType){
            pageList = travelAmusementService
                    .lambdaQuery()
                    .eq(TravelAmusement::getAmusementType, amusementType)
                    .page(page);
        }else if(null != isPay){
            pageList = travelAmusementService
                    .lambdaQuery()
                    .eq(TravelAmusement::getIsPay, isPay)
                    .page(page);
        }else {
            pageList = travelAmusementService
                    .lambdaQuery()
                    .page(page);
        }


        return Result.OK("游玩项目列表/详情", pageList);
    }

    //获取游玩项目详情
    @Override
    public Result<?> queryAmusementById(String token, String amusementId) {
        //查看游玩项目详情
        TravelAmusement amusement = travelAmusementService
                .lambdaQuery()
                .eq(TravelAmusement::getId, amusementId)
                .one();

        //如果用户是登录状态，查询该用户的收藏列表
        List<TravelCollection> collectionList = null;
        if(null != token){
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
            collectionList = travelCollectionService
                    .lambdaQuery()
                    .eq(TravelCollection::getUserId, hanHaiMember.getId())
                    .list();
        }

        //判断该项目是否被用户收藏
        boolean collectionFlag = false;
        if(null != collectionList){
            for (TravelCollection collection : collectionList) {
                if(amusement.getId() .equals(collection.getItemId())){
                    collectionFlag = true;
                    break;
                }
            }
        }

        AmusementApiBean amusementApiBean = new AmusementApiBean();
        amusementApiBean.setCollectonFlag(collectionFlag);
        amusementApiBean.setTravelAmusement(amusement);

        return Result.OK("游玩项目详情", amusementApiBean);
    }

    //获取非遗体验列表
    @Override
    public Result<?> queryExperienceList(PageBean pageBean) {

        Page<TravelExperience> page = new Page<TravelExperience>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelExperience> pageList = travelExperienceService
                .lambdaQuery()
                .page(page);

        return Result.OK("非遗体验列表", pageList);
    }

    //获取非遗体验详情
    @Override
    public Result<?> queryExperienceById(String token, String experienceId) {

        //查看非遗体验详情
        TravelExperience experience = travelExperienceService
                .lambdaQuery()
                .eq(TravelExperience::getId, experienceId)
                .one();

        //如果用户是登录状态，查询该用户的收藏列表
        List<TravelCollection> collectionList = null;
        if(null != token){
            HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
            collectionList = travelCollectionService
                    .lambdaQuery()
                    .eq(TravelCollection::getUserId, hanHaiMember.getId())
                    .list();
        }

        //判断该项目是否被用户收藏
        boolean collectionFlag = false;
        if(null != collectionList){
            for (TravelCollection collection : collectionList) {
                if(experience.getId() .equals(collection.getItemId())){
                    collectionFlag = true;
                    break;
                }
            }
        }

        ExperienceApiBean experienceApiBean = new ExperienceApiBean();
        experienceApiBean.setCollectonFlag(collectionFlag);
        experienceApiBean.setTravelExperience(experience);

        return Result.OK("非遗体验详情", experienceApiBean);
    }

    //获取我的收藏列表
    // collectionType：0-遗产讲述 1-达人同游 2-遗产路径 3-我要跟拍 4-非遗体验 5-我要研学 6-文创好物
    @Override
    public Result<?> queryCollectionList(String token, String collectionType, PageBean pageBean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //收藏列表
        List<TravelCollection> list;
        if(null != collectionType){
            list = travelCollectionService
                    .lambdaQuery()
                    .eq(TravelCollection::getUserId, hanHaiMember.getId())
                    .eq(TravelCollection::getCollectionType, collectionType)
                    .orderByDesc(TravelCollection::getCreateTime)
                    .list();
        }else {
            list = travelCollectionService
                    .lambdaQuery()
                    .eq(TravelCollection::getUserId, hanHaiMember.getId())
                    .orderByDesc(TravelCollection::getCreateTime)
                    .list();
        }


        String type;//项目类型
        String itemId;//项目编号
        List<Object> cellectionList = new ArrayList<>();

        for (TravelCollection travelCollection : list) {
            type = travelCollection.getCollectionType();
            itemId = travelCollection.getItemId();
            switch (type){
                case "0":
                case "1":
                case "3":
                    TravelRoleInfo roleInfo = travelRoleInfoService
                            .lambdaQuery()
                            .eq(TravelRoleInfo::getId, itemId)
                            .one();
                    if(null != roleInfo){
                        cellectionList.add(roleInfo);
                    }
                    break;
                case "2":
                case "5":
                    TravelAmusement amusement = travelAmusementService
                            .lambdaQuery()
                            .eq(TravelAmusement::getId, itemId)
                            .one();
                    if(null != amusement){
                        cellectionList.add(amusement);
                    }
                    break;
                case "4":
                    TravelExperience experience = travelExperienceService
                            .lambdaQuery()
                            .eq(TravelExperience::getId, itemId)
                            .one();
                    if(null != experience){
                        cellectionList.add(experience);
                    }
                    break;
                case "6":
                    TravelWares wares = travelWaresService
                            .lambdaQuery()
                            .eq(TravelWares::getId, itemId)
                            .one();
                    if(null != wares){
                        cellectionList.add(wares);
                    }
                    break;
            }
        }

        //分页显示
        //页码
        int pageNo = pageBean.getPageNo();
        //每一页展示数量
        int pageSize = pageBean.getPageSize();
        //数据总量
        int listCount = cellectionList.size();
        if(listCount < 1){
            return Result.OK("没有收藏");
        }
        //总页数
        int pageCount = listCount%pageSize==0?listCount/pageSize:listCount/pageSize+1;
        //边界判断
        if(pageNo < 1){
            pageNo = 1;
        }else if(pageNo > pageCount){
            pageNo = pageCount;
        }

        //起始下标
        int startIndex = (pageNo-1)*pageBean.getPageSize();
        //结束下标
        int endIndex = startIndex + pageBean.getPageSize();
        if(endIndex > listCount){
            endIndex = listCount;
        }

        List<Object> pageList = cellectionList.subList(startIndex,endIndex);

        return Result.OK("我的收藏列表", pageList);
    }

    //新增收藏
    public Result<?> addCollection(String token, TravelCollection travelCollection) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        travelCollection.setUserId(hanHaiMember.getId());

        List<TravelCollection> list = travelCollectionService
                .lambdaQuery()
                .eq(TravelCollection::getUserId, hanHaiMember.getId())
                .list();

        String itemId = travelCollection.getItemId();
        for (TravelCollection collection : list) {
            if(itemId.equals(collection.getItemId())){
                return Result.OK("该收藏已存在");
            }
        }

        boolean flag = travelCollectionService.save(travelCollection);
        if(flag){
            return Result.OK("收藏成功！");
        }else {
            return Result.OK("收藏失败！");
        }
    }

    //取消收藏
    public Result<?> deleteCollection(String token, String collectionType, String itemId) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        TravelCollection collection = travelCollectionService
                .lambdaQuery()
                .eq(TravelCollection::getUserId, hanHaiMember.getId())
                .eq(TravelCollection::getItemId, itemId)
                .eq(TravelCollection::getCollectionType, collectionType)
                .one();

        Wrapper<TravelCollection> wrapper = travelCollectionService
                .lambdaQuery()
                .eq(TravelCollection::getUserId, hanHaiMember.getId())
                .eq(TravelCollection::getItemId, itemId)
                .eq(TravelCollection::getCollectionType, collectionType)
                .getWrapper();

        if(null != collection){
            boolean flag = travelCollectionService.remove(wrapper);
            if(flag){
                return Result.OK("取消收藏成功！");
            }else {
                return Result.OK("取消收藏失败！");
            }
        }else {
            return Result.OK("取消收藏失败，该收藏不存在，请检查token："+token+"，收藏类型："+collectionType+"关联项目编号："+itemId+"是否正确！");
        }

    }


    //获取地址列表
    @Override
    public Result<?> queryAddress(String token, TravelAddress travelAddress, PageBean pageBean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        Page<TravelAddress> page = new Page<TravelAddress>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelAddress> pageList;

        if(null != travelAddress.getUserId()){
            pageList = travelAddressService
                    .lambdaQuery()
                    .eq(TravelAddress::getUserId,hanHaiMember.getId())
                    .orderByDesc(TravelAddress::getCreateTime)
                    .page(page);
        }else {
            pageList = travelAddressService
                    .lambdaQuery()
                    .eq(TravelAddress::getUserId,hanHaiMember.getId())
                    .orderByDesc(TravelAddress::getCreateTime)
                    .page(page);
        }

        return Result.OK("地址列表", pageList);
    }

    //新增地址
    @Override
    public Result<?> addAddress(String token, TravelAddress travelAddress) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        /*//查询地址是否已经存在
        travelAddress.setUserId(hanHaiMember.getId());
        List<TravelAddress> addresses = travelAddressService.lambdaQuery().setEntity(travelAddress).list();
        if(null != addresses){
            return Result.OK("该地址已存在！");
        }*/

        if("1".equals(travelAddress.getAddressDefault())){
            //将原默认地址改为不默认
            List<TravelAddress> list = travelAddressService
                    .lambdaQuery()
                    .eq(TravelAddress::getAddressDefault,"1")
                    .eq(TravelAddress::getUserId,hanHaiMember.getId())
                    .list();

            if(null != list){
                for (TravelAddress address : list) {
                    address.setAddressDefault("0");
                    boolean flag = travelAddressService.updateById(address);
                }
            }
        }else if(null == travelAddress.getAddressDefault()){
            travelAddress.setAddressDefault("0");
        }

        travelAddress.setUserId(hanHaiMember.getId());
        boolean flag = travelAddressService.save(travelAddress);
        if(flag){
            return Result.OK("新增地址成功！");
        }else {
            return Result.OK("新增地址失败！");
        }
    }

    //删除地址
    @Override
    public Result<?> deleteAddress(String token, String addressId) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean flag = travelAddressService.removeById(addressId);
        if(flag){
            return Result.OK("删除地址成功！");
        }else {
            return Result.OK("删除地址失败！");
        }
    }

    //修改地址
    @Override
    public Result<?> updateAddress(String token, TravelAddress travelAddress) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        if("1".equals(travelAddress.getAddressDefault())){
            //将原默认地址改为不默认
            List<TravelAddress> list = travelAddressService
                    .lambdaQuery()
                    .eq(TravelAddress::getAddressDefault,"1")
                    .eq(TravelAddress::getUserId,travelAddress.getUserId())
                    .list();

            if(null != list){
                for (TravelAddress address : list) {
                    address.setAddressDefault("0");
                    boolean flag = travelAddressService.updateById(address);
                }
            }
        }

        boolean flag = travelAddressService.updateById(travelAddress);
        if(flag){
            return Result.OK("修改地址成功！");
        }else {
            return Result.OK("修改地址失败！");
        }
    }

    //修改默认地址
    @Override
    public Result<?> updateDefaultAddress(String token, String addressId) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        //将原默认地址改为不默认
        List<TravelAddress> list = travelAddressService
                .lambdaQuery()
                .eq(TravelAddress::getAddressDefault,"1")
                .eq(TravelAddress::getUserId,hanHaiMember.getId())
                .list();

        if(null != list){
            for (TravelAddress address : list) {
                address.setAddressDefault("0");
                boolean flag = travelAddressService.updateById(address);
            }
        }
        //boolean flag = travelAddressService.updateById(travelAddress);
        boolean flag = travelAddressService
                .lambdaUpdate()
                .set(TravelAddress::getAddressDefault, "1")
                .eq(TravelAddress::getId, addressId)
                .update();
        if(flag){
            return Result.OK("修改地址成功！");
        }else {
            return Result.OK("修改地址失败！");
        }
    }

    //查询预约时间段
    @Override
    public Result<?> queryOrderTime(String timeType) {
        List<TravelTime> list;

        if(null != timeType){
            list = travelTimeService
                    .lambdaQuery()
                    .eq(TravelTime::getTimeType, timeType)
                    .orderByAsc(TravelTime::getTimeOrder)
                    .list();
        }else {
            list = travelTimeService
                    .lambdaQuery()
                    .orderByAsc(TravelTime::getTimeOrder)
                    .list();
        }
        return Result.OK("时段列表", list);
    }

    //查询预约订单列表
    @Override
    public Result<?> queryOrderList(String token, String orderType, PageBean pageBean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        Page<TravelOrder> page = new Page<TravelOrder>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelOrder> pageList;

        if(null != orderType){
            pageList = travelOrderService
                    .lambdaQuery()
                    .eq(TravelOrder::getUserId, hanHaiMember.getId())
                    .eq(TravelOrder::getOrderType, orderType)
                    .orderByDesc(TravelOrder::getCreateTime)
                    .page(page);
        }else {
            pageList = travelOrderService
                    .lambdaQuery()
                    .eq(TravelOrder::getUserId, hanHaiMember.getId())
                    .orderByDesc(TravelOrder::getCreateTime)
                    .page(page);
        }
        return Result.OK("预约订单列表", pageList);
    }

    //创建预约订单
    @Override
    public Result<?> addOrder(String token, TravelOrder travelOrder) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        TravelOrder one = travelOrderService
                .lambdaQuery()
                .eq(TravelOrder::getUserId, hanHaiMember.getId())
                .eq(TravelOrder::getItemId, travelOrder.getItemId())
                .notIn(TravelOrder::getOrderStatus, 2)
                .one();

        if(null != one){
            return Result.OK("你有未完成的预约！");
        }

        travelOrder.setUserId(hanHaiMember.getId());
        travelOrder.setOrderStatus("0");
        boolean flag = travelOrderService.save(travelOrder);
        if(flag){
            return Result.OK("创建订单成功！");
        }else {
            return Result.OK("创建订单失败！");
        }
    }

    //获取文创好物列表
    public Result<?> queryWaresList(PageBean pageBean) {
        Page<TravelWares> page = new Page<TravelWares>(pageBean.getPageNo(), pageBean.getPageSize());
        Page<TravelWares> pageList;

        pageList = travelWaresService
                .lambdaQuery()
                .orderByDesc(TravelWares::getCreateTime)
                .page(page);

        return Result.OK("文创好物列表", pageList);
    }

    //根据Id获取文创好物详情
    @Override
    public Result<?> queryWaresById(String waresId) {

        TravelWares one = travelWaresService
                .lambdaQuery()
                .eq(TravelWares::getId, waresId)
                .one();

        return Result.OK("文创好物详情", one);
    }

    //获取文创好物订单列表
    @Override
    public Result<?> queryWaresOrderList(String token, String status, PageBean pageBean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        List<WaresOrderApiBean> waresOrderApiBeans = new ArrayList<>();

        //获取订单列表
        List<TravelWaresOrder> travelWaresOrders = null;
        if(null != status){
            travelWaresOrders = travelWaresOrderService
                    .lambdaQuery()
                    .eq(TravelWaresOrder::getUserId, hanHaiMember.getId())
                    .eq(TravelWaresOrder::getStatus, status)
                    .list();
        }else{
            travelWaresOrders = travelWaresOrderService
                    .lambdaQuery()
                    .eq(TravelWaresOrder::getUserId, hanHaiMember.getId())
                    .list();
        }

        //获取订单相关的商品列表
        for (TravelWaresOrder travelWaresOrder : travelWaresOrders) {
            WaresOrderApiBean waresOrderApiBean = new WaresOrderApiBean();

            String waresId = travelWaresOrder.getWaresId();
            String addressId = travelWaresOrder.getAddressId();

            TravelWares wares = travelWaresService
                    .lambdaQuery()
                    .eq(TravelWares::getId, waresId)
                    .one();

            TravelAddress address = travelAddressService
                    .lambdaQuery()
                    .eq(TravelAddress::getId, addressId)
                    .one();

            waresOrderApiBean.setTravelWaresOrder(travelWaresOrder);
            waresOrderApiBean.setTravelWares(wares);

            waresOrderApiBeans.add(waresOrderApiBean);

        }

        //分页查询
        //页码
        int pageNo = pageBean.getPageNo();
        //每一页展示数量
        int pageSize = pageBean.getPageSize();
        //数据总量
        int listCount = waresOrderApiBeans.size();
        if(listCount < 1){
            //没有数据
            Result.OK("没有订单，请先去创建订单！");
        }
        //总页数
        int pageCount = listCount%pageSize==0?listCount/pageSize:listCount/pageSize+1;
        //边界判断
        if(pageNo < 1){
            pageNo = 1;
        }else if(pageNo > pageCount){
            pageNo = pageCount;
        }

        //起始下标
        int startIndex = (pageNo-1)*pageBean.getPageSize();
        //结束下标
        int endIndex = startIndex + pageBean.getPageSize();
        if(endIndex > listCount){
            endIndex = listCount;
        }

        List<WaresOrderApiBean> pageList = waresOrderApiBeans.subList(startIndex,endIndex);

        return Result.OK("文创好物订单列表", pageList);
    }

    //创建文创好物订单
    @Override
    public Result<?> addWaresOrder(String token, TravelWaresOrder travelWaresOrder) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        TravelAddress address = travelAddressService
                .lambdaQuery()
                .eq(TravelAddress::getId, travelWaresOrder.getAddressId())
                .one();

        TravelWares wares = travelWaresService
                .lambdaQuery()
                .eq(TravelWares::getId, travelWaresOrder.getWaresId())
                .one();

        //商品单价
        BigDecimal waresPrice = wares.getWaresPrice();
        //商品数量
        int waresNumber =  travelWaresOrder.getNumber();

        //收件人姓名
        String userName = address.getUserName();
        //收件人电话号码
        String userPhone = address.getUserPhone();
        //收件人地址
        String userAddress = address.getArea()+address.getAddress();
        travelWaresOrder.setUserName(userName);
        travelWaresOrder.setUserPhone(userPhone);
        travelWaresOrder.setUserAddress(userAddress);

        //进位处理 2.35变成2.4
        BigDecimal price = waresPrice.multiply(new BigDecimal(waresNumber)).setScale(2,BigDecimal.ROUND_UP);
        travelWaresOrder.setUserId(hanHaiMember.getId());
        travelWaresOrder.setStatus(0);
        travelWaresOrder.setPrice(price);

        boolean flag = travelWaresOrderService.save(travelWaresOrder);
        if(flag){
            return Result.OK("创建文创好物订单成功！");
        }else {
            return Result.OK("创建文创好物订单失败！");
        }
    }

//    //获取购物车列表
//    @Override
//    public Result<?> queryShopcarList(String token, PageBean pageBean) {
//        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
//
//        Page<TravelShopcar> page = new Page<TravelShopcar>(pageBean.getPageNo(), pageBean.getPageSize());
//        List<TravelShopcar> list;
//
//        list = travelShopcarService
//                .lambdaQuery()
//                .eq(TravelShopcar::getUserId, hanHaiMember.getId())
//                .orderByDesc(TravelShopcar::getCreateTime)
//                .list();
//
//        String waresId;
//        List<TravelWares> waresList = new ArrayList<>();
//        for (TravelShopcar travelShopcar : list) {
//            waresId = travelShopcar.getWaresId();
//            TravelWares wares = travelWaresService
//                    .lambdaQuery()
//                    .eq(TravelWares::getId, waresId)
//                    .one();
//            if(null != wares){
//                waresList.add(wares);
//            }
//        }
//
//        return Result.OK("购物车列表", waresList);
//    }


    //获取购物车列表
    @Override
    public Result<?> queryShopcarList(String token, PageBean pageBean) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        Page<TravelShopcar> page = new Page<TravelShopcar>(pageBean.getPageNo(), pageBean.getPageSize());
        List<TravelShopcar> list;
        List<ShopcarApiBean> shopcarApiBeanList = new ArrayList<>();

        list = travelShopcarService
                .lambdaQuery()
                .eq(TravelShopcar::getUserId, hanHaiMember.getId())
                .orderByDesc(TravelShopcar::getCreateTime)
                .list();


        for (TravelShopcar travelShopcar : list) {
            ShopcarApiBean shopcarApiBean = new ShopcarApiBean();
            String waresId = travelShopcar.getWaresId();
            TravelWares wares = travelWaresService
                    .lambdaQuery()
                    .eq(TravelWares::getId, waresId)
                    .one();
            if(null != wares){
                shopcarApiBean.setWares(wares);
                shopcarApiBean.setShopcar(travelShopcar);
                shopcarApiBeanList.add(shopcarApiBean);
            }
        }

        //分页查询
        //页码
        int pageNo = pageBean.getPageNo();
        //每一页展示数量
        int pageSize = pageBean.getPageSize();
        //数据总量
        int listCount = shopcarApiBeanList.size();
        if(listCount < 1){
            //没有数据
            return null;
        }
        //总页数
        int pageCount = listCount%pageSize==0?listCount/pageSize:listCount/pageSize+1;
        //边界判断
        if(pageNo < 1){
            pageNo = 1;
        }else if(pageNo > pageCount){
            pageNo = pageCount;
        }

        //起始下标
        int startIndex = (pageNo-1)*pageBean.getPageSize();
        //结束下标
        int endIndex = startIndex + pageBean.getPageSize();
        if(endIndex > listCount){
            endIndex = listCount;
        }

        List<ShopcarApiBean> pageList = shopcarApiBeanList.subList(startIndex,endIndex);

        return Result.OK("购物车列表", pageList);
    }

    //添加购物车
    @Override
    public Result<?> addShopcar(String token, TravelShopcar travelShopcar) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        TravelShopcar one = travelShopcarService
                .lambdaQuery()
                .eq(TravelShopcar::getWaresId, travelShopcar.getWaresId())
                .eq(TravelShopcar::getUserId, hanHaiMember.getId())
                .one();

        //如果商品已存在于购物车，则只增加商品数量
        if(null != one){
            TravelShopcar shopcar = new TravelShopcar();
            shopcar.setUserId(one.getUserId());
            shopcar.setId(one.getId());
            shopcar.setWaresId(one.getWaresId());
            int number = one.getShopcarNumber() + travelShopcar.getShopcarNumber();
            shopcar.setShopcarNumber(number);
            boolean flag = travelShopcarService.lambdaUpdate()
                    .eq(TravelShopcar::getUserId, shopcar.getId())
                    .eq(TravelShopcar::getWaresId, shopcar.getWaresId())
                    .eq(TravelShopcar::getId, shopcar.getId())
                    .set(TravelShopcar::getShopcarNumber, number)
                    .update();
            if(flag){
                return Result.OK("添加购物车成功！");
            }else {
                return Result.OK("添加购物车失败！");
            }
        }else {
            travelShopcar.setUserId(hanHaiMember.getId());
            boolean flag = travelShopcarService.save(travelShopcar);
            if(flag){
                return Result.OK("添加购物车成功！");
            }else {
                return Result.OK("添加购物车失败！");
            }
        }


    }

    //取消购物车
    @Override
    public Result<?> deleteShopcar(String token, String shopcarId) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean flag = travelShopcarService.removeById(shopcarId);
        if(flag){
            return Result.OK("取消购物车成功！");
        }else {
            return Result.OK("取消购物车失败！");
        }
    }

    //修改购物车商品数量
    @Override
    public Result<?> updateShopcar(String token, TravelShopcar shopcar) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        boolean flag = travelShopcarService.updateById(shopcar);
        if(flag){
            return Result.OK("修改购物车信息成功！");
        }else {
            return Result.OK("修改购物车信息失败！");
        }
    }

    //获取用户个人信息
    @Override
    public Result<?> queryHanHaiMemberById(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        HanHaiMember one = hanHaiMemberService
                .lambdaQuery()
                .eq(HanHaiMember::getId, hanHaiMember.getId())
                .one();
        return Result.OK("用户个人信息！", one);
    }

    //修改用户个人信息
    @Override
    public Result<?> updateHanHaiMemberById(String token, HanHaiMember hanHaiMember) {
        HanHaiMember hanHaiMemberV = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

        HanHaiMember one = hanHaiMemberService
                .lambdaQuery()
                .eq(HanHaiMember::getId, hanHaiMemberV.getId())
                .one();

        if(null != one){
            boolean flag = hanHaiMemberService
                    .lambdaUpdate()
                    .eq(HanHaiMember::getId, one.getId())
                    .update(hanHaiMember);
        }

        return Result.OK("用户个人信息！", one);
    }

    @Override
    public Result<?> textToAudio(String text) {
        TxtToSoundUtils.textToSpeech(text);
        return null;
    }


    //我要帮助-添加建议
    @Override
    public Result<?> addAdvice(TravelAdvance travelAdvance) {
        boolean flag = travelAdvanceService.save(travelAdvance);
        if(flag){
            return Result.OK("新增建议成功！");
        }else {
            return Result.OK("新增建议失败！");
        }
    }

    //添加志愿者
    @Override
    public Result<?> addVolunteer(TravelVolunteer travelVolunteer) {
        boolean flag = travelVolunteerService.save(travelVolunteer);
        if(flag){
            return Result.OK("新增志愿者成功！");
        }else {
            return Result.OK("新增志愿者失败！");
        }
    }
}
