| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once APPPATH.'/models/Model_flow.php';require_once APPPATH.'/models/Model_process_log.php';// require_once APPPATH.'/models/Model_headorder_item.php';class Model_Process extends Lin_Model {    const STATUS_PROCESSING = 'processing';    const STATUS_COMPLETED = 'completed';    const STATUS_CANCELED = 'canceled';    const STATUS_CLOSE = 'close';    function __construct(){        parent::__construct();        $this->load->database();        $this->table = 'process';        $this->load_table('process');        $this->flow=new Model_Flow();        $this->log=new Model_Process_log();    }    public function initStatus($flow)    {        // 流程的第一个情况        $firstStatus = $this->flow->getFirstStatus($flow);        $this->setStatus($firstStatus);    }    public function toNextStatus()    {        // 流程的下一个情况        $nextStatus = $this->flow->getNextStatus($this->data['status']);        // 根据情况生成新状态        return $this->setStatus($nextStatus);    }     private function operationValidate()    {        if (isset($this->state) && $this->state != self::STATUS_PROCESSING) {            return false;        }        return true;                // if ($this->isPending() == true) {        //     throw new AppException("{$this->name}已挂起,无法继续操作");        // }    }    private function setStatus($status)    {            $state=self::STATUS_PROCESSING;        if($this->compareStatus($status,$this->flow->getFinalStatus($status))){            // 流程执行完            if($this->operationValidate()){                $state = self::STATUS_COMPLETED;            }else{                $state = self::STATUS_CANCELED;            }        }        if($this->compareStatus($status,$this->flow->getCancelStatus($status))||$this->compareStatus($status,$this->flow->getCloseStatus($status))){            $state = self::STATUS_CANCELED;                   }        //todo  日志 要不要把status 加入        $success=$this->save([            'state'=>$state,            'status_id'=>$status['id'],            'status'=>$status['code'],              'updated_at'=>time(),        ]);        if($success){            $info=$this->read($this->id);            $msg='头套订单id: '.$info['order_id'].'状态: '.$info['state'].'阶段: '.$info['status'].$status['name'];            if($info['uid']){                $msg.='员工:'.$info['uid'];            }            $this->log->insert([                'process_id'=>$info['id'],                'flow_id'=>$info['flow_id'],                'msg'=>$msg,                'process_status'=>$status['code'],                  'origindata'=>json_encode($info,true),                'created_at'=>time(),            ]);            $this->afterSetStatus($info,$state);            //todo 判断是否全部完成             return true;        }        return false;    }    private function compareStatus($status,$flowStatus){        return $status['code']==$flowStatus['code']?true:false;    }    /**     * @throws \Exception     */    public function toCloseStatus($flow)    {        $closeStatus = $this->flow->getCloseStatus($flow);        $this->setStatus($closeStatus);    }    /**     * @throws \Exception     */    public function toCancelStatus($flow)    {        $cancelStatus = $this->flow->getCancelStatus($flow);        $this->setStatus($cancelStatus);    }    /**     * @return string     */    public function getNameAttribute()    {        return $this->flow->name;    }    /**     * @return string     */    public function getStateNameAttribute()    {        return $this->allState[$this->state];    }    /**     * @return string     */    public function getStatusNameAttribute()    {        return $this->status->name;    }    /**     * @return string     */    public function getCodeAttribute()    {        return $this->flow->code;    }    public function afterSetStatus($info,$state){        if($state == self::STATUS_COMPLETED){            $orderid=$info['order_id'];            $query=$this->db->from('headorder_item')->where('id',$orderid)->limit(1)->get();            $orderitem=$query->row_array();            $where='a.p_order='.$orderitem['p_order'].' and (b.state !="'.self::STATUS_CANCELED .'" or b.state !="'.self::STATUS_CLOSE.'") and b.status !="cancle"';                        $query=$this->db->select('a.*,b.status,b.state')            ->from('headorder_item as a')            ->join('process as b',' a.`id`=b.`order_id`','inner')            ->join('flow_status as c','b.`status_id`=c.id')            ->where($where)            ->get();            $process_list=$query->result_array();            $sum=array_count_values(array_column($process_list,'status'));            if(isset($sum['completed'])&& !empty($sum['completed'])){                    if(count($process_list)==$sum['completed']){                        // fullorder or smt UPDATE WIGS                                                $res=$this->db->where('orderinfo',$orderitem['p_order'])->update('fullorder',[                            'wigs'=>3,                        ]);                        if(!$this->db->affected_rows()){                            $this->db->where('orderinfo',$orderitem['p_order'])->update('fullordersmt',[                                'wigs'=>3,                            ]);                        }                           }            }        }else{            $orderid=$info['order_id'];            $query=$this->db->from('headorder_item')->where('id',$orderid)->limit(1)->get();            $orderitem=$query->row_array();            $where='a.p_order='.$orderitem['p_order'].' and (b.state !="'.self::STATUS_CANCELED .'" or b.state !="'.self::STATUS_CLOSE.'") and b.status !="cancle"';            $query=$this->db->select('a.*,b.status,b.state')            ->from('headorder_item as a')            ->join('process as b',' a.`id`=b.`order_id`','inner')            ->join('flow_status as c','b.`status_id`=c.id')            ->order_by('c.order DESC')            ->limit(1)            ->where($where)            ->get();            $item=$query->row_array();            $db='fullorder';            $query=$this->db->from($db)->where('orderinfo',$orderitem['p_order'])->limit(1)->get();            $order=$query->row_array();            if(!$order){                $db='fullordersmt';                $query=$this->db->from($db)->where('orderinfo',$orderitem['p_order'])->limit(1)->get();                $order=$query->row_array();                if(!$order){                    return;                }            }            $status=$item['status'];            $wigs=$order['wigs']?$order['wigs']:0;            $nextwigs=0;            if($status=='waitAssign'&&$wigs==9){                $nextwigs=1;            }            if($status=='waitScan' && $wigs==1){                $nextwigs=2;            }            if($status=='completed' && $wigs==2){                $nextwigs=3;            }            if($nextwigs){                $res=$this->db->where('orderinfo',$orderitem['p_order'])->update($db,[                    'wigs'=>$nextwigs,                ]);            }                               }    }       }
 |