Apicjgx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  2. /**
  3. * 作为检测车间功效的定时任务
  4. */
  5. class Apicjgx extends Start_Controller {
  6. public function __construct(){
  7. parent::__construct();
  8. $this->load->_model('Model_systemtransfer','systemtransfer');
  9. $this->load->_model("Model_systemtransfer_cr",'systemtransfer_cr');
  10. $this->load->_model("Model_transfer",'transfer');
  11. $this->load->_model("Model_fullorder","fullorder");
  12. $this->load->_model("Model_fullordersmt","fullordersmt");
  13. $this->load->_model("Model_fullordertt","fullordertt");
  14. }
  15. //定义方法的调用规则 获取URI第二段值
  16. public function _remap($arg,$arg_array)
  17. {
  18. if($arg == 'cjgx' ){
  19. //执行车间效率检测
  20. $this->_cjgx();
  21. }else{
  22. $this->_index();
  23. }
  24. }
  25. public function _index(){
  26. echo "程序异常";
  27. }
  28. public function dd($val){
  29. echo "<pre>";
  30. var_dump($val);
  31. die;
  32. }
  33. public function _cjgx(){
  34. $limit_time = time() - 3600*24*30;
  35. $list = $this->systemtransfer->find_all("status < 10 and time >= ".$limit_time);
  36. if(empty($list)){
  37. die(json_encode(array('code'=>0,'msg'=>'没有需要检测的工单'),JSON_UNESCAPED_UNICODE));
  38. }
  39. echo date('Y-m-d H:i:s')."开始执行车间效率检测".PHP_EOL;
  40. foreach ($list as $val){
  41. $this->doSingleData($val);
  42. }
  43. echo date('Y-m-d H:i:s')."执行车间效率检测结束".PHP_EOL;
  44. }
  45. //单独处理每个工单的状态判断
  46. public function doSingleData($info){
  47. $lbrk = array_reverse(explode('|',trim($info['rk'],'|')));
  48. $lbck = array_reverse(explode('|',trim($info['ck'],'|')));
  49. //如果是11 说明是成品库 说明已经入库完毕了 表示已经完毕
  50. if($lbrk[0] == 11){
  51. $this->checkOver($info['id']);
  52. return;
  53. }
  54. //检测出入库都存在时 是否做过标记 在出库的时候已经同步过了 这里需要判断下
  55. if($lbrk[0] == $lbck[0]){
  56. // if(!in_array($lbrk[0],[11,12,15,16])){
  57. // $this->checkIsOver($info,$lbrk[0]);
  58. // return;
  59. // }
  60. $this->checkCirculat($info,$lbrk[0]);
  61. return ;
  62. }else{
  63. $this->cancelWjf($info);
  64. }
  65. //如果是 成品库 布标打印 发块 滞销款 不做处理 再次判定状态是否是 超时一次 超时两次
  66. if(!in_array($lbrk[0],[11,12,15,16])){
  67. $this->jdgeTimeOut($info,$lbrk[0]);
  68. }
  69. }
  70. //检测一下当前的流转单是够长期没有流转
  71. /**
  72. * $info 主表相关信息
  73. * $transfer 附表相关信息
  74. */
  75. public function checkCirculat($info,$transfer){
  76. $fid = $info['id'];
  77. $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  78. if(empty($list)){
  79. return;
  80. }
  81. //如果没2条就不执行了 因为
  82. if(count($list) < 2){
  83. return ;
  84. }
  85. //操作车间不一致 就直接返回
  86. if($list[0]['lx'] != $list[1]['lx']){
  87. return ;
  88. }
  89. $scan_time = $list[0]['time'];
  90. $do_id = $list[0]['id'];
  91. if(time() - $scan_time < 3600 * 24){
  92. return ;
  93. }elseif(time() - $scan_time < 3600 * 24 * 3){
  94. $this->systemtransfer_cr->save([
  95. 'extra_status'=>1
  96. ],$do_id);
  97. return ;
  98. }else{
  99. $this->systemtransfer_cr->save([
  100. 'extra_status'=>2
  101. ],$do_id);
  102. }
  103. }
  104. public function cancelWjf($info){
  105. $fid = $info['id'];
  106. $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  107. if(empty($list)){
  108. return;
  109. }
  110. //如果没2条就不执行了 因为
  111. if(count($list) < 2){
  112. return ;
  113. }
  114. //操作车间一致 就直接返回
  115. if($list[0]['lx'] == $list[1]['lx']){
  116. return ;
  117. }
  118. if($list[1]['extra_status'] == 1){
  119. $this->systemtransfer_cr->save([
  120. 'extra_status'=>0
  121. ],$list[1]['id']);
  122. }
  123. }
  124. //如果检测到最后的入库是
  125. /**
  126. * $id 工单主键id
  127. */
  128. public function checkOver($id){
  129. $this->systemtransfer->save(array('status'=>10),$id);
  130. $this->load->database();
  131. //同步表示 此单已经到成品库 完结了
  132. $this->db->where('fid',$id);
  133. $this->db->update('systemtransfer_cr',array('is_over'=>3));
  134. $this->db->affected_rows();
  135. }
  136. //检测是否已经完结 如果完结 且已经标记 那么不做处理 如果为做标记 那么就坐下处理
  137. /**
  138. * $fid 工单主键id
  139. * $lx 车间id
  140. */
  141. public function checkIsOver($info,$lx){
  142. $fid = $info['id'];
  143. $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  144. //如果查到的数据小于2 那么就说明此单异常
  145. if(count($list) < 2){
  146. //echo json_encode(array('code'=>0,'msg'=>'程序异常','data'=>['info'=>$info,'list'=>$list]),JSON_UNESCAPED_UNICODE);
  147. return ;
  148. }
  149. //如果查到的数据不等于指定的车间id 说明程序异常了 先不执行
  150. if(($list[0]['lx'] != $lx) || ($list[1]['lx'] != $lx)){
  151. return ;
  152. }
  153. if(($list[0]['is_over'] == 0) || ($list[1]['is_over'] == 0)){
  154. if($list[1]['orver_flag'] == 2){
  155. $this->systemtransfer_cr->save(['is_over'=>1,'orver_flag'=>2],$list[0]['id']);
  156. }else{
  157. $this->systemtransfer_cr->save(['is_over'=>1],$list[0]['id']);
  158. }
  159. $this->systemtransfer_cr->save(['is_over'=>1],$list[1]['id']);
  160. }
  161. return ;
  162. }
  163. //判定是否超时
  164. public function jdgeTimeOut($info,$lx){
  165. $list = $this->systemtransfer_cr->find_all("fid = ".$info['id'],"*",'id desc',0,1);
  166. if(empty($list)){
  167. return ;
  168. }
  169. if($list[0]['lx'] != $lx){
  170. return ;
  171. }
  172. //查询流转部分要求
  173. $depart_info = $this->transfer->find("id = ".$lx);
  174. if(empty($depart_info)){
  175. return ;
  176. }
  177. $now_time = time();
  178. $ru_time = $list[0]['time'];//扫描时间
  179. // $overtime_one = $depart_info['overtime_one'] * 3600;
  180. // $overtime_two = $depart_info['overtime_two'] * 3600;
  181. //如果时间节点一样 就不再执行了
  182. if($depart_info['overtime_one'] == $depart_info['overtime_two']){
  183. return ;
  184. }
  185. $overtime_one = $depart_info['overtime_one'] * 3600 ;
  186. $overtime_two = $depart_info['overtime_two'] * 3600;
  187. //不选超时
  188. if(($now_time - $ru_time) < $overtime_one){
  189. return ;
  190. }
  191. $limit_time = [
  192. 'overtime_one'=> $depart_info['overtime_one'],
  193. 'overtime_two'=>$depart_info['overtime_two'],
  194. ];
  195. $remain_time = $now_time - $ru_time;
  196. $num = $this->weektimes($ru_time , $now_time);
  197. if($num > 0 ){
  198. $remain_time = $remain_time - 24 * 3600 * $num;
  199. }
  200. //前处理有时间1 就是超时提醒
  201. // if($lx == 5){
  202. // //超时一次的
  203. // if(($now_time - $ru_time) >= $overtime_one ){
  204. // if($list['0']['orver_flag'] == 0){
  205. // $this->systemtransfer_cr->save(['orver_flag'=>1,'limit_time_str'=> json_encode($limit_time)],$list['0']['id']);
  206. // }
  207. // }
  208. // }else{
  209. //超时一次的
  210. if(( $remain_time) >= $overtime_one && ( $remain_time) < $overtime_two){
  211. if($list['0']['orver_flag'] == 0){
  212. $type = $this->checkOrderInfo($info['number']);
  213. if($type == 1){//订单取消了或者其他原因 在没出库的情况下就完成了
  214. $this->systemtransfer_cr->save(['orver_flag'=>3],$list[0]['id']);
  215. }elseif($type == 10){
  216. $this->systemtransfer_cr->save(['orver_flag'=>1,'limit_time_str'=> json_encode($limit_time)],$list[0]['id']);
  217. }
  218. }
  219. }
  220. //超时2次的
  221. if(( $remain_time) >= $overtime_two){
  222. if($list[0]['orver_flag'] <= 1){
  223. //$this->systemtransfer_cr->save(['orver_flag'=>2,'limit_time_str'=> json_encode($limit_time)],$list['0']['id']);
  224. $type = $this->checkOrderInfo($info['number']);
  225. if($type == 1){//订单取消了或者其他原因 在没出库的情况下就完成了
  226. $this->systemtransfer_cr->save(['orver_flag'=>3],$list[0]['id']);
  227. }elseif($type == 10){
  228. $this->systemtransfer_cr->save(['orver_flag'=>2,'limit_time_str'=> json_encode($limit_time)],$list[0]['id']);
  229. }else{
  230. }
  231. }
  232. }
  233. //}
  234. }
  235. //检测是否含有周日
  236. protected function weektimes($start_time,$endstime)
  237. {
  238. //是否有周六周日
  239. $week_array = [7, 1, 2, 3, 4, 5, 6];
  240. $i = 0;
  241. while ($start_time <= $endstime) {
  242. $now_time = date('Y-m-d', $start_time);
  243. // 可根据自己想要的返回格式进行调整
  244. $alltime[$i]['week'] = $week_array[date("w", strtotime($now_time))];
  245. $start_time = strtotime('+1 day', $start_time);
  246. $i++;
  247. }
  248. $list = [];
  249. foreach ($alltime as $key => $value) {
  250. //周日数组
  251. if ($value['week'] > 6) {
  252. $list[] = $value['week'];
  253. }
  254. }
  255. return count($list);
  256. }
  257. //检验订单的状态
  258. protected function checkOrderInfo($number){
  259. $nu = $this->fullorder->get_number($number);
  260. if(!$nu)
  261. {
  262. $nu = $this->fullordersmt->get_number($number);
  263. if(!$nu)
  264. {
  265. $nu = $this->fullordertt->get_number($number);
  266. }
  267. }
  268. //此单已取消!请联系店员核实
  269. if($nu['state'] == '214' || $nu['state'] == '217')
  270. {
  271. return 1;
  272. }
  273. //因为某些原因订单虽然现实是完成 但是实际上是终止了 所以就按照取消计算吧
  274. // if($nu['state'] == '216' || $nu['state'] == '215')
  275. // {
  276. // return 1;
  277. // }
  278. return 10;
  279. }
  280. }