package com.prolog.eis.service.bindgrid.impl;

import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.dto.bindgrid.BGDGridDto;
import com.prolog.eis.dto.bindgrid.BindGridDispatchDto;
import com.prolog.eis.service.bindgrid.BindGridAlgorithmService;
import com.prolog.eis.service.bindgrid.BindGridDispatchService;
import com.prolog.eis.service.bindgrid.BindGridInitService;
import com.prolog.eis.service.bindgrid.BindGridSaveResultService;
import com.prolog.eis.service.sys.SysParamService;
import org.springframework.stereotype.Service;

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

/**
 * @Version: 1.0
 * @Author: wuhan
 * @Created: 2024/8/1 15:38
 */
@Service
public class BindGridDispatchServiceImpl implements BindGridDispatchService {
    @Resource
    private BindGridInitService bindGridInitService;
    @Resource
    private BindGridAlgorithmService bindGridAlgorithmService;
    @Resource
    private BindGridSaveResultService bindGridSaveResultService;
    @Resource
    private SysParamService sysParamService;

    @Override
    public int dispatch() throws Exception {
        BindGridDispatchDto dispatchDto = bindGridInitService.init();

        long freeGridCount = dispatchDto.getGridList().stream().filter(r -> r.getBindOrderId() == null).count();
        if (dispatchDto.getOrderIdList().size() > freeGridCount) {
            return 0;
        }

        List<BGDGridDto> gridList = bindGridAlgorithmService.bindGrid(dispatchDto);

        if (sysParamService.findVal(SysParamConstants.NEW_BIND_GRID_TASK_SWITCH, "0").equals("0")) {
            System.out.println("新格口绑定调度任务下发开关关闭");
            return gridList.size();
        }

//        bindGridSaveResultService.saveResult(gridList);
        return gridList.size();
    }
}
