JhOrderModel.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\common\model;
  12. use think\facade\Db;
  13. use think\Model;
  14. class JhOrderModel extends Model
  15. {
  16. protected $name = "jh_order";
  17. public function jhBuySubmit($uid, $id){
  18. $model = new JhGoodsModel();
  19. $ginfo = $model->where('id', $id)->find();
  20. $userinfo = Db::name('user')->where('id', $uid)->find();
  21. if (!$ginfo){
  22. sr_throw("参数错误");
  23. }
  24. if ($ginfo['buy_count'] < 1){
  25. sr_throw('已经被人购买了');
  26. }
  27. try {
  28. $ginfo->where('id', $id)->dec('buy_count', 1)->update();
  29. }catch (Exception $e){
  30. sr_throw('购买失败,请刷新列表');
  31. }
  32. if ($ginfo['uid'] == $uid){
  33. sr_throw('不能购买自己的卖单');
  34. }
  35. if ($userinfo['money'] < $ginfo['total_price']){
  36. sr_throw('余额不足');
  37. }
  38. // 判断用户的余额
  39. // $ginfo->save([
  40. // 'status'=>3
  41. // ]);
  42. $time = time();
  43. $order_sn = creatJhOrderSn($uid);
  44. if (!Db::name('tz_log')->where('uid2', $order_sn)->where('type', 2)->find()){
  45. edit_user_tz(2, $uid, $ginfo['jh_count'], $uid, $order_sn);
  46. }
  47. edit_user_money(14, $uid, $ginfo['total_price'], 0, 0, $ginfo['id']);
  48. edit_user_money(19, $ginfo['uid'], $ginfo['total_price'], 0, 0, $ginfo['id']);
  49. $this->insertGetId([
  50. 'jh_ordersn'=>$order_sn,
  51. 'seller_uid'=>$ginfo['uid'],
  52. 'buyer_uid'=>$uid,
  53. 'b_endtime'=>sr_getcurtime($time+env('JHCONF.BUYER_UPLOAD_IMG')),
  54. 'create_time'=>sr_getcurtime($time),
  55. 'jh_count'=>$ginfo['jh_count'],
  56. 'price'=>$ginfo['price'],
  57. 'total_price'=>$ginfo['total_price'],
  58. 'g_id'=>$ginfo['id'],
  59. 'status'=>4
  60. ]);
  61. $ginfo->save([
  62. 'status'=>4
  63. ]);
  64. // sr_throw();
  65. // $order_info->save([
  66. // 'status'=>4,
  67. // 's_time'=>sr_getcurtime($time)
  68. // ]);
  69. // Db::name('jh_goods')->where('id', $order_info['g_id'])->save([
  70. // 'status'=>4
  71. // ]);
  72. }
  73. public function jhSellSubmitDown($uid, $id){
  74. $model = new JhGoodsModel();
  75. $ginfo = $model->where('id', $id)->find();
  76. $userinfo = Db::name('user')->where('id', $uid)->find();
  77. if (!$ginfo){
  78. sr_throw("参数错误");
  79. }
  80. if ($ginfo['buy_count'] < 1){
  81. sr_throw('已经被人卖出去了');
  82. }
  83. try {
  84. $ginfo->where('id', $id)->dec('buy_count', 1)->update();
  85. }catch (Exception $e){
  86. sr_throw('购买失败,请刷新列表');
  87. }
  88. if ($ginfo['uid'] == $uid){
  89. sr_throw('不能处理自己的买单');
  90. }
  91. if ($userinfo['tz_num'] < $ginfo['jh_count']){
  92. sr_throw('通证不足');
  93. }
  94. // 判断用户的余额
  95. // $ginfo->save([
  96. // 'status'=>3
  97. // ]);
  98. $time = time();
  99. $order_sn = creatJhOrderSn($uid);
  100. // if (!Db::name('tz_log')->where('uid2', $order_sn)->where('type', 2)->find()){
  101. //
  102. // }
  103. edit_user_tz(11, $uid, $ginfo['jh_count'], $uid, $order_sn);
  104. edit_user_tz(12, $ginfo['uid'], $ginfo['jh_count'], $uid, $order_sn);
  105. edit_user_money(22, $uid, $ginfo['total_price'], 0, 0, $ginfo['id']);
  106. // edit_user_money(19, $ginfo['uid'], $ginfo['total_price'], 0, 0, $ginfo['id']);
  107. $this->insertGetId([
  108. 'jh_ordersn'=>$order_sn,
  109. 'seller_uid'=>$uid,
  110. 'buyer_uid'=>$ginfo['uid'],
  111. 'b_endtime'=>sr_getcurtime($time+env('JHCONF.BUYER_UPLOAD_IMG')),
  112. 'create_time'=>sr_getcurtime($time),
  113. 'jh_count'=>$ginfo['jh_count'],
  114. 'price'=>$ginfo['price'],
  115. 'total_price'=>$ginfo['total_price'],
  116. 'g_id'=>$ginfo['id'],
  117. 'status'=>4,
  118. 'type'=>2
  119. ]);
  120. $ginfo->save([
  121. 'status'=>4
  122. ]);
  123. // sr_throw();
  124. // $order_info->save([
  125. // 'status'=>4,
  126. // 's_time'=>sr_getcurtime($time)
  127. // ]);
  128. // Db::name('jh_goods')->where('id', $order_info['g_id'])->save([
  129. // 'status'=>4
  130. // ]);
  131. }
  132. public function jhOrderPayWay($uid, $jh_orderid){
  133. $orderinfo = Db::name('jh_order')->where('jh_ordersn', $jh_orderid)->find();
  134. if (!$orderinfo){
  135. sr_throw('参数错误');
  136. }
  137. if ($orderinfo['buyer_uid'] != $uid){
  138. sr_throw('无权查看,不是你的买单');
  139. }
  140. return $pay_info = getUserPayWay($orderinfo['seller_uid']);
  141. }
  142. public function jhOrderList($uid, $roles, $page, $limit){
  143. if (!in_array($roles, ['seller', 'buyer'])){
  144. sr_throw('参数错误');
  145. }
  146. if ($roles == 'seller'){
  147. $where = array();
  148. $where[] = ['o.seller_uid', '=', $uid];
  149. $where[] = ['o.type', '=', 1];
  150. $list = $this->alias('o')
  151. ->leftJoin('user u', 'u.id = o.buyer_uid')
  152. ->where($where)
  153. ->field('o.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  154. ->withAttr('avatar', function ($val, $data){
  155. return getWebUrl().'/'.$val;
  156. })
  157. ->order('o.create_time desc')
  158. ->page($page, $limit)
  159. ->select()->toArray();
  160. return $list;
  161. }
  162. if ($roles == 'buyer'){
  163. $where = array();
  164. $where[] = ['o.buyer_uid', '=', $uid];
  165. $where[] = ['o.type', '=', 1];
  166. $list = $this->alias('o')
  167. ->leftJoin('user u', 'u.id = o.seller_uid')
  168. ->where($where)
  169. ->field('o.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  170. ->withAttr('avatar', function ($val, $data){
  171. return getWebUrl().'/'.$val;
  172. })
  173. ->order('o.create_time desc')
  174. ->page($page, $limit)
  175. ->select()->toArray();
  176. return $list;
  177. }
  178. }
  179. public function jhBuyOrderList($uid, $roles, $page, $limit){
  180. if (!in_array($roles, ['seller', 'buyer'])){
  181. sr_throw('参数错误');
  182. }
  183. if ($roles == 'seller'){
  184. $where = array();
  185. $where[] = ['o.seller_uid', '=', $uid];
  186. $where[] = ['o.type', '=', 2];
  187. $list = $this->alias('o')
  188. ->leftJoin('user u', 'u.id = o.buyer_uid')
  189. ->where($where)
  190. ->field('o.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  191. ->withAttr('avatar', function ($val, $data){
  192. return getWebUrl().'/'.$val;
  193. })
  194. ->order('o.create_time desc')
  195. ->page($page, $limit)
  196. ->select()->toArray();
  197. return $list;
  198. }
  199. if ($roles == 'buyer'){
  200. $where = array();
  201. $where[] = ['o.buyer_uid', '=', $uid];
  202. $where[] = ['o.type', '=', 2];
  203. $list = $this->alias('o')
  204. ->leftJoin('user u', 'u.id = o.seller_uid')
  205. ->where($where)
  206. ->field('o.*,u.nickname,u.mobile,u.avatar,u.level,u.credit')
  207. ->withAttr('avatar', function ($val, $data){
  208. return getWebUrl().'/'.$val;
  209. })
  210. ->order('o.create_time desc')
  211. ->page($page, $limit)
  212. ->select()->toArray();
  213. return $list;
  214. }
  215. }
  216. public function jhOrderSurePay($uid, $para){
  217. $time = time();
  218. $orderid = $para['jh_orderid'];
  219. if (empty($para['img']) || empty($para['pay_way']) || empty($orderid)){
  220. sr_throw('参数错误');
  221. }
  222. $order_info = $this->where('jh_ordersn', $orderid)->find();
  223. if ($order_info['status'] != 1){
  224. sr_throw('状态错误');
  225. }
  226. if (!$order_info){
  227. sr_throw("参数错误");
  228. }
  229. if ($order_info['buyer_uid'] != $uid){
  230. sr_throw('无权操作');
  231. }
  232. if ($order_info['b_endtime'] < sr_getcurtime($time)){
  233. sr_throw('已超时');
  234. }
  235. $order_info->save([
  236. 'status'=>2,
  237. 's_endtime'=>sr_getcurtime($time+env('JHCONF.SERLL_SURE_TIME')),
  238. 'b_time'=>sr_getcurtime($time),
  239. 'buyer_payway'=>$para['pay_way'],
  240. 'buyer_payimg'=>$para['img']
  241. ]);
  242. }
  243. public function jhOrderSellerSureDown($uid, $para){
  244. $time = time();
  245. $orderid = $para['jh_orderid'];
  246. if (empty($orderid)){
  247. sr_throw('参数错误');
  248. }
  249. $order_info = $this->where('jh_ordersn', $orderid)->find();
  250. if ($order_info['status'] != 2){
  251. sr_throw('状态错误');
  252. }
  253. if (!$order_info){
  254. sr_throw("参数错误");
  255. }
  256. if ($order_info['seller_uid'] != $uid){
  257. sr_throw('无权操作');
  258. }
  259. if ($order_info['s_endtime'] < sr_getcurtime($time)){
  260. sr_throw('已超时');
  261. }
  262. $order_info->save([
  263. 'status'=>4,
  264. 's_time'=>sr_getcurtime($time)
  265. ]);
  266. if (!Db::name('tz_log')->where('uid2', $order_info['jh_ordersn'])->where('type', 2)->find()){
  267. edit_user_tz(2, $order_info['buyer_uid'], $order_info['jh_count'], $uid, $order_info['jh_ordersn']);
  268. }
  269. Db::name('jh_goods')->where('id', $order_info['g_id'])->save([
  270. 'status'=>4
  271. ]);
  272. }
  273. public function jhBuyerDiverOrder($uid, $para){
  274. $time = time();
  275. $order_id = $para['jh_orderid'];
  276. if (empty($para['diver_reason'])){
  277. sr_throw('参数错误');
  278. }
  279. $order_info = $this->where('jh_ordersn', $order_id)->find();
  280. if (!$order_info){
  281. sr_throw('参数错误');
  282. }
  283. if ($order_info['buyer_uid']!= $uid){
  284. sr_throw('无权操作');
  285. }
  286. if (!in_array($order_info['status'], [1,2])){
  287. if ($order_info['status'] == 3){
  288. sr_throw('订单正在申诉中,请耐心等待系统处理');
  289. }
  290. if ($order_info['status'] == 4){
  291. sr_throw('订单已经完成或申诉已处理完成');
  292. }
  293. }
  294. $order_info->save([
  295. 'status'=>3,
  296. 'diver_type'=>2,
  297. 'diver_reason'=>$para['diver_reason'],
  298. 'diver_desc'=>$para['diver_desc'],
  299. 'diver_time'=>sr_getcurtime($time)
  300. ]);
  301. }
  302. public function jhSellerDiverOrder($uid, $para){
  303. $time = time();
  304. $order_id = $para['jh_orderid'];
  305. if (empty($para['diver_reason'])){
  306. sr_throw('参数错误');
  307. }
  308. $order_info = $this->where('jh_ordersn', $order_id)->find();
  309. if (!$order_info){
  310. sr_throw('参数错误');
  311. }
  312. if ($order_info['seller_uid']!= $uid){
  313. sr_throw('无权操作');
  314. }
  315. if (!in_array($order_info['status'], [1,2])){
  316. if ($order_info['status'] == 3){
  317. sr_throw('订单正在申诉中,请耐心等待系统处理');
  318. }
  319. if ($order_info['status'] == 4){
  320. sr_throw('订单已经完成或申诉已处理完成');
  321. }
  322. }
  323. $order_info->save([
  324. 'status'=>3,
  325. 'diver_type'=>1,
  326. 'diver_reason'=>$para['diver_reason'],
  327. 'diver_desc'=>$para['diver_desc'],
  328. 'diver_time'=>sr_getcurtime($time)
  329. ]);
  330. }
  331. }