Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. use think\Model;
  6. use addons\shop\model\Freight;
  7. use addons\shop\model\Carts;
  8. use addons\shop\model\Address;
  9. use Yansongda\Pay\Exceptions\GatewayException;
  10. use addons\epay\library\Service;
  11. use addons\shop\model\OrderAction;
  12. use addons\shop\model\TemplateMsg;
  13. use traits\model\SoftDelete;
  14. /**
  15. * 模型
  16. */
  17. class Order extends Model
  18. {
  19. use SoftDelete;
  20. // 表名
  21. protected $name = 'shop_order';
  22. // 开启自动写入时间戳字段
  23. protected $autoWriteTimestamp = 'int';
  24. // 定义时间戳字段名
  25. protected $createTime = 'createtime';
  26. protected $updateTime = 'updatetime';
  27. protected $deleteTime = 'deletetime';
  28. // 追加属性
  29. protected $append = [
  30. 'url'
  31. ];
  32. protected static $config = [];
  33. protected static $tagCount = 0;
  34. protected static function init()
  35. {
  36. $config = get_addon_config('shop');
  37. self::$config = $config;
  38. }
  39. public function getUrlAttr($value, $data)
  40. {
  41. return url('shop.order/detail', ['orderid' => $data['order_sn']]);
  42. }
  43. public function getPayurlAttr($value, $data)
  44. {
  45. return addon_url('shop/payment/index') . '?orderid=' . $data['order_sn'];
  46. }
  47. public function getCommenturlAttr($value, $data)
  48. {
  49. return url('shop.comment/post') . '?orderid=' . $data['order_sn'];
  50. }
  51. /**
  52. * 获取快递查询URL
  53. */
  54. public function getLogisticsurlAttr($value, $data)
  55. {
  56. $url = self::$config['logisticstype'] == 'kdnapi' ? url('shop.order/logistics') . '?orderid=' . $data['order_sn'] : "https://www.kuaidi100.com/chaxun?com={$data['expressname']}&nu={$data['expressno']}";
  57. return $url;
  58. }
  59. public function getOrderstateList()
  60. {
  61. return ['0' => __('Orderstate 0'), '1' => __('Orderstate 1'), '2' => __('Orderstate 2'), '3' => __('Orderstate 3'), '4' => __('Orderstate 4'), '5' => __('Orderstate 5')];
  62. }
  63. public function getShippingstateList()
  64. {
  65. return ['0' => __('Shippingstate 0'), '1' => __('Shippingstate 1'), '2' => __('Shippingstate 2'), '3' => __('Shippingstate 3')];
  66. }
  67. public function getPaystateList()
  68. {
  69. return ['0' => __('Paystate 0'), '1' => __('Paystate 1')];
  70. }
  71. public function getOrderstateTextAttr($value, $data)
  72. {
  73. $value = $value ? $value : $data['orderstate'];
  74. $list = $this->getOrderstateList();
  75. return $list[$value] ?? '';
  76. }
  77. public function getShippingstateTextAttr($value, $data)
  78. {
  79. $value = $value ? $value : $data['shippingstate'];
  80. $list = $this->getShippingstateList();
  81. return $list[$value] ?? '';
  82. }
  83. public function getPaystateTextAttr($value, $data)
  84. {
  85. $value = $value ? $value : $data['paystate'];
  86. $list = $this->getPaystateList();
  87. return $list[$value] ?? '';
  88. }
  89. public function getStatusTextAttr($value, $data)
  90. {
  91. if ($data['orderstate'] == 0) {
  92. if ($data['paystate'] == 0) {
  93. return '待付款';
  94. }
  95. if ($data['shippingstate'] == 0) {
  96. return '待发货';
  97. } elseif ($data['shippingstate'] == 1) {
  98. return '待收货';
  99. } elseif ($data['shippingstate'] == 2) {
  100. return '待评价';
  101. }
  102. } elseif ($data['orderstate'] == 1) {
  103. return '已取消';
  104. } elseif ($data['orderstate'] == 2) {
  105. return '已失效';
  106. } elseif ($data['orderstate'] == 3) {
  107. return '已完成';
  108. } elseif ($data['orderstate'] == 4) {
  109. return '退货/退款中';
  110. }
  111. return '未知';
  112. }
  113. //获取订单剩余有效时长
  114. public function getRemainsecondsAttr($value, $data)
  115. {
  116. return max(0, $data['expiretime'] - time());
  117. }
  118. /**
  119. * @ DateTime 2021-05-31
  120. * @ 订单信息
  121. * @param $order_sn
  122. * @param $user_id
  123. * @return array|false|\PDOStatement|string|Model
  124. */
  125. public static function getDetail($order_sn, $user_id)
  126. {
  127. return self::with(['orderGoods'])->where('order_sn', $order_sn)->where('user_id', $user_id)->find();
  128. }
  129. /**
  130. * 判断订单是否失效
  131. * @param $order_sn
  132. * @return bool
  133. */
  134. public static function isExpired($order_sn)
  135. {
  136. $orderInfo = Order::getByOrderSn($order_sn);
  137. //订单过期
  138. if (!$orderInfo['orderstate'] && !$orderInfo['paystate'] && time() > $orderInfo['expiretime']) {
  139. // 启动事务
  140. Db::startTrans();
  141. try {
  142. $orderInfo->save(['orderstate' => 2]);
  143. //库存恢复
  144. OrderGoods::setGoodsStocksInc($orderInfo->order_sn);
  145. //恢复优惠券
  146. UserCoupon::resetUserCoupon($orderInfo->user_coupon_id, $orderInfo->order_sn);
  147. // 提交事务
  148. Db::commit();
  149. } catch (\Exception $e) {
  150. // 回滚事务
  151. Db::rollback();
  152. }
  153. return true;
  154. }
  155. return false;
  156. }
  157. /**
  158. * @ 支付
  159. * @param string $orderid
  160. * @param int $user_id
  161. * @param string $paytype
  162. * @param string $method
  163. * @param string $openid
  164. * @param string $notifyurl
  165. * @param string $returnurl
  166. * @return \addons\epay\library\Collection|\addons\epay\library\RedirectResponse|\addons\epay\library\Response|null
  167. * @throws \Exception
  168. */
  169. public static function pay($orderid, $user_id, $paytype, $method = 'web', $openid = '', $notifyurl = null, $returnurl = null)
  170. {
  171. $request = \think\Request::instance();
  172. $order = self::getDetail($orderid, $user_id);
  173. if (!$order) {
  174. throw new \Exception('订单不存在!');
  175. }
  176. if ($order->paystate) {
  177. throw new \Exception('订单已支付!');
  178. }
  179. if ($order->orderstate) {
  180. throw new \Exception('订单已失效!');
  181. }
  182. //支付金额为0,无需支付
  183. if ($order->saleamount == 0) {
  184. throw new \Exception('无需支付!');
  185. }
  186. $order_sn = $order->order_sn;
  187. // 启动事务
  188. Db::startTrans();
  189. try {
  190. //支付方式变更
  191. if (($order['paytype'] == $paytype && $order['method'] != $method)) {
  192. $order_sn = date("Ymdhis") . sprintf("%08d", $user_id) . mt_rand(1000, 9999);
  193. //更新电子面单
  194. $electronics = $order->order_electronics;
  195. foreach ($electronics as $aftersales) {
  196. $aftersales->order_sn = $order_sn;
  197. $aftersales->save();
  198. }
  199. //更新操作日志
  200. $orderAction = $order->order_action;
  201. foreach ($orderAction as $action) {
  202. $action->order_sn = $order_sn;
  203. $action->save();
  204. }
  205. $order->save(['order_sn' => $order_sn]);
  206. //更新订单明细
  207. foreach ($order->order_goods as $item) {
  208. $item->order_sn = $order_sn;
  209. $item->save();
  210. }
  211. }
  212. //更新支付类型和方法
  213. $order->allowField(true)->save(['paytype' => $paytype, 'method' => $method, 'openid' => $openid]);
  214. //提交事务
  215. Db::commit();
  216. } catch (\Exception $e) {
  217. // 回滚事务
  218. Db::rollback();
  219. throw new \Exception($e->getMessage());
  220. }
  221. $response = null;
  222. $epay = get_addon_info('epay');
  223. if ($epay && $epay['state']) {
  224. $notifyurl = $notifyurl ? $notifyurl : $request->root(true) . '/addons/shop/order/epay/type/notify/paytype/' . $paytype;
  225. $returnurl = $returnurl ? $returnurl : $request->root(true) . '/addons/shop/order/epay/type/return/paytype/' . $paytype . '/order_sn/' . $order_sn;
  226. //保证取出的金额一致,不一致将导致订单重复错误
  227. $amount = sprintf("%.2f", $order->saleamount);
  228. $params = [
  229. 'amount' => $amount,
  230. 'orderid' => $order_sn,
  231. 'type' => $paytype,
  232. 'title' => "支付{$amount}元",
  233. 'notifyurl' => $notifyurl,
  234. 'returnurl' => $returnurl,
  235. 'method' => $method,
  236. 'openid' => $openid
  237. ];
  238. try {
  239. $response = Service::submitOrder($params);
  240. } catch (GatewayException $e) {
  241. throw new \Exception(config('app_debug') ? $e->getMessage() : "支付失败,请稍后重试");
  242. }
  243. } else {
  244. throw new \Exception("请在后台安装配置微信支付宝整合插件");
  245. }
  246. return $response;
  247. }
  248. /**
  249. * 订单列表
  250. *
  251. * @param $param
  252. * @return \think\Paginator
  253. */
  254. public static function tableList($param)
  255. {
  256. $pageNum = 15;
  257. if (!empty($param['num'])) {
  258. $pageNum = $param['num'];
  259. }
  260. return self::with(['orderGoods'])
  261. ->where(function ($query) use ($param) {
  262. $query->where('status', 'normal');
  263. if (!empty($param['user_id'])) {
  264. $query->where('user_id', $param['user_id']);
  265. }
  266. if (isset($param['orderstate']) && $param['orderstate'] != '') {
  267. $query->where('orderstate', $param['orderstate']);
  268. }
  269. if (isset($param['shippingstate']) && $param['shippingstate'] != '') {
  270. $query->where('shippingstate', $param['shippingstate']);
  271. }
  272. if (isset($param['paystate']) && $param['paystate'] != '') {
  273. $query->where('paystate', $param['paystate']);
  274. }
  275. if (isset($param['q']) && $param['q'] != '') {
  276. $query->where('order_sn', 'in', function ($query) use ($param) {
  277. return $query->name('shop_order_goods')->where('order_sn|title', 'like', '%' . $param['q'] . '%')->field('order_sn');
  278. });
  279. }
  280. })
  281. ->order('createtime desc')->paginate($pageNum, false, ['query' => request()->get()]);
  282. }
  283. /**
  284. * @ DateTime 2021-06-01
  285. * @ 订单结算
  286. * @param string $order_sn 订单号
  287. * @param float $payamount 支付金额
  288. * @param string $transactionid 流水号
  289. * @return bool
  290. */
  291. public static function settle($order_sn, $payamount, $transactionid = '')
  292. {
  293. $order = self::with(['orderGoods'])->where('order_sn', $order_sn)->find();
  294. if (!$order || $order->paystate == 1) {
  295. return false;
  296. }
  297. if ($payamount != $order->saleamount) {
  298. \think\Log::write("[shop][pay][{$order_sn}][订单支付金额不一致]");
  299. return false;
  300. }
  301. // 启动事务
  302. Db::startTrans();
  303. try {
  304. $order->paystate = 1;
  305. $order->transactionid = $transactionid;
  306. $order->payamount = $payamount;
  307. $order->paytime = time();
  308. $order->paytype = !$order->paytype ? 'system' : $order->paytype;
  309. $order->method = !$order->method ? 'system' : $order->method;
  310. $order->save();
  311. if ($order->payamount == $order->saleamount) {
  312. //支付完成后,商品销量+1
  313. foreach ($order->order_goods as $item) {
  314. $goods = $item->goods;
  315. $sku = $item->sku;
  316. if ($goods) {
  317. $goods->setInc('sales', $item->nums);
  318. }
  319. if ($sku) {
  320. $sku->setInc('sales', $item->nums);
  321. }
  322. }
  323. }
  324. // 提交事务
  325. Db::commit();
  326. } catch (\Exception $e) {
  327. // 回滚事务
  328. Db::rollback();
  329. return false;
  330. }
  331. //记录操作
  332. OrderAction::push($order_sn, '系统', '订单支付成功');
  333. //发送通知
  334. TemplateMsg::sendTempMsg(0, $order->order_sn);
  335. return true;
  336. }
  337. public function user()
  338. {
  339. return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  340. }
  341. public function address()
  342. {
  343. return $this->belongsTo('Address', 'address_id', 'id', [], 'LEFT')->setEagerlyType(0);
  344. }
  345. public function orderGoods()
  346. {
  347. return $this->hasMany('OrderGoods', 'order_sn', 'order_sn');
  348. }
  349. public function orderElectronics()
  350. {
  351. return $this->hasMany('OrderElectronics', 'order_sn', 'order_sn');
  352. }
  353. public function orderAction()
  354. {
  355. return $this->hasMany('OrderAction', 'order_sn', 'order_sn');
  356. }
  357. }