ServiceOrderLogic.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  1. <?php
  2. namespace app\api\logic;
  3. use app\adminapi\service\DistributeLeafletsService;
  4. use app\common\enum\GoodsEnum;
  5. use app\common\enum\PayEnum;
  6. use app\common\enum\RefundEnum;
  7. use app\common\enum\WorkEnum;
  8. use app\common\enum\YesNoEnum;
  9. use app\common\logic\BaseLogic;
  10. use app\common\logic\PaymentLogic;
  11. use app\common\logic\PayNotifyLogic;
  12. use app\common\logic\RefundLogic;
  13. use app\common\model\coupon\CouponCategory;
  14. use app\common\model\coupon\UserCoupon;
  15. use app\common\model\dict\DictData;
  16. use app\common\model\goods\Goods;
  17. use app\common\model\master_commission\MasterWorkerCommissionConfig;
  18. use app\common\model\master_commission\MasterWorkerCommissionNotice;
  19. use app\common\model\master_worker\MasterWorker;
  20. use app\common\model\master_worker\MasterWorkerRule;
  21. use app\common\model\orders\RechargeOrder;
  22. use app\common\model\property\PropertyActivity;
  23. use app\common\model\recharge\OrderGoods;
  24. use app\common\model\refund\RefundRecord;
  25. use app\common\model\service_area\ServiceArea;
  26. use app\common\model\spare_part\SparePart;
  27. use app\common\model\works\ServiceWork;
  28. use app\common\model\works\ServiceWorkAppointmentLog;
  29. use app\common\model\works\ServiceWorkSpare;
  30. use app\workerapi\logic\ServiceWorkLogLogic;
  31. use think\Exception;
  32. use think\facade\Db;
  33. use think\facade\Log;
  34. /**
  35. * 订单逻辑层
  36. * Class ServiceOrderLogic
  37. * @package app\api\logic
  38. */
  39. class ServiceOrderLogic extends BaseLogic
  40. {
  41. /**
  42. * 判断是否在某服务区 返回服务区城市信息
  43. * @param $params
  44. * @return bool
  45. */
  46. public static function isService($params):array
  47. {
  48. // 查询服务区所有的地点
  49. $rules = ServiceArea::field(['id', 'province', 'city','county', 'area_name', 'electronic_fence'])->select()->toArray();
  50. $point=['lng'=> $params['lon'],'lat'=> $params['lat']];
  51. foreach ($rules as $value){
  52. foreach ($value['electronic_fence'] as $polygon) {
  53. if (isPointInPolygon($point, $polygon)) {
  54. return [
  55. 'city'=>$value['city'],
  56. 'county'=>$value['county'],
  57. 'area_name'=>$value['area_name']
  58. ];
  59. }
  60. }
  61. }
  62. throw new Exception('已超出服务区域!-1001');
  63. }
  64. /**
  65. * 提交订单
  66. * @param array $params
  67. * @return array|false
  68. */
  69. public static function submitOrder($params)
  70. {
  71. Db::startTrans();
  72. try {
  73. // 订单位置是否在服务区内
  74. $areas = self::isService($params);
  75. $goods = Goods::findOrEmpty($params['goods_id']);
  76. if($goods->isEmpty()){
  77. throw new Exception('产品不存在!');
  78. }
  79. if(empty($params['user_info']['mobile'])){
  80. throw new Exception('请先补充您的联系方式后在提交订单');
  81. }
  82. //根据服务工单计算当前订单应支付金额
  83. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  84. //一口价订单
  85. $order_total = $goods['service_fee'];
  86. $order_amount = $goods['service_fee'];
  87. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  88. $order_total = $goods['service_fee'];
  89. $order_amount = $goods['service_fee'];
  90. }
  91. else{
  92. $order_total = $goods['base_service_fee'];
  93. $order_amount = $goods['service_fee'];
  94. }
  95. //优惠券验证
  96. if(!empty($params['coupon_id'])){
  97. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  98. ->where('voucher_count','>',0)
  99. ->where('expire_time','>=',time())
  100. ->where('begin_use','<',time())
  101. ->findOrEmpty();
  102. if($user_coupon->isEmpty()){
  103. throw new Exception('该优惠券无法使用');
  104. }
  105. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order_amount<$user_coupon['amount_require']){
  106. throw new Exception('该优惠劵不满足满减使用条件');
  107. }
  108. if($goods['goods_payment_type'] != GoodsEnum::ISGOODS_PAYMENT_TYPE){
  109. throw new Exception('请在支付尾款的时候使用该优惠券');
  110. }
  111. //优惠券折扣
  112. if($user_coupon['mold_type'] == 1){
  113. //按比例折扣
  114. if($user_coupon['discount_ratio']>=1){
  115. throw new Exception('优惠券有误,请联系客服');
  116. }
  117. $order_coupon_amount = $order_amount*(1-$user_coupon['discount_ratio']);
  118. }else{
  119. $order_coupon_amount = $user_coupon['amount'];
  120. }
  121. if(!empty($user_coupon['max_deductible_price'])){
  122. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order_amount-$user_coupon['max_deductible_price']):($order_amount-$order_coupon_amount);
  123. }else{
  124. $order_amount = $order_amount-$order_coupon_amount;
  125. }
  126. $user_coupon->voucher_status = 1;
  127. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  128. $user_coupon->save();
  129. }
  130. //生成服务工单
  131. $work_data = [
  132. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  133. 'real_name' => $params['contact_people'],
  134. 'mobile' => $params['contact_number'],
  135. 'address' => $params['address'],
  136. 'province' => $areas['province']??0,
  137. 'city' => $areas['city']??0,
  138. 'area_name' => $areas['area_name']??'',
  139. 'title' => $goods->goods_name,
  140. 'category_type' => $goods['category_type'],
  141. 'goods_category_ids' => $goods['goods_category_ids'],
  142. 'goods_category_id' => $goods['goods_category_id'],
  143. 'base_service_fee' => $goods['base_service_fee'],
  144. 'service_fee' => $goods['service_fee'],
  145. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  146. 'appointment_time' => strtotime($params['appointment_time']),
  147. 'user_id'=>$params['user_id'],
  148. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  149. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  150. 'property_activity_id'=>!empty($params['property_activity_id'])?$params['property_activity_id']:0,
  151. 'user_equity_id'=>$params['user_equity_id']??0,
  152. ];
  153. //判断是否是加单
  154. if(!empty($params['worker'])){
  155. $worker_id = MasterWorker::where('worker_number',$params['worker'])->value('id');
  156. $work_data['master_worker_id'] = $worker_id;
  157. $work_data['work_status'] = 1;
  158. $work_data['dispatch_time'] = time();
  159. $work_data['work_type'] = 2;
  160. $work_data['data_type'] = 1;
  161. }
  162. //判断是否是复购单
  163. $is_work = ServiceWork::where(['user_id'=>$params['user_id'],'service_status'=>3])->findOrEmpty();
  164. if(!$is_work->isEmpty()){
  165. $work_data['data_type'] = 1;
  166. }
  167. $service_work = ServiceWork::create($work_data);
  168. //使用权益卡时订单应支付金额=0
  169. if(isset($params['user_equity_id']) && $params['user_equity_id']){
  170. $order_total = 0;
  171. $order_amount = 0;
  172. }
  173. //生成服务订单
  174. $data = [
  175. 'work_id'=> $service_work['id'],
  176. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  177. 'order_type'=>0,//服务订单
  178. 'order_terminal' => $params['terminal'],
  179. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  180. 'user_id' => $params['user_id'],
  181. 'pay_status' => PayEnum::UNPAID,
  182. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  183. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  184. 'pay_way' => $params['pay_way'],
  185. 'order_total' => $order_total,
  186. 'order_amount' => $order_amount,
  187. ];
  188. $order = RechargeOrder::create($data);
  189. //生成订单服务详情
  190. OrderGoods::create([
  191. 'sn' => $order['sn'],
  192. 'goods_id' => $params['goods_id'],
  193. 'category_type' => $goods['category_type'],
  194. 'goods_category_ids' => $goods['goods_category_ids'],
  195. 'goods_category_id' => $goods['goods_category_id'],
  196. 'goods_name' => $goods['goods_name'],
  197. 'goods_image' => $goods['goods_image'],
  198. 'goods_video' => $goods['goods_video'],
  199. 'goods_number' => $goods['goods_number'],
  200. 'good_unit' => $goods['good_unit'],
  201. 'goods_size' => $goods['goods_size'],
  202. 'goods_type' => $goods['goods_type'],
  203. 'goods_brand' => $goods['goods_brand'],
  204. 'install_guide' => $goods['install_guide'],
  205. 'goods_payment_type'=>$goods['goods_payment_type'],
  206. 'base_service_fee' => $goods['base_service_fee'],
  207. 'service_total' => $goods['service_total'],
  208. 'service_fee' => $goods['service_fee'],
  209. 'service_image' => $goods['service_image'],
  210. 'warranty_period'=>$goods['warranty_period'],
  211. 'fee_schedule' => $goods['fee_schedule'],
  212. 'goods_status' => $goods['goods_status'],
  213. ]);
  214. Db::commit();
  215. } catch (\Exception $e) {
  216. Db::rollback();
  217. self::setError($e->getMessage());
  218. return false;
  219. }
  220. return [
  221. 'order_id' => (int)$order['id'],
  222. 'work_id' => (int)$order['work_id'],
  223. ];
  224. }
  225. /**
  226. * 提交尾款订单
  227. * @param array $params
  228. * @return array|false
  229. */
  230. public static function submitFinalOrder($params)
  231. {
  232. Db::startTrans();
  233. try {
  234. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  235. if ($order->isEmpty()) {
  236. throw new Exception('订单不存在');
  237. }
  238. //判断订单类型.服务订单尾款处理
  239. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  240. {
  241. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  242. if($order->isEmpty()){
  243. throw new Exception('订单已支付');
  244. }
  245. }
  246. if ($order['pay_status'] == PayEnum::ISPAID) {
  247. throw new Exception('订单已支付');
  248. }
  249. $order_goods = OrderGoods::where('sn',$params['sn'])->findOrEmpty();
  250. $goods = Goods::findOrEmpty($order_goods['goods_id']);
  251. //判断是否存在优惠券
  252. if(!empty($params['coupon_id']) && empty($order['coupon_id'])){
  253. $user_coupon = UserCoupon::where(['id'=>$params['coupon_id'],'user_id'=>$params['user_id'],'voucher_status'=>0])
  254. ->where('voucher_count','>',0)
  255. ->where('expire_time','>=',time())
  256. ->where('begin_use','<',time())
  257. ->findOrEmpty();
  258. if($user_coupon->isEmpty()){
  259. throw new Exception('该优惠券不满足使用条件');
  260. }
  261. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE and $order['order_amount']<$user_coupon['amount_require']){
  262. throw new Exception('该优惠劵不满足满减使用条件');
  263. }
  264. //优惠券折扣
  265. if($user_coupon['mold_type'] == 1){
  266. //按比例折扣
  267. if($user_coupon['discount_ratio']>=1){
  268. throw new Exception('优惠券有误,请联系客服');
  269. }
  270. $order_coupon_amount = $order['order_amount']*(1-$user_coupon['discount_ratio']);
  271. }else{
  272. $order_coupon_amount = $user_coupon['amount'];
  273. }
  274. if(!empty($user_coupon['max_deductible_price'])){
  275. $order_amount = ($order_coupon_amount>$user_coupon['max_deductible_price'])?($order['order_amount']-$user_coupon['max_deductible_price']):($order['order_amount']-$order_coupon_amount);
  276. }else{
  277. $order_amount = $order['order_amount']-$order_coupon_amount;
  278. }
  279. $user_coupon->voucher_status = 1;
  280. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  281. $user_coupon->save();
  282. $order->coupon_id = !empty($params['coupon_id'])?$params['coupon_id']:0;
  283. $order->coupon_price = !empty($order_coupon_amount)?$order_coupon_amount:0;
  284. $order->order_amount = !empty($order_amount)?$order_amount:$order->order_amount;
  285. $order->save();
  286. }
  287. Db::commit();
  288. } catch (\Exception $e) {
  289. Db::rollback();
  290. self::setError($e->getMessage());
  291. return false;
  292. }
  293. return [
  294. 'order_id' => (int)$order['id'],
  295. 'work_id' => (int)$order['work_id'],
  296. ];
  297. }
  298. /**
  299. * 重置订单优惠券
  300. * * @param $params
  301. * * * @return array|false
  302. */
  303. public static function cancelOrderCoupon($params)
  304. {
  305. Db::startTrans();
  306. try {
  307. $order = \app\common\model\recharge\RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  308. if ($order->isEmpty()) {
  309. throw new Exception('订单不存在');
  310. }
  311. //判断订单类型.服务订单尾款处理
  312. if($order['order_type'] == 0 and $order['pay_status'] == PayEnum::ISPAID)//服务工单
  313. {
  314. $order = RechargeOrder::where(['work_id'=>$order['work_id'],'pay_status'=>0])->findOrEmpty();
  315. if($order->isEmpty()){
  316. throw new Exception('订单已支付');
  317. }
  318. }
  319. if ($order['pay_status'] == PayEnum::ISPAID) {
  320. throw new Exception('订单已支付');
  321. }
  322. if(!empty($order['coupon_id'])){
  323. $user_coupon_ed = UserCoupon::findOrEmpty($order['coupon_id']);
  324. if(!$user_coupon_ed->isEmpty()){
  325. $user_coupon_ed->voucher_count = $user_coupon_ed->voucher_count+1;
  326. $user_coupon_ed->voucher_status = 0;
  327. $user_coupon_ed->save();
  328. $order->order_amount = $order->order_amount+$order->coupon_price;
  329. $order->coupon_id = 0;
  330. $order->coupon_price = 0;
  331. $order->save();
  332. }
  333. }
  334. Db::commit();
  335. } catch (\Exception $e) {
  336. Db::rollback();
  337. self::setError($e->getMessage());
  338. return false;
  339. }
  340. }
  341. /**
  342. * 获取订单工程师信息
  343. * * @param $params
  344. * * @return array|false
  345. */
  346. public static function getMasterWorker($params)
  347. {
  348. try {
  349. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  350. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  351. },'service_work'=>function ($query) {
  352. $query->visible(['real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','finished_images','finished_time'])->append(['service_status_text','user_service_status','user_service_status_text']);
  353. }])
  354. ->visible(['id','sn','payment_type','order_total','order_amount','pay_status','create_time','title','work_id'])
  355. ->where([
  356. 'order_type' => 0,
  357. 'user_id' => $params['user_id'],
  358. 'sn'=>$params['sn']
  359. ])->findOrEmpty()->toArray();
  360. $data = [];
  361. //获取工程师参数
  362. if(!empty($order_info['service_work']['master_worker_id'])){
  363. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  364. $data['avatar'] = $worker['avatar'];
  365. $data['real_name'] = $worker['real_name'];
  366. $data['worker_number'] = $worker['worker_number'];
  367. $data['mobile'] = $worker['mobile'];
  368. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  369. $data['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  370. $data['appointment_time'] = $order_info['service_work']['appointment_time'];
  371. }
  372. return $data;
  373. }
  374. catch (\Exception $e) {
  375. self::setError($e->getMessage());
  376. return false;
  377. }
  378. }
  379. /**
  380. * 获取订单详情
  381. * @param $params
  382. * @return array|false
  383. */
  384. public static function detail($params)
  385. {
  386. try {
  387. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  388. $query->visible(['goods_id','goods_name','goods_image','goods_number','good_unit']);
  389. },'service_work'=>function ($query) {
  390. $query->visible(['work_sn','real_name','mobile','address','service_status','appointment_time','master_worker_id','work_images','explanation','finished_images','finished_time','spare_total','service_work_spare_id','refund_approval','property_activity_id'])->append(['service_status_text','user_service_status','user_service_status_text']);
  391. }])
  392. ->visible(['id','sn','payment_type','order_total','order_amount','paid_amount','pay_status','create_time','title','work_id'])
  393. ->where([
  394. 'order_type' => 0,
  395. 'user_id' => $params['user_id'],
  396. 'sn'=>$params['sn']
  397. ])->findOrEmpty()->toArray();
  398. if(empty($order_info)){
  399. throw new Exception('订单不存在');
  400. }
  401. $order_info['master_worker'] = [
  402. 'avatar' => '',
  403. 'real_name'=>'',
  404. 'worker_number'=>'',
  405. 'mobile'=>'',
  406. 'worker_exp'=>''
  407. ];
  408. //查询总价
  409. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  410. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  411. //退款金额
  412. $order_refund_amount = 0;
  413. //获取工程师参数
  414. if(!empty($order_info['service_work']['master_worker_id'])){
  415. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  416. $order_info['master_worker']['avatar'] = !empty($worker)?$worker['avatar']:'';
  417. $order_info['master_worker']['real_name'] = !empty($worker)?$worker['real_name']:'';
  418. $order_info['master_worker']['worker_number'] = !empty($worker)?$worker['worker_number']:'';
  419. $order_info['master_worker']['mobile'] = !empty($worker)?$worker['mobile']:'';
  420. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  421. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  422. }
  423. //搜索当前工单下的所有订单记录
  424. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status ,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_price')->order('id asc')->select()->toArray();
  425. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  426. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  427. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  428. $coupon_price = 0;
  429. foreach ($order_info['pay_orders'] as $k=>&$v){
  430. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  431. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  432. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  433. if($v['refund_status']==1){
  434. $order_refund_amount += $v['order_amount'];
  435. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  436. }
  437. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  438. $v['order_amount_total'] = $v['order_amount'];
  439. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'];
  440. }
  441. $coupon_price += $v['coupon_price'];
  442. }
  443. //汇总优惠卷额度
  444. $order_info['coupon_price'] = $coupon_price;
  445. //退款汇总
  446. $order_info['refund_amount'] = $order_refund_amount;
  447. if(isset($refund_status)){
  448. switch ($refund_status){
  449. case 0:
  450. $order_info['refund_status'] = '退款中';
  451. break;
  452. case 1:
  453. $order_info['refund_status'] = '退款成功';
  454. break;
  455. case 2:
  456. $order_info['refund_status'] = '退款失败';
  457. break;
  458. }
  459. }else{
  460. $order_info['refund_status'] = '未退款';
  461. }
  462. // 所有配件
  463. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  464. $order_info['spare_parts'] = [];
  465. if($order_info['service_work']['service_work_spare_id']){
  466. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  467. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  468. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  469. ->select()
  470. ->toArray();
  471. $spare_parts = array_column($spare_parts,null,'id');
  472. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  473. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  474. }
  475. $order_info['spare_parts'] = array_values($spare_parts)??[];
  476. }
  477. //获取所有的改约记录
  478. $order_info['appoint_list'] = [];
  479. $appoint_log = ServiceWorkAppointmentLog::where('work_id',$order_info['work_id'])->order('id desc')->select()->toArray();
  480. if(!empty($appoint_log)){
  481. $order_info['appoint_list'][0]['appointment_time'] = date('Y-m-d H:i:s',$appoint_log[0]['this_appointment_time']);
  482. foreach ($appoint_log as $k1=>$v1){
  483. $order_info['appoint_list'][$k1+1]['appointment_time'] = date('Y-m-d H:i:s',$v1['last_appointment_time']);
  484. }
  485. }
  486. //获取是否是活动工单
  487. if(!empty($order_info['service_work']['property_activity_id'])){
  488. $propertyActivity = PropertyActivity::findOrEmpty($order_info['service_work']['property_activity_id']);
  489. $order_info['service_work']['url_page'] = rawurlencode($params['domain'].'/static/wxapp/H5/'.$propertyActivity['url_page'].'/index.html?property_activity_id='.$propertyActivity['id'].'&token='.$params['user_info']['token']);
  490. }
  491. return $order_info;
  492. }
  493. catch (\Exception $e) {
  494. self::setError($e->getMessage());
  495. return false;
  496. }
  497. }
  498. public static function getDetailStatus($params)
  499. {
  500. try {
  501. $order_info = \app\common\model\recharge\RechargeOrder::with(['service_work'=>function ($query) {
  502. $query->visible(['work_sn'])->append(['service_status_text','user_service_status','user_service_status_text']);
  503. }])
  504. ->visible(['id','sn'])
  505. ->where([
  506. 'order_type' => 0,
  507. 'user_id' => $params['user_id'],
  508. 'sn'=>$params['sn']
  509. ])->findOrEmpty()->toArray();
  510. if(empty($order_info)){
  511. throw new Exception('订单不存在');
  512. }
  513. return $order_info;
  514. }
  515. catch (\Exception $e) {
  516. self::setError($e->getMessage());
  517. return false;
  518. }
  519. }
  520. /**
  521. * 获取订单支付详情
  522. * @param $params
  523. * @return array|false
  524. */
  525. public static function orderPayInfo($params)
  526. {
  527. Db::startTrans();
  528. try {
  529. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  530. $query->visible(['goods_id','goods_payment_type','goods_category_id']);
  531. },'service_work'=>function ($query) {
  532. $query->visible(['service_fee','spare_total','service_work_spare_id','goods_category_id']);
  533. }])
  534. ->visible(['id','pay_status','sn','work_id','coupon_id','coupon_price'])
  535. ->where([
  536. 'order_type' => 0,
  537. 'user_id' => $params['user_id'],
  538. 'sn'=>$params['sn']
  539. ])->findOrEmpty()->toArray();
  540. if(empty($order_info)){
  541. throw new Exception('订单不存在');
  542. }
  543. //查询总价
  544. $order_info['order_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('order_amount');
  545. $order_info['paid_amount'] = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id']])->sum('paid_amount');
  546. //搜索当前工单下的所有订单记录
  547. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,refund_status,payment_type,pay_way,pay_time,order_total,order_amount,coupon_id,coupon_price')->order('id asc')->select()->toArray();
  548. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  549. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  550. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  551. $coupon_price = 0;
  552. //退款金额
  553. $order_refund_amount = 0;
  554. $payment_type = 0;
  555. foreach ($order_info['pay_orders'] as $k=>&$v){
  556. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  557. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  558. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  559. if($v['refund_status']==1){
  560. $order_refund_amount += $v['order_amount'];
  561. $refund_status = RefundRecord::where('order_id',$v['order_id'])->value('refund_status');
  562. }
  563. if($v['payment_type']!=1 and !empty($order_info['service_work']['spare_total'])){
  564. $v['order_total'] = $v['order_total'] - $order_info['service_work']['spare_total'];
  565. $v['order_amount'] = $v['order_amount'] - $order_info['service_work']['spare_total'] + $v['coupon_price'];
  566. }
  567. if($v['pay_status']!=0){
  568. $coupon_price += $v['coupon_price'];
  569. }
  570. if($v['pay_status'] == 0){
  571. $order_info['coupon_id'] = $v['coupon_id'];
  572. $order_info['coupon_price'] = $v['coupon_price'];
  573. }
  574. $payment_type = $v['payment_type'];
  575. }
  576. $order_info['payment_type'] = $payment_type;
  577. //汇总优惠卷额度
  578. $order_info['coupon_price'] = $coupon_price;
  579. //退款汇总
  580. $order_info['refund_amount'] = $order_refund_amount;
  581. if(isset($refund_status)){
  582. switch ($refund_status){
  583. case 0:
  584. $order_info['refund_status'] = '退款中';
  585. break;
  586. case 1:
  587. $order_info['refund_status'] = '退款成功';
  588. break;
  589. case 2:
  590. $order_info['refund_status'] = '退款失败';
  591. break;
  592. }
  593. }else{
  594. $order_info['refund_status'] = '未退款';
  595. }
  596. // 所有配件
  597. $order_info['spare_total'] = $order_info['service_work']['spare_total']??0;
  598. $order_info['spare_parts'] = [];
  599. if($order_info['service_work']['service_work_spare_id']){
  600. $work_spare_parts = json_decode(ServiceWorkSpare::where('id',$order_info['service_work']['service_work_spare_id'])->value('spare_parts'),true);
  601. $spare_parts = SparePart::where('id','in',array_column($work_spare_parts,'id'))
  602. ->field(['id', 'goods_category_id', 'spare_name', 'spare_image', 'spare_number', 'spare_unit','spare_status'])
  603. ->select()
  604. ->toArray();
  605. $spare_parts = array_column($spare_parts,null,'id');
  606. foreach (array_column($work_spare_parts,null,'id') as $k=>&$v){
  607. $spare_parts[$k] = array_merge($spare_parts[$k],$v);
  608. }
  609. $order_info['spare_parts'] = array_values($spare_parts)??[];
  610. }
  611. //获取是否存在可使用优惠券
  612. if(!empty($order_info['coupon_id'])){
  613. $order_info['order_amount'] = $order_info['order_amount'] + $order_info['coupon_price'];
  614. $order_info['coupon'] = UserCoupon::where('id',$order_info['coupon_id'])->findOrEmpty()->toArray();
  615. $usable_coupon = true;
  616. }else{
  617. if(!empty($order_info['order_goods'][0])){
  618. $usable_coupon = UserCouponLogic::categoryWithAmountLists(['user_id'=>$params['user_id'],'amount'=>$order_info['order_amount'],'goods_category_id'=>$order_info['order_goods'][0]['goods_category_id'],'goods_id'=>$order_info['order_goods'][0]['goods_id']]);
  619. if(!empty($usable_coupon[0]['id'])){
  620. //绑定最新可使用优惠券
  621. $final_order = \app\common\model\recharge\RechargeOrder::where(['order_type'=>0,'user_id'=>$params['user_id'],'work_id'=>$order_info['work_id'],'pay_status'=>0])->findOrEmpty();
  622. if(!empty($final_order)){
  623. $user_coupon = UserCoupon::where('id',$usable_coupon[0]['id'])->findOrEmpty();
  624. $user_coupon->voucher_status = 1;
  625. $user_coupon->voucher_count = $user_coupon->voucher_count-1;
  626. $user_coupon->save();
  627. $final_order->coupon_id = $usable_coupon[0]['id'];
  628. $final_order->coupon_price = $usable_coupon[0]['amount'];
  629. $final_order->order_amount = $final_order->order_amount - $usable_coupon[0]['amount'];
  630. $final_order->save();
  631. $order_info['coupon'] = $usable_coupon[0];
  632. }
  633. }
  634. }
  635. }
  636. $order_info['usable_coupon'] = !empty($usable_coupon)?true:false;
  637. Db::commit();
  638. return $order_info;
  639. } catch (\Exception $e) {
  640. Db::rollback();
  641. self::setError($e->getMessage());
  642. return false;
  643. }
  644. }
  645. /**
  646. * 取消订单
  647. * @param $params
  648. * @return false|void
  649. */
  650. public static function cancelOrder($params)
  651. {
  652. Db::startTrans();
  653. try {
  654. $work_id = \app\common\model\recharge\RechargeOrder::where([
  655. 'order_type' => 0,
  656. 'user_id' => $params['user_id'],
  657. 'sn'=>$params['sn']
  658. ])->value('work_id');
  659. if(empty($work_id)){
  660. throw new Exception('订单不存在');
  661. }
  662. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  663. if(!$payed_order->isEmpty()){
  664. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  665. }
  666. //软删除订单
  667. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  668. $cancel_order->pay_status = 2;
  669. $cancel_order->save();
  670. //更新工单状态为已取消
  671. $service_work = ServiceWork::find($work_id);
  672. $service_work->service_status = 4;
  673. $service_work->save();
  674. //判断如果存在优惠券,返还优惠券
  675. if(!empty($payed_order->coupon_id)){
  676. $user_coupon = UserCoupon::where(['user_id'=>$payed_order->user_id,'id'=>$payed_order->coupon_id])->findOrEmpty();
  677. $user_coupon->voucher_status = 0;
  678. $user_coupon->voucher_count = $user_coupon->voucher_count+1;
  679. $user_coupon->save();
  680. }
  681. Db::commit();
  682. if($cancel_order->pay_time){
  683. // 订单取消通知【给用户】
  684. event('Notice', [
  685. 'scene_id' => 122,
  686. 'params' => [
  687. 'user_id' => $params['user_id']
  688. ]
  689. ]);
  690. }
  691. }
  692. catch (\Exception $e) {
  693. Db::rollback();
  694. self::setError($e->getMessage());
  695. return false;
  696. }
  697. }
  698. /**
  699. * 用户确认尾款报价单
  700. * @param $params
  701. * @return false|void
  702. */
  703. public static function confirmOrder($params)
  704. {
  705. Db::startTrans();
  706. try {
  707. $work_id = \app\common\model\recharge\RechargeOrder::where([
  708. 'order_type' => 0,
  709. 'user_id' => $params['user_id'],
  710. 'sn'=>$params['sn']
  711. ])->value('work_id');
  712. if(empty($work_id)){
  713. throw new Exception('订单不存在');
  714. }
  715. //更新工单状态为已取消
  716. $service_work = ServiceWork::find($work_id);
  717. if($service_work->user_confirm_status==2){
  718. throw new Exception('请勿重复操作');
  719. }
  720. $service_work->work_status = 5;
  721. $service_work->user_confirm_status = 2;
  722. $service_work->save();
  723. $work_log = [
  724. 'work_id'=>$work_id,
  725. 'master_worker_id'=>$service_work->master_worker_id,
  726. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  727. ];
  728. ServiceWorkLogLogic::add($work_log);
  729. Db::commit();
  730. }
  731. catch (\Exception $e) {
  732. Db::rollback();
  733. self::setError($e->getMessage());
  734. return false;
  735. }
  736. }
  737. /**
  738. * 用户确认服务完成
  739. * @param $params
  740. * @return false|void
  741. */
  742. public static function confirmServiceFinish($params)
  743. {
  744. Db::startTrans();
  745. try {
  746. $work_id = \app\common\model\recharge\RechargeOrder::where([
  747. 'order_type' => 0,
  748. 'user_id' => $params['user_id'],
  749. 'sn'=>$params['sn']
  750. ])->value('work_id');
  751. if(empty($work_id)){
  752. throw new Exception('订单不存在');
  753. }
  754. $service_work = ServiceWork::find($work_id);
  755. if($service_work->user_confirm_status!=3){
  756. throw new Exception('请勿重复操作');
  757. }
  758. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  759. //确认所有订单总金额和结算金额
  760. //若订单是全款已支付订单
  761. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  762. $service_work->work_status = 7;
  763. $service_work->user_confirm_status = 5;
  764. $service_work->service_status = 3;
  765. $service_work->work_pay_status = 1;
  766. }else{
  767. $service_work->work_status = 6;
  768. $service_work->user_confirm_status = 4;
  769. }
  770. $service_work->finished_time = time();
  771. $service_work->save();
  772. //更新工程师的进行工单数量
  773. MasterWorker::setWorktotal('dec',$service_work->master_worker_id);
  774. $work_log = [
  775. 'work_id'=>$work_id,
  776. 'master_worker_id'=>$service_work->master_worker_id,
  777. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  778. ];
  779. ServiceWorkLogLogic::add($work_log);
  780. Db::commit();
  781. }
  782. catch (\Exception $e) {
  783. Db::rollback();
  784. self::setError($e->getMessage());
  785. return false;
  786. }
  787. }
  788. public static function approvalChangePrice($params)
  789. {
  790. Db::startTrans();
  791. try {
  792. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  793. if($order->isEmpty()){
  794. throw new Exception('订单不存在');
  795. }
  796. $work = ServiceWork::findOrEmpty($order->work_id);
  797. if($work->isEmpty()){
  798. throw new Exception('工单不存在');
  799. }
  800. $work->work_status = 4;
  801. $work->user_confirm_status = 0;
  802. $work->price_approval = 1;
  803. $work->save();
  804. Db::commit();
  805. }
  806. catch (\Exception $e) {
  807. Db::rollback();
  808. self::setError($e->getMessage());
  809. return false;
  810. }
  811. }
  812. public static function approvalChangeAppointment($params)
  813. {
  814. Db::startTrans();
  815. try {
  816. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  817. if($order->isEmpty()){
  818. throw new Exception('订单不存在');
  819. }
  820. $work = ServiceWork::findOrEmpty($order->work_id);
  821. if($work->isEmpty()){
  822. throw new Exception('工单不存在');
  823. }
  824. //更新工单未确认上门的状态
  825. $work->work_status = 3;
  826. $work->user_confirm_status = 0;
  827. ServiceWorkAppointmentLog::create([
  828. 'work_id'=>$work->id,
  829. 'last_appointment_time'=>strtotime($work->appointment_time),
  830. 'this_appointment_time'=>strtotime($params['appointment_time']),
  831. ]);
  832. $work->appointment_time = strtotime($params['appointment_time']);
  833. $work->appoint_approval = 1;
  834. $work->save();
  835. Db::commit();
  836. }
  837. catch (\Exception $e) {
  838. Db::rollback();
  839. self::setError($e->getMessage());
  840. return false;
  841. }
  842. return $work;
  843. }
  844. public static function approvalRefund($params)
  845. {
  846. Db::startTrans();
  847. try {
  848. $order = RechargeOrder::where('sn',$params['sn'])->findOrEmpty();
  849. if($order->isEmpty()){
  850. throw new Exception('订单不存在');
  851. }
  852. $work = ServiceWork::findOrEmpty($order->work_id);
  853. if($work->isEmpty()){
  854. throw new Exception('工单不存在');
  855. }
  856. $work->refund_approval = 1;
  857. $work->save();
  858. //判断是否已下单时间过了两小时,并且师傅暂未上门,距离预约时间两小时以上
  859. if(($order['pay_time']+7200)>time() and $work['work_status']<4 and (strtotime($work['appointment_time'])-7200)>time()){
  860. //生成退款订单
  861. \app\common\model\recharge\RechargeOrder::update([
  862. 'id' => $order['id'],
  863. 'refund_status' => YesNoEnum::YES,
  864. ]);
  865. // 生成退款记录
  866. $recordSn = generate_sn(RefundRecord::class, 'sn');
  867. $record = RefundRecord::create([
  868. 'sn' => $recordSn,
  869. 'user_id' => $order['user_id'],
  870. 'order_id' => $order['id'],
  871. 'order_sn' => $order['sn'],
  872. 'order_type' => RefundEnum::ORDER_TYPE_ORDER,
  873. 'order_amount' => $order['order_amount'],
  874. 'refund_amount' => $order['order_amount'],
  875. 'refund_type' => RefundEnum::TYPE_ADMIN,
  876. 'transaction_id' => $order['transaction_id'] ?? '',
  877. 'refund_way' => RefundEnum::getRefundWayByPayWay($order['pay_way']),
  878. ]);
  879. //更新工单状态
  880. ServiceWork::update([
  881. 'id'=>$order['work_id'],
  882. 'service_status'=>5
  883. ]);
  884. // 退款
  885. RefundLogic::refund($order, $record['id'], $order['order_amount'], 1);
  886. }
  887. Db::commit();
  888. }
  889. catch (\Exception $e) {
  890. Db::rollback();
  891. self::setError($e->getMessage());
  892. return false;
  893. }
  894. }
  895. /**
  896. * 订单完成通知【给用户】 - 全款 -通知
  897. * @param $params
  898. * @return bool
  899. */
  900. public static function serviceFinishNotice($params)
  901. {
  902. try {
  903. $order = RechargeOrder::where('sn', $params['sn'])
  904. ->where('payment_type','=',0)
  905. ->where('pay_status','=',1)
  906. ->findOrEmpty();
  907. if(!$order->isEmpty()){
  908. event('Notice', [
  909. 'scene_id' => 120,
  910. 'params' => [
  911. 'user_id' => $order['user_id']
  912. ]
  913. ]);
  914. }
  915. return true;
  916. }catch (\Exception $e) {
  917. return false;
  918. }
  919. }
  920. /**
  921. * 工程师完单的时候设置单量规则 - 通知
  922. * @param $params
  923. * @return bool
  924. */
  925. public static function orderQuantityRule($params)
  926. {
  927. try {
  928. $work_id = \app\common\model\recharge\RechargeOrder::where([
  929. 'order_type' => 0,
  930. 'user_id' => $params['user_id'],
  931. 'sn'=>$params['sn']
  932. ])->value('work_id');
  933. if(empty($work_id)){
  934. throw new Exception('订单不存在');
  935. }
  936. $service_work = ServiceWork::find($work_id);
  937. $commission_config_id = MasterWorkerCommissionConfig::where('voucher_status','=',2)
  938. ->where('master_worker_id', $service_work['master_worker_id'])
  939. ->value('id');
  940. if($commission_config_id){
  941. // 工程师完单的时候设置该规则关闭,以及短信通知工程师
  942. $noticeRule = Db::name('master_worker_commission_notice')->alias('n')
  943. ->leftJoin('service_work sw', 'n.master_worker_id = sw.master_worker_id AND sw.create_time >= n.signing_date AND sw.create_time <= n.signing_date_end')
  944. ->field([
  945. 'n.id', 'n.commission_config_id', 'n.master_worker_id', 'n.day_num', 'n.order_num', 'n.signing_date', 'n.signing_date_end','n.is_notice',
  946. Db::raw("COUNT(sw.id) AS order_count")
  947. ])
  948. ->where('n.commission_config_id', $commission_config_id)
  949. ->group('n.master_worker_id, n.id')
  950. ->select()->toArray();
  951. $is_off = true;
  952. foreach ($noticeRule as $item) {
  953. if($item['order_num'] <= $item['order_count'] && empty($item['is_notice'])){
  954. Log::info('orderQuantityRule:'.json_encode($item));
  955. MasterWorkerCommissionNotice::where('id',$item['id'])->update([
  956. 'is_notice'=>1
  957. ]);
  958. event('Notice', [
  959. 'scene_id' => 128,
  960. 'params' => [
  961. 'user_id' => $service_work['master_worker_id'],
  962. 'number1' => $item['day_num'],
  963. 'number' => $item['order_num']
  964. ]
  965. ]);
  966. }
  967. if($item['order_num'] > $item['order_count']){
  968. $is_off = false;
  969. }
  970. }
  971. if($is_off){
  972. MasterWorkerCommissionConfig::where('id',$commission_config_id)->update([
  973. 'voucher_status'=>1
  974. ]);
  975. }
  976. }
  977. return true;
  978. }catch (\Exception $e) {
  979. Log::info('orderQuantityRule:'.$e->getMessage());
  980. return false;
  981. }
  982. }
  983. /**
  984. * 提交订单
  985. * @param array $params
  986. * @return array|false
  987. */
  988. public static function firmSubmitOrder($params)
  989. {
  990. Db::startTrans();
  991. try {
  992. $goods = Goods::findOrEmpty($params['goods_id']);
  993. if($goods->isEmpty()){
  994. throw new Exception('产品不存在!');
  995. }
  996. if(empty($params['user_info']['mobile'])){
  997. throw new Exception('请先补充您的联系方式后在提交订单');
  998. }
  999. //根据服务工单计算当前订单应支付金额
  1000. if($goods['goods_payment_type'] == GoodsEnum::ISGOODS_PAYMENT_TYPE){
  1001. //一口价订单
  1002. $order_total = $goods['service_fee'];
  1003. $order_amount = $goods['service_fee'];
  1004. }else if ($goods['goods_payment_type'] == GoodsEnum::DEP_GOODS_PAYMENT_TYPE){
  1005. $order_total = $goods['service_fee'];
  1006. $order_amount = $goods['service_fee'];
  1007. }
  1008. else{
  1009. $order_total = $goods['base_service_fee'];
  1010. $order_amount = $goods['service_fee'];
  1011. }
  1012. //生成服务工单
  1013. $work_data = [
  1014. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  1015. 'real_name' => $params['contact_people'],
  1016. 'mobile' => $params['contact_number'],
  1017. 'address' => $params['address'],
  1018. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  1019. 'category_type' => $goods['category_type'],
  1020. 'goods_category_ids' => $goods['goods_category_ids'],
  1021. 'goods_category_id' => $goods['goods_category_id'],
  1022. 'base_service_fee' => $goods['base_service_fee'],
  1023. 'service_fee' => $goods['service_fee'],
  1024. 'work_pay_status'=>WorkEnum::UN_PAY_STATUS,
  1025. 'work_type'=> 1,
  1026. 'appointment_time' => strtotime($params['appointment_time']),
  1027. 'user_id'=>$params['user_id'],
  1028. 'lon'=>!empty($params['lon'])?$params['lon']:0,
  1029. 'lat'=>!empty($params['lat'])?$params['lat']:0,
  1030. ];
  1031. $service_work = ServiceWork::create($work_data);
  1032. //生成服务订单
  1033. $data = [
  1034. 'work_id'=> $service_work['id'],
  1035. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  1036. 'order_type'=>0,//服务订单
  1037. 'order_terminal' => $params['terminal'],
  1038. 'payment_type'=>$goods['goods_payment_type']==GoodsEnum::ISGOODS_PAYMENT_TYPE?0:1,
  1039. 'user_id' => $params['user_id'],
  1040. 'pay_status' => PayEnum::UNPAID,
  1041. 'coupon_id'=>!empty($params['coupon_id'])?$params['coupon_id']:0,
  1042. 'coupon_price'=>!empty($order_coupon_amount)?$order_coupon_amount:0,
  1043. 'pay_way' => $params['pay_way'],
  1044. 'order_total' => $order_total,
  1045. 'order_amount' => $order_amount,
  1046. ];
  1047. $order = RechargeOrder::create($data);
  1048. //生成订单服务详情
  1049. OrderGoods::create([
  1050. 'sn' => $order['sn'],
  1051. 'goods_id' => $params['goods_id'],
  1052. 'category_type' => $goods['category_type'],
  1053. 'goods_category_ids' => $goods['goods_category_ids'],
  1054. 'goods_category_id' => $goods['goods_category_id'],
  1055. 'goods_name' => $goods['goods_name'],
  1056. 'goods_image' => $goods['goods_image'],
  1057. 'goods_video' => $goods['goods_video'],
  1058. 'goods_number' => $goods['goods_number'],
  1059. 'good_unit' => $goods['good_unit'],
  1060. 'goods_size' => $goods['goods_size'],
  1061. 'goods_type' => $goods['goods_type'],
  1062. 'goods_brand' => $goods['goods_brand'],
  1063. 'install_guide' => $goods['install_guide'],
  1064. 'goods_payment_type'=>$goods['goods_payment_type'],
  1065. 'base_service_fee' => $goods['base_service_fee'],
  1066. 'service_total' => $goods['service_total'],
  1067. 'service_fee' => $goods['service_fee'],
  1068. 'service_image' => $goods['service_image'],
  1069. 'warranty_period'=>$goods['warranty_period'],
  1070. 'fee_schedule' => $goods['fee_schedule'],
  1071. 'goods_status' => $goods['goods_status'],
  1072. ]);
  1073. Db::commit();
  1074. } catch (\Exception $e) {
  1075. Db::rollback();
  1076. self::setError($e->getMessage());
  1077. return false;
  1078. }
  1079. return [
  1080. 'order_id' => (int)$order['id'],
  1081. 'work_id' => (int)$order['work_id'],
  1082. ];
  1083. }
  1084. /**
  1085. * 绑定工程师-设置为加单
  1086. * * @param array $params
  1087. * * @return array|false
  1088. */
  1089. public static function bindWorkerAndWork(array $params)
  1090. {
  1091. Db::startTrans();
  1092. try {
  1093. $order = RechargeOrder::where(['sn'=>$params['sn'],'user_id'=>$params['user_id']])->findOrEmpty();
  1094. if($order->isEmpty()){
  1095. throw new Exception('订单不存在');
  1096. }
  1097. $work = ServiceWork::where(['id'=>$order['work_id']])->findOrEmpty();
  1098. if($work->isEmpty()){
  1099. throw new Exception('工单不存在');
  1100. }
  1101. if($work['work_status']!=WorkEnum::WORK_STATUS_WAIT_SERVICE){
  1102. throw new Exception('工单状态不正确,无法绑定');
  1103. }
  1104. $worker = MasterWorker::where('id',$params['worker_id'])->findOrEmpty();
  1105. if($worker->isEmpty()){
  1106. throw new Exception('工程师不存在');
  1107. }
  1108. $work->master_worker_id = $params['worker_id'];
  1109. $work->work_status = WorkEnum::WORK_STATUS_WORKER;
  1110. $work->receive_time = time();
  1111. $work->work_type = 2;//加单状态
  1112. $work->save();
  1113. Db::commit();
  1114. } catch (\Exception $e) {
  1115. Db::rollback();
  1116. self::setError($e->getMessage());
  1117. return false;
  1118. }
  1119. }
  1120. /**
  1121. * 0元支付直接完成支付
  1122. * @param array $params attach out_trade_no
  1123. * @return bool|void
  1124. */
  1125. public static function zeroDirectlyPayment(array $params, $extra = [])
  1126. {
  1127. try {
  1128. $message['out_trade_no'] = mb_substr($params['out_trade_no'], 0, 18);
  1129. $order = RechargeOrder::where(['sn' => $params['out_trade_no']])->findOrEmpty();
  1130. if($order->isEmpty() || $order->pay_status == PayEnum::ISPAID) {
  1131. return true;
  1132. }
  1133. switch ($params['attach']) {
  1134. case 'recharge':
  1135. PayNotifyLogic::handle('recharge', $params['out_trade_no'], $extra);
  1136. break;
  1137. case 'goods':
  1138. $res = PayNotifyLogic::handle('goods', $params['out_trade_no'], $extra);
  1139. if($res === true){
  1140. // 用户下单后,给订单运营专员(配置固定ID)发送公众号提醒(订单信息)
  1141. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1142. ->where('payment_type','IN',[0,1])
  1143. ->where('pay_status','=',1)
  1144. ->findOrEmpty();
  1145. if(!$order->isEmpty()){
  1146. $workDetail = ServiceWork::findOrEmpty($order->work_id);
  1147. if(!$workDetail->isEmpty()){
  1148. event('Notice', [
  1149. 'scene_id' => 100,
  1150. 'params' => [
  1151. 'user_id' => 0,
  1152. 'order_id' => $workDetail['id'],
  1153. 'thing3' => $workDetail['title'],
  1154. 'time6' => $workDetail['appointment_time'],
  1155. 'phone_number8' => asteriskString($workDetail['mobile']),
  1156. 'thing5' => (iconv_strlen($workDetail['address'])>15)?(mb_substr($workDetail['address'],0,15,'UTF-8').'...'):$workDetail['address'],
  1157. ]
  1158. ]);
  1159. }
  1160. }
  1161. // 订单完成通知【给用户】 - 尾款 -通知
  1162. $order = RechargeOrder::where('sn', $message['out_trade_no'])
  1163. ->where('payment_type','=',2)
  1164. ->where('pay_status','=',1)
  1165. ->findOrEmpty();
  1166. if(!$order->isEmpty()){
  1167. event('Notice', [
  1168. 'scene_id' => 120,
  1169. 'params' => [
  1170. 'user_id' => $order['user_id']
  1171. ]
  1172. ]);
  1173. }
  1174. }
  1175. break;
  1176. }
  1177. return true;
  1178. } catch (\Exception $e) {
  1179. self::setError($e->getMessage());
  1180. return false;
  1181. }
  1182. }
  1183. }