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.ExperienceService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.studytourBindperson.entity.StudytourBindperson;
import org.jeecg.modules.studytourImage.entity.StudytourImageContent;
import org.jeecg.modules.studytourRecord.entity.StudytourRecord;
import org.jeecg.modules.studytourRecord.service.IStudytourRecordService;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ExperienceServiceImpl implements ExperienceService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //权限验证
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //权限验证
    @Resource
    private IStudytourRecordService studytourRecordService;
    /******************************************************************************************************************/

    //成长档案-查询成长档案列表
    @Override
    public Result<?> queryExperienceList(String token, StudytourRecord studytourRecord, PageBean pageBean) {
        log.info("开始查询成长档案信息列表");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";
        //分页信息
        Page<StudytourRecord> page = null;
        //查询信息
        LambdaQueryChainWrapper<StudytourRecord> query = null;
        //返回信息
        Page<StudytourRecord> pageList = null;

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

            //组装查询条件
//            if(StringUtils.isNotEmpty(studytourRecord)){
//                query.eq(StudytourRecord::getImageId, imageId);
//            }

            //所属用户
            query.eq(StudytourRecord::getUserId, hanHaiMember.getId());

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

            //获取图片直播信息列表
            pageList = query.page(page);

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

    //成长档案-查询成长档案详情
    @Override
    public Result<?> queryExperienceById(String token, String recordId) {
        log.info("开始查询成长档案详情");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";

        try{
            //成长档案详情
            StudytourRecord one = studytourRecordService
                    .lambdaQuery()
                    .eq(StudytourRecord::getId, recordId)
                    .eq(StudytourRecord::getUserId, hanHaiMember.getId())
                    .one();

            log.info("成长档案详情查询结束");
            return Result.OK("成长档案详情", one);
        }catch (Exception e){
            log.info("成长档案详情查询失败");
            e.printStackTrace();
            return Result.error("成长档案详情查询失败");
        }
    }

    //成长档案-新增成长档案
    @Override
    public Result<?> addExperience(String token, StudytourRecord studytourRecord) {
        log.info("开始新增成长档案信息");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";

        try{
            //1、新增成长档案信息
            studytourRecord.setUserId(hanHaiMember.getId());
            boolean result =  studytourRecordService.save(studytourRecord);

            if(result){
                log.info("成长档案信息新增成功");
                return Result.OK("成长档案信息新增成功");
            }else {
                log.info("成长档案信息新增失败");
                return Result.error("成长档案信息新增失败");
            }

        }catch (Exception e){
            log.info("成长档案信息新增失败");
            e.printStackTrace();
            return Result.error("成长档案信息新增失败");
        }
    }

}
