package com.gree.mall.manager.schedule;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.gree.mall.manager.enums.AdminNoticTypeEnum;
import com.gree.mall.manager.enums.IsYesNoEnum;
import com.gree.mall.manager.enums.NoticeEnum;
import com.gree.mall.manager.enums.NoticeTypeEnum;
import com.gree.mall.manager.enums.engin.EnginPayManageStatusEnum;
import com.gree.mall.manager.plus.entity.AdminUser;
import com.gree.mall.manager.plus.entity.AdminUserWebsitRela;
import com.gree.mall.manager.plus.entity.EnginPayManage;
import com.gree.mall.manager.plus.entity.NoticeRecord;
import com.gree.mall.manager.plus.service.AdminUserService;
import com.gree.mall.manager.plus.service.AdminUserWebsitRelaService;
import com.gree.mall.manager.plus.service.EnginPayManageService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@ConditionalOnProperty(name = "schedule.enable", havingValue = "true", matchIfMissing = true)
@Component
public class EnginPayManageSchedule {

    @Resource
    EnginPayManageService enginPayManageService;
    @Resource
    AdminUserWebsitRelaService adminUserWebsitRelaService;
    @Resource
    AdminUserService adminUserService;

    /**
     * 处理导入辅材配件销售单库存执行
     */
//    @Scheduled(fixedDelay = 60 * 1000)
    @Scheduled(cron = "0 0 0 * * ?")
    @Transactional
    public void task() {
        final List<EnginPayManage> manages = enginPayManageService.lambdaQuery()
                .lt(EnginPayManage::getConfirmTime, DateUtil.offsetDay(DateUtil.date(), -30))
                .in(EnginPayManage::getStatus, EnginPayManageStatusEnum.WAIT_PAY.getKey(), EnginPayManageStatusEnum.PART_PAY.getKey())
                .list();

        // 处理 工程收款单审核后30天未付款完成的，弹窗消息提醒及消息中心提醒；
        if (CollectionUtil.isNotEmpty(manages)) {
            List<AdminUserWebsitRela> adminUserWebsitRelas = adminUserWebsitRelaService.lambdaQuery()
                    .in(AdminUserWebsitRela::getCompanyWechatId, manages.stream()
                            .map(EnginPayManage::getCompanyWechatId)
                            .collect(Collectors.toList()))
                    .in(AdminUserWebsitRela::getAdminWebsitId, manages.stream()
                            .map(EnginPayManage::getWebsitId)
                            .collect(Collectors.toList()))
                    .list();

            final Map<String, List<EnginPayManage>> manageGroupMap = manages.stream().collect(Collectors.groupingBy(v -> v.getCompanyWechatId() + v.getWebsitId()));

            for (AdminUserWebsitRela websitRela : adminUserWebsitRelas) {
                if (manageGroupMap.containsKey(websitRela.getCompanyWechatId() + websitRela.getAdminWebsitId())) {
                    final List<EnginPayManage> manageList = manageGroupMap.get(websitRela.getCompanyWechatId() + websitRela.getAdminWebsitId());
                    for (EnginPayManage manage : manageList) {
                        AdminUser one = adminUserService.getById(websitRela.getAdminUserId());
                        NoticeRecord noticeRecord = new NoticeRecord();
                        noticeRecord.setNoticeId(manage.getId());
                        String format = DateUtil.format(manage.getCreateTime(), "yyyy-MM-dd");
                        noticeRecord.setTitle("工程待收款：" + manage.getProjectNo());
                        noticeRecord.setContent(manage.getProjectName() + " 创建时间：" + format + ", 未完成收款请及时处理!");
                        noticeRecord.setType(NoticeEnum.B.getKey());
                        noticeRecord.setNoticeType(NoticeTypeEnum.SYSTEM.getKey());
                        noticeRecord.setIsNotice(IsYesNoEnum.YES.getKey());
                        noticeRecord.setReadFlag(IsYesNoEnum.NO.getKey());
                        noticeRecord.setAdminUserId(websitRela.getAdminUserId());
                        noticeRecord.setAdminNickName(one.getNickName());
                        noticeRecord.setIssueTime(new Date());
                        noticeRecord.setAdminCompanyId(manage.getCompanyWechatId());
                        noticeRecord.setAdminCompanyName(manage.getCompanyWechatName());
                        noticeRecord.setAdminNoticeType(AdminNoticTypeEnum.ENGIN_PAY_ORDER.getKey());
                        noticeRecord.setOrderId(manage.getId());
                        noticeRecord.insert();
                    }
                }
            }
        }
    }

}
