123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
- <?php
- namespace refill;
- use think\Model;
- use const mtopcard\ThirdOnlineProduct;
- use const mtopcard\ThirdRefillCard;
- use Exception;
- class third_pcode
- {
- private $product_name;
- private $product_amount;
- private $plat_code;
- private $mch_id;
- private $mch_price;
- private $store_code;
- private $store_id;
- private $store_price;
- /**
- * @throws Exception for node 数据有错误.
- */
- public function __construct($node)
- {
- if(!$this->check($node)) {
- throw new Exception('');
- }
- $this->product_amount = intval($node['product_amount'] ?? 0);
- $this->product_name = $node['product_name'];
- $this->plat_code = $node['plat_code'];
- $this->mch_id = intval($node['mch_id']);
- $this->mch_price = ncPriceFormat($node['mch_price']);
- $this->store_code = $node['store_code'];
- $this->store_id = intval($node['store_id']);
- $this->store_price = ncPriceFormat($node['store_price']);
- }
- private function check($node) : bool
- {
- if(empty($node['product_name'])) return false;
- if(empty($node['plat_code'])) return false;
- if(empty($node['store_code'])) return false;
- if($node['product_amount'] <= 0) return false;
- if($node['mch_id'] <= 0) return false;
- if($node['store_id'] <= 0) return false;
- if(empty($node['mch_price'])) return false;
- if(empty($node['store_price'])) return false;
- return true;
- }
- public function produce()
- {
- [$succ,$msg] = $this->mk_plat_code();
- if(!$succ) {
- return [false,$msg];
- }
- [$succ,$msg] = $this->mk_store_code();
- if(!$succ) {
- return [false,$msg];
- }
- [$succ,$msg] = $this->set_mch_price();
- if(!$succ) {
- return [false,$msg];
- }
- }
- private function mk_plat_code()
- {
- $isExiter = function ($mod_third,$pcode) {
- $item = $mod_third->findThirdProduct($pcode);
- return empty($item);
- };
- $mod_third = Model('refill_third');
- if($isExiter($mod_third,$this->plat_code)) {
- return [false,'已经有这个产品编码'];
- }
- $val = [
- 'system_code' => $this->plat_code,
- 'product_name' => $this->product_name,
- 'refill_amount' => $this->product_amount,
- 'product_type' => ThirdOnlineProduct
- ];
- $ret = $mod_third->addThirdProduct($val);
- return [true,$ret];
- }
- private function mk_store_code()
- {
- $channel_namer = function ($store_id)
- {
- $mod = Model('refill_provider');
- $item = $mod->getProviderInfo(['store_id' => $store_id]);
- if(empty($item)) {
- return false;
- } else {
- return $item['name'];
- }
- };
- $good_finder = function ($chname)
- {
- global $config;
- $cfg_map = $config['third_providers'];
- $goods_id = 0;
- foreach ($cfg_map as $cfg) {
- if($cfg['name'] != $chname) continue;
- $goods_id = $cfg['cfg']['amount'][100][0]['goods_id'];
- }
- return $goods_id;
- };
- $store_product_add = function ($ch_name,$goods_id)
- {
- $mod = Model('refill_third');
- $insert['channel_name'] = $ch_name;
- $insert['store_id'] = $this->store_id;
- $insert['channel_product_name'] = $this->product_name;
- $insert['goods_id'] = $goods_id;
- $insert['channel_code'] = $this->store_code;
- $insert['channel_amount'] = $this->store_price;
- $insert['recharge_type'] = 1; //直冲
- $insert['system_code'] = $this->plat_code;
- $insert['product_type'] = ThirdOnlineProduct;
- $result = $mod->addProviderProduct($insert);
- return $result;
- };
- $ch_name = $channel_namer($this->store_id);
- if(empty($ch_name)) {
- return [false,'无法找到通道名称.'];
- }
- $goods_id = $good_finder($ch_name);
- if($goods_id == 0) {
- return [false,'无法找到通道对应的goods id.'];
- }
- $ret = $store_product_add($ch_name,$goods_id);
- return [true,$ret];
- }
- private function set_mch_price()
- {
- $isExist = function ($mchid,$pcode)
- {
- $item = Model('')->table('merchant_price')
- ->where(['mchid' => $mchid, 'quality' => 1, 'pcode' => $this->plat_code])->find();
- return !empty($item);
- };
- $model_merchant = Model('merchant');
- $val['mchid'] = $this->mch_id;
- $val['spec'] = $this->product_amount;
- $val['price'] = $this->mch_price;
- $val['card_types'] = ThirdRefillCard;
- $val['quality'] = 1;
- $val['pcode'] = $this->plat_code;
- if (!$isExist($this->mch_id, $this->plat_code)) {
- $ret = $model_merchant->table('merchant_price')->insert($val);
- }
- else
- {
- $ret = $model_merchant->table('merchant_price')
- ->where(['mchid' => $this->mch_id, 'quality' => 1, 'pcode' => $this->plat_code])
- ->update($val);
- }
- return [true,$ret];
- }
- }
|