package org.springblade.modules.quartz.task;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.extern.slf4j.Slf4j;
import org.springblade.common.base.BaseEntity;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.modules.distribution.dto.AppointDTO;
import org.springblade.modules.distribution.entity.Appoint;
import org.springblade.modules.distribution.service.IAppointService;
import org.springblade.modules.finance.entity.AppointGoods;
import org.springblade.modules.finance.service.IAppointGoodsService;
import org.springblade.modules.groupon.entity.Groupon;
import org.springblade.modules.groupon.service.IGrouponService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 定时任务调度测试
 * 
 * @author ruoyi
 */
@Slf4j
@Component
public class AppointTask{

    @Lazy
    @Resource
    private IAppointService appointService;

    @Lazy
    @Resource
    private IAppointGoodsService appointGoodsService;

    @Lazy
    @Resource
    private IGrouponService grouponService;

    @Transactional
    public void createAppoint(){
        try {
            List<AppointDTO> appointDTOS = appointService.countAppointList();
            log.info("生成履约单定时任务执行中，appointDTOS size:[{}]", appointDTOS.size());
            if(appointDTOS.size() > 0){
                Map<Long, List<AppointDTO>> map = appointDTOS.stream().collect(Collectors.groupingBy(Appoint::getGrouponId));//根据团进行分组
                List<Appoint> appoints = new ArrayList<>();
                List<AppointGoods> appointGoodsList = new ArrayList<>();
                List<Long> grouponIds = new ArrayList<>();
                map.forEach((k,v) ->{
                    grouponIds.add(k);
                    Map<Long, List<AppointDTO>> collect = v.stream().collect(Collectors.groupingBy(Appoint::getTakeId));//根据自提点进行分组
                    collect.forEach((k1,v1) -> {
                        Map<Long, List<AppointDTO>> listMap = v1.stream().collect(Collectors.groupingBy(Appoint::getStoreId));//根据供应商进行分组
                        listMap.forEach((k2,v2) -> {
                            AppointDTO appointDTO1 = v2.get(0);
                            Appoint appoint = new Appoint();
                            BeanUtil.copy(appointDTO1, appoint);
                            String stringUpper = RandomUtil.randomStringUpper(6);//生成到货码
                            appoint.setArrivalCode(stringUpper);
                            appoints.add(appoint);
                            for (AppointDTO appointDTO : v2) {
                                AppointGoods appointGoods = new AppointGoods();
                                appointGoods.setGoodsId(appointDTO.getGoodsId());
                                appointGoods.setGoodsNum(appointDTO.getGoodsNum());
                                appointGoods.setGoodsName(appointDTO.getGoodsName());
                                appointGoods.setGoodsImage(appointDTO.getGoodsImage());
                                appointGoods.setArrivalCode(stringUpper);
                                appointGoodsList.add(appointGoods);
                            }
                        });
                    });
                });
                grouponService.update(Wrappers.lambdaUpdate(Groupon.class).in(BaseEntity::getId, grouponIds).set(Groupon::getIsAppoint, 1));
                appointService.saveBatch(appoints);
                appointGoodsService.saveBatch(appointGoodsList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("生成履约单异常_" + e.getMessage(), e);
            throw e;
        }
    }
}
