ServiceOrderLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. <?php
  2. namespace app\api\logic;
  3. use app\common\enum\GoodsEnum;
  4. use app\common\enum\PayEnum;
  5. use app\common\enum\WorkEnum;
  6. use app\common\logic\BaseLogic;
  7. use app\common\model\dict\DictData;
  8. use app\common\model\goods\Goods;
  9. use app\common\model\master_worker\MasterWorker;
  10. use app\common\model\orders\RechargeOrder;
  11. use app\common\model\performance\PerformanceRules;
  12. use app\common\model\recharge\OrderGoods;
  13. use app\common\model\works\ServiceWork;
  14. use app\workerapi\logic\ServiceWorkLogLogic;
  15. use think\Exception;
  16. use think\facade\Db;
  17. /**
  18. * 订单逻辑层
  19. * Class ServiceOrderLogic
  20. * @package app\api\logic
  21. */
  22. class ServiceOrderLogic extends BaseLogic
  23. {
  24. public function setSearch(): array
  25. {
  26. return [
  27. '=' => ['service_status'],
  28. ];
  29. }
  30. /**
  31. * 提交订单
  32. * @param array $params
  33. * @return array|false
  34. */
  35. public static function submitOrder($params)
  36. {
  37. Db::startTrans();
  38. try {
  39. $goods = Goods::findOrEmpty($params['goods_id']);
  40. if($goods->isEmpty()){
  41. throw new Exception('产品不存在!');
  42. }
  43. if(empty($params['user_info']['mobile'])){
  44. throw new Exception('请先补充您的联系方式后在提交订单');
  45. }
  46. //根据服务工单计算当前订单应支付金额
  47. $order_total = $goods['service_total'];
  48. if($goods['goods_payment_type'] = GoodsEnum::ISGOODS_PAYMENT_TYPE){
  49. //一口价订单
  50. $order_amount = $goods['service_fee'];
  51. $pay_status = PayEnum::UNPAID;
  52. $work_pay_status = WorkEnum::UN_PAY_STATUS;
  53. }else{
  54. $order_amount = $goods['base_service_fee'];
  55. $pay_status = !empty($order_amount)?PayEnum::UNPAID:PayEnum::ISPAID;
  56. $work_pay_status = !empty($order_amount)?WorkEnum::UN_PAY_STATUS:WorkEnum::IS_PAY_STATUS;
  57. }
  58. //生成服务工单
  59. $work_data = [
  60. 'work_sn' => generate_sn(ServiceWork::class, 'work_sn'),
  61. 'real_name' => !empty($params['user_info']['real_name'])?$params['user_info']['real_name']:$params['user_info']['nickname'],
  62. 'mobile' => $params['user_info']['mobile'],
  63. 'address' => $params['address'],
  64. 'title' => $goods->goods_name . '*' . $goods->goods_number.$goods->good_unit,
  65. 'category_type' => $goods['category_type'],
  66. 'goods_category_ids' => $goods['goods_category_ids'],
  67. 'goods_category_id' => $goods['goods_category_id'],
  68. 'base_service_fee' => $goods['base_service_fee'],
  69. 'service_fee' => $goods['service_fee'],
  70. 'work_pay_status'=>$work_pay_status,
  71. 'appointment_time' => strtotime($params['appointment_time']),
  72. 'user_id'=>$params['user_id']
  73. ];
  74. $service_work = ServiceWork::create($work_data);
  75. //生成服务订单
  76. $data = [
  77. 'work_id'=> $service_work['id'],
  78. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  79. 'order_type'=>0,//服务订单
  80. 'order_terminal' => $params['terminal'],
  81. 'user_id' => $params['user_id'],
  82. 'pay_status' => $pay_status,
  83. 'pay_way' => $params['pay_way'],
  84. 'order_total' => $order_total,
  85. 'order_amount' => $order_amount,
  86. ];
  87. $order = RechargeOrder::create($data);
  88. //生成订单服务详情
  89. OrderGoods::create([
  90. 'sn' => $order['sn'],
  91. 'goods_id' => $params['goods_id'],
  92. 'category_type' => $goods['category_type'],
  93. 'goods_category_ids' => $goods['goods_category_ids'],
  94. 'goods_category_id' => $goods['goods_category_id'],
  95. 'goods_name' => $goods['goods_name'],
  96. 'goods_image' => $goods['goods_image'],
  97. 'goods_video' => $goods['goods_video'],
  98. 'goods_number' => $goods['goods_number'],
  99. 'good_unit' => $goods['good_unit'],
  100. 'goods_size' => $goods['goods_size'],
  101. 'goods_type' => $goods['goods_type'],
  102. 'goods_brand' => $goods['goods_brand'],
  103. 'install_guide' => $goods['install_guide'],
  104. 'goods_payment_type'=>$goods['goods_payment_type'],
  105. 'base_service_fee' => $goods['base_service_fee'],
  106. 'service_total' => $goods['service_total'],
  107. 'service_fee' => $goods['service_fee'],
  108. 'service_image' => $goods['service_image'],
  109. 'warranty_period'=>$goods['warranty_period'],
  110. 'fee_schedule' => $goods['fee_schedule'],
  111. 'goods_status' => $goods['goods_status'],
  112. ]);
  113. Db::commit();
  114. } catch (\Exception $e) {
  115. self::setError($e->getMessage());
  116. return false;
  117. }
  118. return [
  119. 'order_id' => (int)$order['id'],
  120. ];
  121. }
  122. /**
  123. * 获取订单详情
  124. * @param $params
  125. * @return array|false
  126. */
  127. public static function detail($params)
  128. {
  129. try {
  130. $order_info = \app\common\model\recharge\RechargeOrder::with(['order_goods'=>function ($query) {
  131. $query->visible(['goods_name','goods_image','goods_number','good_unit']);
  132. },'service_work'=>function ($query) {
  133. $query->visible(['service_status','appointment_time','address','master_worker_id']);
  134. }])
  135. ->visible(['id','sn','order_total','order_amount','pay_status','create_time','title','work_id'])
  136. ->where([
  137. 'order_type' => 0,
  138. 'user_id' => $params['user_id'],
  139. 'sn'=>$params['sn']
  140. ])->findOrEmpty()->toArray();
  141. $order_info['master_worker'] = [
  142. 'real_name'=>'',
  143. 'worker_number'=>'',
  144. 'mobile'=>'',
  145. 'worker_exp'=>''
  146. ];
  147. if(empty($order_info)){
  148. throw new Exception('订单不存在');
  149. }
  150. //获取师傅参数
  151. if(!empty($order_info['service_work']['master_worker_id'])){
  152. $worker = MasterWorker::find($order_info['service_work']['master_worker_id']);
  153. $order_info['master_worker']['real_name'] = $worker['real_name'];
  154. $order_info['master_worker']['worker_number'] = $worker['worker_number'];
  155. $order_info['master_worker']['mobile'] = $worker['mobile'];
  156. $maintain_exp_type = !empty($worker->worker_register->maintain_exp_type)?$worker->worker_register->maintain_exp_type:'';
  157. $order_info['master_worker']['worker_exp'] = DictData::where(['type_value'=>'worker_exp_type','value'=>$maintain_exp_type])->value('name');
  158. }
  159. //搜索当前工单下的所有订单记录
  160. $order_info['pay_orders'] = \app\common\model\recharge\RechargeOrder::where(['work_id'=>$order_info['work_id']])->field('id as order_id, pay_status,payment_type,pay_way,pay_time,order_amount')->order('id asc')->select()->toArray();
  161. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  162. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  163. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  164. foreach ($order_info['pay_orders'] as $k=>&$v){
  165. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  166. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  167. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  168. }
  169. return $order_info;
  170. }
  171. catch (\Exception $e) {
  172. self::setError($e->getMessage());
  173. return false;
  174. }
  175. }
  176. /**
  177. * 取消订单
  178. * @param $params
  179. * @return false|void
  180. */
  181. public static function cancelOrder($params)
  182. {
  183. Db::startTrans();
  184. try {
  185. $work_id = \app\common\model\recharge\RechargeOrder::where([
  186. 'order_type' => 0,
  187. 'user_id' => $params['user_id'],
  188. 'sn'=>$params['sn']
  189. ])->value('work_id');
  190. if(empty($work_id)){
  191. throw new Exception('订单不存在');
  192. }
  193. $payed_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id,'pay_status'=>1])->findOrEmpty();
  194. if(!$payed_order->isEmpty()){
  195. throw new Exception('存在已支付订单,不允许取消订单,请联系客服');
  196. }
  197. //软删除订单
  198. $cancel_order = \app\common\model\recharge\RechargeOrder::where(['user_id'=>$params['user_id'],'work_id'=>$work_id])->findOrEmpty();
  199. $cancel_order->delete();
  200. //更新工单状态为已取消
  201. $service_work = ServiceWork::find($work_id);
  202. $service_work->service_status = 4;
  203. $service_work->save();
  204. Db::commit();
  205. }
  206. catch (\Exception $e) {
  207. self::setError($e->getMessage());
  208. return false;
  209. }
  210. }
  211. /**
  212. * 用户确认尾款报价单
  213. * @param $params
  214. * @return false|void
  215. */
  216. public static function confirmOrder($params)
  217. {
  218. Db::startTrans();
  219. try {
  220. $work_id = \app\common\model\recharge\RechargeOrder::where([
  221. 'order_type' => 0,
  222. 'user_id' => $params['user_id'],
  223. 'sn'=>$params['sn']
  224. ])->value('work_id');
  225. if(empty($work_id)){
  226. throw new Exception('订单不存在');
  227. }
  228. //更新工单状态为已取消
  229. $service_work = ServiceWork::find($work_id);
  230. if($service_work->user_confirm_status==1){
  231. throw new Exception('请勿重复操作');
  232. }
  233. $service_work->work_status = 5;
  234. $service_work->user_confirm_status = 2;
  235. $service_work->save();
  236. $work_log = [
  237. 'work_id'=>$work_id,
  238. 'master_worker_id'=>$service_work->master_worker_id,
  239. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认了报价单',
  240. ];
  241. ServiceWorkLogLogic::add($work_log);
  242. Db::commit();
  243. }
  244. catch (\Exception $e) {
  245. self::setError($e->getMessage());
  246. return false;
  247. }
  248. }
  249. /**
  250. * 用户确认服务完成
  251. * @param $params
  252. * @return false|void
  253. */
  254. public static function confirmServiceFinish($params)
  255. {
  256. Db::startTrans();
  257. try {
  258. $work_id = \app\common\model\recharge\RechargeOrder::where([
  259. 'order_type' => 0,
  260. 'user_id' => $params['user_id'],
  261. 'sn'=>$params['sn']
  262. ])->value('work_id');
  263. if(empty($work_id)){
  264. throw new Exception('订单不存在');
  265. }
  266. //更新工单状态为已取消
  267. $service_work = ServiceWork::find($work_id);
  268. if($service_work->user_confirm_status!=3){
  269. throw new Exception('请勿重复操作');
  270. }
  271. $orders = RechargeOrder::where(['work_id'=>$work_id,'user_id'=>$params['user_id']])->select()->toArray();
  272. //确认所有订单总金额和结算金额
  273. //若订单是全款已支付订单
  274. if(count($orders)==1 and $orders[0]['payment_type']==0 and $orders[0]['pay_status']==1){
  275. $service_work->work_status = 7;
  276. $service_work->user_confirm_status = 5;
  277. $service_work->work_total = $orders[0]['order_total'];
  278. $service_work->work_amount = $orders[0]['order_amount'];
  279. }else{
  280. $service_work->work_status = 6;
  281. $service_work->user_confirm_status = 4;
  282. $order_total = 0;
  283. foreach ($orders as $k=>$v){
  284. $order_total += $v['order_total'];
  285. }
  286. $service_work->work_total = $order_total;
  287. }
  288. $service_work->save();
  289. $work_log = [
  290. 'work_id'=>$work_id,
  291. 'master_worker_id'=>$service_work->master_worker_id,
  292. 'opera_log'=>'用户'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'确认服务完成',
  293. ];
  294. ServiceWorkLogLogic::add($work_log);
  295. Db::commit();
  296. }
  297. catch (\Exception $e) {
  298. self::setError($e->getMessage());
  299. return false;
  300. }
  301. }
  302. public static function queryEffective($params)
  303. {
  304. Db::startTrans();
  305. try {
  306. $work_id = \app\common\model\recharge\RechargeOrder::where([
  307. 'order_type' => 0,
  308. 'user_id' => $params['user_id'],
  309. 'sn'=>$params['sn']
  310. ])->value('work_id');
  311. if(empty($work_id)){
  312. throw new Exception('订单不存在');
  313. }
  314. return true;
  315. }catch(\Exception $e){
  316. Db::rollback();
  317. self::setError($e->getMessage());
  318. return false;
  319. }
  320. }
  321. }