Model_process.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. require_once APPPATH.'/models/Model_flow.php';
  3. require_once APPPATH.'/models/Model_process_log.php';
  4. // require_once APPPATH.'/models/Model_headorder_item.php';
  5. class Model_Process extends Lin_Model
  6. {
  7. const STATUS_PROCESSING = 'processing';
  8. const STATUS_COMPLETED = 'completed';
  9. const STATUS_CANCELED = 'canceled';
  10. const STATUS_CLOSE = 'close';
  11. function __construct(){
  12. parent::__construct();
  13. $this->load->database();
  14. $this->table = 'process';
  15. $this->load_table('process');
  16. $this->flow=new Model_Flow();
  17. $this->log=new Model_Process_log();
  18. }
  19. public function initStatus($flow)
  20. {
  21. // 流程的第一个情况
  22. $firstStatus = $this->flow->getFirstStatus($flow);
  23. $this->setStatus($firstStatus);
  24. }
  25. public function toNextStatus()
  26. {
  27. // 流程的下一个情况
  28. $nextStatus = $this->flow->getNextStatus($this->data['status']);
  29. // 根据情况生成新状态
  30. return $this->setStatus($nextStatus);
  31. }
  32. private function operationValidate()
  33. {
  34. if (isset($this->state) && $this->state != self::STATUS_PROCESSING) {
  35. return false;
  36. }
  37. return true;
  38. // if ($this->isPending() == true) {
  39. // throw new AppException("{$this->name}已挂起,无法继续操作");
  40. // }
  41. }
  42. private function setStatus($status)
  43. {
  44. $state=self::STATUS_PROCESSING;
  45. if($this->compareStatus($status,$this->flow->getFinalStatus($status))){
  46. // 流程执行完
  47. if($this->operationValidate()){
  48. $state = self::STATUS_COMPLETED;
  49. }else{
  50. $state = self::STATUS_CANCELED;
  51. }
  52. }
  53. if($this->compareStatus($status,$this->flow->getCancelStatus($status))||$this->compareStatus($status,$this->flow->getCloseStatus($status))){
  54. $state = self::STATUS_CANCELED;
  55. }
  56. //todo 日志 要不要把status 加入
  57. $success=$this->save([
  58. 'state'=>$state,
  59. 'status_id'=>$status['id'],
  60. 'status'=>$status['code'],
  61. 'updated_at'=>time(),
  62. ]);
  63. if($success){
  64. $info=$this->read($this->id);
  65. $msg='头套订单id: '.$info['order_id'].'状态: '.$info['state'].'阶段: '.$info['status'].$status['name'];
  66. if($info['uid']){
  67. $msg.='员工:'.$info['uid'];
  68. }
  69. $this->log->insert([
  70. 'process_id'=>$info['id'],
  71. 'flow_id'=>$info['flow_id'],
  72. 'msg'=>$msg,
  73. 'process_status'=>$status['code'],
  74. 'origindata'=>json_encode($info,true),
  75. 'created_at'=>time(),
  76. ]);
  77. $this->afterSetStatus($info,$state);
  78. //todo 判断是否全部完成
  79. return true;
  80. }
  81. return false;
  82. }
  83. private function compareStatus($status,$flowStatus){
  84. return $status['code']==$flowStatus['code']?true:false;
  85. }
  86. /**
  87. * @throws \Exception
  88. */
  89. public function toCloseStatus($flow)
  90. {
  91. $closeStatus = $this->flow->getCloseStatus($flow);
  92. $this->setStatus($closeStatus);
  93. }
  94. /**
  95. * @throws \Exception
  96. */
  97. public function toCancelStatus($flow)
  98. {
  99. $cancelStatus = $this->flow->getCancelStatus($flow);
  100. $this->setStatus($cancelStatus);
  101. }
  102. /**
  103. * @return string
  104. */
  105. public function getNameAttribute()
  106. {
  107. return $this->flow->name;
  108. }
  109. /**
  110. * @return string
  111. */
  112. public function getStateNameAttribute()
  113. {
  114. return $this->allState[$this->state];
  115. }
  116. /**
  117. * @return string
  118. */
  119. public function getStatusNameAttribute()
  120. {
  121. return $this->status->name;
  122. }
  123. /**
  124. * @return string
  125. */
  126. public function getCodeAttribute()
  127. {
  128. return $this->flow->code;
  129. }
  130. public function afterSetStatus($info,$state){
  131. if($state == self::STATUS_COMPLETED){
  132. $orderid=$info['order_id'];
  133. $query=$this->db->from('headorder_item')->where('id',$orderid)->limit(1)->get();
  134. $orderitem=$query->row_array();
  135. $where='a.p_order='.$orderitem['p_order'].' and (b.state !="'.self::STATUS_CANCELED .'" or b.state !="'.self::STATUS_CLOSE.'") and b.status !="cancle"';
  136. $query=$this->db->select('a.*,b.status,b.state')
  137. ->from('headorder_item as a')
  138. ->join('process as b',' a.`id`=b.`order_id`','inner')
  139. ->join('flow_status as c','b.`status_id`=c.id')
  140. ->where($where)
  141. ->get();
  142. $process_list=$query->result_array();
  143. $sum=array_count_values(array_column($process_list,'status'));
  144. if(isset($sum['completed'])&& !empty($sum['completed'])){
  145. if(count($process_list)==$sum['completed']){
  146. // fullorder or smt UPDATE WIGS
  147. $res=$this->db->where('orderinfo',$orderitem['p_order'])->update('fullorder',[
  148. 'wigs'=>3,
  149. ]);
  150. if(!$this->db->affected_rows()){
  151. $this->db->where('orderinfo',$orderitem['p_order'])->update('fullordersmt',[
  152. 'wigs'=>3,
  153. ]);
  154. }
  155. }
  156. }
  157. }else{
  158. $orderid=$info['order_id'];
  159. $query=$this->db->from('headorder_item')->where('id',$orderid)->limit(1)->get();
  160. $orderitem=$query->row_array();
  161. $where='a.p_order='.$orderitem['p_order'].' and (b.state !="'.self::STATUS_CANCELED .'" or b.state !="'.self::STATUS_CLOSE.'") and b.status !="cancle"';
  162. $query=$this->db->select('a.*,b.status,b.state')
  163. ->from('headorder_item as a')
  164. ->join('process as b',' a.`id`=b.`order_id`','inner')
  165. ->join('flow_status as c','b.`status_id`=c.id')
  166. ->order_by('c.order DESC')
  167. ->limit(1)
  168. ->where($where)
  169. ->get();
  170. $item=$query->row_array();
  171. $db='fullorder';
  172. $query=$this->db->from($db)->where('orderinfo',$orderitem['p_order'])->limit(1)->get();
  173. $order=$query->row_array();
  174. if(!$order){
  175. $db='fullordersmt';
  176. $query=$this->db->from($db)->where('orderinfo',$orderitem['p_order'])->limit(1)->get();
  177. $order=$query->row_array();
  178. if(!$order){
  179. return;
  180. }
  181. }
  182. $status=$item['status'];
  183. $wigs=$order['wigs']?$order['wigs']:0;
  184. $nextwigs=0;
  185. if($status=='waitAssign'&&$wigs==9){
  186. $nextwigs=1;
  187. }
  188. if($status=='waitScan' && $wigs==1){
  189. $nextwigs=2;
  190. }
  191. if($status=='completed' && $wigs==2){
  192. $nextwigs=3;
  193. }
  194. if($nextwigs){
  195. $res=$this->db->where('orderinfo',$orderitem['p_order'])->update($db,[
  196. 'wigs'=>$nextwigs,
  197. ]);
  198. }
  199. }
  200. }
  201. }