ModeCate.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. namespace app\api\controller\v1;
  3. use app\api\controller\Base;
  4. use app\common\util\AppMsg;
  5. use app\hander\HelpHander;
  6. use think\Controller;
  7. use think\Db;
  8. use think\Exception;
  9. class ModeCate extends Base
  10. {
  11. public function list(){
  12. $lists = Db::name('mode_cate')
  13. ->where('del',0)
  14. ->where('enable',1)
  15. ->where('org_id',$this->orgId)
  16. ->order('sort desc,id desc')
  17. ->select();
  18. foreach ($lists as $k=>$v){
  19. if($v['mode'] == 2){
  20. $atable = 'cleaning_type b';
  21. }else{
  22. $atable = 'convey_cate b';
  23. }
  24. $typeList = Db::name('mode_cate_type')
  25. ->alias('a')
  26. ->field('b.id,b.title,b.color')
  27. ->join($atable,'b.id=a.bus_id')
  28. ->where('a.mode',$v['mode'])
  29. ->where('a.cate_id',$v['id'])
  30. ->order('a.sort desc,a.id desc')
  31. ->select();
  32. $lists[$k]['type_list'] = $typeList?$typeList:[];
  33. }
  34. HelpHander::success($lists);
  35. }
  36. public function addOrder(){
  37. $mode = input('mode');
  38. $typeId = input('typeId',0);
  39. if(!in_array($mode,[2,3])){
  40. HelpHander::error('参数错误');
  41. }
  42. if($typeId < 1){
  43. HelpHander::error('参数错误');
  44. }
  45. $addrId = Db::name('user')->where('id',$this->userId)->value('addr_id');
  46. if($addrId < 1){
  47. HelpHander::error('始发空间不能为空');
  48. }
  49. $addrInfo = Db::name('address')->where('id',$addrId)->find();
  50. $bjToUserId = $addrInfo['bj_user_id'] ?$addrInfo['bj_user_id']:0;
  51. $ysToUserId = $addrInfo['ys_user_id'] ?$addrInfo['ys_user_id']:0;
  52. $toUserId = 0;
  53. $content = '';
  54. if($mode == 2){
  55. $content = Db::name('cleaning_type')->where('del',0)->where('id',$typeId)->value('title');
  56. if(!$bjToUserId){
  57. HelpHander::error('保洁责任人不能为空');
  58. }
  59. $toUserId = $bjToUserId;
  60. }elseif($mode == 3){
  61. $conveyCate = Db::name('convey_cate')->where('del',0)->where('is_sss',1)->where('id',$typeId)->find();
  62. if(!$conveyCate){
  63. HelpHander::error('类型不存在');
  64. }
  65. $content = $conveyCate['title'];
  66. if(!$ysToUserId){
  67. HelpHander::error('运送责任人不能为空');
  68. }
  69. $toUserId = $ysToUserId;
  70. if($conveyCate['addr_id'] < 1){
  71. HelpHander::error('地点不能为空');
  72. }
  73. if($addrId == $conveyCate['addr_id']){
  74. HelpHander::error('开始地点和结束地点不能一致');
  75. }
  76. }
  77. // $depId = Db::name('user_dep')
  78. // ->alias('ud')
  79. // ->join('dep d','d.id=ud.dep_id')
  80. // ->where('d.del',0)
  81. // ->where('ud.user_id',$this->userId)
  82. // ->value('d.id');
  83. $odata = [
  84. 'org_id'=>$this->orgId,
  85. 'user_id'=>$this->userId,
  86. 'create_time' => getTime(),
  87. 'create_yyyy' => date('Y'),
  88. 'create_yyyymm' => date('Ym'),
  89. 'create_yyyymmdd' => date('Ymd'),
  90. 'sn' => get_unique_sn(get_config('sn_prefix')),
  91. 'content' => $content,
  92. 'work_type_mode' => $mode,
  93. 'order_mode'=>4,
  94. 'source_type' => 2,
  95. 'dep_id' => 0,
  96. 'send_time' => getTime(),
  97. 'is_deal' => 1,
  98. 'from' => 0,
  99. ];
  100. Db::startTrans();
  101. try {
  102. $orderId = Db::name('orders')->insertGetId($odata);
  103. if(!$orderId){
  104. \exception('订单生成失败');
  105. }
  106. if($mode == 2){
  107. $octData = [
  108. 'order_id'=>$orderId,
  109. 'type_id'=>$typeId,
  110. 'create_time'=>getTime()
  111. ];
  112. $oct = Db::name('order_clean_type')->insert($octData);
  113. if(!$oct){
  114. \exception('订单保洁类型扩展生成失败');
  115. }
  116. }
  117. if($mode == 3){
  118. $conveyCateInfo = Db::name('convey_cate')->where('del',0)->where('id',$typeId)->find();
  119. $time = Db::name('time')->where('del',0)->where('id',$conveyCateInfo['time_id'])->find();
  120. $ywcTime = time() + $time['bz_time']*60;
  121. if($conveyCateInfo['type'] == 0){
  122. $saddr = $addrId;
  123. $eaddr = $conveyCateInfo['addr_id'];
  124. }else{
  125. $saddr = $conveyCateInfo['addr_id'];
  126. $eaddr = $addrId;
  127. }
  128. $ocData = [
  129. 'order_id'=>$orderId,
  130. 'type'=>$typeId,
  131. 'start'=>$saddr,
  132. 'end'=>$eaddr,
  133. 'xq_time'=>getTime(),
  134. 'ywc_time'=>date('Y-m-d H:i:s',$ywcTime),
  135. 'score'=>$conveyCateInfo['score'],
  136. 'priority'=>0,
  137. ];
  138. $ocAdd = Db::name('order_convey')->insert($ocData);
  139. if(!$ocAdd){
  140. \exception('运送扩展生成失败');
  141. }
  142. }
  143. $todoData = [
  144. 'order_id' => $orderId,
  145. 'todo_content' => $content,
  146. 'create_user_id' => $this->userId,
  147. 'org_id' => $this->orgId,
  148. 'create_time' => getTime(),
  149. 'confirm_time' => getTime(),
  150. 'todo_mode' => 2,
  151. 'work_type_mode' => $mode,
  152. 'create_yyyy' => date('Y'),
  153. 'create_yyyymm' => date('Ym'),
  154. 'create_yyyymmdd' => date('Ymd'),
  155. 'quality_type' => 0,
  156. 'to_user_id'=>$toUserId,
  157. 'sn'=> get_unique_sn(get_config('sn_prefix')),
  158. ];
  159. $todoId = Db::name('todo')->insertGetId($todoData);
  160. if(!$todoId){
  161. \exception('分派订单失败');
  162. }
  163. $taskData = [
  164. 'org_id' => $this->orgId,
  165. 'type' => 1,
  166. 'start_time' => getTime(),
  167. 'create_time' => getTime(),
  168. 'user_id' => $toUserId,
  169. 'bus_id' => $todoId,
  170. ];
  171. $res = Db::name('task')
  172. ->insert($taskData);
  173. if (!$res) {
  174. \exception('任务保存失败');
  175. }
  176. send_jpush([$toUserId],AppMsg::PUSH_WORKER_ORDER_SEND,'',['id'=>$todoId]);
  177. Db::commit();
  178. }catch (\Exception $e){
  179. Db::rollback();
  180. HelpHander::error($e->getMessage());
  181. }
  182. HelpHander::success([],'操作成功');
  183. }
  184. public function getTodoList(){
  185. $type = input('type',0);
  186. $page = input('page',1);
  187. $size = input('size',10);
  188. if(!in_array($type,[0,1])){
  189. HelpHander::error('参数错误');
  190. }
  191. $map[] = ['org_id','=',$this->orgId];
  192. $map[] = ['del','=',0];
  193. $map[] = ['create_user_id','=',$this->userId];
  194. $map[] = ['work_type_mode','in',[2,3]];
  195. if($type == 0){
  196. $map[] = ['todo_mode','=',2];
  197. }else{
  198. $map[] = ['todo_mode','in',[3,6]];
  199. }
  200. $list = Db::name('todo')
  201. ->where($map)
  202. ->page($page,$size)
  203. ->order('id','desc')
  204. ->select();
  205. $data = [];
  206. foreach ($list as $k=>$v){
  207. $data[$k]['id'] = $v['id'];
  208. $data[$k]['work_type_mode'] = $v['work_type_mode'];
  209. $data[$k]['create_time'] = $v['create_time'];
  210. $data[$k]['todo_content'] = $v['todo_content'];
  211. $data[$k]['order_id'] = $v['order_id'];
  212. $data[$k]['todo_mode'] = $v['todo_mode'];
  213. $data[$k]['done_time'] = $v['done_time'];
  214. $touser = Db::name('user')->where('id',$v['to_user_id'])->find();
  215. $data[$k]['to_user_name'] = $touser['real_name'];
  216. $addrName = Db::name('address')->where('id',$touser['addr_id'])->value('title');
  217. $data[$k]['s_addr'] = $addrName ?$addrName :'';
  218. $data[$k]['todo_mode_text'] = Db::name('todo_mode')->where('id',$v['todo_mode'])->value('out_content');
  219. $data[$k]['work_type_mode_text'] = Db::name('work_type_mode')->where('id',$v['work_type_mode'])->value('name');
  220. $data[$k]['type_name'] = '';
  221. $data[$k]['e_addr'] = '';
  222. $data[$k]['last_addr'] = '';
  223. $data[$k]['last_addr_time'] = '';
  224. if($v['work_type_mode'] == 2){
  225. $typeTitle = Db::name('cleaning_type')
  226. ->alias('ct')
  227. ->join('order_clean_type oct','oct.type_id=ct.id')
  228. ->where('oct.order_id',$v['order_id'])
  229. ->value('ct.title');
  230. $data[$k]['type_name'] = $typeTitle ? $typeTitle :'';
  231. }
  232. if($v['work_type_mode'] == 3){
  233. $typeInfo = Db::name('convey_cate')
  234. ->alias('cc')
  235. ->field('oc.*,cc.title')
  236. ->join('order_convey oc','oc.type=cc.id')
  237. ->where('oc.order_id',$v['order_id'])
  238. ->find();
  239. if($typeInfo){
  240. $saddr = Db::name('address')->where('id',$typeInfo['start'])->value('title');
  241. $eaddr = Db::name('address')->where('id',$typeInfo['end'])->value('title');
  242. $data[$k]['type_name'] = $typeInfo ? $typeInfo['title'] :'';
  243. $data[$k]['s_addr'] = $saddr ? $saddr :'';
  244. $data[$k]['e_addr'] = $eaddr ? $eaddr :'';
  245. $addr = Db::name('order_convey')
  246. ->alias('cpr')
  247. ->join('todo os', 'cpr.order_id = os.order_id')
  248. ->join('orders ods', 'cpr.order_id = ods.id')
  249. ->join('address ca', 'ca.id = cpr.start')
  250. ->join('address cad', 'cad.id = cpr.end')
  251. ->field('os.todo_mode,ca.title as start_name,cad.title as end_name,os.create_time')
  252. ->where('os.to_user_id', $v['to_user_id'])
  253. ->where('os.del', 0)
  254. ->where('ods.del', 0)
  255. ->order('os.id desc')
  256. ->find();
  257. if(in_array($addr['todo_mode'],[1,2])){
  258. $data[$k]['last_addr'] = $addr['start_name'];
  259. }else{
  260. $data[$k]['last_addr'] = $addr['end_name'];
  261. }
  262. $data[$k]['last_addr_time'] = $addr['create_time'];
  263. }
  264. }
  265. }
  266. HelpHander::success($data,'操作成功');
  267. }
  268. public function updateState(){
  269. $todoId = input('todoId/d',0);
  270. if($todoId <= 0){
  271. HelpHander::error('参数错误');
  272. }
  273. $todoMode =input('todoMode',0);
  274. if(!in_array($todoMode,[2,4,6])){
  275. HelpHander::error('参数错误');
  276. }
  277. $nodoReason = input('nodoReason','');
  278. $rejectVoice = input('rejectVoice','');
  279. $ret = model('Todo')->updateState($todoId,$todoMode,$nodoReason,$this->userId,$rejectVoice);
  280. if(!$ret){
  281. HelpHander::error(model('Todo')->getError());
  282. }
  283. HelpHander::success([],'操作成功');
  284. }
  285. }