PropertyOrderLogic.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 app\common\model\works\ServiceWork;
  21. use think\Exception;
  22. use think\facade\Db;
  23. use think\facade\Log;
  24. /**
  25. * PropertyOrder逻辑
  26. * Class PropertyOrderLogic
  27. * @package app\adminapi\logic
  28. */
  29. class PropertyOrderLogic extends BaseLogic
  30. {
  31. /**
  32. * @notes 添加
  33. * @param array $params
  34. * @return bool
  35. * @author likeadmin
  36. * @date 2024/09/19 14:48
  37. */
  38. public static function add(array $params): bool
  39. {
  40. // 判断户主是否存在 返回户主id
  41. $propertyUserId = PropertyUserLogic::getPropertyUserIdByMobile($params);
  42. Db::startTrans();
  43. try {
  44. PropertyOrder::create([
  45. 'property_head_id' => $params['property_head_id'],
  46. 'property_user_id' => $propertyUserId,
  47. 'remark' => $params['remark']
  48. ]);
  49. Db::commit();
  50. return true;
  51. } catch (\Exception $e) {
  52. Db::rollback();
  53. self::setError($e->getMessage());
  54. return false;
  55. }
  56. }
  57. /**
  58. * @notes 编辑
  59. * @param array $params
  60. * @return bool
  61. * @author likeadmin
  62. * @date 2024/09/19 14:48
  63. */
  64. public static function edit(array $params): bool
  65. {
  66. Db::startTrans();
  67. try {
  68. $upData = ['remark' => $params['remark']];
  69. if($params['order_status'] == 2){
  70. $upData['order_status'] = $params['order_status'];
  71. }
  72. $orderStatus = PropertyOrder::where('id', $params['id'])->value('order_status');
  73. if($orderStatus == 0){
  74. PropertyOrder::where('id', $params['id'])->update($upData);
  75. }
  76. Db::commit();
  77. return true;
  78. } catch (\Exception $e) {
  79. Db::rollback();
  80. self::setError($e->getMessage());
  81. return false;
  82. }
  83. }
  84. /**
  85. * @notes 删除
  86. * @param array $params
  87. * @return bool
  88. * @author likeadmin
  89. * @date 2024/09/19 14:48
  90. */
  91. public static function delete(array $params): bool
  92. {
  93. return PropertyOrder::destroy($params['id']);
  94. }
  95. /**
  96. * @notes 获取详情
  97. * @param $params
  98. * @return array
  99. * @author likeadmin
  100. * @date 2024/09/19 14:48
  101. */
  102. public static function detail($params): array
  103. {
  104. return PropertyOrder::findOrEmpty($params['id'])->toArray();
  105. }
  106. /**
  107. * @notes 客服接单即由后台提交工单
  108. * @param array $params
  109. * @return bool
  110. * @author likeadmin
  111. * @date 2024/09/19 14:48
  112. */
  113. public static function placeOrder(array $params): bool
  114. {
  115. // 客服手动下单
  116. /*
  117. 1. 按照用户原预约下单参数创建 工单
  118. 2. 由 工单id 状态-已接单 更新当前订单状态信息
  119. // 订单状态:0=未接单,1=已接单,2=取消单,3=已完结
  120. */
  121. $orderInfo = PropertyOrder::where('id', $params['id'])->findOrEmpty()->toArray();
  122. $propertyUserInfo = PropertyUserLogic::detail(['id'=>$orderInfo['property_user_id']]);
  123. $userInfo = UserLogic::detail($propertyUserInfo['user_id']);
  124. if($orderInfo['order_status'] != 0){
  125. self::setError('当前订单状态不允许操作');
  126. return false;
  127. }
  128. $propertyHeadInfo = PropertyHeadLogic::detail(['id'=>$orderInfo['property_head_id']]);
  129. //$propertyHeadInfo['lon'] $propertyHeadInfo['lat']
  130. Db::startTrans();
  131. try {
  132. if($orderInfo['work_id'] == 0 && $orderInfo['order_status'] == 0){
  133. /*// 'address','appointment_time','pay_way','goods_id','contact_number','contact_people'
  134. 'sn.require' => '订单编号错误',
  135. 'address.require' => '请填写地址',
  136. 'appointment_time.require' => '请填写预约上门时间',
  137. 'appointment_time.dateFormat' => '预约上门时间格式错误',
  138. 'pay_way.require' => '请选择支付方式',
  139. 'goods_id.require' => '订单商品不存在',
  140. 'contact_number.require' => '联系电话不存在',
  141. 'contact_people.require' => '联系人不存在',*/
  142. // 物业地址拼接户主详细门牌号
  143. $params['address'] = $params['address']?:($propertyHeadInfo['address'].$propertyUserInfo['address']);
  144. $serviceOrderParams = array_merge($params,[
  145. 'user_id' => $propertyUserInfo['user_id'],
  146. 'terminal' => 4,
  147. 'user_info' => ['mobile'=>$userInfo['mobile']],
  148. 'lon' => $propertyHeadInfo['lon']?:0,
  149. 'lat' => $propertyHeadInfo['lat']?:0,
  150. ]);
  151. Log::write(json_encode($serviceOrderParams,JSON_UNESCAPED_UNICODE));
  152. $result = ServiceOrderLogic::submitOrder($serviceOrderParams);
  153. if($result === false){
  154. throw new Exception(PropertyOrderLogic::getError());
  155. }
  156. // $result['order_id'] $result['work_id']
  157. $upData = [];
  158. if(isset($result['work_id']) && !empty($result['work_id'])){
  159. $upData['work_id'] = $result['work_id'];
  160. $upData['order_status'] = 1;
  161. }
  162. if($upData){
  163. $propertyOrder = PropertyOrder::where('id', $params['id'])->update($upData);
  164. }
  165. if($propertyOrder){
  166. //订单信息 pay_way=2 goods order_id
  167. $params['from'] = 'goods';
  168. $order = PaymentLogic::getPayOrderInfo(['order_id'=>$result['order_id']]);
  169. Log::info('ServiceOrder:',$result);
  170. if (false === $order) {
  171. Log::info(PaymentLogic::getError());
  172. return false;
  173. //throw new Exception(PaymentLogic::getError());
  174. }
  175. if ($order['order_amount'] == 0) {
  176. //0元时自动支付
  177. $redirectUrl = $params['redirect'] ?? '/pages/payment/payment';
  178. PaymentLogic::pay($params['pay_way'], $params['from'], $order, 4, $redirectUrl);
  179. }
  180. }
  181. }
  182. Db::commit();
  183. return true;
  184. } catch (\Exception $e) {
  185. Db::rollback();
  186. self::setError($e->getMessage());
  187. return false;
  188. }
  189. }
  190. /**
  191. * @notes 取消
  192. * @param array $params
  193. * @return bool
  194. * @author likeadmin
  195. * @date 2024/09/19 14:48
  196. */
  197. public static function cancel(array $params): bool
  198. {
  199. Db::startTrans();
  200. try {
  201. $upData['order_status'] = 2;
  202. $orderStatus = PropertyOrder::where('id', $params['id'])->value('order_status');
  203. if($orderStatus == 0){
  204. PropertyOrder::where('id', $params['id'])->update($upData);
  205. }else{
  206. throw new Exception('当前订单状态不允许操作');
  207. }
  208. Db::commit();
  209. return true;
  210. } catch (\Exception $e) {
  211. Db::rollback();
  212. self::setError($e->getMessage());
  213. return false;
  214. }
  215. }
  216. }