Wlps.php 8.5 KB

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