PropertyOrderLogic.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. $orderStatus = PropertyOrder::where('id', $params['id'])->value('order_status');
  72. if($orderStatus == 0){
  73. PropertyOrder::where('id', $params['id'])->update($upData);
  74. }
  75. Db::commit();
  76. return true;
  77. } catch (\Exception $e) {
  78. Db::rollback();
  79. self::setError($e->getMessage());
  80. return false;
  81. }
  82. }
  83. /**
  84. * @notes 删除
  85. * @param array $params
  86. * @return bool
  87. * @author likeadmin
  88. * @date 2024/09/19 14:48
  89. */
  90. public static function delete(array $params): bool
  91. {
  92. return PropertyOrder::destroy($params['id']);
  93. }
  94. /**
  95. * @notes 获取详情
  96. * @param $params
  97. * @return array
  98. * @author likeadmin
  99. * @date 2024/09/19 14:48
  100. */
  101. public static function detail($params): array
  102. {
  103. return PropertyOrder::findOrEmpty($params['id'])->toArray();
  104. }
  105. /**
  106. * @notes 客服接单即由后台提交工单
  107. * @param array $params
  108. * @return bool
  109. * @author likeadmin
  110. * @date 2024/09/19 14:48
  111. */
  112. public static function placeOrder(array $params): bool
  113. {
  114. // 客服手动下单
  115. /*
  116. 1. 按照用户原预约下单参数创建 工单
  117. 2. 由 工单id 状态-已接单 更新当前订单状态信息
  118. // 订单状态:0=未接单,1=已接单,2=取消单,3=已完结
  119. */
  120. $orderInfo = PropertyOrder::where('id', $params['id'])->findOrEmpty()->toArray();
  121. $propertyUserInfo = PropertyUserLogic::detail(['id'=>$orderInfo['property_user_id']]);
  122. $userInfo = UserLogic::detail($propertyUserInfo['user_id']);
  123. if($orderInfo['order_status'] != 0){
  124. self::setError('当前订单状态不允许操作');
  125. return false;
  126. }
  127. Db::startTrans();
  128. try {
  129. if($orderInfo['work_id'] == 0 && $orderInfo['order_status'] == 0){
  130. /*// 'address','appointment_time','pay_way','goods_id','contact_number','contact_people'
  131. 'sn.require' => '订单编号错误',
  132. 'address.require' => '请填写地址',
  133. 'appointment_time.require' => '请填写预约上门时间',
  134. 'appointment_time.dateFormat' => '预约上门时间格式错误',
  135. 'pay_way.require' => '请选择支付方式',
  136. 'goods_id.require' => '订单商品不存在',
  137. 'contact_number.require' => '联系电话不存在',
  138. 'contact_people.require' => '联系人不存在',*/
  139. $serviceOrderParams = array_merge($params,[
  140. 'user_id' => $propertyUserInfo['user_id'],
  141. 'terminal' => 4,
  142. 'user_info' => ['mobile'=>$userInfo['mobile']]
  143. ]);
  144. Log::write(json_encode($serviceOrderParams,JSON_UNESCAPED_UNICODE));
  145. $result = ServiceOrderLogic::submitOrder($serviceOrderParams);
  146. if($result === false){
  147. throw new Exception(PropertyOrderLogic::getError());
  148. }
  149. // $result['order_id'] $result['work_id']
  150. $upData = [];
  151. if(isset($result['work_id']) && !empty($result['work_id'])){
  152. $upData['work_id'] = $result['work_id'];
  153. $upData['order_status'] = 1;
  154. }
  155. if($upData){
  156. $propertyOrder = PropertyOrder::where('id', $params['id'])->update($upData);
  157. }
  158. if($propertyOrder){
  159. //订单信息 pay_way=2 goods order_id
  160. $params['from'] = 'goods';
  161. $order = PaymentLogic::getPayOrderInfo(['order_id'=>$result['order_id']]);
  162. Log::info('ServiceOrder:',$result);
  163. if (false === $order) {
  164. Log::info(PaymentLogic::getError());
  165. return false;
  166. //throw new Exception(PaymentLogic::getError());
  167. }
  168. if ($order['order_amount'] == 0) {
  169. //0元时自动支付
  170. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  171. PaymentLogic::pay($params['pay_way'], $params['from'], $order, 4, $redirectUrl);
  172. }
  173. }
  174. }
  175. Db::commit();
  176. return true;
  177. } catch (\Exception $e) {
  178. Db::rollback();
  179. self::setError($e->getMessage());
  180. return false;
  181. }
  182. }
  183. /**
  184. * @notes 取消
  185. * @param array $params
  186. * @return bool
  187. * @author likeadmin
  188. * @date 2024/09/19 14:48
  189. */
  190. public static function cancel(array $params): bool
  191. {
  192. Db::startTrans();
  193. try {
  194. $upData['order_status'] = 2;
  195. $orderStatus = PropertyOrder::where('id', $params['id'])->value('order_status');
  196. if($orderStatus == 0){
  197. PropertyOrder::where('id', $params['id'])->update($upData);
  198. }else{
  199. throw new Exception('当前订单状态不允许操作');
  200. }
  201. Db::commit();
  202. return true;
  203. } catch (\Exception $e) {
  204. Db::rollback();
  205. self::setError($e->getMessage());
  206. return false;
  207. }
  208. }
  209. }