third_pcode.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace refill;
  3. use const mtopcard\ThirdOnlineProduct;
  4. use const mtopcard\ThirdRefillCard;
  5. use Exception;
  6. class third_pcode
  7. {
  8. private $product_name;
  9. private $product_amount;
  10. private $plat_code;
  11. private $mch_id;
  12. private $mch_price;
  13. private $store_code;
  14. private $store_id;
  15. private $store_price;
  16. /**
  17. * @throws Exception for node 数据有错误.
  18. */
  19. public function __construct($node)
  20. {
  21. if(!$this->check($node)) {
  22. throw new Exception('');
  23. }
  24. $this->product_amount = intval($node['product_amount'] ?? 0);
  25. $this->product_name = $node['product_name'];
  26. $this->plat_code = $node['plat_code'];
  27. $this->mch_id = intval($node['mch_id']);
  28. $this->mch_price = ncPriceFormat($node['mch_price']);
  29. $this->store_code = $node['store_code'];
  30. $this->store_id = intval($node['store_id']);
  31. $this->store_price = ncPriceFormat($node['store_price']);
  32. }
  33. private function check($node) : bool
  34. {
  35. if(empty($node['product_name'])) return false;
  36. if(empty($node['plat_code'])) return false;
  37. if(empty($node['store_code'])) return false;
  38. if($node['product_amount'] <= 0) return false;
  39. if($node['mch_id'] <= 0) return false;
  40. if($node['store_id'] <= 0) return false;
  41. if(empty($node['mch_price'])) return false;
  42. if(empty($node['store_price'])) return false;
  43. return true;
  44. }
  45. public function produce()
  46. {
  47. [$succ,$msg] = $this->mk_plat_code();
  48. if(!$succ) {
  49. return [false,$msg];
  50. }
  51. [$succ,$msg] = $this->mk_store_code();
  52. if(!$succ) {
  53. return [false,$msg];
  54. }
  55. [$succ,$msg] = $this->set_mch_price();
  56. if(!$succ) {
  57. return [false,$msg];
  58. }
  59. return [true,''];
  60. }
  61. private function mk_plat_code()
  62. {
  63. $isExiter = function ($mod_third,$pcode) {
  64. $item = $mod_third->findThirdProduct($pcode);
  65. return !empty($item);
  66. };
  67. $val = [
  68. 'system_code' => $this->plat_code,
  69. 'product_name' => $this->product_name,
  70. 'refill_amount' => $this->product_amount,
  71. 'product_type' => ThirdOnlineProduct
  72. ];
  73. $mod_third = Model('refill_third');
  74. if ($isExiter($mod_third, $this->plat_code)) {
  75. $ret = $mod_third->editThirdProduct($this->plat_code, $val);
  76. } else {
  77. $ret = $mod_third->addThirdProduct($val);
  78. }
  79. return [true,$ret];
  80. }
  81. private function mk_store_code()
  82. {
  83. $channel_namer = function ($store_id)
  84. {
  85. $mod = Model('refill_provider');
  86. $item = $mod->getProviderInfo(['store_id' => $store_id]);
  87. if(empty($item)) {
  88. return false;
  89. } else {
  90. return $item['name'];
  91. }
  92. };
  93. $good_finder = function ($chname)
  94. {
  95. global $config;
  96. $cfg_map = $config['third_providers'];
  97. $goods_id = 0;
  98. foreach ($cfg_map as $cfg) {
  99. if($cfg['name'] != $chname) continue;
  100. $goods_id = $cfg['cfg']['amount'][100][0]['goods_id'];
  101. }
  102. return $goods_id;
  103. };
  104. $isExister = function ($store_id,$sys_code,$goods_id) {
  105. $mod = Model('refill_third');
  106. $item = $mod->table('third_proprice')->where(['store_id' => $store_id,'system_code' => $sys_code,'goods_id' => $goods_id])->find();
  107. return !empty($item);
  108. };
  109. $store_product_add = function ($ch_name,$goods_id,$fExist)
  110. {
  111. $val['channel_name'] = $ch_name;
  112. $val['store_id'] = $this->store_id;
  113. $val['channel_product_name'] = $this->product_name;
  114. $val['goods_id'] = $goods_id;
  115. $val['channel_code'] = $this->store_code;
  116. $val['channel_amount'] = $this->store_price;
  117. $val['recharge_type'] = 1; //直冲
  118. $val['system_code'] = $this->plat_code;
  119. $val['product_type'] = ThirdOnlineProduct;
  120. $mod = Model('refill_third');
  121. if($fExist) {
  122. $ret = $mod->edit_provider_product($this->store_id,$this->plat_code,$goods_id,$val);
  123. } else {
  124. $ret = $mod->addProviderProduct($val);
  125. }
  126. return $ret;
  127. };
  128. $ch_name = $channel_namer($this->store_id);
  129. if(empty($ch_name)) {
  130. return [false,'无法找到通道名称.'];
  131. }
  132. $goods_id = $good_finder($ch_name);
  133. if($goods_id == 0) {
  134. return [false,'无法找到通道对应的goods id.'];
  135. }
  136. $fExist = $isExister($this->store_id, $this->plat_code, $goods_id);
  137. $ret = $store_product_add($ch_name,$goods_id,$fExist);
  138. return [true,$ret];
  139. }
  140. private function set_mch_price()
  141. {
  142. $isExist = function ($mchid,$pcode)
  143. {
  144. $item = Model('')->table('merchant_price')
  145. ->where(['mchid' => $mchid, 'quality' => 1, 'pcode' => $pcode])->find();
  146. return !empty($item);
  147. };
  148. $model_merchant = Model('merchant');
  149. $val['mchid'] = $this->mch_id;
  150. $val['spec'] = $this->product_amount;
  151. $val['price'] = $this->mch_price;
  152. $val['card_types'] = ThirdRefillCard;
  153. $val['quality'] = 1;
  154. $val['pcode'] = $this->plat_code;
  155. if (!$isExist($this->mch_id, $this->plat_code)) {
  156. $ret = $model_merchant->table('merchant_price')->insert($val);
  157. }
  158. else
  159. {
  160. $ret = $model_merchant->table('merchant_price')
  161. ->where(['mchid' => $this->mch_id, 'quality' => 1, 'pcode' => $this->plat_code])
  162. ->update($val);
  163. }
  164. return [true,$ret];
  165. }
  166. }