ChainOrder.php 9.9 KB

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