ServiceOrderLogic.php 55 KB

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