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.BindService;
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.studytourBindperson.service.IStudytourBindpersonService;
import org.jeecg.modules.studytourRecord.entity.StudytourRecord;
import org.jeecg.modules.studytourRecord.service.IStudytourRecordService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class BindServiceImpl implements BindService {

    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //权限验证
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //权限验证
    @Resource
    private IStudytourBindpersonService studytourBindpersonService;
    /******************************************************************************************************************/
    //成员管理-查询绑定人信息
    @Override
    public Result<?> queryBindUser(String bindUserId) {
        log.info("开始查询绑定人信息");

        //返回信息
        String massege = "";

        try{
            //查询绑定人信息
            HanHaiMember userInfo = hanHaiMemberService.getById(bindUserId);
            log.info("绑定人信息查询结束");
            return Result.OK("绑定人信息", userInfo);
        }catch (Exception e){
            log.info("绑绑定人信息查询失败");
            e.printStackTrace();
            return Result.error("绑定人信息查询失败");
        }

    }

    //成员管理-查询绑定成员列表
    @Override
    public Result<?> queryBindList(String token, StudytourBindperson studytourBindperson, PageBean pageBean) {
        log.info("开始查询绑定成员列表");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";
        //分页信息
        Page<StudytourBindperson> page = null;
        //查询信息
        LambdaQueryChainWrapper<StudytourBindperson> query = null;
        //返回信息
        Page<StudytourBindperson> pageList = null;

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

            //组装查询条件

            //主绑定人
            query.eq(StudytourBindperson::getBindId, hanHaiMember.getId());

            //绑定状态
            if(StringUtils.isNotEmpty(studytourBindperson.getStatus())){
                query.eq(StudytourBindperson::getStatus, studytourBindperson.getStatus());
            }

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

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

            log.info("绑定成员列表查询结束");
            return Result.OK("绑定成员列表", pageList);
        }catch (Exception e){
            log.info("绑定成员列表查询失败");
            e.printStackTrace();
            return Result.error("绑定成员列表查询失败");
        }
    }

    //成员管理-绑定申请
    @Override
    public Result<?> addBind(String token, StudytourBindperson studytourBindperson) {
        log.info("开始新增绑定申请");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";

        try{
            //1、新增绑定申请
            studytourBindperson.setUserId(hanHaiMember.getId());
            boolean result =  studytourBindpersonService.save(studytourBindperson);

            if(result){
                log.info("绑定申请新增成功");
                return Result.OK("绑定申请新增成功");
            }else {
                log.info("绑定申请新增失败");
                return Result.error("绑定申请新增失败");
            }

        }catch (Exception e){
            log.info("绑定申请新增失败");
            e.printStackTrace();
            return Result.error("绑定申请新增失败");
        }
    }

    //成员管理-审批绑定申请
    @Override
    public Result<?> updateBind(String token, StudytourBindperson studytourBindperson) {
        log.info("开始审批绑定申请");
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById(token);
        //返回信息
        String massege = "";

        try{
            //0、校验绑定信息
            if(StringUtils.isEmpty(studytourBindperson.getId())){
                return Result.error("绑定信息id为空，请传入id");
            }

            StudytourBindperson bindperson = studytourBindpersonService.getById(studytourBindperson.getId());
            if(null == bindperson){
                return Result.error("绑定信息不存在，请检查id是否正确："+ studytourBindperson.getId());
            }else if(!bindperson.getBindId().equals(hanHaiMember.getId())){
                return Result.error("该绑定信息与当前用户不匹配");
            }


            //1、新增绑定申请
            studytourBindperson.setBindId(hanHaiMember.getId());
            studytourBindperson.setUserId(bindperson.getUserId());
            boolean result =  studytourBindpersonService.updateById(studytourBindperson);

            if(result){
                log.info("绑定申请已审批");
                return Result.OK("绑定申请已审批");
            }else {
                log.info("绑定申请审批失败");
                return Result.error("绑定申请审批失败");
            }

        }catch (Exception e){
            log.info("绑定申请审批失败");
            e.printStackTrace();
            return Result.error("绑定申请审批失败");
        }
    }
}
