123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174 |
- <?php
- /**
- * 库区Model
- * Created by PhpStorm.
- * User: haoren
- * Date: 2020/12/14
- * Time: 16:00
- */
- namespace JinDouYun\Model\Stock;
- use JinDouYun\Dao\Stock\DStorageLocation;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Stock\DReservoirArea;
- use JinDouYun\Model\MBaseModel;
- class MReservoirArea extends MBaseModel
- {
- private $objDReservoirArea;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDReservoirArea = new DReservoirArea('stock');
- }
- /**
- * 库区添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addReservoir($params)
- {
- $params['createTime'] = time();
- $dbResult = $this->objDReservoirArea->insert($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 库区修改
- * @param $update
- * @param $where
- * @return ResultWrapper
- */
- public function updateReservoir($update, $where)
- {
- $update['updateTime'] = time();
- $dbResult = $this->objDReservoirArea->update($update,$where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 库区列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllReservoir($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDReservoirArea->select($selectParams, '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- $count = $this->objDReservoirArea->count($selectParams);
- if ($count === false) {
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
-
- $modelResult = self::format($dbResult);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $data = $modelResult->getData();
- unset($modelResult);
-
- $return = [
- 'data' => $data,
- 'total' => $count,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 库区列表(不分页)
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getListReservoir($selectParams)
- {
- $selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDReservoirArea->select($selectParams, '*', 'createTime desc');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- foreach($dbResult as &$value){
- $value['reservoirName'] = $value['name'].'('.$value['code'].')';
- }
- unset($value);
- return ResultWrapper::success($dbResult);
- }
- /**
- * 库区详情
- * @param $where
- * @return ResultWrapper
- */
- public function getReservoirInfo($where)
- {
- $dbResult = $this->objDReservoirArea->get($where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- $data = self::format([$dbResult]);
- if(!$data->isSuccess()){
- return ResultWrapper::fail($this->objDReservoirArea->error(), ErrorCode::$dberror);
- }
- $data = $data->getData();
- return ResultWrapper::success(array_shift($data));
- }
-
- /**
- * 格式化库区数据
- * @param $params
- * @return ResultWrapper
- */
- public function format($params)
- {
- if(empty($params)){
- return ResultWrapper::success($params);
- }
-
- // 提取仓库ids
- $warehouseIds = [];
- foreach ($params as $key => $value){
- $warehouseIds[] = $value['warehouseId'];
- }
-
- $warehouseData = [];
- if(!empty($warehouseIds)){
- $objMWarehouse = new MWarehouse($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMWarehouse->getNameByIds($warehouseIds);
- if(!$modelResult->isSuccess()){
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $warehouseData = $modelResult->getData();
- unset($modelResult);
- }
- foreach($params as &$value){
- $value['warehouseName'] = isset($warehouseData[$value['warehouseId']]) ? $warehouseData[$value['warehouseId']]['warehouseName'] : '';
- }
- unset($value);
- return ResultWrapper::success($params);
- }
- }
|