ChainOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class ChainOrder extends BaseModel {
  16. public $page_info;
  17. /**
  18. * 取单条订单信息
  19. * @access public
  20. * @author csdeshang
  21. * @param array $condition 检索条件
  22. * @param str $fields 字段
  23. * @return type
  24. */
  25. public function getChainOrderInfo($condition = array(), $fields = '*') {
  26. return Db::name('chain_order')->field($fields)->where($condition)->find();
  27. }
  28. /**
  29. * 插入订单支付表信息
  30. * @access public
  31. * @author csdeshang
  32. * @param array $data 参数内容
  33. * @return type
  34. */
  35. public function addChainOrder($data) {
  36. return Db::name('chain_order')->insert($data);
  37. }
  38. /**
  39. * 更改信息
  40. * @access public
  41. * @author csdeshang
  42. * @param array $data 更新数据
  43. * @param array $condition 条件
  44. * @return type
  45. */
  46. public function editChainOrder($data, $condition) {
  47. return Db::name('chain_order')->where($condition)->update($data);
  48. }
  49. /**
  50. * 更改信息(包裹到达自提服务站)
  51. * @access public
  52. * @author csdeshang
  53. * @param array $data
  54. * @param array $condition 条件
  55. * @return bool
  56. */
  57. public function editChainOrderArrive($data, $condition) {
  58. $data['chain_order_state'] = ORDER_STATE_PICKUP;
  59. return $this->editChainOrder($data, $condition);
  60. }
  61. /**
  62. * 更改信息(买家从物流自提服务张取走包裹)
  63. * @access public
  64. * @author csdeshang
  65. * @param array $data 更新数据
  66. * @param array $condition 条件
  67. * @return bool
  68. */
  69. public function editChainOrderPickup($data, $condition) {
  70. $data['chain_order_state'] = ORDER_STATE_SUCCESS;
  71. return $this->editChainOrder($data, $condition);
  72. }
  73. /**
  74. * 取订单列表信息
  75. * @access public
  76. * @author csdeshang
  77. * @param array $condition 检索条件
  78. * @param string $fields 字段
  79. * @param number $pagesize 分页信息
  80. * @param string $order 排序
  81. * @param int $limit 数目限制
  82. * @return array
  83. */
  84. public function getChainOrderList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
  85. if ($pagesize) {
  86. $res = Db::name('chain_order')->field($fields)->where($condition)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  87. $this->page_info = $res;
  88. return $res->items();
  89. } else {
  90. return Db::name('chain_order')->field($fields)->where($condition)->order($order)->limit($limit)->select()->toArray();
  91. }
  92. }
  93. /**
  94. * 取未到站订单列表
  95. * @access public
  96. * @author csdeshang
  97. * @param array $condition 检索条件
  98. * @param string $fields 字段
  99. * @param number $pagesize 分页信息
  100. * @param string $order 排序
  101. * @param int $limit 数目限制
  102. * @return array
  103. */
  104. public function getChainOrderDefaultList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
  105. $condition[] = array('chain_order_state', '=', ORDER_STATE_PAY);
  106. return $this->getChainOrderList($condition, $fields, $pagesize, $order, $limit);
  107. }
  108. /**
  109. * 取未到站/已到站订单列表
  110. * @access public
  111. * @author csdeshang
  112. * @param unknown $condition 检索条件
  113. * @param string $fields 字段
  114. * @param number $pagesize 分页信息
  115. * @param string $order 排序
  116. * @param int $limit 数目限制
  117. * @return array
  118. */
  119. public function getChainOrderDefaultAndArriveList($condition = array(), $fields = '*', $pagesize = 0, $order = 'order_id desc', $limit = 0) {
  120. $condition[] = array('chain_order_state', 'not in', [ORDER_STATE_CANCEL, ORDER_STATE_SUCCESS]);
  121. return $this->getChainOrderList($condition, $fields, $pagesize, $order, $limit);
  122. }
  123. /**
  124. * 删除
  125. * @access public
  126. * @author csdeshang
  127. * @param array $condition 条件
  128. * @return type
  129. */
  130. public function delChainOrder($condition) {
  131. return Db::name('chain_order')->where($condition)->delete();
  132. }
  133. /**
  134. * 更改门店订单状态
  135. * @access public
  136. * @author csdeshang
  137. * @param array $condition 条件
  138. * @return type
  139. */
  140. public function editChainOrderPay($order_id) {
  141. $condition = array();
  142. $condition[] = array('order_id', '=', $order_id);
  143. $condition[] = array('chain_order_state', '=', ORDER_STATE_NEW);
  144. $chain_order_info = $this->getChainOrderInfo($condition);
  145. if ($chain_order_info) {
  146. if ($chain_order_info['chain_order_type'] == 2) {//自提
  147. $this->editChainOrder(array('chain_order_state' => ORDER_STATE_PICKUP), $condition);
  148. $update_order['order_state'] = ORDER_STATE_PICKUP;
  149. $order_model = model('order');
  150. $order_model->editOrder($update_order, array(
  151. 'order_id' => $order_id,
  152. 'order_state'=>ORDER_STATE_PAY
  153. ));
  154. } elseif ($chain_order_info['chain_order_type'] == 1) {//代收
  155. $this->editChainOrder(array('chain_order_state' => ORDER_STATE_PAY), $condition);
  156. }
  157. }
  158. }
  159. /**
  160. * 取消门店订单
  161. * @access public
  162. * @author csdeshang
  163. * @param array $condition 条件
  164. * @return type
  165. */
  166. public function editChainOrderCancel($order_id, $refund_state = 0, $return_state = 0) {
  167. $condition = array();
  168. $condition[] = array('order_id', '=', $order_id);
  169. $chain_order_info = $this->getChainOrderInfo($condition);
  170. if ($chain_order_info) {
  171. $data = array();
  172. if ($refund_state) {
  173. $data['chain_order_refund_state'] = $refund_state;
  174. }
  175. if ($chain_order_info['chain_order_type'] == 2) {//自提
  176. if($return_state){
  177. $data['chain_order_state'] = ORDER_STATE_CANCEL;
  178. //退库存
  179. $order_model=model('order');
  180. $ordergoods_list=$order_model->getOrdergoodsList(array(array('order_id','=',$chain_order_info['order_id'])));
  181. if($ordergoods_list){
  182. foreach($ordergoods_list as $key => $val){
  183. 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();
  184. }
  185. }
  186. }else{
  187. if($chain_order_info['chain_order_state']<ORDER_STATE_SUCCESS){//还未提货则取消
  188. $data['chain_order_state'] = ORDER_STATE_CANCEL;
  189. }
  190. }
  191. } elseif ($chain_order_info['chain_order_type'] == 1) {//代收
  192. if($chain_order_info['chain_order_state']>ORDER_STATE_PAY){//如果已经到站则是完成状态
  193. $data['chain_order_state'] = ORDER_STATE_SUCCESS;
  194. }else{
  195. $data['chain_order_state'] = ORDER_STATE_CANCEL;
  196. }
  197. }
  198. if(!empty($data)){
  199. $this->editChainOrder($data, $condition);
  200. }
  201. }
  202. }
  203. /**
  204. * 订单锁定
  205. * @access public
  206. * @author csdeshang
  207. * @param type $order_id 订单编号
  208. * @return boolean
  209. */
  210. public function editChainOrderLock($order_id) {
  211. $order_id = intval($order_id);
  212. if ($order_id > 0) {
  213. $condition = array();
  214. $condition[] = array('order_id', '=', $order_id);
  215. $data = array();
  216. $data['chain_order_lock_state'] = Db::raw('chain_order_lock_state+1');
  217. $result = $this->editChainOrder($data, $condition);
  218. return $result;
  219. }
  220. return false;
  221. }
  222. /**
  223. * 订单解锁
  224. * @access public
  225. * @author csdeshang
  226. * @param type $order_id 订单编号
  227. * @return boolean
  228. */
  229. public function editChainOrderUnlock($order_id) {
  230. $order_id = intval($order_id);
  231. if ($order_id > 0) {
  232. $condition = array();
  233. $condition[] = array('order_id', '=', $order_id);
  234. $condition[] = array('chain_order_lock_state', '>=', '1');
  235. $data = array();
  236. $data['chain_order_lock_state'] = Db::raw('chain_order_lock_state-1');
  237. $result = $this->editChainOrder($data, $condition);
  238. return $result;
  239. }
  240. return false;
  241. }
  242. /**
  243. * 添加订单代收表内容
  244. */
  245. public function saveChainOrder($param)
  246. {
  247. if (!is_array($param['order_sn_list']))
  248. return ds_callback(true);
  249. $data = array();
  250. foreach ($param['order_sn_list'] as $order_id => $v) {
  251. $data['order_id'] = $order_id;
  252. $data['order_sn'] = $v['order_sn'];
  253. $data['chain_order_add_time'] = $v['add_time'];
  254. $data['chain_id'] = $param['chain_id'];
  255. $data['chain_order_type'] = 1;
  256. $data['store_id'] = $v['store_id'];
  257. $insert = $this->addChainOrder($data);
  258. if (!$insert) {
  259. return ds_callback(false, '保存代收订单信息失败order_sn:' . $v['order_sn']);
  260. }
  261. }
  262. return ds_callback(true);
  263. }
  264. }