ChainOrder.php 10 KB

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