third_pcode.php 5.3 KB

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