package com.zhongshu.card.server.core.service.statistics;

import com.github.microservice.net.ResultContent;
import com.zhongshu.card.client.model.org.UserCountSearchParam;
import com.zhongshu.card.client.model.userStatistics.PlatformUserStatisModel;
import com.zhongshu.card.client.type.Sex;
import com.zhongshu.card.client.utils.DateUtils;
import com.zhongshu.card.server.core.dao.org.UserCountDao;
import com.zhongshu.card.server.core.service.base.SuperService;
import com.zhongshu.card.server.core.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;

/**
 * 用户统计服务
 *
 * @author TRX
 * @date 2024/12/11
 */
@Slf4j
@Service
public class UserStatisticsService extends SuperService {

    @Autowired
    private UserCountDao userCountDao;

    /**
     * 统计平台用户返回
     *
     * @return
     */
    public ResultContent<PlatformUserStatisModel> statisticsPlatformUserInfo() {
        return ResultContent.buildSuccess(statisticsPlatformUser());
    }

    /**
     * 统计平台的用户信息
     *
     * @return
     */
    public PlatformUserStatisModel statisticsPlatformUser() {
        PlatformUserStatisModel model = new PlatformUserStatisModel();
        long total = userCountDao.countByIsDelete(Boolean.FALSE);
        model.setTotal(total);

        // 男士
        long manTotal = userCountDao.countByIsDeleteAndSex(Boolean.FALSE, Sex.Male);
        model.setManTotal(manTotal);
        model.setManScale(CommonUtil.countScale(manTotal, total));

        // 女士
        long womanTotal = userCountDao.countByIsDeleteAndSex(Boolean.FALSE, Sex.Female);
        model.setWomanTotal(womanTotal);
        model.setWomanScale(CommonUtil.countScale(womanTotal, total));

        if (total > 0) {
            model.setUnSetTotal(total - womanTotal - manTotal);
        }

        // 昨天
        UserCountSearchParam searchParam = new UserCountSearchParam();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -1);
        Long yStart = DateUtils.getDayStartTime(c.getTimeInMillis());
        Long yEnd = DateUtils.getDayEndTime(c.getTimeInMillis());
        searchParam.setStartTime(yStart);
        searchParam.setEndTime(yEnd);
        long yNumber = userCountDao.countNumber(searchParam);
        // 今天
        Long start = DateUtils.getDayStartTime(System.currentTimeMillis());
        Long end = DateUtils.getDayEndTime(System.currentTimeMillis());
        searchParam.setStartTime(start);
        searchParam.setEndTime(end);
        long number = userCountDao.countNumber(searchParam);
        // 较昨日
        model.setChainDay(CommonUtil.chainScale(number, yNumber));

        return model;
    }


}
