Apicjgx.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. }
  63. //如果是 成品库 布标打印 发块 滞销款 不做处理 再次判定状态是否是 超时一次 超时两次
  64. if(!in_array($lbrk[0],[11,12,15,16])){
  65. $this->jdgeTimeOut($info,$lbrk[0]);
  66. }
  67. }
  68. //检测一下当前的流转单是够长期没有流转
  69. /**
  70. * $info 主表相关信息
  71. * $transfer 附表相关信息
  72. */
  73. public function checkCirculat($info,$transfer){
  74. $fid = $info['id'];
  75. $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  76. if(empty($list)){
  77. return;
  78. }
  79. //如果没2条就不执行了 因为
  80. if(count($list) < 2){
  81. return ;
  82. }
  83. //操作车间不一致 就直接返回
  84. if($list[0]['lx'] != $list[1]['lx']){
  85. return ;
  86. }
  87. $scan_time = $list[0]['time'];
  88. $do_id = $list[0]['id'];
  89. if(time() - $scan_time < 3600 * 24){
  90. return ;
  91. }elseif(time() - $scan_time < 3600 * 24 * 3){
  92. $this->systemtransfer_cr->save([
  93. 'extra_status'=>1
  94. ],$do_id);
  95. return ;
  96. }else{
  97. $this->systemtransfer_cr->save([
  98. 'extra_status'=>2
  99. ],$do_id);
  100. }
  101. }
  102. // public function cancelWjf($info){
  103. // $fid = $info['id'];
  104. // $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  105. // if(empty($list)){
  106. // return;
  107. // }
  108. // //如果没2条就不执行了 因为
  109. // if(count($list) < 2){
  110. // return ;
  111. // }
  112. // //操作车间一致 就直接返回
  113. // if($list[0]['lx'] == $list[1]['lx']){
  114. // return ;
  115. // }
  116. // if($list[1]['extra_status'] == 1){
  117. // $this->systemtransfer_cr->save([
  118. // 'extra_status'=>0
  119. // ],$list[1]['id']);
  120. // }
  121. // }
  122. //如果检测到最后的入库是
  123. /**
  124. * $id 工单主键id
  125. */
  126. public function checkOver($id){
  127. $this->systemtransfer->save(array('status'=>10),$id);
  128. $this->load->database();
  129. //同步表示 此单已经到成品库 完结了
  130. $this->db->where('fid',$id);
  131. $this->db->update('systemtransfer_cr',array('is_over'=>3));
  132. $this->db->affected_rows();
  133. }
  134. //检测是否已经完结 如果完结 且已经标记 那么不做处理 如果为做标记 那么就坐下处理
  135. /**
  136. * $fid 工单主键id
  137. * $lx 车间id
  138. */
  139. public function checkIsOver($info,$lx){
  140. $fid = $info['id'];
  141. $list = $this->systemtransfer_cr->find_all("fid = ".$fid,"*",'id desc',0,2);
  142. //如果查到的数据小于2 那么就说明此单异常
  143. if(count($list) < 2){
  144. //echo json_encode(array('code'=>0,'msg'=>'程序异常','data'=>['info'=>$info,'list'=>$list]),JSON_UNESCAPED_UNICODE);
  145. return ;
  146. }
  147. //如果查到的数据不等于指定的车间id 说明程序异常了 先不执行
  148. if(($list[0]['lx'] != $lx) || ($list[1]['lx'] != $lx)){
  149. return ;
  150. }
  151. if(($list[0]['is_over'] == 0) || ($list[1]['is_over'] == 0)){
  152. if($list[1]['orver_flag'] == 2){
  153. $this->systemtransfer_cr->save(['is_over'=>1,'orver_flag'=>2],$list[0]['id']);
  154. }else{
  155. $this->systemtransfer_cr->save(['is_over'=>1],$list[0]['id']);
  156. }
  157. $this->systemtransfer_cr->save(['is_over'=>1],$list[1]['id']);
  158. }
  159. return ;
  160. }
  161. //判定是否超时
  162. public function jdgeTimeOut($info,$lx){
  163. $list = $this->systemtransfer_cr->find_all("fid = ".$info['id'],"*",'id desc',0,1);
  164. if(empty($list)){
  165. return ;
  166. }
  167. if($list[0]['lx'] != $lx){
  168. return ;
  169. }
  170. //查询流转部分要求
  171. $depart_info = $this->transfer->find("id = ".$lx);
  172. if(empty($depart_info)){
  173. return ;
  174. }
  175. $now_time = time();
  176. $ru_time = $list[0]['time'];//扫描时间
  177. // $overtime_one = $depart_info['overtime_one'] * 3600;
  178. // $overtime_two = $depart_info['overtime_two'] * 3600;
  179. //如果时间节点一样 就不再执行了
  180. if($depart_info['overtime_one'] == $depart_info['overtime_two']){
  181. return ;
  182. }
  183. $overtime_one = $depart_info['overtime_one'] * 3600 ;
  184. $overtime_two = $depart_info['overtime_two'] * 3600;
  185. //不选超时
  186. if(($now_time - $ru_time) < $overtime_one){
  187. return ;
  188. }
  189. $limit_time = [
  190. 'overtime_one'=> $depart_info['overtime_one'],
  191. 'overtime_two'=>$depart_info['overtime_two'],
  192. ];
  193. $remain_time = $now_time - $ru_time;
  194. $num = $this->weektimes($ru_time , $now_time);
  195. if($num > 0 ){
  196. $remain_time = $remain_time - 24 * 3600 * $num;
  197. }
  198. //前处理有时间1 就是超时提醒
  199. // if($lx == 5){
  200. // //超时一次的
  201. // if(($now_time - $ru_time) >= $overtime_one ){
  202. // if($list['0']['orver_flag'] == 0){
  203. // $this->systemtransfer_cr->save(['orver_flag'=>1,'limit_time_str'=> json_encode($limit_time)],$list['0']['id']);
  204. // }
  205. // }
  206. // }else{
  207. //超时一次的
  208. if(( $remain_time) >= $overtime_one && ( $remain_time) < $overtime_two){
  209. if($list['0']['orver_flag'] == 0){
  210. $type = $this->checkOrderInfo($info['number']);
  211. if($type == 1){//订单取消了或者其他原因 在没出库的情况下就完成了
  212. $this->systemtransfer_cr->save(['orver_flag'=>3],$list[0]['id']);
  213. }elseif($type == 10){
  214. $this->systemtransfer_cr->save(['orver_flag'=>1,'limit_time_str'=> json_encode($limit_time)],$list[0]['id']);
  215. }
  216. }
  217. }
  218. //超时2次的
  219. if(( $remain_time) >= $overtime_two){
  220. if($list[0]['orver_flag'] <= 1){
  221. //$this->systemtransfer_cr->save(['orver_flag'=>2,'limit_time_str'=> json_encode($limit_time)],$list['0']['id']);
  222. $type = $this->checkOrderInfo($info['number']);
  223. if($type == 1){//订单取消了或者其他原因 在没出库的情况下就完成了
  224. $this->systemtransfer_cr->save(['orver_flag'=>3],$list[0]['id']);
  225. }elseif($type == 10){
  226. $this->systemtransfer_cr->save(['orver_flag'=>2,'limit_time_str'=> json_encode($limit_time)],$list[0]['id']);
  227. }else{
  228. }
  229. }
  230. }
  231. //}
  232. }
  233. //检测是否含有周日
  234. protected function weektimes($start_time,$endstime)
  235. {
  236. //是否有周六周日
  237. $week_array = [7, 1, 2, 3, 4, 5, 6];
  238. $i = 0;
  239. while ($start_time <= $endstime) {
  240. $now_time = date('Y-m-d', $start_time);
  241. // 可根据自己想要的返回格式进行调整
  242. $alltime[$i]['week'] = $week_array[date("w", strtotime($now_time))];
  243. $start_time = strtotime('+1 day', $start_time);
  244. $i++;
  245. }
  246. $list = [];
  247. foreach ($alltime as $key => $value) {
  248. //周日数组
  249. if ($value['week'] > 6) {
  250. $list[] = $value['week'];
  251. }
  252. }
  253. return count($list);
  254. }
  255. //检验订单的状态
  256. protected function checkOrderInfo($number){
  257. $nu = $this->fullorder->get_number($number);
  258. if(!$nu)
  259. {
  260. $nu = $this->fullordersmt->get_number($number);
  261. if(!$nu)
  262. {
  263. $nu = $this->fullordertt->get_number($number);
  264. }
  265. }
  266. //此单已取消!请联系店员核实
  267. if($nu['state'] == '214' || $nu['state'] == '217')
  268. {
  269. return 1;
  270. }
  271. //因为某些原因订单虽然现实是完成 但是实际上是终止了 所以就按照取消计算吧
  272. // if($nu['state'] == '216' || $nu['state'] == '215')
  273. // {
  274. // return 1;
  275. // }
  276. return 10;
  277. }
  278. }