PropertyOrderLogic.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\property;
  15. use app\adminapi\logic\user\UserLogic;
  16. use app\api\logic\ServiceOrderLogic;
  17. use app\common\logic\PaymentLogic;
  18. use app\common\model\property\PropertyOrder;
  19. use app\common\logic\BaseLogic;
  20. use think\Exception;
  21. use think\facade\Db;
  22. use think\facade\Log;
  23. /**
  24. * PropertyOrder逻辑
  25. * Class PropertyOrderLogic
  26. * @package app\adminapi\logic
  27. */
  28. class PropertyOrderLogic extends BaseLogic
  29. {
  30. /**
  31. * @notes 添加
  32. * @param array $params
  33. * @return bool
  34. * @author likeadmin
  35. * @date 2024/09/19 14:48
  36. */
  37. public static function add(array $params): bool
  38. {
  39. // 判断户主是否存在 返回户主id
  40. $propertyUserId = PropertyUserLogic::getPropertyUserIdByMobile($params);
  41. Db::startTrans();
  42. try {
  43. PropertyOrder::create([
  44. 'property_head_id' => $params['property_head_id'],
  45. 'property_user_id' => $propertyUserId,
  46. 'remark' => $params['remark']
  47. ]);
  48. Db::commit();
  49. return true;
  50. } catch (\Exception $e) {
  51. Db::rollback();
  52. self::setError($e->getMessage());
  53. return false;
  54. }
  55. }
  56. /**
  57. * @notes 编辑
  58. * @param array $params
  59. * @return bool
  60. * @author likeadmin
  61. * @date 2024/09/19 14:48
  62. */
  63. public static function edit(array $params): bool
  64. {
  65. Db::startTrans();
  66. try {
  67. $upData = ['remark' => $params['remark']];
  68. if($params['order_status'] == 2){
  69. $upData['order_status'] = $params['order_status'];
  70. }
  71. PropertyOrder::where('id', $params['id'])->update($upData);
  72. Db::commit();
  73. return true;
  74. } catch (\Exception $e) {
  75. Db::rollback();
  76. self::setError($e->getMessage());
  77. return false;
  78. }
  79. }
  80. /**
  81. * @notes 删除
  82. * @param array $params
  83. * @return bool
  84. * @author likeadmin
  85. * @date 2024/09/19 14:48
  86. */
  87. public static function delete(array $params): bool
  88. {
  89. return PropertyOrder::destroy($params['id']);
  90. }
  91. /**
  92. * @notes 获取详情
  93. * @param $params
  94. * @return array
  95. * @author likeadmin
  96. * @date 2024/09/19 14:48
  97. */
  98. public static function detail($params): array
  99. {
  100. return PropertyOrder::findOrEmpty($params['id'])->toArray();
  101. }
  102. /**
  103. * @notes 客服下单
  104. * @param array $params
  105. * @return bool
  106. * @author likeadmin
  107. * @date 2024/09/19 14:48
  108. */
  109. public static function placeOrder(array $params): bool
  110. {
  111. // 客服手动下单
  112. /*
  113. 1. 按照用户原预约下单参数创建 工单
  114. 2. 由 工单id 状态-已接单 更新当前订单状态信息
  115. // 订单状态:0=未接单,1=已接单,2=取消单,3=已完结
  116. */
  117. $orderInfo = PropertyOrder::where('id', $params['id'])->findOrEmpty()->toArray();
  118. $propertyUserInfo = PropertyUserLogic::detail(['id'=>$orderInfo['property_user_id']]);
  119. $userInfo = UserLogic::detail($propertyUserInfo['user_id']);
  120. if($orderInfo['order_status'] != 0){
  121. self::setError('当前订单状态不允许操作');
  122. return false;
  123. }
  124. Db::startTrans();
  125. try {
  126. if($orderInfo['work_id'] == 0 && $orderInfo['order_status'] == 0){
  127. /*// 'address','appointment_time','pay_way','goods_id','contact_number','contact_people'
  128. 'sn.require' => '订单编号错误',
  129. 'address.require' => '请填写地址',
  130. 'appointment_time.require' => '请填写预约上门时间',
  131. 'appointment_time.dateFormat' => '预约上门时间格式错误',
  132. 'pay_way.require' => '请选择支付方式',
  133. 'goods_id.require' => '订单商品不存在',
  134. 'contact_number.require' => '联系电话不存在',
  135. 'contact_people.require' => '联系人不存在',*/
  136. $serviceOrderParams = array_merge($params,[
  137. 'user_id' => $propertyUserInfo['user_id'],
  138. 'terminal' => 4,
  139. 'user_info' => ['mobile'=>$userInfo['mobile']]
  140. ]);
  141. Log::write(json_encode($serviceOrderParams,JSON_UNESCAPED_UNICODE));
  142. $result = ServiceOrderLogic::submitOrder($serviceOrderParams);
  143. if($result === false){
  144. throw new Exception(PropertyOrderLogic::getError());
  145. }
  146. // $result['order_id'] $result['work_id']
  147. $upData = [];
  148. if(isset($result['work_id']) && !empty($result['work_id'])){
  149. $upData['work_id'] = $result['work_id'];
  150. $upData['order_status'] = 1;
  151. }
  152. if($upData){
  153. $propertyOrder = PropertyOrder::where('id', $params['id'])->update($upData);
  154. }
  155. if($propertyOrder){
  156. //订单信息 pay_way=2 goods order_id
  157. $params['from'] = 'goods';
  158. $order = PaymentLogic::getPayOrderInfo(['order_id'=>$result['order_id']]);
  159. Log::info('ServiceOrder:',$result);
  160. if (false === $order) {
  161. Log::info(PaymentLogic::getError());
  162. return false;
  163. //throw new Exception(PaymentLogic::getError());
  164. }
  165. if ($order['order_amount'] == 0) {
  166. //0元时自动支付
  167. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  168. PaymentLogic::pay($params['pay_way'], $params['from'], $order, 4, $redirectUrl);
  169. }
  170. }
  171. }
  172. Db::commit();
  173. return true;
  174. } catch (\Exception $e) {
  175. Db::rollback();
  176. self::setError($e->getMessage());
  177. return false;
  178. }
  179. }
  180. }