detail_ops.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace refill;
  3. use Log;
  4. class detail_ops
  5. {
  6. public function __construct()
  7. {
  8. }
  9. public function correct_state($partition,$start,$last)
  10. {
  11. $detail_gen = function ($part, $start,$last)
  12. {
  13. while (true)
  14. {
  15. $end = $start + 1000;
  16. $cond = ['detail_id&detail_id' => ['_multi' => true, ['egt', $start], ['lt', $end]]];
  17. $start = 0;
  18. $items = Model()->table('refill_detail')
  19. ->partition($part)
  20. ->field('detail_id,mchid,mch_order,order_state')
  21. ->where($cond)
  22. ->order('detail_id asc')->limit("{$start},1000")->select();
  23. if(empty($items)) break;
  24. $last_item = end($items);
  25. $start = intval($last_item['detail_id']);
  26. foreach ($items as $item)
  27. {
  28. if ($item['order_state'] == ORDER_STATE_QUEUE) {
  29. yield $item;
  30. }
  31. }
  32. if($start > $last) {
  33. break;
  34. }
  35. }
  36. };
  37. $state_check = function ($partition,$mchid,$mch_order)
  38. {
  39. $mod_refill = Model();
  40. $cond_complete = ['mchid' => $mchid, 'mch_order' => $mch_order, 'inner_status' => 0];
  41. $cond_sending = ['mchid' => $mchid, 'mch_order' => $mch_order];
  42. $item = $mod_refill->table('refill_order')->partition($partition)->where($cond_complete)->find();
  43. if(!empty($item))
  44. {
  45. if($item['mch_notify_state'] != 0) {
  46. return ORDER_STATE_HANDLED;
  47. }
  48. else {
  49. return ORDER_STATE_SEND;
  50. }
  51. }
  52. else
  53. {
  54. $items = $mod_refill->table('refill_order')->partition($partition)->where($cond_sending)->select();
  55. if(count($items) > 0) {
  56. return ORDER_STATE_SEND;
  57. }
  58. else {
  59. return -1;
  60. }
  61. }
  62. };
  63. $modify_state = function ($partition,$detail_id,$state)
  64. {
  65. if($state > 0 && $detail_id > 0) {
  66. Model()->table('refill_detail')->where(['detail_id' => $detail_id])->update(['order_state' => $state]);
  67. }
  68. };
  69. $details = $detail_gen($partition,$start,$last);
  70. foreach ($details as $item)
  71. {
  72. $mchid = $item['mchid'];
  73. $mch_order = $item['mch_order'];
  74. $detail_id = $item['detail_id'];
  75. $state = $state_check($partition,$mchid,$mch_order);
  76. Log::record("detail_id={$detail_id},mchid={$mchid},mch_order={$mch_order} order_state={$state}",Log::DEBUG);
  77. if($state > -1) {
  78. $modify_state($partition, $detail_id, $state);
  79. }
  80. if($state == ORDER_STATE_SEND) {
  81. Log::record("SENDING: detail_id={$detail_id},mchid={$mchid},mch_order={$mch_order} order_state={$state}",Log::DEBUG);
  82. }
  83. }
  84. }
  85. }