ChainOrder.php 10 KB

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