PropertyOrderLogic.php 9.0 KB

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