MStorageLocation.Class.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: kang
  5. * Date: 2021/6/9
  6. * Time: 17:31
  7. */
  8. namespace JinDouYun\Model\Stock;
  9. use JinDouYun\Dao\Stock\DReservoirArea;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\StatusCode;
  12. use Mall\Framework\Core\ResultWrapper;
  13. use JinDouYun\Dao\Stock\DStorageLocation;
  14. use JinDouYun\Model\MBaseModel;
  15. class MStorageLocation extends MBaseModel
  16. {
  17. private $objDStorageLocation;
  18. private $userCenterId;
  19. private $enterpriseId;
  20. public function __construct($enterpriseId, $userCenterId = false)
  21. {
  22. $this->enterpriseId = $enterpriseId;
  23. $this->userCenterId = $userCenterId;
  24. $this->objDStorageLocation = new DStorageLocation('stock');
  25. }
  26. /**
  27. * 仓库库位添加
  28. * @param $params
  29. * @return ResultWrapper
  30. * @throws Exception
  31. */
  32. public function addStorageLocation($params)
  33. {
  34. $params['createTime'] = time();
  35. $dbResult = $this->objDStorageLocation->insert($params);
  36. if($dbResult === false){
  37. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  38. }
  39. return ResultWrapper::success($dbResult);
  40. }
  41. /**
  42. * 仓库库位修改
  43. * @param $update
  44. * @param $where
  45. * @return ResultWrapper
  46. */
  47. public function updateStorageLocation($update, $where)
  48. {
  49. $update['updateTime'] = time();
  50. $dbResult = $this->objDStorageLocation->update($update,$where);
  51. if($dbResult === false){
  52. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  53. }
  54. return ResultWrapper::success($dbResult);
  55. }
  56. /**
  57. * 仓库库位列表
  58. * @param $selectParams
  59. * @return ResultWrapper
  60. */
  61. public function getAllStorageLocation($selectParams)
  62. {
  63. $limit = $selectParams['limit'];
  64. unset($selectParams['limit']);
  65. $offset = $selectParams['offset'];
  66. unset($selectParams['offset']);
  67. $where ='';
  68. $where .=' where l.deleteStatus ='.StatusCode::$standard . ' and l.enterpriseId = '. $this->enterpriseId;
  69. if(!empty($selectParams['areaId'])){
  70. $where .=' and l.areaId ='.$selectParams['areaId'];
  71. }
  72. if(!empty($selectParams['enableStatus'])){
  73. $where .=' and l.enableStatus ='.$selectParams['enableStatus'];
  74. }
  75. if(!empty($selectParams['warehouseId'])){
  76. $where .=' and a.warehouseId ='.$selectParams['warehouseId'];
  77. }
  78. $sql = 'SELECT l.*,a.warehouseId,a.code as areaCode FROM qianniao_storage_Location as l LEFT JOIN qianniao_reservoir_area as a on a.id = l.areaId'.$where.' order by l.createTime desc limit '.$offset.','.$limit;
  79. $dbResult = $this->objDStorageLocation->query($sql);
  80. if ($dbResult === false) {
  81. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  82. }
  83. $sql = 'SELECT count(*) as total ,a.warehouseId,a.code as areaCode FROM qianniao_storage_Location as l LEFT JOIN qianniao_reservoir_area as a on a.id = l.areaId'.$where;
  84. $count = $this->objDStorageLocation->query($sql);
  85. if ($count === false) {
  86. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  87. }
  88. $total = 0;
  89. foreach ($count as $key => $value){
  90. $total = $count[$key]['total'];
  91. }
  92. $data = self::format($dbResult);
  93. if(!$data->isSuccess()){
  94. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  95. }
  96. $data = $data->getData();
  97. $return = [
  98. 'data' => $data,
  99. 'total' => $total ? $total : 0,
  100. ];
  101. return ResultWrapper::success($return);
  102. }
  103. /**
  104. * 仓库库位列表(不分页)
  105. * @param $selectParams
  106. * @return ResultWrapper
  107. */
  108. public function getListStorageLocation($selectParams)
  109. {
  110. $selectParams['deleteStatus'] = StatusCode::$standard;
  111. $dbResult = $this->objDStorageLocation->select($selectParams, '*', 'createTime desc');
  112. if ($dbResult === false) {
  113. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  114. }
  115. foreach($dbResult as &$value){
  116. $value['reservoirName'] = $value['name'].'('.$value['code'].')';
  117. }
  118. unset($value);
  119. return ResultWrapper::success($dbResult);
  120. }
  121. /**
  122. * 仓库库位详情
  123. * @param $where
  124. * @return ResultWrapper
  125. */
  126. public function getStorageLocationInfo($where)
  127. {
  128. $dbResult = $this->objDStorageLocation->get($where);
  129. if($dbResult === false){
  130. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  131. }
  132. $data = self::format([$dbResult]);
  133. if(!$data->isSuccess()){
  134. return ResultWrapper::fail($this->objDStorageLocation->error(), ErrorCode::$dberror);
  135. }
  136. $data = $data->getData();
  137. return ResultWrapper::success(array_pop($data));
  138. }
  139. /**
  140. * 格式化
  141. * @return array
  142. */
  143. public function format($data)
  144. {
  145. // 根据库区id格式化库区名称
  146. $areaIdBindName = [];
  147. $areaIds = [];
  148. foreach ($data as $k => $v){
  149. if(!empty($v['areaId'])){
  150. $areaIds[] = $v['areaId'];
  151. }
  152. }
  153. if(!empty($areaIds)){
  154. $objDReservoirArea = new DReservoirArea();
  155. $areaData = $objDReservoirArea->select($areaIds, 'id,name');
  156. if($areaData === false){
  157. return ResultWrapper::fail($objDReservoirArea->error(), ErrorCode::$dberror);
  158. }
  159. if(!empty($areaData)){
  160. foreach ($areaData as $k => $v){
  161. $areaIdBindName[$v['id']] = $v['name'];
  162. }
  163. }
  164. // 循环主数据,进行对应的字段映射操作
  165. foreach ($data as $key => $value){
  166. $data[$key]['areaName'] = isset($areaIdBindName[$value['areaId']]) ? $areaIdBindName[$value['areaId']] :'';
  167. }
  168. }
  169. return ResultWrapper::success($data);
  170. }
  171. }