12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- <?php
- namespace refill;
- use Log;
- class detail_ops
- {
- public function __construct()
- {
- }
- public function correct_state($partition,$start,$last)
- {
- $detail_gen = function ($part, $start,$last)
- {
- while (true)
- {
- $end = $start + 1000;
- $cond = ['detail_id&detail_id' => ['_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);
- }
- }
- }
- }
|