ServiceWorkLogic.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\adminapi\logic\works;
  15. use app\common\model\dict\DictData;
  16. use app\common\model\master_worker\MasterWorker;
  17. use app\common\model\master_worker\MasterWorkerAccountLog;
  18. use app\common\model\recharge\RechargeOrder;
  19. use app\common\model\works\ServiceWork;
  20. use app\common\logic\BaseLogic;
  21. use app\workerapi\logic\ServiceWorkerAllocateWorkerLogic;
  22. use app\workerapi\logic\ServiceWorkLogLogic;
  23. use think\db\Query;
  24. use think\Exception;
  25. use think\facade\Db;
  26. /**
  27. * ServiceWork逻辑
  28. * Class ServiceWorkLogic
  29. * @package app\adminapi\logic\works
  30. */
  31. class ServiceWorkLogic extends BaseLogic
  32. {
  33. /**
  34. *
  35. * @return false|void
  36. */
  37. public static function pickWork($params)
  38. {
  39. Db::startTrans();
  40. try {
  41. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  42. if($work->isEmpty()){
  43. throw new Exception('工单不存在');
  44. }
  45. $receive_time = time();
  46. $work->work_status = 2;//待联系
  47. $work->service_status = 1;//服务中
  48. $work->receive_time = $receive_time;
  49. $work->save();
  50. //添加变更日志
  51. $work_log = [
  52. 'work_id'=>$work->id,
  53. 'master_worker_id'=>$work->master_worker_id,
  54. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',$receive_time).'领取了工单',
  55. ];
  56. ServiceWorkLogLogic::add($work_log);
  57. Db::commit();
  58. }
  59. catch (\Exception $e) {
  60. Db::rollback();
  61. self::setError($e->getMessage());
  62. return false;
  63. }
  64. }
  65. /**
  66. * 预约成功,等待上门
  67. * @return false|void
  68. */
  69. public static function appointWork($params)
  70. {
  71. Db::startTrans();
  72. try {
  73. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  74. if($work->isEmpty()){
  75. throw new Exception('工单不存在');
  76. }
  77. if($work->work_status != 2){
  78. throw new Exception('请勿重复点击');
  79. }
  80. //验证更改的预约时间必须是在领单时间内的半小内修改,否则不允许修改
  81. if(strtotime($work->appointment_time) != strtotime($params['appointment_time']) && (time()-strtotime($work->receive_time))>1800){
  82. throw new Exception('距离领单时间已超过半小时,无法修改预约时间,请联系客服');
  83. }
  84. $work->work_status = 3;//待上门
  85. $work->appointment_time = strtotime($params['appointment_time']);
  86. $work->save();
  87. //添加变更日志
  88. $work_log = [
  89. 'work_id'=>$work->id,
  90. 'master_worker_id'=>$work->master_worker_id,
  91. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'联系了客户,确认了于'.$params['appointment_time'].$params['address'].'预约上门',
  92. ];
  93. ServiceWorkLogLogic::add($work_log);
  94. Db::commit();
  95. }
  96. catch (\Exception $e) {
  97. self::setError($e->getMessage());
  98. return false;
  99. }
  100. }
  101. /**
  102. * 师傅确认上门
  103. * @param $params
  104. * @return false|void
  105. */
  106. public static function confirmDoor($params)
  107. {
  108. Db::startTrans();
  109. try {
  110. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  111. if($work->isEmpty()){
  112. throw new Exception('工单不存在');
  113. }
  114. $order = RechargeOrder::where(['sn'=>$params['order_sn'],'work_id'=>$work['id']])->findOrEmpty();
  115. if($order->isEmpty()){
  116. throw new Exception('订单不存在');
  117. }
  118. if($work->work_status != 3){
  119. throw new Exception('请勿重复点击');
  120. }
  121. $work->work_status = 4;//已上门
  122. $work->save();
  123. //添加变更日志
  124. $work_log = [
  125. 'work_id'=>$work->id,
  126. 'master_worker_id'=>$work->master_worker_id,
  127. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'已上门',
  128. ];
  129. ServiceWorkLogLogic::add($work_log);
  130. Db::commit();
  131. }
  132. catch (\Exception $e) {
  133. Db::rollback();
  134. self::setError($e->getMessage());
  135. return false;
  136. }
  137. }
  138. /**
  139. * 师傅确认报价单
  140. * @param $params
  141. * @return false|void
  142. */
  143. public static function confirmPrice($params)
  144. {
  145. Db::startTrans();
  146. try {
  147. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  148. if($work->isEmpty()){
  149. throw new Exception('工单不存在');
  150. }
  151. //搜索待支付订单
  152. $paid_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>1])->findOrEmpty()->toArray();
  153. if(empty($paid_order)){
  154. throw new Exception('订单错误');
  155. }
  156. if($work->work_status != 4){
  157. throw new Exception('请勿重复操作');
  158. }
  159. //定金存在尾款结算功能,全款直接提交
  160. if($paid_order['payment_type']==1){
  161. $un_order = RechargeOrder::where(['work_id'=>$work['id'],'pay_status'=>0])->findOrEmpty();
  162. if($un_order->isEmpty()){
  163. //新增待支付尾款
  164. $order_data = [
  165. 'order_type'=>$paid_order['order_type'],
  166. 'sn'=>generate_sn(\app\common\model\orders\RechargeOrder::class, 'sn'),
  167. 'work_id'=>$paid_order['work_id'],
  168. 'user_id'=>$paid_order['user_id'],
  169. 'payment_type'=>2,
  170. 'order_total'=>$params['amount'],
  171. 'order_amount'=>$params['amount'],
  172. 'order_terminal'=>$paid_order['order_terminal']
  173. ];
  174. RechargeOrder::create($order_data);
  175. }else{
  176. //修改尾款信息
  177. $un_order->order_total = $params['amount'];
  178. $un_order->order_amount = $params['amount'];
  179. $un_order->save();
  180. }
  181. }
  182. $work->work_images = $params['work_images'];
  183. $work->user_confirm_status = 1;//待确认报价
  184. $work->save();
  185. //添加变更日志
  186. $work_log = [
  187. 'work_id'=>$work->id,
  188. 'master_worker_id'=>$work->master_worker_id,
  189. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了报价单',
  190. ];
  191. ServiceWorkLogLogic::add($work_log);
  192. Db::commit();
  193. }
  194. catch (\Exception $e) {
  195. Db::rollback();
  196. self::setError($e->getMessage());
  197. return false;
  198. }
  199. }
  200. /**
  201. * 师傅确认服务完成
  202. * @param $params
  203. * @return false|void
  204. */
  205. public static function confirmServiceFinish($params)
  206. {
  207. Db::startTrans();
  208. try {
  209. $work = ServiceWork::where(['master_worker_id'=>$params['user_id'],'work_sn'=>$params['work_sn']])->findOrEmpty();
  210. if($work->isEmpty()){
  211. throw new Exception('工单不存在');
  212. }
  213. if($work->user_confirm_status !=2){
  214. throw new Exception('请勿重复操作');
  215. }
  216. $work->finished_images = $params['finished_images'];
  217. $work->user_confirm_status = 3;//待确认服务完成
  218. $work->save();
  219. //添加变更日志
  220. $work_log = [
  221. 'work_id'=>$work->id,
  222. 'master_worker_id'=>$work->master_worker_id,
  223. 'opera_log'=>'编号['.$params['user_info']['worker_number'].']'.$params['user_info']['real_name'].'于'.date('y-m-d H:i:s',time()).'于'.date('Y-m-d H:i:s',time()).'提交了待用户确认服务完成',
  224. ];
  225. ServiceWorkLogLogic::add($work_log);
  226. Db::commit();
  227. } catch (\Exception $e) {
  228. Db::rollback();
  229. self::setError($e->getMessage());
  230. return false;
  231. }
  232. }
  233. public static function allocateWorker($params,$userInfo){
  234. Db::startTrans();
  235. try {
  236. $work = ServiceWork::findOrEmpty($params['id']);
  237. if($work->isEmpty()){
  238. throw new Exception('工单不存在');
  239. }
  240. if($work->work_status >=6 ){
  241. throw new \Exception('工单状态只能修改待结算之前的');
  242. }
  243. if($work->master_worker_id == $params['master_worker_id']){
  244. throw new \Exception('分配的师傅相同');
  245. }
  246. $worker = MasterWorker::where(['id'=>$params['master_worker_id'],'is_disable' =>0])->findOrEmpty();
  247. if($worker->isEmpty()){
  248. throw new \Exception('师傅不存在或被禁用');
  249. }
  250. if($worker->master_worker_id){
  251. MasterWorker::setWorktotal('dec',$worker->master_worker_id);
  252. }
  253. $work->master_worker_id = $params['master_worker_id'];
  254. $work->work_status = 1;
  255. $work->dispatch_time = time();
  256. MasterWorker::setWorktotal('inc',$params['master_worker_id']);
  257. $work->save();
  258. $work_log = [
  259. 'work_id'=>$work->id,
  260. 'master_worker_id'=>$work->master_worker_id,
  261. 'opera_log'=>'后台用户['.$userInfo['admin_id'].']'.$userInfo['name'].'于'.date('Y-m-d H:i:s',time()).'分配了师傅'.'编号['.$worker->worker_number.']'.$worker->real_name
  262. ];
  263. ServiceWorkerAllocateWorkerLogic::add($work_log);
  264. Db::commit();
  265. return true;
  266. }catch(\Exception $e){
  267. Db::rollback();
  268. self::setError($e->getMessage());
  269. return false;
  270. }
  271. }
  272. /**
  273. * 工单详情
  274. * @param $params
  275. * @return array|false
  276. */
  277. public static function detail($params){
  278. try {
  279. $result = ServiceWork::with([
  280. 'worker'=> function(Query $query) {
  281. $query->field('id,worker_number,real_name');
  282. },
  283. 'allocateWorkerLog' =>function(Query $query){
  284. $query->field('id,work_id,opera_log,create_time');
  285. },
  286. 'serviceWorkLog' =>function(Query $query){
  287. $query->field('id,work_id,opera_log,create_time');
  288. }
  289. ])->append(['id','work_status_text','service_status_text'])
  290. ->findOrEmpty($params['id'])->toArray();
  291. //师傅工单按钮状态
  292. $work_service_status = 0;
  293. $work_service_status_text = '待派单';
  294. //工单状态
  295. if($result['work_status'] == 1){
  296. $work_service_status = 1;
  297. $work_service_status_text = '待领单';
  298. }
  299. if($result['work_status'] == 2){
  300. $work_service_status = 2;
  301. $work_service_status_text = '预约上门';
  302. }
  303. if($result['work_status'] == 3){
  304. $work_service_status = 3;
  305. $work_service_status_text = '等待上门';
  306. if(date('Y-m-d') === date('Y-m-d',strtotime($result['appointment_time']))){
  307. $work_service_status = 4;
  308. $work_service_status_text = '确认上门';
  309. }
  310. }
  311. if($result['work_status'] == 4 and $result['user_confirm_status']==0){
  312. $work_service_status = 5;
  313. $work_service_status_text = '确认报价';
  314. }
  315. if($result['work_status'] == 4 and $result['user_confirm_status']==1){
  316. $work_service_status = 6;
  317. $work_service_status_text = '用户确认报价中';
  318. }
  319. if($result['work_status'] == 5 and $result['user_confirm_status']==2){
  320. $work_service_status = 7;
  321. $work_service_status_text = '完成服务';
  322. }
  323. if($result['work_status'] == 5 and $result['user_confirm_status']==3){
  324. $work_service_status = 8;
  325. $work_service_status_text = '用户确认完成服务中';
  326. }
  327. if($result['work_status'] ==6){
  328. $work_service_status = 9;
  329. $work_service_status_text = '待结算';
  330. }
  331. if($result['work_status'] ==7){
  332. $work_service_status = 10;
  333. $work_service_status_text = '已完结';
  334. }
  335. if($result['work_status'] ==8){
  336. $work_service_status = 11;
  337. $work_service_status_text = '已评价';
  338. }
  339. $result['work_service_status'] = $work_service_status;
  340. $result['work_service_status_text'] = $work_service_status_text;
  341. //搜索当前工单下的所有订单记录
  342. $result['pay_orders'] = RechargeOrder::with(['orderGoods'=>function(Query $query){
  343. $query->field('id,sn,goods_id,goods_name,goods_image,goods_number,good_unit,goods_size,goods_type,goods_brand,base_service_fee,service_total,service_fee')->order(['id'=>'desc']);
  344. }])->where(['work_id'=>$result['id']])->field('id as order_id,sn,order_type,pay_status,payment_type,pay_way,pay_time,order_amount,order_total,coupon_price,create_time')->order('id asc')->select()->toArray();
  345. $pay_status_data = DictData::where('type_value','pay_status')->column('name','value');
  346. $payment_type_data = DictData::where('type_value','payment_type')->column('name','value');
  347. $pay_way_data = DictData::where('type_value','pay_way')->column('name','value');
  348. $order_type_data = DictData::where('type_value','order_type')->column('name','value');
  349. $coupon_price = 0;
  350. foreach ($result['pay_orders'] as $k=>&$v){
  351. $v['pay_status_name'] = $pay_status_data[$v['pay_status']];
  352. $v['payment_type_name'] = $payment_type_data[$v['payment_type']];
  353. $v['pay_way_name'] = $pay_way_data[$v['pay_way']];
  354. $v['order_type_name'] = $order_type_data[$v['order_type']];
  355. $v['pay_time'] = $v['pay_time'] && is_numeric($v['pay_time']) ? date('Y-m-d H:i:s',$v['pay_time']):'';
  356. $coupon_price += $v['coupon_price'];
  357. }
  358. //汇总优惠卷额度
  359. $result['coupon_price'] = $coupon_price;
  360. //工单总支付金额
  361. $result['worker_account'] = $result['work_amount'];
  362. return $result;
  363. }catch(\Exception $e){
  364. Db::rollback();
  365. self::setError($e->getMessage());
  366. return false;
  367. }
  368. }
  369. }