RiceCardOrder.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\model\order;
  13. use app\common\model\order\RiceCardOrder as RiceCardOrderModel;
  14. use app\api\service\User as UserService;
  15. use think\facade\Db;
  16. use app\api\model\Receipt as ReceiptModel;
  17. use app\common\model\card\RiceCard;
  18. /**
  19. * 米卡订单模型
  20. * Class Goods
  21. * @package app\api\model
  22. */
  23. class RiceCardOrder extends RiceCardOrderModel
  24. {
  25. /**
  26. * 追加字段
  27. * @var array
  28. */
  29. protected $append = [
  30. 'state_text', // 订单状态
  31. 'djs_time' // 订单状态
  32. ];
  33. /**
  34. * 待支付订单倒计时时间戳
  35. * @param $value
  36. * @param $data
  37. * @return int
  38. * @author: zjwhust
  39. * @Time: 2021/10/8 17:06
  40. */
  41. public function getDjsTimeAttr($value, $data){
  42. if($data['pay_status']==0){ //待支付
  43. $now = time();
  44. $djs_time = 60 *30;
  45. if($now - strtotime($data['create_time'])<$djs_time){
  46. return strtotime($data['create_time']) + $djs_time - $now;
  47. }
  48. }
  49. return 0;
  50. }
  51. /**
  52. * 获取用户米卡订单列表
  53. * @return \think\Paginator
  54. * @throws \app\common\exception\BaseException
  55. * @throws \think\db\exception\DbException
  56. */
  57. public function getList(){
  58. // 检索查询条件
  59. $filter = ['is_del'=>0];
  60. // // 售后单状态
  61. // $state > -1 && $filter[] = ['status', '=', $state];
  62. // 当前用户ID
  63. $userId = UserService::getCurrentLoginUserId();
  64. // 查询列表记录
  65. return $this->with([])
  66. ->where($filter)
  67. ->where('user_id', '=', $userId)
  68. ->order(['create_time' => 'desc'])
  69. ->paginate(15);
  70. }
  71. /**
  72. * 用户删除订单
  73. * @param $data
  74. * @return false|int
  75. */
  76. public function del(){
  77. $user = UserService::getCurrentLoginUser(true);
  78. if($this['user_id']!=$user->user_id){
  79. $this->error = '当前订单不合法,不允许该操作';
  80. return false;
  81. }
  82. return $this->transaction(function () use ($user) {
  83. $this->save([
  84. 'is_del' => 1,
  85. ]);
  86. return true;
  87. });
  88. $this->error = '当前售后单不合法,不允许该操作';
  89. return false;
  90. }
  91. //创建订单
  92. public function createOrderEvent($order){
  93. return $this->transaction(function () use ($order) {
  94. $this->save($order);
  95. $order_id = $this->id;
  96. $card_id = $order['rice_card_id'];
  97. $rcModel = RiceCard::where('id',$card_id)->find();
  98. $stock = $rcModel->stock - $order['buy_num'];
  99. $rcModel->onUpdate($card_id,['stock'=>$stock]);
  100. return $order_id;
  101. });
  102. $this->error = '添加失败';
  103. return false;
  104. }
  105. /**
  106. * 用户取消订单
  107. * @param $data
  108. * @return false|int
  109. */
  110. public function cancel($cancel_reason){
  111. $user = UserService::getCurrentLoginUser(true);
  112. if($this['user_id']!=$user->user_id){
  113. $this->error = '当前订单不合法,不允许该操作';
  114. return false;
  115. }
  116. $type = false;
  117. if($this['pay_status'] == 1){//已支付
  118. $this->error = '当前订单已经支付,不允许该操作';
  119. return false;
  120. }
  121. if($this['order_status'] == 30){//已完成
  122. $this->error = '当前订单已经完成,不允许该操作';
  123. return false;
  124. }
  125. if($this['order_status'] == 40){//已完成
  126. $this->error = '当前订单交易关闭,不允许该操作';
  127. return false;
  128. }
  129. return $this->transaction(function () use ($cancel_reason) {
  130. $this->save([
  131. 'order_status' =>40,
  132. 'cancel_reason'=>$cancel_reason,
  133. 'cancel_time'=>Date("Y-m-d H:i:s",time())
  134. ]);
  135. $card_id = $this['rice_card_id'];
  136. $rcModel = RiceCard::where('id',$card_id)->find();
  137. $stock = $rcModel->stock +$this['buy_num'];
  138. $rcModel->onUpdate($card_id,['stock'=>$stock]);
  139. return true;
  140. });
  141. $this->error = '当前订单不合法,不允许该操作';
  142. return false;
  143. }
  144. /**
  145. * 订单状态文字描述
  146. * @param $value
  147. * @param $data
  148. * @return string
  149. */
  150. public function getStateTextAttr($value, $data){
  151. $value = '-';
  152. // 已完成
  153. if($data['order_status'] == 30) {
  154. $value = '已完成';
  155. }
  156. // 已取消
  157. if($data['order_status'] == 40) {
  158. $value = '交易关闭';
  159. }
  160. if($data['order_status']==10&&$data['pay_status']==0){
  161. $value = '待支付';
  162. }
  163. return $value;
  164. }
  165. /**
  166. * 获取当前用户的米卡订单详情
  167. * @param int $orderRefundId 售后单ID
  168. * @param bool $isWith 是否关联
  169. * @return static|null
  170. * @throws BaseException
  171. */
  172. public static function getDetail(int $orderId){
  173. // 关联查询
  174. $with = [];
  175. // 获取记录
  176. $user_id = UserService::getCurrentLoginUserId();
  177. $detail = static::detail([
  178. 'user_id' => $user_id,
  179. 'id' => $orderId
  180. ], $with);
  181. // $detail = static::where("order_refund_id",$orderRefundId)->find();
  182. if (empty($detail)) throwError('未找到该米卡订单');
  183. return $detail;
  184. }
  185. /**
  186. * 获取订单详情(待付款状态)
  187. * @param $orderNo
  188. * @return array|null|static
  189. */
  190. public static function getPayDetail(string $orderNo)
  191. {
  192. return self::detail(['order_no' => $orderNo, 'pay_status' => 0]);
  193. }
  194. }