TodoConvey.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Exception;
  5. class TodoConvey extends Base
  6. {
  7. public function sweepCode($data,$userId){
  8. $todo = Db::name('todo')
  9. ->where('id',$data['todoId'])
  10. ->where('del',0)
  11. ->find();
  12. if(!$todo || $todo['to_user_id'] != $userId){
  13. $this->error = '工单不存在';
  14. return false;
  15. }
  16. $orderConvey = Db::name('order_convey')
  17. ->where('order_id',$todo['order_id'])
  18. ->find();
  19. if(!$orderConvey){
  20. $this->error = '运送信息不存在';
  21. return false;
  22. }
  23. if($orderConvey && (new Config())->getConfig('org_scan_qrcode',$todo['org_id']) == 1){ // 强制扫码
  24. if(!$data['code']){
  25. $this->error = '请扫描二维码';
  26. return false;
  27. }else{
  28. $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
  29. if(!$addrs){
  30. $this->error = '二维码不正确';
  31. return false;
  32. }
  33. if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
  34. $this->error = '二维码不正确';
  35. return false;
  36. }
  37. if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
  38. $this->error = '二维码不正确';
  39. return false;
  40. }
  41. }
  42. if($data['isStart'] == 1 && $todo['work_type_mode'] == 3){
  43. // 判断是不是病人运送
  44. $convey = Db::name('order_convey')
  45. ->alias('a')
  46. ->join('convey_cate b','a.type = b.id')
  47. ->where('a.order_id',$todo['order_id'])
  48. ->where('b.cate',1)
  49. ->field('a.*')
  50. ->find();
  51. if($convey){ // 检查途径路线是否全部已扫码
  52. $ret = Db::name('order_convey_end')->where('order_id',$todo['order_id'])->where('scan',0)->find();
  53. if(!$ret){
  54. $this->error = '存在途经点未扫码,请确认';
  55. return false;
  56. }
  57. }
  58. }
  59. }else{
  60. if($data['code']){
  61. $addrs = model('address')->checkAddrCode($data['code'],2,$todo['org_id']);
  62. if(!$addrs){
  63. $this->error = '二维码不正确';
  64. return false;
  65. }
  66. if($data['isStart'] == 0 && $addrs['id'] != $orderConvey['start']){
  67. $this->error = '二维码不正确';
  68. return false;
  69. }
  70. if($data['isStart'] == 1 && $addrs['id'] != $orderConvey['end']){
  71. $this->error = '二维码不正确';
  72. return false;
  73. }
  74. }
  75. }
  76. $startstatus = 0;
  77. $endstatus = 0;
  78. $todoconvey = Db::name('todo_convey')
  79. ->where('todo_id',$data['todoId'])
  80. ->find();
  81. if($todoconvey){
  82. if($todoconvey['start_time']){
  83. $startstatus = 1;
  84. }
  85. if($todoconvey['end_time']){
  86. $endstatus = 1;
  87. }
  88. }
  89. if($data['isStart'] == 1 && ( $startstatus == 0 || $endstatus == 1)){
  90. $this->error = '无权限操作';
  91. return false;
  92. }
  93. if($data['isStart'] == 0 && ( $startstatus == 1 || $endstatus == 1)){
  94. $this->error = '无权限操作';
  95. return false;
  96. }
  97. $this->startTrans();
  98. try{
  99. if($todoconvey){
  100. if($data['isStart'] == 0){
  101. $d = [
  102. 'start' => $orderConvey['start'],
  103. 'start_time' => date('Y-m-d H:i:s')
  104. ];
  105. $ret = $this
  106. ->where('id',$todoconvey['id'])
  107. ->update($d);
  108. if(!$ret){
  109. \exception('操作失败');
  110. }
  111. }else{
  112. $d = [
  113. 'end' => $orderConvey['end'],
  114. 'end_time' => date('Y-m-d H:i:s')
  115. ];
  116. $ret = $this->where('id',$todoconvey['id'])->update($d);
  117. if(!$ret){
  118. \exception('操作失败');
  119. }
  120. $curTime = date('Y-m-d H:i:s');
  121. $sd = [
  122. 'todo_mode' => 3,
  123. 'images' => $data['img'],
  124. 'done_time' => date('Y-m-d H:i:s'),
  125. 'nodo_reason' => isset($data['content'])?$data['content']:'',
  126. 'wc_time' => strtotime($curTime) - strtotime($todo['confirm_time']),
  127. ];
  128. $ret = Db::name('todo')
  129. ->where('id',$data['todoId'])->update($sd);
  130. if(!$ret){
  131. \exception('操作失败');
  132. }
  133. $ret = (new Todo())->checkToDo($todo['order_id'],$data['todoId'],3);
  134. if(!$ret){
  135. \exception('订单修改失败');
  136. }
  137. }
  138. }else{
  139. $d = [
  140. 'start' => $orderConvey['start'],
  141. 'start_time' => date('Y-m-d H:i:s'),
  142. 'todo_id' => $data['todoId'],
  143. 'end' => $orderConvey['end']
  144. ];
  145. $res = $this->insert($d);
  146. if (!$res) {
  147. \exception('操作失败');
  148. }
  149. }
  150. $crdata = [
  151. 'org_id' => $data['orgId'],
  152. 'user_id' => $userId,
  153. 'create_time' => date('Y-m-d H:i:s'),
  154. 'create_yyyymmdd' => date('Ymd'),
  155. 'create_yyyymm' => date('Ym'),
  156. 'create_yyyy' => date('Y')
  157. ];
  158. if($data['isStart'] == 0){ //始发
  159. $crdata['addr_id'] = $orderConvey['start'];
  160. $crdata['type'] = 1;
  161. }else{
  162. $crdata['addr_id'] = $orderConvey['end'];
  163. $crdata['type'] = 2;
  164. }
  165. //位置签到表
  166. $res = Db::name('convey_plan_record')->insert($crdata);
  167. if (!$res) {
  168. \exception('操作失败');
  169. }
  170. $this->commit();
  171. }catch (Exception $e){
  172. $this->rollback();
  173. $this->error = $e->getMessage();
  174. return false;
  175. }
  176. return true;
  177. }
  178. // 保存签名
  179. public function saveSign($todoId,$type,$img,$userId){
  180. $todo = db('todo')
  181. ->where('id',$todoId)
  182. ->where('del',0)
  183. ->find();
  184. if(!$todo || $todo['to_user_id'] != $userId){
  185. $this->error = '工单不存在';
  186. return false;
  187. }
  188. $orderConvey = db('order_convey')
  189. ->where('order_id',$todo['order_id'])
  190. ->find();
  191. if(!$orderConvey){
  192. $this->error = '工单不存在';
  193. return false;
  194. }
  195. $this->startTrans();
  196. try{
  197. $todoconvey = db('todo_convey')
  198. ->where('todo_id',$todoId)
  199. ->find();
  200. if($todoconvey){
  201. if($type == 1){
  202. $d = [
  203. 'start_img' => $img
  204. ];
  205. }else{
  206. $d = [
  207. 'end_img' => $img
  208. ];
  209. }
  210. $ret = db('todo_convey')
  211. ->where('id',$todoconvey['id'])
  212. ->update($d);
  213. if(!$ret){
  214. \exception('操作失败');
  215. }
  216. }else{
  217. $d = [
  218. 'start' => $orderConvey['start'],
  219. 'todo_id' => $todoId,
  220. 'end' => $orderConvey['end'],
  221. 'start_img' => $type == 1?$img:'',
  222. 'end_img' => $type == 2?$img:'',
  223. ];
  224. $res = db('todo_convey')
  225. ->insert($d);
  226. if (!$res) {
  227. \exception('操作失败');
  228. }
  229. }
  230. $this->commit();
  231. }catch (Exception $e){
  232. $this->rollback();
  233. $this->error = $e->getMessage();
  234. return false;
  235. }
  236. return true;
  237. }
  238. }