package com.cf.imes.module.executor.service.order.intervalometer;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.cf.imes.framework.organ.core.aop.OrganIgnore;
import com.cf.imes.module.executor.dal.mysql.goods.GoodsMapper;
import com.cf.imes.module.executor.dal.mysql.order.OrderMapper;
import com.cf.imes.module.executor.dal.mysql.orderBody.OrderBodyMapper;
import com.cf.imes.module.executor.dal.mysql.orderGroup.OrderGroupMapper;
import com.cf.imes.module.executor.dal.mysql.orderItem.OrderItemMapper;
import com.cf.imes.module.executor.dal.mysql.orderParts.OrderPartsMapper;
import com.cf.imes.module.executor.dal.mysql.plate.PlateMapper;
import com.cf.imes.module.executor.dal.mysql.rawgoods.RawGoodsMapper;
import com.cf.imes.module.executor.service.order.OrderInputProcessor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;

import static com.cf.imes.module.executor.enums.EsIndexEnum.ORDER_PARTS_REMARK_MODEL;
import static com.cf.imes.module.executor.enums.EsIndexEnum.ORDER_PLATE_MODEL;


/**
 * 生产单定时处理器
 */
@Slf4j
@Component
@DS("slave")
public class OrderScheduledImpl {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private RawGoodsMapper rawGoodsMapper;

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private OrderPartsMapper orderPartsMapper;

    @Resource
    private PlateMapper plateMapper;

    @Resource
    private OrderBodyMapper orderBodyMapper;

    @Resource
    private OrderGroupMapper orderGroupMapper;

    @Resource
    private OrderItemMapper orderItemMapper;

    @Resource
    private OrderInputProcessor orderInputProcessor;


    //    @Scheduled(cron = "0 0 2 * * ?")
    @OrganIgnore
    @Transactional(rollbackFor = Exception.class)
    public void fixedDeletedOrder() {

        List<Long> orderDOList = orderMapper.selectOrderListByUpdateTime();

        // 作废 生产单表，body，group，goods，parts，plate，raw_goods，item
        rawGoodsMapper.deleteBatch(orderDOList);
        goodsMapper.deleteBatch(orderDOList);
        orderItemMapper.deleteBatch(orderDOList);
        orderBodyMapper.deleteBatch(orderDOList);
        orderGroupMapper.deleteBatch(orderDOList);

        orderPartsMapper.deleteBatch(orderDOList);
        plateMapper.deleteBatch(orderDOList);

        orderMapper.deleteBatchIds(orderDOList); // 生产单删除

//        删除板材的造型数据 es
        orderInputProcessor.deleteByOrderId(orderDOList, ORDER_PLATE_MODEL.getIndex());
//        删除备注信息 es
        orderInputProcessor.deleteByOrderId(orderDOList, ORDER_PARTS_REMARK_MODEL.getIndex());

    }

}
