Cabinet.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. <?php
  2. namespace app\index\controller;
  3. define('BASE_ROOT_PATH',str_replace('/application/index/controller','',dirname(__FILE__)));
  4. require_once(BASE_ROOT_PATH . '/extend/queue.php');
  5. require_once(BASE_ROOT_PATH . '/extend/queue.logic.php');
  6. use app\index\model\BoxActionModel;
  7. use app\index\model\CabinetModel;
  8. use app\index\model\BoxModel;
  9. use app\index\model\OrderModel;
  10. use app\index\model\UserModel;
  11. use think\App;
  12. use think\facade\Log;
  13. use think\Session;
  14. use QueueClient;
  15. use QueueServer;
  16. use Exception;
  17. class Cabinet extends Base
  18. {
  19. public function __construct(App $app = null)
  20. {
  21. parent::__construct($app);
  22. session('start','1');
  23. }
  24. /**
  25. * 自提柜批量生产
  26. */
  27. public function AddCabinet(){
  28. $param = input('param.');
  29. $url = urldecode($param['url']);
  30. $alias = $param['alias'];
  31. $count = intval($param['count']);
  32. $number = $param['number'];
  33. $login_key = $param['login_key'];
  34. if($count < 0 || empty($url)) {
  35. return json(json_error_exception('1006','参数不正确'));
  36. }
  37. $CabinetModel = new CabinetModel();
  38. $flag = $CabinetModel->insertCabinet($url,$alias,$count,$number,$login_key);
  39. if($flag['code'] != 1){
  40. return json(json_error_exception('1006',$flag['msg']));
  41. }
  42. json_success([]);
  43. }
  44. public function OpenBox()
  45. {
  46. $params['id'] = $this->getid();
  47. $cabinet_code = input('param.cabinet_number');
  48. $box_number = input('param.box_number');
  49. $box_position_list[] = $box_number;
  50. $params['box_position_list'] = $box_position_list;
  51. $params['delay_time'] = 0;
  52. $cabinet = $this->get_cabinet($cabinet_code);
  53. if($cabinet == false){
  54. return json(json_error_exception('1008'));
  55. }
  56. $params['ret_queue'] = 'RET' . session_id();
  57. $params['req_url'] = $cabinet['req_url'];
  58. $params['key'] = $cabinet['login_key'];
  59. $params['cabinet_code'] = $cabinet['cabinet_code'];
  60. $params['method'] = 'OpenBoxR';
  61. $result = $this->proc_request(self::queue_name,'OpenBox',$params);
  62. if($result == false) {
  63. json_error(2000);
  64. } else {
  65. json_success($result);
  66. }
  67. }
  68. public function LockCabinet()
  69. {
  70. $params['id'] = $this->getid();
  71. $cabinet_code = input('param.cabinet_number');
  72. $cabinet = $this->get_cabinet($cabinet_code);
  73. $params['delay_time'] = 0;
  74. if($cabinet == false){
  75. return json(json_error_exception('1008'));
  76. }
  77. $params['ret_queue'] = 'RET' . session_id();
  78. $params['req_url'] = $cabinet['req_url'];
  79. $params['key'] = $cabinet['login_key'];
  80. $params['cabinet_code'] = $cabinet['cabinet_code'];
  81. $params['method'] = 'LockCabinetR';
  82. $result = $this->proc_request(self::queue_name,'LockCabinet',$params);
  83. if($result == false) {
  84. json_error(2000);
  85. } else {
  86. json_success($result);
  87. }
  88. }
  89. public function UnLockCabinet()
  90. {
  91. $params['id'] = $this->getid();
  92. $cabinet_code = input('param.cabinet_number');
  93. $cabinet = $this->get_cabinet($cabinet_code);
  94. $params['delay_time'] = 0;
  95. if($cabinet == false){
  96. return json(json_error_exception('1008'));
  97. }
  98. $params['ret_queue'] = 'RET' . session_id();
  99. $params['req_url'] = $cabinet['req_url'];
  100. $params['key'] = $cabinet['login_key'];
  101. $params['cabinet_code'] = $cabinet['cabinet_code'];
  102. $params['method'] = 'UnLockCabinetR';
  103. $result = $this->proc_request(self::queue_name,'UnLockCabinet',$params);
  104. if($result == false) {
  105. json_error(2000);
  106. } else {
  107. json_success($result);
  108. }
  109. }
  110. public function ReleaseAlarm()
  111. {
  112. $params['id'] = $this->getid();
  113. $cabinet_code = input('param.cabinet_number');
  114. $params['delay_time'] = 0;
  115. $cabinet = $this->get_cabinet($cabinet_code);
  116. if($cabinet == false){
  117. return json(json_error_exception('1008'));
  118. }
  119. $params['ret_queue'] = 'RET' . session_id();
  120. $params['req_url'] = $cabinet['req_url'];
  121. $params['key'] = $cabinet['login_key'];
  122. $params['cabinet_code'] = $cabinet['cabinet_code'];
  123. $params['method'] = 'ReleaseAlarmR';
  124. $result = $this->proc_request(self::queue_name,'ReleaseAlarm',$params);
  125. if($result == false) {
  126. json_error(2000);
  127. } else {
  128. json_success($result);
  129. }
  130. }
  131. public function Reboot()
  132. {
  133. $params['id'] = $this->getid();
  134. $cabinet_code = input('param.cabinet_number');
  135. $params['delay_time'] = 0;
  136. $cabinet = $this->get_cabinet($cabinet_code);
  137. if($cabinet == false){
  138. return json(json_error_exception('1008'));
  139. }
  140. $params['ret_queue'] = 'RET' . session_id();
  141. $params['req_url'] = $cabinet['req_url'];
  142. $params['key'] = $cabinet['login_key'];
  143. $params['cabinet_code'] = $cabinet['cabinet_code'];
  144. $params['method'] = 'RebootR';
  145. $result = $this->proc_request(self::queue_name,'Reboot',$params);
  146. if($result == false) {
  147. json_error(2000);
  148. } else {
  149. json_success($result);
  150. }
  151. }
  152. public function QueryState()
  153. {
  154. $params['id'] = $this->getid();
  155. $cabinet_code = input('param.cabinet_number');
  156. $params['delay_time'] = 0;
  157. $cabinet = $this->get_cabinet($cabinet_code);
  158. if($cabinet == false){
  159. return json(json_error_exception('1008'));
  160. }
  161. $params['ret_queue'] = 'RET' . session_id();
  162. $params['req_url'] = $cabinet['req_url'];
  163. $params['key'] = $cabinet['login_key'];
  164. $params['cabinet_code'] = $cabinet['cabinet_code'];
  165. $params['method'] = 'RebootR';
  166. $result = $this->proc_request(self::queue_name,'QueryState',$params);
  167. if($result == false) {
  168. json_error(2000);
  169. } else {
  170. json_success($result);
  171. }
  172. }
  173. public function CabinetList(){
  174. $param = input('param.');
  175. $CabinetModel = new CabinetModel();
  176. $selectResult = $CabinetModel->getCabinetsByWhere();
  177. $cabinet_status = ['1' => '运行中' , '2' => '已锁定'];
  178. // 拼装参数
  179. foreach($selectResult as $key=>$vo){
  180. $selectResult[$key]['status_text'] = $cabinet_status[$vo['cabinet_status']];
  181. }
  182. $return['total'] = $CabinetModel->getAllCabinets(); //总数量
  183. $return['rows'] = $selectResult;
  184. json_success($return);
  185. }
  186. public function BoxList(){
  187. $cabinet_number = input('param.cabinet_number');
  188. if(empty($cabinet_number)){
  189. json_error(1007 , '柜子号码不能为空');
  190. }
  191. $BoxModel = new BoxModel();
  192. $where['sc_box.cabinet_number'] = $cabinet_number;
  193. $selectResult = $BoxModel->getBoxsByWhere($where);
  194. $box_status = ['1' => '空闲' , '2' => '已使用' , '3' => '锁定中'];
  195. // 拼装参数
  196. foreach($selectResult as $key=>$vo){
  197. $selectResult[$key]['status_text'] = $box_status[$vo['box_status']];
  198. }
  199. $return['total'] = $BoxModel->getAllCounts(); //总数量
  200. $return['rows'] = $selectResult;
  201. json_success($return);
  202. }
  203. public function BoxActionList(){
  204. $param = input('param.');
  205. if(empty($param['cabinet_number']) || empty($param['box_number'])){
  206. json_error(1007 , '柜子号码或箱子号码不能为空');
  207. }
  208. $where['cabinet_number'] = $param['cabinet_number'];
  209. $where['box_number'] = $param['box_number'];
  210. $limit = isset($param['pageSize']) ? $param['pageSize'] : 10;
  211. $param['pageNumber'] = isset($param['pageNumber']) ? $param['pageNumber'] : 1;
  212. $offset = ($param['pageNumber'] - 1) * $limit;
  213. $BoxActionModel = new BoxActionModel();
  214. $selectResult = $BoxActionModel->getBoxActionsByWhere($where , $offset, $limit);
  215. $status = ['1' => '开箱' , '2' => '关箱'];
  216. // 拼装参数
  217. foreach($selectResult as $key=>$vo){
  218. $selectResult[$key]['status_text'] = $status[$vo['type']];
  219. }
  220. $return['total'] = $BoxActionModel->getAllCounts($where); //总数量
  221. $return['rows'] = $selectResult;
  222. json_success($return);
  223. }
  224. public function GetCabinetsBoxs(){
  225. $CabinetModel = new CabinetModel();
  226. $cabinets = $CabinetModel->getCabinetsByWhere();
  227. $BoxModel = new BoxModel();
  228. foreach ($cabinets as $key => $value){
  229. $cabinets[$key]['box_data'] = $BoxModel->getBoxNumbers(['cabinet_number' => $value['id']]);
  230. }
  231. json_success($cabinets);
  232. }
  233. public function BoxChangeStatus(){
  234. $cabinet_number = input('param.cabinet_number');
  235. $box_number = input('param.box_number');
  236. $box_status = input('param.box_status');
  237. $ret = $this->ChangeStatus($cabinet_number , $box_number , $box_status);
  238. if($ret['code'] == -1){
  239. json_error(1008 , $ret['msg']);
  240. }
  241. if($ret == false){
  242. json_error(1006);
  243. }
  244. json_success([]);
  245. }
  246. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  247. private function proc_request($queue_name, $key, $params){
  248. QueueClient::push($queue_name,self::redis_host , self::redis_port ,$key,$params);
  249. [$method,$args] = $this->wait_result($params['ret_queue']);
  250. Log::record($args);
  251. return $args;
  252. }
  253. private function getid(){
  254. return md5(rand(1000000,9999999));
  255. }
  256. private function wait_result($queue_name)
  257. {
  258. $times = 10;
  259. while ($times--)
  260. {
  261. try
  262. {
  263. $worker = new QueueServer($queue_name,self::redis_host , self::redis_port);
  264. $queues = $worker->scan();
  265. $content = $worker->pop($queues, 1);
  266. if(is_array($content)) {
  267. $method = key($content);
  268. $arg = current($content);
  269. return [$method,$arg];
  270. }
  271. }
  272. catch (Exception $e)
  273. {
  274. $err = $e->getMessage();
  275. $code = $e->getCode();
  276. }
  277. }
  278. return [false,false];
  279. }
  280. /**
  281. * 自提柜箱子修改状态
  282. */
  283. private function ChangeStatus($cabinet_number , $box_number , $status){
  284. $BoxModel = new BoxModel();
  285. $box = $BoxModel->getOneCabinetBox($cabinet_number,$box_number);
  286. if(empty($box)){
  287. return msg(-1, '', '箱子不存在');
  288. }
  289. if($status == $box['box_status']){
  290. return true;
  291. }
  292. $where['cabinet_number'] = $cabinet_number;
  293. $where['box_number'] = $box_number;
  294. $update['box_status'] = $status;
  295. return $BoxModel->editData($where,$update);
  296. }
  297. public function test(){
  298. pre(1);
  299. }
  300. /**
  301. * 自提柜箱子绑定订单号
  302. */
  303. private function box_bind_order($cabinet_number , $box_number , $order_sn){
  304. $where['cabinet_number'] = $cabinet_number;
  305. $where['box_number'] = $box_number;
  306. $update['order_sn'] = $order_sn;
  307. $update['box_status'] = 2;
  308. $BoxModel = new BoxModel();
  309. return $BoxModel->editData($where,$update);
  310. }
  311. /**
  312. * 记录箱子开启/关闭
  313. */
  314. private function box_action_record($cabinet_number , $box_number , $type){
  315. $BoxActionModel = new BoxActionModel();
  316. $params['cabinet_number'] = $cabinet_number;
  317. $params['box_number'] = $box_number;
  318. $params['type'] = $type;
  319. $params['datetime'] = date("Y-m-d H:i:s");
  320. $BoxActionModel->save($params);
  321. }
  322. /**
  323. * 获取柜子信息
  324. */
  325. private function get_cabinet($cabinet_number){
  326. $cabinet = CabinetModel::find($cabinet_number);
  327. if(empty($cabinet)){
  328. return false;
  329. }
  330. $cabinet = $cabinet->toArray();
  331. return $cabinet;
  332. }
  333. }