FarmLandOrderServer.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\admin\server\life;
  3. use app\admin\model\LifeFarmLandOrder;
  4. use app\admin\model\LifeFarmLand;
  5. use app\admin\model\LifeFarmLandOrderDetail;
  6. class FarmLandOrderServer
  7. {
  8. /**
  9. * Notes:获取订单列表
  10. * @param string $keywords
  11. * @param int $page
  12. * @param int $limit
  13. * @return array
  14. * User: YCP
  15. * Date: 2022/10/25
  16. */
  17. public static function getOrderList(int $page, int $limit, string $keywords)
  18. {
  19. [$list, $count] = LifeFarmLandOrder::getOrderList($page, $limit,$keywords);
  20. return compact('list', 'page', 'limit', 'count');
  21. }
  22. /**
  23. * Notes:获取订单详情
  24. * @param int $order_id
  25. * @return int
  26. * User: YCP
  27. * Date: 2022/10/25
  28. */
  29. public static function orderInfo(int $order_id)
  30. {
  31. LifeFarmLandOrder::affairBegin();
  32. try {
  33. $where = [];
  34. $where['order_id'] = $order_id;
  35. $result = LifeFarmLandOrder::with(['User','LifeFarmLandOrderDetail'])->where($where)->first();
  36. if (!empty($result)){
  37. LifeFarmLandOrder::affairCommit();
  38. return $result;
  39. }else{
  40. return false;
  41. }
  42. }catch (\Exception $exception){
  43. LifeFarmLandOrder::affairRollback();
  44. throw new \Exception($exception->getMessage(), 500);
  45. }
  46. }
  47. /**
  48. * Notes: 下单
  49. * @param string $package_name
  50. * @param array $package_rules
  51. * @return int
  52. * User: YCP
  53. * Date: 2022/10/25
  54. */
  55. public static function insertOrder(array $params, int $user_id)
  56. {
  57. $land_id = explode(',',$params['order_goods_id']);
  58. $params['order_price'] = 0;
  59. foreach ($land_id as &$v) {
  60. $res = LifeFarmLand::where(['land_id'=>$v])->first();
  61. if(empty($res) || $res === false)
  62. {
  63. throw new \Exception('地块id'.$v.'信息不存在');
  64. }
  65. $params['order_price']+=$res['land_price'];
  66. $log = LifeFarmLandOrderDetail::where(['detail_goods_id'=>$v,'detail_pay_status'=>2])->where('detail_expire_time','>',time())->first();
  67. //var_dump($log);
  68. if($log){
  69. throw new \Exception($res['land_name'].'已售出');
  70. }
  71. }
  72. LifeFarmLandOrder::affairBegin();
  73. try {
  74. $params['order_user_id'] = $user_id;
  75. //$params['order_price'] = $res['land_price'];
  76. $params['order_dno'] = "YMXT".rand(99999,999999);
  77. $params['order_create_time'] = time();
  78. $result = LifeFarmLandOrder::insertGetId($params);
  79. if (!empty($result)){
  80. $msg = '用户' . $user_id . '在:' . date("Y-m-d H:i:s", time()) . '下单-编号: ' . $result;
  81. plog('life-farm-land-order', '悦活-生态农场-一亩心田-下单', $msg);
  82. //写入订单详情
  83. foreach ($land_id as $value){
  84. $land = LifeFarmLand::where(['land_id'=>$value])->first();
  85. $item = [
  86. 'detail_order_id' =>$result,
  87. 'detail_user_id' =>$user_id,
  88. 'detail_goods_id' =>$value,
  89. 'detail_goods_name' =>$land['land_name'],
  90. 'detail_goods_price' =>$land['land_price'],
  91. 'detail_create_time' =>time(),
  92. 'detail_expire_time' =>time()+365*24*60*60,//一年有效期
  93. ];
  94. LifeFarmLandOrderDetail::insert($item);
  95. }
  96. LifeFarmLandOrder::affairCommit();
  97. return $result;
  98. }
  99. throw new \Exception('操作失败!');
  100. }catch (\Exception $exception){
  101. LifeFarmLandOrder::affairRollback();
  102. throw new \Exception($exception->getMessage(), 500);
  103. }
  104. }
  105. /**
  106. * Notes:修改订单支付状态
  107. * @param string $order_id
  108. * @param int $order_finish
  109. * @return int
  110. * User: YCP
  111. * Date: 2022/11/28
  112. */
  113. public static function updatePayStatus($order_id, $order_pay_status)
  114. {
  115. LifeFarmLandOrder::affairBegin();
  116. try {
  117. $where = [];
  118. $where['order_id'] = $order_id;
  119. $data = [];
  120. $data['order_pay_status'] = $order_pay_status;
  121. if($order_pay_status == 1){
  122. $data['order_pay_time'] = time();
  123. }else{
  124. $data['order_pay_time'] = "";
  125. }
  126. $result = LifeFarmLandOrder::where($where)->update($data);
  127. if ($result !== false){
  128. LifeFarmLandOrder::affairCommit();
  129. return true;
  130. }
  131. throw new \Exception('操作失败!');
  132. }catch (\Exception $exception){
  133. LifeFarmLandOrder::affairRollback();
  134. throw new \Exception($exception->getMessage(), 500);
  135. }
  136. }
  137. }