package org.jeecg.modules.xcx.team.service.impl;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.modules.clockinteam.entity.ClockinTeam;
import org.jeecg.modules.clockinteam.service.IClockinTeamService;
import org.jeecg.modules.clockinteamlog.entity.ClockinTeamLog;
import org.jeecg.modules.clockinteamlog.service.IClockinTeamLogService;
import org.jeecg.modules.hanHaiMember.entity.HanHaiMember;
import org.jeecg.modules.xcx.team.req.ApplyTeamReq;
import org.jeecg.modules.xcx.team.service.ITeamService;
import org.springframework.stereotype.Service;

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

@Service
public class TeamServiceImpl implements ITeamService {

    @Resource
    private IClockinTeamService clockinTeamService;
    @Resource
    private IClockinTeamLogService clockinTeamLogService;
    @Resource
    private ShiroRealm shiroRealm;

    @Override
    public Result<?> getTeam() {
        List<ClockinTeam> clockinTeamList = clockinTeamService.lambdaQuery()
                .eq(ClockinTeam::getDelFlag, 0)
                .orderByAsc(ClockinTeam::getSort)
                .list();
        return Result.OK(clockinTeamList);
    }

    @Override
    public Result<?> applyTeam(ApplyTeamReq applyTeamReq) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(applyTeamReq.getToken());
        ClockinTeamLog clockinTeamLog = clockinTeamLogService.lambdaQuery()
                .eq(ClockinTeamLog::getDelFlag, 0)
                .eq(ClockinTeamLog::getUserId, hanHaiMember.getId())
                .one();
        if(clockinTeamLog != null){
            throw new JeecgBootException("您已申请过团队");
        }
        ClockinTeamLog clockinTeamLog1 = new ClockinTeamLog();
        clockinTeamLog1.setUserId(hanHaiMember.getId());
        clockinTeamLog1.setTeamId(applyTeamReq.getTeamId());
        clockinTeamLog1.setName(applyTeamReq.getName());
        clockinTeamLogService.save(clockinTeamLog1);
        return Result.OK();
    }
}
