['_multi' => true, ['egt', $start], ['lt', $end]]]; $start = 0; $items = Model()->table('refill_detail') ->partition($part) ->field('detail_id,mchid,mch_order,order_state') ->where($cond) ->order('detail_id asc')->limit("{$start},1000")->select(); if(empty($items)) break; $last_item = end($items); $start = intval($last_item['detail_id']); foreach ($items as $item) { if ($item['order_state'] == ORDER_STATE_QUEUE) { yield $item; } } if($start > $last) { break; } } }; $state_check = function ($partition,$mchid,$mch_order) { $mod_refill = Model(); $cond_complete = ['mchid' => $mchid, 'mch_order' => $mch_order, 'inner_status' => 0]; $cond_sending = ['mchid' => $mchid, 'mch_order' => $mch_order]; $item = $mod_refill->table('refill_order')->partition($partition)->where($cond_complete)->find(); if(!empty($item)) { if($item['mch_notify_state'] != 0) { return ORDER_STATE_HANDLED; } else { return ORDER_STATE_SEND; } } else { $items = $mod_refill->table('refill_order')->partition($partition)->where($cond_sending)->select(); if(count($items) > 0) { return ORDER_STATE_SEND; } else { return -1; } } }; $modify_state = function ($partition,$detail_id,$state) { if($state > 0 && $detail_id > 0) { Model()->table('refill_detail')->where(['detail_id' => $detail_id])->update(['order_state' => $state]); } }; $details = $detail_gen($partition,$start,$last); foreach ($details as $item) { $mchid = $item['mchid']; $mch_order = $item['mch_order']; $detail_id = $item['detail_id']; $state = $state_check($partition,$mchid,$mch_order); Log::record("detail_id={$detail_id},mchid={$mchid},mch_order={$mch_order} order_state={$state}",Log::DEBUG); if($state > -1) { $modify_state($partition, $detail_id, $state); } if($state == ORDER_STATE_SEND) { Log::record("SENDING: detail_id={$detail_id},mchid={$mchid},mch_order={$mch_order} order_state={$state}",Log::DEBUG); } } } }