PropertyOrderLogic.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. $propertyHeadInfo = PropertyHeadLogic::detail(['id'=>$orderInfo['property_head_id']]);
  128. //$propertyHeadInfo['lon'] $propertyHeadInfo['lat']
  129. Db::startTrans();
  130. try {
  131. if($orderInfo['work_id'] == 0 && $orderInfo['order_status'] == 0){
  132. /*// 'address','appointment_time','pay_way','goods_id','contact_number','contact_people'
  133. 'sn.require' => '订单编号错误',
  134. 'address.require' => '请填写地址',
  135. 'appointment_time.require' => '请填写预约上门时间',
  136. 'appointment_time.dateFormat' => '预约上门时间格式错误',
  137. 'pay_way.require' => '请选择支付方式',
  138. 'goods_id.require' => '订单商品不存在',
  139. 'contact_number.require' => '联系电话不存在',
  140. 'contact_people.require' => '联系人不存在',*/
  141. // 物业地址拼接户主详细门牌号
  142. $params['address'] = $params['address']?:($propertyHeadInfo['address'].$propertyUserInfo['address']);
  143. $serviceOrderParams = array_merge($params,[
  144. 'user_id' => $propertyUserInfo['user_id'],
  145. 'terminal' => 4,
  146. 'user_info' => ['mobile'=>$userInfo['mobile']],
  147. 'lon' => $propertyHeadInfo['lon']?:0,
  148. 'lat' => $propertyHeadInfo['lat']?:0,
  149. ]);
  150. Log::write(json_encode($serviceOrderParams,JSON_UNESCAPED_UNICODE));
  151. $result = ServiceOrderLogic::submitOrder($serviceOrderParams);
  152. if($result === false){
  153. throw new Exception(PropertyOrderLogic::getError());
  154. }
  155. // $result['order_id'] $result['work_id']
  156. $upData = [];
  157. if(isset($result['work_id']) && !empty($result['work_id'])){
  158. $upData['work_id'] = $result['work_id'];
  159. $upData['order_status'] = 1;
  160. }
  161. if($upData){
  162. $propertyOrder = PropertyOrder::where('id', $params['id'])->update($upData);
  163. }
  164. if($propertyOrder){
  165. //订单信息 pay_way=2 goods order_id
  166. $params['from'] = 'goods';
  167. $order = PaymentLogic::getPayOrderInfo(['order_id'=>$result['order_id']]);
  168. Log::info('ServiceOrder:',$result);
  169. if (false === $order) {
  170. Log::info(PaymentLogic::getError());
  171. return false;
  172. //throw new Exception(PaymentLogic::getError());
  173. }
  174. if ($order['order_amount'] == 0) {
  175. //0元时自动支付
  176. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  177. PaymentLogic::pay($params['pay_way'], $params['from'], $order, 4, $redirectUrl);
  178. }
  179. }
  180. }
  181. Db::commit();
  182. return true;
  183. } catch (\Exception $e) {
  184. Db::rollback();
  185. self::setError($e->getMessage());
  186. return false;
  187. }
  188. }
  189. /**
  190. * @notes 取消
  191. * @param array $params
  192. * @return bool
  193. * @author likeadmin
  194. * @date 2024/09/19 14:48
  195. */
  196. public static function cancel(array $params): bool
  197. {
  198. Db::startTrans();
  199. try {
  200. $upData['order_status'] = 2;
  201. $orderStatus = PropertyOrder::where('id', $params['id'])->value('order_status');
  202. if($orderStatus == 0){
  203. PropertyOrder::where('id', $params['id'])->update($upData);
  204. }else{
  205. throw new Exception('当前订单状态不允许操作');
  206. }
  207. Db::commit();
  208. return true;
  209. } catch (\Exception $e) {
  210. Db::rollback();
  211. self::setError($e->getMessage());
  212. return false;
  213. }
  214. }
  215. }