Apicjgx.php 10 KB

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