//package shop.alien.gateway.service.impl;
//
//import com.alibaba.fastjson.JSONObject;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
//import com.baomidou.mybatisplus.core.toolkit.StringUtils;
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import lombok.RequiredArgsConstructor;
//import org.springframework.beans.BeanUtils;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//import org.springframework.transaction.annotation.Transactional;
//import shop.alien.entity.result.R;
//import shop.alien.entity.store.LawFirm;
//import shop.alien.entity.store.LawyerServiceArea;
//import shop.alien.entity.store.LawyerUser;
//import shop.alien.entity.store.dto.LawyerUserDto;
//import shop.alien.entity.store.vo.LawyerUserVo;
//import shop.alien.gateway.config.BaseRedisService;
//import shop.alien.gateway.mapper.LawyerServiceAreaMapper;
//import shop.alien.gateway.mapper.LawyerUserMapper;
//import shop.alien.util.common.JwtUtil;
//
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * 二期-门店用户 服务实现类
// *
// * @author ssk
// * @since 2024-12-11
// */
//@Transactional
//@Service
//@RequiredArgsConstructor
//public class LawyerUserLogInServiceImpl extends ServiceImpl<LawyerUserMapper, LawyerUser> implements LawyerUserLogInService {
//
//    @Value("${jwt.expiration-time}")
//    private String effectiveTime;
//
//    /**
//     * 设定初始化默认密码
//     */
//
//    private final BaseRedisService baseRedisService;
//    private final LawyerUserMapper lawyerUserMapper;
//    private final LawyerServiceAreaMapper lawyerServiceAreaMapper;
//    private final LawFirmMapper lawFirmMapper;
//
//    /**
//     * token
//     *
//     * @param lawyerUser
//     * @return
//     */
//    @Override
//    public R<LawyerUserVo> createToKen(LawyerUser lawyerUser) {
//        int effectiveTimeInt = Integer.parseInt(effectiveTime.substring(0, effectiveTime.length() - 1));
//        String effectiveTimeUnit = effectiveTime.substring(effectiveTime.length() - 1);
//        long effectiveTimeIntLong = 0L;
//        switch (effectiveTimeUnit) {
//            case "s": {
//                effectiveTimeIntLong = effectiveTimeInt * 1000L;
//                break;
//            }
//            case "m": {
//                effectiveTimeIntLong = effectiveTimeInt * 60L * 1000L;
//                break;
//            }
//            case "h": {
//                effectiveTimeIntLong = effectiveTimeInt * 60L * 60L * 1000L;
//                break;
//            }
//            case "d": {
//                effectiveTimeIntLong = effectiveTimeInt * 24L * 60L * 60L * 1000L;
//                break;
//            }
//        }
//
//        LawyerUserVo lawyerUserVo = new LawyerUserVo();
//        BeanUtils.copyProperties(lawyerUser, lawyerUserVo);
//        Map<String, String> tokenMap = new HashMap<>();
//        tokenMap.put("phone", lawyerUser.getPhone());
//        tokenMap.put("userName", lawyerUser.getName());
//        tokenMap.put("userId", lawyerUser.getId().toString());
//        tokenMap.put("userType", "lawyer");
//        tokenMap.put("passType", lawyerUser.getPassType().toString());
//        lawyerUserVo.setToken(JwtUtil.createJWT("lawyer_" + lawyerUser.getPhone(), lawyerUser.getName(), JSONObject.toJSONString(tokenMap), effectiveTimeIntLong));
//        baseRedisService.setString("lawyer_" + lawyerUser.getPhone(), lawyerUserVo.getToken());
//        return R.data(lawyerUserVo);
//    }
//
//    @Override
//    public R<LawyerUserVo> logIn(LawyerUserDto lawyerUserDto) {
//        LawyerUser lawyerUser = lawyerUserMapper.selectOne(new LambdaQueryWrapper<LawyerUser>()
//                .eq(LawyerUser::getPhone, lawyerUserDto.getPhone()).last("limit 1"));
//        if (ObjectUtils.isNotEmpty(lawyerUser)) {
//            if (lawyerUser.getStatus() == 0) {
//                return R.fail("账号被禁用");
//            }
//            lawyerUser.setPassType(1);
//            return createToKen(lawyerUser);
//        }else {
//            LawyerUser user = new LawyerUser();
//            BeanUtils.copyProperties(lawyerUserDto, user);
//            user.setLogoutFlag(0);
//            user.setStatus(1);
//            user.setDeleteFlag(0);
//            user.setIsOnline(1);
//            user.setIsRecommended(0);
//            user.setOrderReceivingStatus(0);
//            lawyerUserMapper.insert(user);
//            lawyerUserDto.getProblemScenarioIds().forEach(item -> {
//                LawyerServiceArea lawyerServiceArea = new LawyerServiceArea();
//                lawyerServiceArea.setLawyerUserId(user.getId());
//                lawyerServiceArea.setProblemScenarioId(item);
//                lawyerServiceArea.setCreatedTime(new Date());
//                lawyerServiceAreaMapper.insert(lawyerServiceArea);
//            });
//            LambdaQueryWrapper<LawyerUser> lawFirmLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            lawFirmLambdaQueryWrapper.eq(LawyerUser::getFirmId, lawyerUserDto.getFirmId());
//            List<LawyerUser> lawyerUsers = lawyerUserMapper.selectList(lawFirmLambdaQueryWrapper);
//            if (ObjectUtils.isNotEmpty(lawyerUsers)) {
//                String lawFirmIds = lawyerUsers.stream().map(u -> String.valueOf(u.getFirmId())).filter(Objects::nonNull).distinct().collect(Collectors.joining(","));
//                LawFirm lawFirm = new LawFirm();
//                lawFirm.setId(lawyerUserDto.getFirmId());
//                lawFirmMapper.updateById(lawFirm);
//            }
//            user.setPassType(0);
//            return createToKen(user);
//        }
//    }
//
//    @Override
//    public R checkMsgCode(LawyerUserDto lawyerUserDto) {
//        String string = baseRedisService.getString("lawyer_" + lawyerUserDto.getPhone());
//        if (StringUtils.isNotEmpty(string)) {
//            if (lawyerUserDto.getMsgCode().equals(string)) {
//                LawyerUser lawyerUser = lawyerUserMapper.selectOne(new LambdaQueryWrapper<LawyerUser>()
//                        .eq(LawyerUser::getPhone, lawyerUserDto.getPhone()));
//                return R.data(lawyerUser);
//            }else {
//                return R.fail("验证码错误，请重新输入");
//            }
//        }else {
//            return R.fail("验证码已过期");
//        }
//    }
//
//}
