MReservoirArea.Class.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * 库区Model
  4. * Created by PhpStorm.
  5. * User: haoren
  6. * Date: 2020/12/14
  7. * Time: 16:00
  8. */
  9. namespace JinDouYun\Model\Stock;
  10. use JinDouYun\Dao\Stock\DStorageLocation;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Stock\DReservoirArea;
  15. use JinDouYun\Model\MBaseModel;
  16. class MReservoirArea extends MBaseModel
  17. {
  18. private $objDReservoirArea;
  19. private $userCenterId;
  20. private $enterpriseId;
  21. public function __construct($enterpriseId, $userCenterId = false)
  22. {
  23. $this->enterpriseId = $enterpriseId;
  24. $this->userCenterId = $userCenterId;
  25. $this->objDReservoirArea = new DReservoirArea('stock');
  26. }
  27. /**
  28. * 库区添加
  29. * @param $params
  30. * @return ResultWrapper
  31. * @throws Exception
  32. */
  33. public function addReservoir($params)
  34. {
  35. $params['createTime'] = time();
  36. $dbResult = $this->objDReservoirArea->insert($params);
  37. if($dbResult === false){
  38. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  39. }
  40. return ResultWrapper::success($dbResult);
  41. }
  42. /**
  43. * 库区修改
  44. * @param $update
  45. * @param $where
  46. * @return ResultWrapper
  47. */
  48. public function updateReservoir($update, $where)
  49. {
  50. $update['updateTime'] = time();
  51. $dbResult = $this->objDReservoirArea->update($update,$where);
  52. if($dbResult === false){
  53. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  54. }
  55. return ResultWrapper::success($dbResult);
  56. }
  57. /**
  58. * 库区列表
  59. * @param $selectParams
  60. * @return ResultWrapper
  61. */
  62. public function getAllReservoir($selectParams)
  63. {
  64. $limit = $selectParams['limit'];
  65. unset($selectParams['limit']);
  66. $offset = $selectParams['offset'];
  67. unset($selectParams['offset']);
  68. $selectParams['deleteStatus'] = StatusCode::$standard;
  69. $dbResult = $this->objDReservoirArea->select($selectParams, '*', 'createTime desc', $limit, $offset);
  70. if ($dbResult === false) {
  71. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  72. }
  73. $count = $this->objDReservoirArea->count($selectParams);
  74. if ($count === false) {
  75. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  76. }
  77. $modelResult = self::format($dbResult);
  78. if(!$modelResult->isSuccess()){
  79. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  80. }
  81. $data = $modelResult->getData();
  82. unset($modelResult);
  83. $return = [
  84. 'data' => $data,
  85. 'total' => $count,
  86. ];
  87. return ResultWrapper::success($return);
  88. }
  89. /**
  90. * 库区列表(不分页)
  91. * @param $selectParams
  92. * @return ResultWrapper
  93. */
  94. public function getListReservoir($selectParams)
  95. {
  96. $selectParams['deleteStatus'] = StatusCode::$standard;
  97. $dbResult = $this->objDReservoirArea->select($selectParams, '*', 'createTime desc');
  98. if ($dbResult === false) {
  99. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  100. }
  101. foreach($dbResult as &$value){
  102. $value['reservoirName'] = $value['name'].'('.$value['code'].')';
  103. }
  104. unset($value);
  105. return ResultWrapper::success($dbResult);
  106. }
  107. /**
  108. * 库区详情
  109. * @param $where
  110. * @return ResultWrapper
  111. */
  112. public function getReservoirInfo($where)
  113. {
  114. $dbResult = $this->objDReservoirArea->get($where);
  115. if($dbResult === false){
  116. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  117. }
  118. $data = self::format([$dbResult]);
  119. if(!$data->isSuccess()){
  120. return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
  121. }
  122. $data = $data->getData();
  123. return ResultWrapper::success(array_shift($data));
  124. }
  125. /**
  126. * 格式化库区数据
  127. * @param $params
  128. * @return ResultWrapper
  129. */
  130. public function format($params)
  131. {
  132. if(empty($params)){
  133. return ResultWrapper::success($params);
  134. }
  135. // 提取仓库ids
  136. $warehouseIds = [];
  137. foreach ($params as $key => $value){
  138. $warehouseIds[] = $value['warehouseId'];
  139. }
  140. $warehouseData = [];
  141. if(!empty($warehouseIds)){
  142. $objMWarehouse = new MWarehouse($this->enterpriseId, $this->userCenterId);
  143. $modelResult = $objMWarehouse->getNameByIds($warehouseIds);
  144. if(!$modelResult->isSuccess()){
  145. return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
  146. }
  147. $warehouseData = $modelResult->getData();
  148. unset($modelResult);
  149. }
  150. foreach($params as &$value){
  151. $value['warehouseName'] = isset($warehouseData[$value['warehouseId']]) ? $warehouseData[$value['warehouseId']]['warehouseName'] : '';
  152. }
  153. unset($value);
  154. return ResultWrapper::success($params);
  155. }
  156. }