Order.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php namespace Seller\Lib;
  2. use Dever;
  3. use Dever\Helper\Str;
  4. class Order
  5. {
  6. # 新增订单
  7. public function add($info, $goods, $sku, $account, $order, $num = 1)
  8. {
  9. /*
  10. $data = Dever::store()->transaction(array($this, 'addAct'), array($info, $goods, $sku, $account, $order, $num), '下单失败');*/
  11. $data = $this->addAct($info, $goods, $sku, $account, $order, $num);
  12. if ($data) {
  13. //\Dever\Helper\Redis::push('submit_' . $data['seller_id'], $data['id']);
  14. \Dever\Helper\Redis::push('submit_' . DEVER_PROJECT, $data['id']);
  15. Dever::load('info', 'seller')->log($data['seller_id'], $data['id'], $data['price'], 3);
  16. }
  17. return $data;
  18. }
  19. public function addAct($info, $goods, $sku, $account, $order, $num = 1)
  20. {
  21. # 验证规则
  22. $cate = Dever::db('cate', 'goods')->find($goods['cate_id']);
  23. if ($cate && $cate['rule'] && !preg_match($cate['rule'], $account)) {
  24. Dever::error($account . '不符合规则');
  25. }
  26. # 验证订单号
  27. /*
  28. $check = Dever::db('order', 'seller')->find(array('seller_id' => $info['id'], 'seller_order_num' => $order));
  29. if ($check) {
  30. Dever::error('订单重复');
  31. }*/
  32. $cash = $this->cash($info['id'], $sku['info_id'], $sku['id'], $sku['value'], $num);
  33. # 开始下单
  34. $data = array();
  35. $data['status'] = 1;
  36. $data['finish'] = 2;
  37. $data['account'] = $account;
  38. $data['cate_id'] = $goods['cate_id'];
  39. $data['goods_id'] = $sku['info_id'];
  40. $data['sku_id'] = $sku['id'];
  41. $data['num'] = $num;
  42. $data['cash'] = $sku['value'];
  43. $data['price'] = $cash;
  44. $data['seller_id'] = $info['id'];
  45. $data['seller_order_num'] = $order;
  46. $data['seller_request'] = json_encode(Dever::input(), JSON_UNESCAPED_UNICODE);
  47. $data['id'] = Dever::db('order', 'seller')->insert($data);
  48. if (!$data['id']) {
  49. Dever::error('下单失败');
  50. }
  51. return $data;
  52. }
  53. public function handle($info, $selected = array())
  54. {
  55. //$data = Dever::store()->transaction(array($this, 'handleAct'), array($info, $selected), '下单失败');
  56. $data = $this->handleAct($info, $selected);
  57. $order = Dever::db('order', 'seller')->find($info['id']);
  58. if ($info['status'] < 10 && $order['status'] >= 10) {
  59. if ($order['status'] == 11) {
  60. # 失败,余额加回来
  61. Dever::load('info', 'seller')->inc($order['seller_id'], $order['price']);
  62. Dever::load('info', 'seller')->log($order['seller_id'], $order['id'], $order['price'], 4);
  63. }
  64. Dever::db('order_log1', 'seller')->insert($order);
  65. }
  66. return $data;
  67. }
  68. public function handleAct($info, $selected = array())
  69. {
  70. if ($info['status'] >= 10) {
  71. return;
  72. }
  73. $update['status'] = 2;
  74. if (!$info['order_num']) {
  75. $info['order_num'] = $update['order_num'] = $this->createOrder();
  76. }
  77. Dever::db('order', 'seller')->update($info['id'], $update);
  78. $channel = $this->channel($info['seller_id'], $info['goods_id'], $info['sku_id']);
  79. if (!$channel) {
  80. return $this->notify($info, '通道未开启', $update);
  81. }
  82. $result = array();
  83. if ($channel['type'] == 1) {
  84. # 通信
  85. # 向渠道发起请求 下单
  86. $param['order_id'] = $info['id'];
  87. $param['order_num'] = $info['order_num'];
  88. $param['account'] = $info['account'];
  89. $param['cash'] = $info['cash'];
  90. $param['num'] = $info['num'];
  91. if ($info['other']) {
  92. $info['other'] = Dever::json_decode($info['other']);
  93. $param = array_merge($info['other'], $param);
  94. }
  95. if (isset($channel['goods']['code']) && $channel['goods']['code']) {
  96. $param['code'] = $channel['goods']['code'];
  97. } else {
  98. $sku = Dever::db('info_sku', 'goods')->find($info['sku_id']);
  99. $param['code'] = $sku['code'];
  100. }
  101. $result = Dever::load('func/api', 'connect')->run(1, $info['cate_id'], $channel, $param);
  102. } elseif ($channel['type'] == 2) {
  103. # 需要审核
  104. # 获取卡密并占用
  105. $param['seller_id'] = array('in', '-1,' . $info['seller_id']);
  106. $param['channel_id'] = $channel['id'];
  107. $param['goods_id'] = $info['goods_id'];
  108. $param['sku_id'] = $info['sku_id'];
  109. $param['status'] = 1;
  110. $param['use_status'] = 1;
  111. $card = Dever::db('card', 'channel')->select($param, array('order' => 'seller_id desc','limit' => '0, ' . $info['num']), true);
  112. $total = count($card);
  113. if ($total < $info['num']) {
  114. return $this->notify($info, '卡密剩余数量不足', $update);
  115. }
  116. foreach ($card as $k => $v) {
  117. Dever::db('card', 'channel')->update($v['id'], array('use_status' => 3, 'order_id' => $info['id']));
  118. }
  119. $result['status'] = 1;
  120. } elseif ($channel['type'] == 11) {
  121. $result['status'] = 1;
  122. }
  123. $update = array();
  124. $update['channel_id'] = $channel['id'];
  125. $update['channel_order_date'] = time();
  126. if (isset($result['request'])) {
  127. $update['channel_request'] = json_encode($result['request'], JSON_UNESCAPED_UNICODE);
  128. }
  129. if (isset($result['response'])) {
  130. $update['channel_response'] = json_encode($result['response'], JSON_UNESCAPED_UNICODE);
  131. }
  132. if (isset($channel['goods']['id'])) {
  133. $update['channel_goods_id'] = $channel['goods']['id'];
  134. }
  135. $update['channel_goods_discount'] = $channel['discount'];
  136. if (isset($channel['goods']['discount']) && $channel['goods']['discount']) {
  137. $update['channel_goods_discount'] = $channel['goods']['discount'];
  138. }
  139. if (!$update['channel_goods_discount']) {
  140. $update['channel_goods_discount'] = 1;
  141. }
  142. $update['buy_price'] = round($info['cash'] * $update['channel_goods_discount'], 2) * $info['num'];
  143. if ($result['status'] == 1) {
  144. # 下单成功
  145. Dever::db('order', 'seller')->update($info['id'], $update);
  146. return 'ok';
  147. } else {
  148. # 下单失败
  149. return $this->notify($info, '下单失败', $update);
  150. }
  151. }
  152. # 向商户发起回调
  153. public function notify($info, $msg, $update = array(), $total = 5)
  154. {
  155. $seller = Dever::db('info', 'seller')->find($info['seller_id']);
  156. $status = 11;
  157. if ($msg == 'ok') {
  158. $status = 10;
  159. }
  160. # 获取商户回调地址,向商户发起回调
  161. $seller_request = Dever::json_decode($info['seller_request']);
  162. if (isset($seller_request['notify']) && $seller_request['notify']) {
  163. $notify = $seller_request['notify'];
  164. } else {
  165. $notify = $seller['notify'];
  166. }
  167. if ($notify) {
  168. if ($info['seller_callback'] != 'ok' && $info['seller_callback_num'] <= $total) {
  169. $param = array();
  170. $param['appkey'] = $seller['appkey'];
  171. $param['order_num'] = $info['seller_order_num'];
  172. $param['system_order_num'] = $info['order_num'];
  173. $param['order_status'] = $status;
  174. $param['order_cash'] = $info['cash'];
  175. $param['official_order_num'] = $param['official_msg'] = '';
  176. if (isset($info['official_order_num'])) {
  177. $param['official_order_num'] = $info['official_order_num'];
  178. }
  179. if (isset($info['official_msg'])) {
  180. $param['official_msg'] = $info['official_msg'];
  181. }
  182. if (isset($update['official_order_num'])) {
  183. $param['official_order_num'] = $update['official_order_num'];
  184. }
  185. if (isset($update['official_msg'])) {
  186. $param['official_msg'] = $update['official_msg'];
  187. }
  188. $param = \Dever\Helper\Secure::get($param, $seller['appsecret']);
  189. $notify = urldecode($notify);
  190. $response = Dever::curl($notify, $param, 'post')->result();
  191. # ok是成功
  192. $update['seller_callback'] = $response;
  193. $update['seller_callback_date'] = time();
  194. $update['seller_callback_num'] = $info['seller_callback_num'] + 1;
  195. }
  196. } else {
  197. $update['seller_callback'] = 'ok';
  198. $update['seller_callback_date'] = time();
  199. $update['seller_callback_num'] = $info['seller_callback_num'] + 1;
  200. }
  201. $id = $info['id'];
  202. if ($info['status'] < 10 && $status >= 10) {
  203. # 订单完成
  204. $info['status'] = $update['status'] = $status;
  205. $update['finish'] = 1;
  206. $update['finish_date'] = time();
  207. $info = array_merge($info, $update);
  208. Dever::load('order', 'seller')->finish($info);
  209. }
  210. if (!$info['order_num']) {
  211. $update['order_num'] = $this->createOrder();
  212. }
  213. Dever::db('order', 'seller')->update($id, $update);
  214. return $msg;
  215. }
  216. public function finish($info)
  217. {
  218. if ($info['status'] == 10) {
  219. if ($info['channel_id']) {
  220. $channel = Dever::db('info', 'channel')->find($info['channel_id']);
  221. if (!$channel) {
  222. Dever::error('未分配渠道');
  223. }
  224. if ($channel['type'] == 2) {
  225. # 审核通过
  226. $param['order_id'] = $info['id'];
  227. //$param['status'] = 1;
  228. $param['use_status'] = 3;
  229. Dever::db('card', 'channel')->update($param, array('use_status' => 2));
  230. }
  231. }
  232. }
  233. if ($info['status'] == 11) {
  234. if ($info['channel_id']) {
  235. $channel = Dever::db('info', 'channel')->find($info['channel_id']);
  236. if (!$channel) {
  237. Dever::error('未分配渠道');
  238. }
  239. if ($channel['type'] == 2) {
  240. # 审核失败
  241. $param['order_id'] = $info['id'];
  242. //$param['status'] = 1;
  243. $param['use_status'] = 3;
  244. Dever::db('card', 'channel')->update($param, array('use_status' => 1));
  245. }
  246. }
  247. # 失败,余额加回来
  248. Dever::load('info', 'seller')->inc($info['seller_id'], $info['price']);
  249. Dever::load('info', 'seller')->log($info['seller_id'], $info['id'], $info['price'], 4);
  250. }
  251. /*
  252. $order_log = Dever::db('order_log', 'seller')->find(array('order_num' => $info['order_num']));
  253. if (!$order_log) {
  254. Dever::db('order_log', 'seller')->insert($info);
  255. }*/
  256. $order_log = Dever::db('order_log1', 'seller')->find(array('order_num' => $info['order_num']));
  257. if (!$order_log) {
  258. Dever::db('order_log1', 'seller')->insert($info);
  259. }
  260. }
  261. # 扣费
  262. public function cash($seller_id, $goods_id, $sku_id, $value, $num = 1)
  263. {
  264. if (!$value || $value <= 0) {
  265. Dever::error('面值无效');
  266. }
  267. # 查询折扣 扣费
  268. $seller_goods = Dever::db('goods', 'seller')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => $sku_id));
  269. if (!$seller_goods) {
  270. $seller_goods = Dever::db('goods', 'seller')->find(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'sku_id' => -1));
  271. }
  272. $info = Dever::db('info', 'seller')->find($seller_id);
  273. if ($seller_goods && $seller_goods['discount']) {
  274. $info['discount'] = $seller_goods['discount'];
  275. }
  276. if (!$info['discount']) {
  277. $info['discount'] = 1;
  278. }
  279. $cash = round($value * $info['discount'], 2) * $num;
  280. # 查询余额是否充足
  281. $info['yue'] = $info['credit'] + $info['cash'];
  282. if ($info['yue'] < $cash) {
  283. Dever::error('余额不足');
  284. }
  285. $state = Dever::load('info', 'seller')->dec($info['id'], $cash);
  286. if (!$state) {
  287. Dever::error('余额不足');
  288. }
  289. return $cash;
  290. }
  291. # 获取渠道
  292. public function channel($seller_id, $goods_id, $sku_id, $selected = array())
  293. {
  294. # 查找渠道
  295. $channel_list = Dever::db('channel', 'seller')->select(array('seller_id' => $seller_id, 'goods_id' => $goods_id, 'status' => 1));
  296. if (!$channel_list) {
  297. return false;
  298. }
  299. $max = 1;
  300. $channel = $goods = array();
  301. foreach ($channel_list as $k => $v) {
  302. if ($selected && isset($selected[$v['channel_id']])) {
  303. continue;
  304. }
  305. if (!$v['sku_id']) {
  306. $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
  307. if ($goods) {
  308. $channel = $v['channel_id'];
  309. $max = $v['max'];
  310. break;
  311. } else {
  312. $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => -1));
  313. if ($goods) {
  314. $channel = $v['channel_id'];
  315. $max = $v['max'];
  316. break;
  317. }
  318. }
  319. } else {
  320. $all_sku_id = explode(',', $v['sku_id']);
  321. if (in_array($sku_id, $all_sku_id)) {
  322. $channel = $v['channel_id'];
  323. $max = $v['max'];
  324. $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => $sku_id));
  325. if (!$goods) {
  326. $goods = Dever::db('goods', 'channel')->find(array('channel_id' => $v['channel_id'], 'goods_id' => $goods_id, 'sku_id' => -1));
  327. }
  328. break;
  329. }
  330. }
  331. }
  332. if (!$channel) {
  333. return false;
  334. }
  335. $channel = Dever::db('info', 'channel')->find($channel);
  336. if (!$channel) {
  337. return false;
  338. }
  339. if ($channel['status'] == 2) {
  340. return false;
  341. }
  342. $channel['goods'] = $goods;
  343. $channel['max'] = $max;
  344. return $channel;
  345. }
  346. public function createOrder()
  347. {
  348. $where['order_num'] = Str::order('C');
  349. $state = Dever::db('order', 'seller')->find($where);
  350. if (!$state) {
  351. return $where['order_num'];
  352. } else {
  353. return $this->createOrder();
  354. }
  355. }
  356. }