Wlps.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use app\common\util\AppMsg;
  5. class Wlps extends Base
  6. {
  7. private $orgId=103;
  8. public function deal_wlps($v){
  9. $cate = 25;
  10. $orgId = 103;
  11. $dep = 798;
  12. $userId = 3401;
  13. if(!$v['s_bmid'] || !$v['department_no']){
  14. Db::connect('db_config_jili')->name('wlps')->where('id',$v['id'])->update(['is_deal'=>2,'reason'=>'发起部门编号或病区编码为空']);
  15. $this->error = '发起部门编号或病区编码为空';
  16. return false;
  17. }
  18. $start = Db::name('address')->where('del',0)->where('org_id',$orgId)->where('','exp',Db::raw("FIND_IN_SET(2,types)"))->where('third_sn',$v['s_bmid'])->find();
  19. $end = Db::name('address')->where('del',0)->where('org_id',$orgId)->where('','exp',Db::raw("FIND_IN_SET(2,types)"))->where('third_sn',$v['department_no'])->find();
  20. if(!$start || !$end){
  21. Db::connect('db_config_jili')->name('wlps')->where('id',$v['id'])->update(['is_deal'=>2,'reason'=>'未查找到相关联的发起部门编号或病区编码']);
  22. $this->error = '未查找到相关联的发起部门编号或病区编码';
  23. return false;
  24. }
  25. $this->startTrans();
  26. try{
  27. $content = '';
  28. if($v['kdg_id']){
  29. $content .= '快递柜编号:'.$v['kdg_id'].';';
  30. }
  31. if($v['qjm']){
  32. $content .= '取件码:'.$v['qjm'].';';
  33. }
  34. if($v['psx_id']){
  35. $content .= '配送箱编号:'.$v['psx_id'].';';
  36. }
  37. if($v['z_num']){
  38. $content .= '总数量:'.$v['z_num'].';';
  39. }
  40. if($v['tpn_num'] > 0){
  41. $content .= $v['tpn_name'].':'.$v['tpn_num'].';';
  42. }
  43. if($v['pt_num'] > 0){
  44. $content .= $v['pt_name'].':'.$v['pt_num'].';';
  45. }
  46. if($v['kss_num'] > 0){
  47. $content .= $v['kss_name'].':'.$v['kss_num'].';';
  48. }
  49. if($v['hl_num'] > 0){
  50. $content .= $v['hl_name'].':'.$v['hl_num'].';';
  51. }
  52. if($v['qjmsg_desc']){
  53. $content .= $v['qjmsg_desc'];
  54. }
  55. // 检查是否设置路线
  56. $auto = Db::name('auto_user')
  57. ->where('org_id',$orgId)
  58. ->where('start',$start['id'])
  59. ->where('end',$end['id'])
  60. ->where('enable',1)
  61. ->find();
  62. $data = [
  63. 'org_id' => $orgId,
  64. 'dep_id' => $dep,
  65. 'order_mode' => $auto?4:1,
  66. 'content' => $content,
  67. 'sn' => get_unique_sn(get_config('sn_prefix')),
  68. 'user_id' => $userId,
  69. 'create_time' => date('Y-m-d H:i:s'),
  70. 'create_yyyy' => date('Y'),
  71. 'create_yyyymm' => date('Ym'),
  72. 'create_yyyymmdd' => date('Ymd'),
  73. 'source_type' => 2,
  74. 'work_type_mode' => 3
  75. ];
  76. if($auto){
  77. $data['send_time'] = date('Y-m-d H:i:s');
  78. }
  79. $ret = Db::name('orders')->insertGetId($data);
  80. if (!$ret) {
  81. exception('订单保存失败');
  82. }
  83. $orderId = $ret;
  84. $cd = [
  85. 'order_id' => $orderId,
  86. 'type' => $cate,
  87. 'start' => $start['id'],
  88. 'end' => $end['id'],
  89. 'xq_time' => date('Y-m-d H:i:s'),
  90. 'ywc_time' => date('Y-m-d H:i:s',time() + 30*60),
  91. 'priority' => 2
  92. ];
  93. $res = Db::name('order_convey')->insert($cd);
  94. if (!$res) {
  95. exception('订单保存失败');
  96. }
  97. $wd = [
  98. 'is_deal'=>1,
  99. 'order_id'=>$orderId
  100. ];
  101. if($auto){
  102. $wd['send_time'] = date('Y-m-d H:i:s');
  103. }
  104. $res = Db::connect('db_config_jili')->name('wlps')->where('id',$v['id'])->update($wd);
  105. if (!$res) {
  106. exception('订单保存失败');
  107. }
  108. // 自动派单
  109. if($auto){
  110. //添加工单
  111. $todo = array(
  112. 'order_id' => $orderId,
  113. 'todo_content' => '',
  114. 'org_id' => $orgId,
  115. 'create_time' => date('Y-m-d H:i:s'),
  116. 'sn' => get_unique_sn(get_config('sn_prefix')),
  117. 'todo_mode' => 1, //待处理
  118. 'work_type_mode' => 3,
  119. 'to_user_id' => $auto['user_id']
  120. );
  121. $todores = $this->add_todo($todo);
  122. if(!$todores){
  123. exception('工单添加失败');
  124. }
  125. }
  126. $this->commit();
  127. if($auto){
  128. //极光推送
  129. send_jpush(array($auto['user_id']),AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$todores]);
  130. }
  131. return true;
  132. }catch (\Exception $e){
  133. $this->rollback();
  134. $this->error = $e->getMessage();
  135. return false;
  136. }
  137. }
  138. public function add_todo($data){
  139. try{
  140. //TODO::检查是否存在重复工单
  141. $data['create_yyyy'] = date('Y',strtotime($data['create_time']));
  142. $data['create_yyyymm'] = date('Ym',strtotime($data['create_time']));
  143. $data['create_yyyymmdd'] = date('Ymd',strtotime($data['create_time']));
  144. $todoid = Db::name('todo')->insertGetId($data);
  145. if(!$todoid){
  146. exception('调度日志保存失败');
  147. }
  148. // 添加调度记录
  149. $dis = array(
  150. 'user_id' => 3401,
  151. 'create_time' => $data['create_time'],
  152. 'content' => $data['todo_content'],
  153. 'order_id' => $data['order_id']
  154. );
  155. $dlid=Db::name('dispatch_log')->insert($dis);
  156. if(!$dlid){
  157. exception('调度日志保存失败');
  158. }
  159. // 添加任务到任务表
  160. $taskData['user_id'] = $data['to_user_id'];
  161. $taskData['bus_id'] = $todoid;
  162. $taskData['type'] = 1;
  163. $res = Db::name('task')
  164. ->insert($taskData);
  165. if (!$res) {
  166. \exception('执行人:' . $taskData['user_id'] . '任务保存失败');
  167. }
  168. return $todoid;
  169. }catch (\Exception $e){
  170. return false;
  171. }
  172. }
  173. // 更新运送用户信息
  174. public function updateYsUser($userId,$name,$phone,$rolesId,$orgId){
  175. if($this->orgId != $orgId){
  176. return true;
  177. }
  178. // 检查角色是否是运送工人
  179. $roles = Db::name('roles')->where('id',$rolesId)->find();
  180. if(!$roles || ($roles['parent_id'] != 6)){
  181. return true;
  182. }
  183. // 检查用户是否已存在
  184. $ysuser = Db::name('ysuser')->where('user_id',$userId)->find();
  185. if(!$ysuser){
  186. Db::name('ysuser')->insert([
  187. 'user_id' => $userId,
  188. 'name' => $name,
  189. 'phone' => $phone,
  190. 'jluser_id' => 'JL'.$userId
  191. ]);
  192. }else{
  193. Db::name('ysuser')->where('id',$ysuser['id'])->update([
  194. 'name' => $name,
  195. 'phone' => $phone,
  196. ]);
  197. }
  198. return true;
  199. }
  200. // 初始化运送用户信息
  201. public function initYsUser(){
  202. $rolesids = Db::name('roles')
  203. ->where('org_id',$this->orgId)
  204. ->where('enable',1)
  205. ->column('id');
  206. if($rolesids){
  207. $map[] = $this->whereIn('ur.roles_id',$rolesids);
  208. }else{
  209. $map[] = $this->where('ur.roles_id',-1);
  210. }
  211. $map = $map?$map:true;
  212. $users = Db::name('user_roles')
  213. ->alias('ur')
  214. ->field('u.id,u.real_name,u.mobile')
  215. ->join('user as u','u.id = ur.user_id')
  216. ->join('roles as r','ur.roles_id = r.id')
  217. ->where('u.enable',1)
  218. ->where('u.del',0)
  219. ->where($map)
  220. ->select();
  221. $users = $users?$users:[];
  222. foreach ($users as $k=>$v){
  223. // 检查用户是否已存在
  224. $ysuser = Db::name('ysuser')->where('user_id',$v['user_id'])->find();
  225. if(!$ysuser){
  226. Db::name('ysuser')->insert([
  227. 'user_id' => $v['user_id'],
  228. 'name' => $v['real_name'],
  229. 'phone' => $v['mobile'],
  230. 'jluser_id' => 'JL'.$v['user_id']
  231. ]);
  232. }else{
  233. Db::name('ysuser')->where('id',$ysuser['id'])->update([
  234. 'name' => $v['real_name'],
  235. 'phone' => $v['mobile'],
  236. ]);
  237. }
  238. }
  239. return true;
  240. }
  241. }