Wlps.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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['start_time'] = getTime();
  161. $taskData['create_time'] = getTime();
  162. $taskData['user_id'] = $data['to_user_id'];
  163. $taskData['bus_id'] = $todoid;
  164. $taskData['org_id'] = $this->orgId;
  165. $taskData['type'] = 1;
  166. $res = Db::name('task')
  167. ->insert($taskData);
  168. if (!$res) {
  169. \exception('执行人:' . $taskData['user_id'] . '任务保存失败');
  170. }
  171. return $todoid;
  172. }catch (\Exception $e){
  173. return false;
  174. }
  175. }
  176. // 更新运送用户信息
  177. public function updateYsUser($userId,$name,$phone,$rolesId,$orgId){
  178. if($this->orgId != $orgId){
  179. return true;
  180. }
  181. // 检查角色是否是运送工人
  182. $roles = Db::name('roles')->where('id',$rolesId)->find();
  183. if(!$roles || ($roles['parent_id'] != 6)){
  184. return true;
  185. }
  186. // 检查用户是否已存在
  187. $ysuser = Db::name('ysuser')->where('user_id',$userId)->find();
  188. if(!$ysuser){
  189. Db::name('ysuser')->insert([
  190. 'user_id' => $userId,
  191. 'name' => $name,
  192. 'phone' => $phone,
  193. 'jluser_id' => 'JL'.$userId
  194. ]);
  195. }else{
  196. Db::name('ysuser')->where('id',$ysuser['id'])->update([
  197. 'name' => $name,
  198. 'phone' => $phone,
  199. ]);
  200. }
  201. return true;
  202. }
  203. // 初始化运送用户信息
  204. public function initYsUser(){
  205. $rolesids = Db::name('roles')
  206. ->where('org_id',$this->orgId)
  207. ->where('enable',1)
  208. ->column('id');
  209. if($rolesids){
  210. $map[] = $this->whereIn('ur.roles_id',$rolesids);
  211. }else{
  212. $map[] = $this->where('ur.roles_id',-1);
  213. }
  214. $map = $map?$map:true;
  215. $users = Db::name('user_roles')
  216. ->alias('ur')
  217. ->field('u.id,u.real_name,u.mobile')
  218. ->join('user as u','u.id = ur.user_id')
  219. ->join('roles as r','ur.roles_id = r.id')
  220. ->where('u.enable',1)
  221. ->where('u.del',0)
  222. ->where($map)
  223. ->select();
  224. $users = $users?$users:[];
  225. foreach ($users as $k=>$v){
  226. // 检查用户是否已存在
  227. $ysuser = Db::name('ysuser')->where('user_id',$v['user_id'])->find();
  228. if(!$ysuser){
  229. Db::name('ysuser')->insert([
  230. 'user_id' => $v['user_id'],
  231. 'name' => $v['real_name'],
  232. 'phone' => $v['mobile'],
  233. 'jluser_id' => 'JL'.$v['user_id']
  234. ]);
  235. }else{
  236. Db::name('ysuser')->where('id',$ysuser['id'])->update([
  237. 'name' => $v['real_name'],
  238. 'phone' => $v['mobile'],
  239. ]);
  240. }
  241. }
  242. return true;
  243. }
  244. }