123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace app\common\model;
- use think\facade\Db;
- class ChainOrder extends BaseModel {
- public $page_info;
-
- public function getChainOrderInfo($condition = array(), $fields = '*') {
- return Db::name('chain_order')->field($fields)->where($condition)->find();
- }
-
- public function addChainOrder($data) {
- return Db::name('chain_order')->insert($data);
- }
-
- public function editChainOrder($data, $condition) {
- return Db::name('chain_order')->where($condition)->update($data);
- }
-
- public function editChainOrderArrive($data, $condition) {
- $data['chain_order_state'] = ORDER_STATE_PICKUP;
- return $this->editChainOrder($data, $condition);
- }
-
- public function editChainOrderPickup($data, $condition) {
- $data['chain_order_state'] = ORDER_STATE_SUCCESS;
- return $this->editChainOrder($data, $condition);
- }
-
- public function getChainOrderList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
- if ($pagesize) {
- $res = Db::name('chain_order')->field($fields)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
- $this->page_info = $res;
- return $res->items();
- } else {
- return Db::name('chain_order')->field($fields)->where($condition)->order($order)->limit($limit)->select()->toArray();
- }
- }
-
- public function getChainOrderDefaultList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
- $condition[] = array('chain_order_state', '=', ORDER_STATE_PAY);
- return $this->getChainOrderList($condition, $fields, $pagesize, $order, $limit);
- }
-
- public function getChainOrderDefaultAndArriveList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
- $condition[] = array('chain_order_state', 'not in', [ORDER_STATE_CANCEL, ORDER_STATE_SUCCESS]);
- return $this->getChainOrderList($condition, $fields, $pagesize, $order, $limit);
- }
-
- public function delChainOrder($condition) {
- return Db::name('chain_order')->where($condition)->delete();
- }
-
- public function editChainOrderPay($order_id) {
- $condition = array();
- $condition[] = array('order_id', '=', $order_id);
- $condition[] = array('chain_order_state', '=', ORDER_STATE_NEW);
- $chain_order_info = $this->getChainOrderInfo($condition);
- if ($chain_order_info) {
- if ($chain_order_info['chain_order_type'] == 2) {
- $this->editChainOrder(array('chain_order_state' => ORDER_STATE_PICKUP), $condition);
- $update_order['order_state'] = ORDER_STATE_PICKUP;
- $order_model = model('order');
- $order_model->editOrder($update_order, array(
- 'order_id' => $order_id,
- 'order_state'=>ORDER_STATE_PAY
- ));
- } elseif ($chain_order_info['chain_order_type'] == 1) {
- $this->editChainOrder(array('chain_order_state' => ORDER_STATE_PAY), $condition);
- }
- }
- }
-
- public function editChainOrderCancel($order_id, $refund_state = 0, $return_state = 0) {
- $condition = array();
- $condition[] = array('order_id', '=', $order_id);
- $chain_order_info = $this->getChainOrderInfo($condition);
- if ($chain_order_info) {
- $data = array();
- if ($refund_state) {
- $data['chain_order_refund_state'] = $refund_state;
- }
- if ($chain_order_info['chain_order_type'] == 2) {
- if($return_state){
- $data['chain_order_state'] = ORDER_STATE_CANCEL;
-
- $order_model=model('order');
- $ordergoods_list=$order_model->getOrdergoodsList(array(array('order_id','=',$chain_order_info['order_id'])));
- if($ordergoods_list){
- foreach($ordergoods_list as $key => $val){
- Db::name('chain_goods')->where(array(array('chain_id','=',$chain_order_info['chain_id']),array('goods_id','=',$val['goods_id'])))->inc('goods_storage',$val['goods_num'])->update();
- }
- }
- }else{
- if($chain_order_info['chain_order_state']<ORDER_STATE_SUCCESS){
- $data['chain_order_state'] = ORDER_STATE_CANCEL;
- }
- }
- } elseif ($chain_order_info['chain_order_type'] == 1) {
- if($chain_order_info['chain_order_state']>ORDER_STATE_PAY){
- $data['chain_order_state'] = ORDER_STATE_SUCCESS;
- }else{
- $data['chain_order_state'] = ORDER_STATE_CANCEL;
- }
- }
- if(!empty($data)){
- $this->editChainOrder($data, $condition);
- }
- }
- }
-
- public function editChainOrderLock($order_id) {
- $order_id = intval($order_id);
- if ($order_id > 0) {
- $condition = array();
- $condition[] = array('order_id', '=', $order_id);
- $data = array();
- $data['chain_order_lock_state'] = Db::raw('chain_order_lock_state+1');
- $result = $this->editChainOrder($data, $condition);
- return $result;
- }
- return false;
- }
-
- public function editChainOrderUnlock($order_id) {
- $order_id = intval($order_id);
- if ($order_id > 0) {
- $condition = array();
- $condition[] = array('order_id', '=', $order_id);
- $condition[] = array('chain_order_lock_state', '>=', '1');
- $data = array();
- $data['chain_order_lock_state'] = Db::raw('chain_order_lock_state-1');
- $result = $this->editChainOrder($data, $condition);
- return $result;
- }
- return false;
- }
-
- public function saveChainOrder($param)
- {
- if (!is_array($param['order_sn_list']))
- return ds_callback(true);
- $data = array();
- foreach ($param['order_sn_list'] as $order_id => $v) {
- $data['order_id'] = $order_id;
- $data['order_sn'] = $v['order_sn'];
- $data['chain_order_add_time'] = $v['add_time'];
- $data['chain_id'] = $param['chain_id'];
- $data['chain_order_type'] = 1;
- $data['store_id'] = $v['store_id'];
- $insert = $this->addChainOrder($data);
- if (!$insert) {
- return ds_callback(false, '保存代收订单信息失败order_sn:' . $v['order_sn']);
- }
- }
- return ds_callback(true);
- }
- }
|