AccountOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <?php
  2. /**
  3. * AccountOrder.php UTF-8
  4. *
  5. *
  6. * @date : 2018/6/13 16:01
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : luowei <lw@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huoAccountDeal\controller;
  13. use huo\controller\common\Base;
  14. use huo\controller\member\MemCache;
  15. use huo\model\member\MemGameModel;
  16. use huoAccountDeal\logic\AccountLogic;
  17. use huoAccountDeal\logic\AccountOrderLogic;
  18. use huoAccountDeal\model\AccountGoodsModel;
  19. use huoAccountDeal\model\AccountOrderModel;
  20. use huolib\constant\AccountConst;
  21. use huolib\constant\CommonConst;
  22. use huolib\constant\OrderConst;
  23. use huolib\constant\WalletConst;
  24. use huolib\status\AccountStatus;
  25. use huolib\status\CommonStatus;
  26. use huolib\status\MemberStatus;
  27. use huolib\status\OrderStatus;
  28. use huolib\tool\StrUtils;
  29. use think\Log;
  30. class AccountOrder extends Base {
  31. protected function retSucMsg($code, $data = []) {
  32. $_msg = OrderStatus::getMsg($code);
  33. return $this->huoSuccess($code, $_msg, $data);
  34. }
  35. protected function retErrMsg($code) {
  36. $_err_msg = OrderStatus::getMsg($code);
  37. return $this->huoError($code, $_err_msg);
  38. }
  39. /**
  40. * 预下单
  41. *
  42. * @param $mem_id
  43. * @param $ags_id
  44. * @param $payway
  45. *
  46. * @return array|bool|string
  47. */
  48. public function preorder($mem_id, $ags_id) {
  49. if (empty($mem_id)) {
  50. $_code = MemberStatus::LOGIN_IS_OUT;
  51. return $this->huoError($_code, MemberStatus::getMsg($_code));
  52. }
  53. $_mc_class = MemCache::ins();
  54. $_mem_data = $_mc_class->getInfoById($mem_id);
  55. $_me_data = $_mc_class->getMeInfoById($mem_id);
  56. if (empty($_mem_data) || empty($_me_data)) {
  57. $_code = MemberStatus::UID_INVALID;
  58. return $this->huoError($_code, MemberStatus::getMsg($_code));
  59. }
  60. //小号商品验证
  61. $_account_goods = (new AccountLogic())->getDetail($ags_id);
  62. if (empty($_account_goods)) {
  63. $_code = AccountStatus::INVALID_ACCOUNT_GOODS;
  64. return $this->huoError($_code, AccountStatus::getMsg($_code));
  65. }
  66. if ($mem_id == $_account_goods['mem_id']) {
  67. $_code = AccountStatus::CANNOT_BUY_YOUR_ACCOUNT_GOODS;
  68. return $this->huoError($_code, AccountStatus::getMsg($_code));
  69. }
  70. if (AccountConst::STATUS_PULL_ON_SHELVES != $_account_goods['status']) {
  71. $_code = AccountStatus::CANNOT_BUY_NOT_PULL_ON_SHELVES;
  72. return $this->huoError($_code, AccountStatus::getMsg($_code));
  73. }
  74. if (CommonConst::CONST_NOT_DELETE != $_account_goods['is_delete']) {
  75. $_code = AccountStatus::ACCOUNT_HAS_BEEN_DELETED;
  76. return $this->huoError($_code, AccountStatus::getMsg($_code));
  77. }
  78. // TODO: luowei 2018/6/13 支付方式验证
  79. $_order_id = StrUtils::genOrderId(
  80. $_mem_data['agent_id'], $_mem_data['agent_id'], $mem_id,
  81. WalletConst::WALLET_ORDER_PREFIX_AG
  82. );
  83. $_data = [
  84. 'order_id' => $_order_id,
  85. 'sell_mem_id' => $_account_goods['mem_id'],
  86. 'buy_mem_id' => $mem_id,
  87. 'ags_id' => $_account_goods['id'],
  88. 'app_id' => $_account_goods['app_id'],
  89. 'mg_mem_id' => $_account_goods['mg_mem_id'],
  90. 'price' => $_account_goods['price'],
  91. 'real_price' => $_account_goods['price'],
  92. 'status' => OrderConst::PAY_STATUS_NOT,
  93. 'payway' => '',
  94. ];
  95. $_rs = (new AccountLogic())->addPreorderData($_data);
  96. if ($_rs) {
  97. $_rdata = [
  98. 'order_id' => $_order_id,
  99. 'real_amount' => $_account_goods['price'],
  100. 'status' => OrderConst::PAY_STATUS_NOT,
  101. ];
  102. $_code = CommonStatus::NO_ERROR;
  103. return $this->huoError($_code, CommonStatus::getMsg($_code), $_rdata);
  104. } else {
  105. $_code = CommonStatus::INNER_ERROR;
  106. return $this->huoError($_code, CommonStatus::getMsg($_code));
  107. }
  108. }
  109. /**
  110. * @param $order_id
  111. *
  112. * @return bool
  113. */
  114. public function getStatus($order_id = '') {
  115. if (empty($order_id)) {
  116. return false;
  117. }
  118. $_ao_model = new AccountOrderModel();
  119. $_rs = $_ao_model->getStatus($order_id);
  120. if (false == $_rs) {
  121. return false;
  122. }
  123. return $_rs;
  124. }
  125. /**
  126. *
  127. * @return array
  128. */
  129. public function getPayWays() {
  130. // TODO: wuyonghong 2018/4/28 从数据库读取
  131. /* 获取支付方式 */
  132. $_rdata[0]['payway'] = 'alipay';
  133. $_rdata[0]['name'] = '支付宝';
  134. $_rdata[0]['icon'] = '';
  135. $_rdata[0]['url'] = STATICSITE.'/h5sdk/images/arwec.png';
  136. $_rdata[1]['payway'] = 'wxpay';
  137. $_rdata[1]['name'] = '微信';
  138. $_rdata[1]['icon'] = STATICSITE.'/h5sdk/images/wergcder.png';
  139. $_rdata[1]['url'] = '支付宝';
  140. $_code = CommonStatus::NO_ERROR;
  141. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_rdata);
  142. }
  143. /**
  144. * 查询微信订单
  145. *
  146. * @param $order_id
  147. */
  148. public function queryWxOrder($order_id) {
  149. // TODO: wuyonghong 2018/5/7 查询订单完成情况
  150. }
  151. public function getOrderList($_map = [], $page = '1,10') {
  152. $_order_data = (new AccountOrderLogic())->getOrderList($_map, $page);
  153. $_code = CommonStatus::NO_ERROR;
  154. return $this->huoSuccess($_code, CommonStatus::getMsg($_code), $_order_data);
  155. }
  156. /**
  157. * 取消订单
  158. *
  159. * @param $mem_id
  160. * @param $order_id
  161. *
  162. * @param int $is_sell
  163. *
  164. * @return array
  165. */
  166. public function cancel($mem_id, $order_id, $is_sell = 0) {
  167. //验证订单
  168. $_account_order = (new AccountOrderModel())->getInfoByOrderId($order_id);
  169. if (empty($_account_order)) {
  170. $_code = AccountStatus::ORDER_NOT_EXISTS;
  171. $this->huoError($_code, AccountStatus::getMsg($_code));
  172. }
  173. if ($_account_order['status'] != OrderConst::PAY_STATUS_NOT) {
  174. $_code = AccountStatus::CANNOT_CANCEL_ORDER;
  175. $this->huoError($_code, AccountStatus::getMsg($_code));
  176. }
  177. if ($_account_order['buy_mem_id'] != $mem_id) {
  178. $_code = AccountStatus::ONLY_OPERATE_YOUR_OWN_ORDER;
  179. $this->huoError($_code, AccountStatus::getMsg($_code));
  180. }
  181. $_rs = (new AccountOrderLogic())->cancel($order_id, $_account_order['ags_id']);
  182. if ($_rs) {
  183. $_code = CommonStatus::NO_ERROR;
  184. return $this->huoSuccess($_code, CommonStatus::getMsg($_code));
  185. } else {
  186. $_code = CommonStatus::INNER_ERROR;
  187. return $this->huoError($_code, CommonStatus::getMsg($_code));
  188. }
  189. }
  190. /**
  191. * 支付成功操作
  192. *
  193. * @param $order_id
  194. * @param $trade_no
  195. * @param $amount
  196. * @param $payway
  197. *
  198. * @return bool
  199. */
  200. public function payNotifyByAccount($order_id, $trade_no, $amount, $payway) {
  201. $_notify_data['order_id'] = $order_id;
  202. $_notify_data['trade_no'] = $trade_no;
  203. $_notify_data['amount'] = $amount;
  204. $_notify_data['payway'] = $payway;
  205. if (empty($order_id)) {
  206. Log::write(
  207. "func=".__FUNCTION__."&class=".__CLASS__."&step1&code=".OrderStatus::ORDER_ID_EMPTY.'&notify_data.'
  208. .http_build_query($_notify_data),
  209. LOG::ERROR
  210. );
  211. return false;
  212. }
  213. if (empty($amount)) {
  214. Log::write(
  215. "func=".__FUNCTION__."&class=".__CLASS__."&step2&code=".OrderStatus::ORDER_AMOUNT_IS_ZERO
  216. .'&notify_data.'
  217. .http_build_query($_notify_data),
  218. LOG::ERROR
  219. );
  220. return false;
  221. }
  222. $_ao_model = new AccountOrderModel();
  223. $_order_data = $_ao_model->getDetail($order_id);
  224. if (false == $_order_data) {
  225. Log::write(
  226. "func=".__FUNCTION__."&class=".__CLASS__."&step3&code=".OrderStatus::ORDER_NOT_EXISTS.'&notify_data.'
  227. .http_build_query($_notify_data),
  228. LOG::ERROR
  229. );
  230. return false;
  231. }
  232. $_amount = number_format($amount, 2, '.', '');
  233. $_order_amount = number_format($_order_data['real_price'], 2, '.', '');
  234. if (($_amount < $_order_amount)) {
  235. Log::write(
  236. "func=".__FUNCTION__."&class=".__CLASS__."&step4&code=".OrderStatus::NOTIFY_AMOUNT_ERROR.'&notify_data.'
  237. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  238. LOG::ERROR
  239. );
  240. return false;
  241. }
  242. if (OrderConst::PAY_STATUS_SUC == $_order_data['status']) {
  243. Log::write(
  244. "func=".__FUNCTION__."&class=".__CLASS__."&step5&code=".OrderStatus::ORDER_HAS_PAY.'&notify_data.'
  245. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  246. LOG::LOG
  247. );
  248. // return true;
  249. }
  250. /* 更新订单支付状态 */
  251. $_order_data['status'] = OrderConst::PAY_STATUS_SUC;
  252. $_order_data['trade_no'] = $trade_no;
  253. $_order_data['pay_time'] = time();
  254. $_rs = $_ao_model->updateOrder($_order_data, $_order_data['id']);
  255. if (false == $_rs) {
  256. return false;
  257. }
  258. //修改商品状态
  259. $_ag_data = ['status' => AccountConst::STATUS_SOLD_OUT];
  260. $_ag_model = new AccountGoodsModel();
  261. $_ag_model->updateData($_ag_data, $_order_data['ags_id']);
  262. //转移小号并启用
  263. $_account_goods = $_ag_model->getInfoById($_order_data['ags_id']);
  264. if ($_account_goods) {
  265. $_mg_data['mem_id'] = $_order_data['buy_mem_id'];
  266. $_mg_data['status'] = MemGameModel::STATUS_NORMAL;
  267. $_mg_model = new MemGameModel();
  268. $_rs = $_mg_model->updateData($_mg_data, $_account_goods['mg_mem_id']);
  269. if (false === $_rs) {
  270. Log::write(
  271. "func=".__FUNCTION__."&class=".__CLASS__."&step6&code=".OrderStatus::ORDER_HAS_PAY.'&notify_data.'
  272. .http_build_query($_notify_data).'&order_data='.http_build_query($_order_data),
  273. LOG::ERROR
  274. );
  275. return true;
  276. }
  277. }
  278. return true;
  279. }
  280. }