MOrderPicking.Class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Gss
  5. * Date: 2021/5/19 0019
  6. * Time: 10:51
  7. */
  8. namespace JinDouYun\Model\Order;
  9. use JinDouYun\Controller\Market\VipCard;
  10. use JinDouYun\Controller\Order\Order;
  11. use JinDouYun\Dao\Order\DOrder;
  12. use JinDouYun\Dao\Order\DOrderGoods;
  13. use JinDouYun\Dao\Order\DOrderIndex;
  14. use JinDouYun\Dao\Order\DOrderPicking;
  15. use JinDouYun\Dao\Order\DOrderReceive;
  16. use JinDouYun\Model\MBaseModel;
  17. use JinDouYun\Model\SysAreaChina\MSysAreaChina;
  18. use Mall\Framework\Core\ErrorCode;
  19. use Mall\Framework\Core\ResultWrapper;
  20. use Mall\Framework\Core\StatusCode;
  21. class MOrderPicking extends MBaseModel
  22. {
  23. /**
  24. * @var int 用户id
  25. */
  26. private $onlineUserId;
  27. /**
  28. * @var int 企业id
  29. */
  30. private $onlineEnterpriseId;
  31. /**
  32. * @var int 单表数据量
  33. */
  34. private $cutTable = 200000;
  35. private $loginUserCenterId;
  36. private $isCashier;
  37. private $isFront;
  38. private $objDOrderPicking;
  39. /**
  40. * MOrder constructor.
  41. * @param $onlineUserId
  42. * @param $onlineEnterpriseId
  43. * @param null $loginUserCenterId
  44. * @param bool $isFront
  45. * @throws \Exception
  46. */
  47. public function __construct($onlineUserId, $onlineEnterpriseId, $loginUserCenterId = null, $isFront = false, $isCashier = false)
  48. {
  49. $this->isCashier = $isCashier;
  50. $this->isFront = $isFront;
  51. $this->onlineUserId = $onlineUserId;
  52. $this->onlineEnterpriseId = $onlineEnterpriseId;
  53. $this->loginUserCenterId = $loginUserCenterId;
  54. parent::__construct($this->onlineEnterpriseId, $loginUserCenterId);
  55. $this->objDOrderPicking = new DOrderPicking('default');
  56. self::orderPickSubTable($onlineEnterpriseId, $onlineUserId);
  57. }
  58. /**
  59. * 订单拣货表切表
  60. */
  61. public function orderPickSubTable($enterpriseId, $userId)
  62. {
  63. $tableName = $this->objDOrderPicking->getTableName('qianniao_order_picking_' . $enterpriseId, $userId, $this->cutTable);
  64. $this->objDOrderPicking->setTable($tableName);
  65. }
  66. /**
  67. * 获取订单拣货表表名
  68. */
  69. public function getOrderPickTable($enterpriseId, $userId){
  70. return $this->objDOrderPicking->getTableName('qianniao_order_picking_' . $enterpriseId, $userId, $this->cutTable);
  71. }
  72. /**
  73. * 列表
  74. */
  75. public function getAllOrderPicking($selectParams)
  76. {
  77. $limit = $selectParams['limit'];
  78. unset($selectParams['limit']);
  79. $offset = $selectParams['offset'];
  80. unset($selectParams['offset']);
  81. $whereSql = '';
  82. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  83. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  84. $whereSql .= $where . ' no like "%' . $selectParams['no'] .'%"';
  85. }
  86. //时间删选 有timeName字段优先
  87. if( isset($selectParams['timeName']) && !empty($selectParams['timeName']) ){
  88. $timeName = $selectParams['timeName'];
  89. $timestamp = gettimestamp($timeName,time());
  90. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  91. $whereSql .= $where . ' createTime BETWEEN ' . $timestamp['start'] . ' AND '. $timestamp['end'];
  92. }else{
  93. if ( (isset($selectParams['startTime']) && !empty($selectParams['startTime']))&&(isset($selectParams['endTime']) && !empty($selectParams['endTime'])) ) {
  94. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  95. $whereSql .= $where . ' createTime BETWEEN ' . $selectParams['startTime'] . ' AND '. $selectParams['endTime'];
  96. }
  97. }
  98. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  99. $whereSql .= $where . ' orderStatus != ' . StatusCode::$orderStatus['finish'].' and orderStatus != '.StatusCode::$orderStatus['close'].' and pickingStatus = '.StatusCode::$orderPickStatus['notPicking'] ;
  100. $objOrder = new DOrder();
  101. $objDOrderIndex = new DOrderIndex();
  102. $objDOrderReceive = new DOrderReceive();
  103. $objMSysAreaChina = new MSysAreaChina();
  104. $indexSql = 'select * from qianniao_order_index_'.$this->onlineEnterpriseId.$whereSql . ' ORDER BY createTime DESC LIMIT ' . $offset . ' , ' . $limit;
  105. $indexDbResult = $objDOrderIndex->query($indexSql);
  106. if ($indexDbResult === false) {
  107. return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
  108. }
  109. $totalSql = 'SELECT COUNT(1) as count FROM qianniao_order_index_'.$this->onlineEnterpriseId.$whereSql;
  110. $dbTotalResult = $objDOrderIndex->query($totalSql);
  111. if ($dbTotalResult === false) {
  112. return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
  113. }
  114. if(empty($dbTotalResult)){
  115. return ResultWrapper::success([]);
  116. }
  117. $allOrderData = [];//订单集
  118. $allOrderReceiveData = [];//订单收货地址集
  119. $allGroupData = []; // 订单id
  120. if(!empty($indexDbResult)){
  121. foreach ($indexDbResult as $indexKey => $indexValue) {
  122. $tableNum = ceil($indexValue['userCenterId'] / $this->cutTable);
  123. $allGroupData[$tableNum][] = $indexValue['id'];
  124. }
  125. //
  126. foreach ($allGroupData as $k => $orderId) {
  127. $objOrder->setTable('qianniao_order_' . $this->onlineEnterpriseId . '_' . $k);
  128. $tmpOrderId = implode(',',$orderId);
  129. $orderSql = 'select o.*, p.sort,p.outNum,p.notOutNum,p.orderNum,p.notPickingNum,p.pickingNum,p.otherNum,p.mangeId,p.mangeName from '.$objOrder->get_Table().' as o left join '.$this->objDOrderPicking->get_Table().' as p on o.id=p.orderId where o.id in ('.$tmpOrderId.') ORDER BY o.createTime desc';
  130. $dbResult = $objOrder->query($orderSql);
  131. // $dbResult = $objOrder->select(['id' => $orderId], '*', 'createTime desc');//查询订单
  132. if ($dbResult === false) {
  133. return ResultWrapper::fail($objOrder->error(), ErrorCode::$dberror);
  134. }
  135. $objDOrderReceive->setTable('qianniao_order_receive_' . $this->onlineEnterpriseId . '_' . $k);
  136. $orderReceiveResult = $objDOrderReceive->select(['orderId' => $orderId], '*', 'createTime desc');//订单收货
  137. if ($orderReceiveResult === false) {
  138. return ResultWrapper::fail($objDOrderReceive->error(), ErrorCode::$dberror);
  139. }
  140. $allOrderData = array_merge($dbResult, $allOrderData);
  141. $allOrderReceiveData = array_merge($orderReceiveResult, $allOrderReceiveData);
  142. }
  143. }
  144. //根据订单处理地址信息
  145. foreach ($allOrderReceiveData as $receiveKey => $receiveValue){
  146. // 渲染地址
  147. if (isset($receiveValue['provinceCode']) && isset($receiveValue['cityCode']) && isset($receiveValue['districtCode'])) {
  148. $areaName = $objMSysAreaChina->getNameByCode([
  149. $receiveValue['provinceCode'],
  150. $receiveValue['cityCode'],
  151. $receiveValue['districtCode']
  152. ]);
  153. $allOrderReceiveData[$receiveKey]['area']['provinceName'] = isset($areaName[$receiveValue['provinceCode']]) ? $areaName[$receiveValue['provinceCode']] : '';
  154. $allOrderReceiveData[$receiveKey]['area']['cityName'] = isset($areaName[$receiveValue['cityCode']]) ? $areaName[$receiveValue['cityCode']] : '';
  155. $allOrderReceiveData[$receiveKey]['area']['districtName'] = isset($areaName[$receiveValue['districtCode']]) ? $areaName[$receiveValue['districtCode']] : '';
  156. }
  157. $allOrderReceiveData[$receiveValue['orderId']] = $allOrderReceiveData[$receiveKey];
  158. unset($allOrderReceiveData[$receiveKey]);
  159. }
  160. foreach ($allOrderData as $buildKey=>$buildValue){
  161. //物流信息展示
  162. if( !empty($buildValue['logisticsData']) ){
  163. $allOrderData[$buildKey]['logisticsData'] = json_decode($buildValue['logisticsData'],true);
  164. }
  165. //备注
  166. if( !empty($buildValue['extends']) ){
  167. $allOrderData[$buildKey]['extends'] = json_decode($buildValue['extends'],true);
  168. }
  169. //渲染编号
  170. if( !empty($buildValue['no']) ){
  171. $allOrderData[$buildKey]['no'] = StatusCode::$noPrefix[1].'-'.$buildValue['no'];
  172. }
  173. //配送方式
  174. if (isset($buildValue['deliveryType'])) {
  175. $allOrderData[$buildKey]['deliveryMsg'] = isset(StatusCode::$deliveryType[$buildValue['deliveryType']]) ? StatusCode::$deliveryType[$buildValue['deliveryType']] : '';
  176. }
  177. //根据订单id来组装地址
  178. $allOrderData[$buildKey]['orderReceiveData'] = $allOrderReceiveData[$buildValue['id']];
  179. }
  180. $return = [
  181. 'data' => $allOrderData,
  182. 'total' => $dbTotalResult[0]['count'],
  183. ];
  184. return ResultWrapper::success($return);
  185. }
  186. /**
  187. * 订单拣货详情
  188. */
  189. public function getOrderPickingInfo($pickingId)
  190. {
  191. $objOrder = new DOrder();
  192. $objDOrderIndex = new DOrderIndex();
  193. $objDOrderReceive = new DOrderReceive();
  194. $objMSysAreaChina = new MSysAreaChina();
  195. $objDOrderGoods = new DOrderGoods();
  196. $indexDbResult = $objDOrderIndex->query('select * from qianniao_order_index_'.$this->onlineEnterpriseId.' where id = '.$pickingId);
  197. if ($indexDbResult === false) {
  198. return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
  199. }
  200. //订单表名
  201. $tableNum = ceil($indexDbResult[0]['userCenterId'] / $this->cutTable);
  202. //获取订单状态不是已完成,已关闭的
  203. $orderDate = $objOrder->query('select * from qianniao_order_'.$this->onlineEnterpriseId.'_'.$tableNum.' where id = '.$pickingId);
  204. if ($orderDate === false) {
  205. return ResultWrapper::fail($objOrder->error(), ErrorCode::$dberror);
  206. }
  207. $orderDate = array_shift($orderDate);
  208. //配送方式
  209. if (isset($orderDate['deliveryType'])) {
  210. $orderDate['deliveryMsg'] = isset(StatusCode::$deliveryType[$orderDate['deliveryType']]) ? StatusCode::$deliveryType[$orderDate['deliveryType']] : '';
  211. }
  212. //渲染编号
  213. if( !empty($orderDate['no']) ){
  214. $orderDate['no'] = StatusCode::$noPrefix[1].'-'.$orderDate['no'];
  215. }
  216. $objDOrderReceive->setTable('qianniao_order_receive_' . $this->onlineEnterpriseId . '_' . $tableNum);
  217. $orderReceiveData = $objDOrderReceive->get(['orderId' => $pickingId], '*', 'createTime desc');
  218. if ($orderReceiveData === false) {
  219. return ResultWrapper::fail($objDOrderReceive->error(), ErrorCode::$dberror);
  220. }
  221. //根据订单处理地址信息
  222. if (isset($orderReceiveData['provinceCode']) && isset($orderReceiveData['cityCode']) && isset($orderReceiveData['districtCode'])) {
  223. $areaName = $objMSysAreaChina->getNameByCode([
  224. $orderReceiveData['provinceCode'],
  225. $orderReceiveData['cityCode'],
  226. $orderReceiveData['districtCode']
  227. ]);
  228. $orderReceiveData['area']['provinceName'] = isset($areaName[$orderReceiveData['provinceCode']]) ? $areaName[$orderReceiveData['provinceCode']] : '';
  229. $orderReceiveData['area']['cityName'] = isset($areaName[$orderReceiveData['cityCode']]) ? $areaName[$orderReceiveData['cityCode']] : '';
  230. $orderReceiveData['area']['districtName'] = isset($areaName[$orderReceiveData['districtCode']]) ? $areaName[$orderReceiveData['districtCode']] : '';
  231. }
  232. $orderDate['orderReceive'] = $orderReceiveData;
  233. //获取订单商品信息
  234. $goodsDbResult = $objDOrderGoods->query('select * from qianniao_order_goods_'.$this->onlineEnterpriseId.'_'.$tableNum.' where orderId='.$pickingId .' and deleteStatus='.StatusCode::$standard);
  235. if ($goodsDbResult === false) {
  236. return ResultWrapper::fail($objDOrderGoods->error(), ErrorCode::$dberror);
  237. }
  238. $orderDate['goodsDate'] = $goodsDbResult;
  239. return ResultWrapper::success($orderDate);
  240. }
  241. /**
  242. * 待拣货明细
  243. */
  244. public function getAllPickingGoodsDetail($selectParams)
  245. {
  246. $limit = $selectParams['limit'];
  247. unset($selectParams['limit']);
  248. $offset = $selectParams['offset'];
  249. unset($selectParams['offset']);
  250. $whereSql = '';
  251. if (isset($selectParams['no']) && !empty($selectParams['no'])) {
  252. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  253. $whereSql .= $where . ' o.no like "%' . $selectParams['no'] .'%"';
  254. }
  255. if (isset($selectParams['goodsName']) && !empty($selectParams['goodsName'])) {
  256. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  257. $whereSql .= $where . ' g.goodsName like "%'.$selectParams['goodsName'].'%"';
  258. }
  259. if (isset($selectParams['operatorId']) && !empty($selectParams['operatorId'])) {
  260. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  261. $whereSql .= $where . ' o.operatorId =' . $selectParams['operatorId'];
  262. }
  263. if ( (isset($selectParams['start']) && !empty($selectParams['start']))&&(isset($selectParams['end']) && !empty($selectParams['end'])) ) {
  264. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  265. $whereSql .= $where . ' o.createTime BETWEEN ' . $selectParams['start'] . ' AND '. $selectParams['end'];
  266. }
  267. $where = empty($whereSql) ? ' WHERE ' : ' AND ';
  268. $whereSql .= $where . ' o.orderStatus != ' . StatusCode::$orderStatus['finish'].' and o.orderStatus != '.StatusCode::$orderStatus['close'].' and o.pickingStatus = '.StatusCode::$orderPickStatus['notPicking'] ;
  269. $objMOrder = new MOrder($this->onlineUserId,$this->onlineEnterpriseId);
  270. $orderTable = $objMOrder->getOrderTable($this->onlineEnterpriseId,$this->onlineUserId);
  271. $tableNum = substr($orderTable,-1);
  272. $objDOrderIndex = new DOrderIndex();
  273. $orderSql = 'select o.`no`,o.customerId,o.customerName,o.operatorId,o.operatorName,o.createTime,g.* from '.$orderTable.' as o left join qianniao_order_goods_64_'.$tableNum.' as g on o.id=g.orderId '.$whereSql.' ORDER BY o.createTime desc LIMIT ' . $offset . ' , ' . $limit;
  274. $indexDbResult = $objDOrderIndex->query($orderSql);
  275. if ($indexDbResult === false) {
  276. return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
  277. }
  278. // 商品规格信息
  279. foreach ($indexDbResult as $key => $value){
  280. // 订单类型
  281. $indexDbResult[$key]['orderType'] = '销售订单';
  282. if( isset($value['specGroup']) ){
  283. if ($value['specGroup'] == 'null'){
  284. $indexDbResult[$key]['specGroup'] = json_encode([],true);
  285. }
  286. if (!empty($value['specGroup']) && $value['specGroup'] != 'null' && $value['specGroup'] != '[]'){
  287. $indexDbResult[$key]['specGroup'] = json_encode($value['specGroup'],true);
  288. }
  289. }
  290. }
  291. $totalSql = 'select COUNT(1) AS count from '.$orderTable.' as o left join qianniao_order_goods_64_'.$tableNum.' as g on o.id=g.orderId '.$whereSql;
  292. $dbTotalResult = $objDOrderIndex->query($totalSql);
  293. if ($dbTotalResult === false) {
  294. return ResultWrapper::fail($objDOrderIndex->error(), ErrorCode::$dberror);
  295. }
  296. if(empty($dbTotalResult)){
  297. return ResultWrapper::success([]);
  298. }
  299. $return = [
  300. 'data' => $indexDbResult,
  301. 'total' => $dbTotalResult[0]['count'],
  302. ];
  303. return ResultWrapper::success($return);
  304. }
  305. /**
  306. * 订单拣货表 新增订单时用
  307. */
  308. public function addOrderPick($params)
  309. {
  310. $orderPickingDate = [
  311. 'orderId' => getArrayItem($params,'orderId',0),
  312. 'sort' => getArrayItem($params,'sort',0),
  313. 'outNum' => getArrayItem($params,'outNum',0),
  314. 'notOutNum' => getArrayItem($params,'notOutNum',0),
  315. 'orderNum' => getArrayItem($params,'orderNum',0),
  316. 'notPickingNum' => getArrayItem($params,'notPickingNum',0),
  317. 'pickingNum' => getArrayItem($params,'pickingNum',0),
  318. 'outTime' => getArrayItem($params,'outTime',0),
  319. 'mangeId' => getArrayItem($params,'mangeId',0),
  320. 'mangeName' => getArrayItem($params,'mangeName',''),
  321. ];
  322. foreach ($orderPickingDate as $key => $value) {
  323. if (empty($value) && $value !== 0) {
  324. return ResultWrapper::fail('参数错误,拣货单创建失败', ErrorCode::$paramError);
  325. }
  326. }
  327. $orderPickingDate['createTime'] = time();
  328. $beginTransactionstatus = $this->objDOrderPicking->beginTransaction();
  329. $dbResult = $this->objDOrderPicking->insert($orderPickingDate);
  330. if ($dbResult === false) {
  331. $this->objDOrderPicking->rollBack();
  332. return ResultWrapper::fail($this->objDOrderPicking->error(), ErrorCode::$dberror);
  333. }
  334. if( $beginTransactionstatus ){
  335. $this->objDOrderPicking->commit();
  336. }
  337. return ResultWrapper::success($dbResult);
  338. }
  339. }