Wlps.php 8.8 KB

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