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.config.shiro.ShiroRealm;
import org.jeecg.modules.apiBean.PageBean;
import org.jeecg.modules.apiService.LeaderService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.teambuyCategory.entity.TeambuyCategory;
import org.jeecg.modules.teambuyGoods.entity.TeambuyGoods;
import org.jeecg.modules.teambuyTeamLeader.entity.TeambuyTeamLeader;
import org.jeecg.modules.teambuyTeamLeader.service.ITeambuyTeamLeaderService;
import org.jeecg.modules.teambuyTeamRelation.entity.TeambuyTeamRelation;
import org.jeecg.modules.teambuyTeamRelation.service.ITeambuyTeamRelationService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class LeaderServiceImpl implements LeaderService {
    /******************************************************************************************************************/
    //权限验证
    @Resource
    private ShiroRealm shiroRealm;
    //用户信息
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    //团长信息
    @Resource
    private ITeambuyTeamLeaderService teambuyTeamLeaderService;
    //团员团长关联
    @Resource
    private ITeambuyTeamRelationService teambuyTeamRelationService;
    /******************************************************************************************************************/

    //团长申请
    @Override
    public Result<?> addLeader(String token, TeambuyTeamLeader teamLeader) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919587097244078081");
        //返回信息
        String massege = "";

        try{
            //判断是否已经存在审核已通过状态团长信息
            TeambuyTeamLeader leader1 = teambuyTeamLeaderService
                    .lambdaQuery()
                    .eq(TeambuyTeamLeader::getUserId, hanHaiMember.getId())
                    .eq(TeambuyTeamLeader::getStatus, 1)
                    .one();
            if(null != leader1){
                teamLeader.setId(leader1.getId());
            }

            //判断是否已经存在审核未通过状态团长信息
            TeambuyTeamLeader leader2 = teambuyTeamLeaderService
                    .lambdaQuery()
                    .eq(TeambuyTeamLeader::getUserId, hanHaiMember.getId())
                    .in(TeambuyTeamLeader::getStatus, 0,2)
                    .one();
            if(null != leader2){
                teamLeader.setId(leader2.getId());
            }

            //执行新增操作(不传id进行新增，传了id则进行修改)
            teamLeader.setUserId(hanHaiMember.getId());
            boolean result = teambuyTeamLeaderService.saveOrUpdate(teamLeader);

            //判断是否新增成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.OK("操作失败");
            }

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

    //修改团长信息
    @Override
    public Result<?> updateLeaderInfo(String token, TeambuyTeamLeader teamLeader) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String massege = "";

        try{
            //判断传入的id是否是自己的
            //执行新增操作(不传id进行新增，传了id则进行修改)
            teamLeader.setUserId(hanHaiMember.getId());
            boolean result = teambuyTeamLeaderService.updateById(teamLeader);

            //判断是否新增成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.OK("操作失败");
            }

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



    //解绑团长
    @Override
    public Result<?> deleteLeader(String token, TeambuyTeamRelation teamRelation) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919587097244078081");
        //返回信息
        String massege = "";

        try{
            //解绑团长
            boolean result = teambuyTeamRelationService
                    .lambdaUpdate()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .eq(TeambuyTeamRelation::getLeaderId, teamRelation.getLeaderId())
                    .set(TeambuyTeamRelation::getStatus, "0")
                    .update();

            //判断是否新增成功
            if(result){
                return Result.OK("解绑中，请耐心等待管理员审核");
            }else {
                return Result.OK("操作失败");
            }

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

    //取消解绑
    @Override
    public Result<?> cancelDeleteLeader(String token, TeambuyTeamRelation teamRelation) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);

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

        try{
            //取消解绑
            boolean result = teambuyTeamRelationService
                    .lambdaUpdate()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .eq(TeambuyTeamRelation::getLeaderId, teamRelation.getLeaderId())
                    .set(TeambuyTeamRelation::getStatus, "1")
                    .update();

            //判断是否新增成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.OK("操作失败");
            }

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

    //更换团长
    @Override
    public Result<?> updateLeader(String token, TeambuyTeamRelation teamRelation) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String massege = "";

        try{
            //查询用户是否有绑定团长，如果有，需要先解除绑定
            TeambuyTeamRelation relation = teambuyTeamRelationService.lambdaQuery()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .one();

            if(null == relation){
                //还没有绑定团长,先绑定团长
                TeambuyTeamRelation teambuyTeamRelation = new TeambuyTeamRelation();
                teambuyTeamRelation.setLeaderId(teamRelation.getLeaderId());
                teambuyTeamRelation.setMemberId(hanHaiMember.getId());
                boolean result = teambuyTeamRelationService.save(teambuyTeamRelation);
                //判断是否更换成功
                if(result){
                    return Result.OK("操作成功");
                }else {
                    return Result.error("操作失败");
                }
            }else {
                if("0".equals(relation.getStatus())){
                    return Result.error("原团长解绑中，解绑成功后方可更换团长");
                }
                if("1".equals(relation.getStatus())){
                    return Result.error("请先解除当前团长绑定");
                }
            }

            //更换团长
            boolean result = teambuyTeamRelationService
                    .lambdaUpdate()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .set(TeambuyTeamRelation::getLeaderId, teamRelation.getLeaderId())
                    .set(TeambuyTeamRelation::getStatus, "1")
                    .update();

            //判断是否更换成功
            if(result){
                return Result.OK("操作成功");
            }else {
                return Result.error("操作失败");
            }

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

    //查询团长信息
    @Override
    public Result<?> queryLeaderInfo(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //返回信息
        String massege = "";

        try{
            TeambuyTeamLeader one = teambuyTeamLeaderService
                    .lambdaQuery()
                    .eq(TeambuyTeamLeader::getUserId, hanHaiMember.getId())
                    .one();

            //修改团长等级
            if("1".equals(one.getStatus())){
                hanHaiMember.setLeaderLevelId("1920313132579389441");
                hanHaiMemberService.updateById(hanHaiMember);
            }else {
                hanHaiMember.setLeaderLevelId("0");
                hanHaiMemberService.updateById(hanHaiMember);
            }

            return Result.OK("团长信息详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("团长信息详情查询失败");
        }
    }

    //查询团团员列表
    @Override
    public Result<?> queryMemberList(String token, PageBean pageBean) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919587097244078081");
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyTeamRelation> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyTeamRelation> query = null;
        //返回信息
        Page<TeambuyTeamRelation> pageList = null;

        try{
            //团长信息
            TeambuyTeamLeader teamLeader = teambuyTeamLeaderService
                    .lambdaQuery()
                    .eq(TeambuyTeamLeader::getUserId, hanHaiMember.getId())
                    .one();
            if(null == teamLeader || !"1".equals(teamLeader.getStatus())){
                return Result.error("你还没有成为团长");
            }

            //分页
            page = new Page<TeambuyTeamRelation>(pageBean.getPageNo(), pageBean.getPageSize());
            query = teambuyTeamRelationService
                    .lambdaQuery();

            //团长id
            query.eq(TeambuyTeamRelation::getLeaderId, teamLeader.getId());
            //按照排序编号升序排列
            query.orderByDesc(TeambuyTeamRelation::getCreateTime);

            //获取商品信息信息
            pageList = query.page(page);

            for (TeambuyTeamRelation record : pageList.getRecords()) {
                //团长信息
                record.setTeamLeader(teamLeader);

                //团员信息
                HanHaiMember member = hanHaiMemberService.getById(record.getMemberId());
                record.setMember(member);
            }

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

    //查询团长列表
    @Override
    public Result<?> queryLeaderList(PageBean pageBean) {
        //返回信息
        String massege = "";
        //分页信息
        Page<TeambuyTeamLeader> page = null;
        //查询信息
        LambdaQueryChainWrapper<TeambuyTeamLeader> query = null;
        //返回信息
        Page<TeambuyTeamLeader> pageList = null;

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

            //团长状态为审核通过
            query.eq(TeambuyTeamLeader::getStatus, "1");
            //按照排序编号升序排列
            query.orderByDesc(TeambuyTeamLeader::getCreateTime);

            //获取商品信息信息
            pageList = query.page(page);

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

    //查询绑定团长信息
    @Override
    public Result<?> queryMyLeader(String token) {
        //权限验证
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiOpenId(token);
        //HanHaiMember hanHaiMember = hanHaiMemberService.getById("1919587097244078081");
        //返回信息
        String massege = "";

        try{
            //获取我的团长
            TeambuyTeamRelation relation = teambuyTeamRelationService
                    .lambdaQuery()
                    .eq(TeambuyTeamRelation::getMemberId, hanHaiMember.getId())
                    .one();

            //获取团长信息
            TeambuyTeamLeader one = teambuyTeamLeaderService
                    .lambdaQuery()
                    .eq(TeambuyTeamLeader::getId, relation.getLeaderId())
                    .one();
            one.setBindStatus(relation.getStatus());//绑定状态

            return Result.OK("绑定团长信息详情", one);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error("绑定团长信息详情查询失败");
        }
    }
}
