package cn.iocoder.yudao.module.member.job;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.dal.dataobject.business.BusinessDO;
import cn.iocoder.yudao.module.member.dal.dataobject.card.CardDO;
import cn.iocoder.yudao.module.member.dal.dataobject.group.MemberGroupDO;
import cn.iocoder.yudao.module.member.dal.dataobject.groupcount.GroupCountDO;
import cn.iocoder.yudao.module.member.dal.dataobject.storebusiness.StoreBusinessDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.business.BusinessMapper;
import cn.iocoder.yudao.module.member.dal.mysql.card.CardMapper;
import cn.iocoder.yudao.module.member.dal.mysql.group.MemberGroupMapper;
import cn.iocoder.yudao.module.member.dal.mysql.groupcount.GroupCountMapper;
import cn.iocoder.yudao.module.member.dal.mysql.storebusiness.StoreBusinessMapper;
import cn.iocoder.yudao.module.member.enums.CostTypeEnum;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.member.util.MemberTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author:qjq
 * @Date:2024/4/3 14:50
 */
@Slf4j
@Component
public class GroupCountJob implements JobHandler {
    @Resource
    private MemberGroupMapper groupMapper;

    @Resource
    private GroupCountMapper groupCountMapper;

    @Resource
    private CardMapper cardMapper;

    @Resource
    private MemberUserService userService;

    /**
     * 执行任务
     *
     * @param param 参数
     * @return 结果
     * @throws Exception 异常
     */
    @Override
    @TenantIgnore
    public String execute(String param) {
        //查询所有部门分组
        List<MemberGroupDO> memberGroupDOS = groupMapper.selectListByStatus(CommonStatusEnum.ENABLE.getStatus());

        List<GroupCountDO> adds = new ArrayList<>();
        //查询上个月的充值金额
        for (MemberGroupDO memberGroupDO : memberGroupDOS) {
            List<MemberUserDO> memberByGroupId = groupMapper.getByGroupId(memberGroupDO.getId());
            if (CollectionUtil.isEmpty(memberByGroupId)) {
                continue;
            }
            List<Long> collect = memberByGroupId.stream().map(MemberUserDO::getId).collect(Collectors.toList());
            DateTime currentDate = DateUtil.date();
            DateTime lastMonthDate = DateUtil.offsetMonth(currentDate, -1);
            Integer year = MemberTimeUtils.getYear(lastMonthDate);
            Integer month = MemberTimeUtils.getMonth(lastMonthDate);
            BigDecimal money = cardMapper.getTotalMoneyByGroup(collect, year, month);

            GroupCountDO groupCountDO = new GroupCountDO();
            groupCountDO.setGroupId(memberGroupDO.getId());
            groupCountDO.setGroupName(memberGroupDO.getName());
            groupCountDO.setMoney(money);
            String mon =  month<10?"0"+month:month.toString();
            groupCountDO.setTime(year+"-"+mon);
            if(memberGroupDO.getUserId()!=null){
                MemberUserDO user = userService.getUser(memberGroupDO.getUserId());
                if (ObjectUtil.isNotEmpty(user)){
                    groupCountDO.setMobile(user.getMobile());
                }
            }
            adds.add(groupCountDO);
        }
        groupCountMapper.insertBatch(adds);

        log.info("[execute][生成上月统计充值数据 {}条]", adds.size());
        return String.format("生成上月统计充值数据 %s 条   ", adds.size());
    }
}
