package shop.alien.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import shop.alien.entity.store.LawyerUser;
import shop.alien.entity.store.LifeUser;
import shop.alien.entity.store.StoreUser;
import shop.alien.entity.store.vo.RegisteredLawyerUserCountVo;
import shop.alien.entity.store.vo.RegisteredStoreUserCountVo;
import shop.alien.entity.store.vo.RegisteredUserCountVo;
import shop.alien.mapper.LawyerUserMapper;
import shop.alien.mapper.LifeUserMapper;
import shop.alien.mapper.StoreUserMapper;
import shop.alien.store.service.RegisteredUserStatisticsService;

@Slf4j
@Service
@RequiredArgsConstructor
public class RegisteredUserStatisticsServiceImpl implements RegisteredUserStatisticsService {

    private final LifeUserMapper lifeUserMapper;
    private final StoreUserMapper storeUserMapper;
    private final LawyerUserMapper lawyerUserMapper;

    @Override
    public RegisteredUserCountVo countRegisteredUsers() {
        RegisteredUserCountVo vo = new RegisteredUserCountVo();
        vo.setRegisteredUserCount(countRegisteredUserTotal());
        return vo;
    }

    @Override
    public long countRegisteredUserTotal() {
        return toLong(lifeUserMapper.selectCount(new LambdaQueryWrapper<LifeUser>()));
    }

    @Override
    public RegisteredStoreUserCountVo countRegisteredStoreUsers() {
        RegisteredStoreUserCountVo vo = new RegisteredStoreUserCountVo();
        vo.setRegisteredStoreUserCount(countRegisteredStoreUserTotal());
        return vo;
    }

    @Override
    public long countRegisteredStoreUserTotal() {
        long total = toLong(storeUserMapper.selectCount(new LambdaQueryWrapper<StoreUser>()));
        log.info("RegisteredUserStatisticsServiceImpl.countRegisteredStoreUserTotal total={}", total);
        return total;
    }

    @Override
    public RegisteredLawyerUserCountVo countRegisteredLawyerUsers() {
        RegisteredLawyerUserCountVo vo = new RegisteredLawyerUserCountVo();
        vo.setRegisteredLawyerUserCount(countRegisteredLawyerUserTotal());
        return vo;
    }

    @Override
    public long countRegisteredLawyerUserTotal() {
        long total = toLong(lawyerUserMapper.selectCount(new LambdaQueryWrapper<LawyerUser>()));
        log.info("RegisteredUserStatisticsServiceImpl.countRegisteredLawyerUserTotal total={}", total);
        return total;
    }

    private static long toLong(Number count) {
        return count != null ? count.longValue() : 0L;
    }
}
