package com.yida.data.school.smart.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yida.data.common.core.entity.smart.EduSmartWelcomeGuide;
import com.yida.data.common.core.entity.smart.EduSmartWelcomeGuideRoster;
import com.yida.data.common.core.entity.smart.EduSmartWelcomeGuideRosterRelation;
import com.yida.data.common.core.enums.EnableStatusEnum;
import com.yida.data.school.smart.mapper.EduSmartWelcomeGuideRosterRelationMapper;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideRosterRelationService;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideRosterService;
import com.yida.data.school.smart.service.EduSmartWelcomeGuideService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 智慧迎新指南花名册-微信用户绑定关系表Service实现
 *
 * @author ZYJ
 * @date 2023-05-17 13:53:18
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class EduSmartWelcomeGuideRosterRelationServiceImpl extends ServiceImpl
    <EduSmartWelcomeGuideRosterRelationMapper, EduSmartWelcomeGuideRosterRelation> implements
    EduSmartWelcomeGuideRosterRelationService {

    @Lazy
    @Resource
    private EduSmartWelcomeGuideService eduSmartWelcomeGuideService;

    @Lazy
    @Resource
    private EduSmartWelcomeGuideRosterService eduSmartWelcomeGuideRosterService;

    @Override
    public EduSmartWelcomeGuideRosterRelation getOpenIdRelation(String openId, Long deptId) {
        List<EduSmartWelcomeGuideRosterRelation> relationList = list(
            Wrappers.lambdaQuery(new EduSmartWelcomeGuideRosterRelation())
                .eq(EduSmartWelcomeGuideRosterRelation::getOpenId, openId)
                .eq(EduSmartWelcomeGuideRosterRelation::getDeptId, deptId));

        log.info("openID:【{}】,deptId:【{}】", openId, deptId);
        EduSmartWelcomeGuideRosterRelation returnRelation = null;

        if (CollUtil.isNotEmpty(relationList)) {
            for (EduSmartWelcomeGuideRosterRelation relation : relationList) {
                //判断当前指南是否启用
                EduSmartWelcomeGuide guide = eduSmartWelcomeGuideService.getById(relation.getGuideId());
                if (ObjectUtil.isNotNull(guide)) {
                    if (guide.getEnableStatus().equals(EnableStatusEnum.ENABLE_STATUS.getStatus())) {
                        EduSmartWelcomeGuideRoster guideRoster = eduSmartWelcomeGuideRosterService
                            .getOne(Wrappers.lambdaQuery(new EduSmartWelcomeGuideRoster())
                                .eq(EduSmartWelcomeGuideRoster::getId, relation.getRosterId()));
                        if(ObjectUtil.isNotNull(guideRoster)){
                            guideRoster.setQrCodeNoticeMessage(guide.getQrCodeNoticeMessage());
                            relation.setGuideRoster(guideRoster);
                            returnRelation = relation;
                            break;
                        }

                    }
                }
            }
        }
        return returnRelation;
    }
}
