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

import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.config.shiro.ShiroRealm;
import org.jeecg.config.vo.Shiro;
import org.jeecg.modules.clockinauth.entity.ClockinAuth;
import org.jeecg.modules.clockinauth.service.IClockinAuthService;
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.hanHaiMember.service.IHanHaiMemberService;
import org.jeecg.modules.xcx.user.req.EditUserReq;
import org.jeecg.modules.xcx.user.service.IUserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements IUserService {

    @Resource
    private ShiroRealm shiroRealm;
    @Resource
    private IClockinTeamLogService clockinTeamLogService;
    @Resource
    private IClockinTeamService clockinTeamService;
    @Resource
    private IHanHaiMemberService hanHaiMemberService;
    @Resource
    private IClockinAuthService clockinAuthService;

    @Override
    public Result<?> getUserInfo(String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);
        ClockinTeamLog clockinTeamLog = clockinTeamLogService.lambdaQuery()
                .eq(ClockinTeamLog::getDelFlag, 0)
                .eq(ClockinTeamLog::getUserId, hanHaiMember.getId())
                .one();
        if(clockinTeamLog != null){
            ClockinTeam clockinTeam = clockinTeamService.lambdaQuery()
                    .eq(ClockinTeam::getDelFlag, 0)
                    .eq(ClockinTeam::getId, clockinTeamLog.getTeamId())
                    .one();
            hanHaiMember.setTeam(clockinTeam);
        }
        ClockinAuth clockinAuth = clockinAuthService.lambdaQuery()
                .eq(ClockinAuth::getDelFlag, 0)
                .in(ClockinAuth::getStatus,0,1)
                .eq(ClockinAuth::getUserId, hanHaiMember.getId())
                .one();
        hanHaiMember.setAuth(clockinAuth);
        return Result.OK(hanHaiMember);
    }

    @Override
    public Result<?> editUserInfo(EditUserReq editUserReq, String token) {
        HanHaiMember hanHaiMember = shiroRealm.checkUserTokenIsEffectHanHaiAccount(token);

        HanHaiMember hanHaiMember1 = new HanHaiMember();
        hanHaiMember1.setId(hanHaiMember.getId());
        if(StringUtils.isNotBlank(editUserReq.getHeadImg())){
            hanHaiMember1.setHeadImage(editUserReq.getHeadImg());
        }
        if(StringUtils.isNotBlank(editUserReq.getNickname())){
            hanHaiMember1.setNickName(editUserReq.getNickname());
        }
        hanHaiMemberService.updateById(hanHaiMember1);
        return Result.OK();
    }
}
