ServiceOrderLogic.php 52 KB

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