123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350 |
- <?php
- /**
- * 仓库管理Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/11/09
- * Time: 15:00
- */
- namespace JinDouYun\Model\Stock;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Cache\GoodsBasicRelevant;
- use JinDouYun\Model\SysAreaChina\MSysAreaChina;
- use Jindouyun\Model\Shop\MShop;
- use JinDouYun\Dao\Stock\DWarehouse;
- use JinDouYun\Dao\Stock\DWarehouseBeginning;
- class MWarehouse
- {
- private $objDWarehouse;
- private $objDWarehouseBeginning;
- private $userCenterId;
- private $enterpriseId;
- public function __construct($enterpriseId, $userCenterId = false)
- {
- $this->enterpriseId = $enterpriseId;
- $this->userCenterId = $userCenterId;
- $this->objDWarehouse = new DWarehouse('stock');
- $this->objDWarehouseBeginning = new DWarehouseBeginning('stock');
- $this->objDWarehouse->setTable('qianniao_warehouse_'.$enterpriseId);
- $this->objDWarehouseBeginning->setTable('qianniao_warehouse_beginning_'.$enterpriseId);
- }
- /**
- * 仓库添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addWarehouse($params)
- {
- $params['createTime'] = time();
- $params['updateTime'] = time();
- $dbResult = $this->objDWarehouse->insert($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- $warehouseId = $dbResult;
- unset($dbResult);
- $update = [
- 'warehouseCode' => createCode(StatusCode::$code['warehouse']['prefix'], $warehouseId, StatusCode::$code['warehouse']['length']),
- 'updateTime' => time(),
- ];
- $result = $this->objDWarehouse->update($update, ['id' => $warehouseId]);
- if($result === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($warehouseId);
- }
- /**
- * 仓库期初记录添加
- * @param $params
- * @return ResultWrapper
- * @throws Exception
- */
- public function addWarehouseBeginning($params)
- {
- $warehouseId = 0;
- $addWarehouseBeginningData = [];
- foreach($params as $value){
- $warehouseId = $value['warehouseId'];
- $addWarehouseBeginningData[] = [
- 'no' => createOrderSn(StatusCode::$source['manage'], StatusCode::$orderType['warehouseBeginning'],$value['operatorId'] ),
- 'warehouseId' => $value['warehouseId'],
- 'operatorId' => $value['operatorId'],
- 'operatorName' => $value['operatorName'],
- 'remark' => $value['remark'],
- 'materielId' => $value['materielId'],
- 'materielName' => $value['materielName'],
- 'materielCode' => $value['materielCode'],
- 'skuId' => $value['skuId'],
- 'num' => $value['num'],
- 'costPrice' => $value['costPrice'],
- 'updateTime' => time(),
- 'createTime' => time(),
- ];
- }
- if(empty($warehouseId)) return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
- $dbResult = $this->objDWarehouse->get($warehouseId, 'id,beginningStatus');
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- if($dbResult['beginningStatus'] == StatusCode::$delete){
- return ResultWrapper::fail('期初功能已失效', ErrorCode::$paramError);
- }
- $this->objDWarehouseBeginning->beginTransaction();
- //添加期初记录
- $dbResult = $this->objDWarehouseBeginning->insert($addWarehouseBeginningData, true);
- if($dbResult === false){
- $this->objDWarehouseBeginning->rollBack();
- return ResultWrapper::fail($this->objDWarehouseBeginning->error(), ErrorCode::$dberror);
- }
- $addIds = $dbResult;
- unset($dbResult);
- //修改期初状态
- $dbResult = $this->objDWarehouse->update(['beginningStatus'=>StatusCode::$delete],['id'=>$warehouseId]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- //添加库存
- $objMInventory = new MInventory($this->enterpriseId, $this->userCenterId);
- $modelResult = $objMInventory->addInventoryByWarehouseBeginning($addWarehouseBeginningData, $addIds);
- if(!$modelResult->isSuccess()){
- $this->objDWarehouseBeginning->rollBack();
- return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode());
- }
- $this->objDWarehouseBeginning->commit();
- return ResultWrapper::success($dbResult);
- }
- /**
- * 仓库删除
- * @param $where
- * @return ResultWrapper
- */
- public function deleteWarehouse($where)
- {
- $params = [
- 'deleteStatus' => StatusCode::$delete,
- 'updateTime' => time(),
- ];
- $dbResult = $this->objDWarehouse->update($params, $where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 仓库启用/禁用
- * @param $where
- * @return ResultWrapper
- */
- public function enableWarehouse($where)
- {
- $params['updateTime'] = time();
- $params['enableStatus'] = $where['enableStatus'];
- unset($where['enableStatus']);
- $dbResult = $this->objDWarehouse->update($params, ['id' => $where['id']]);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 仓库修改
- * @param $params
- * @param $where
- * @return ResultWrapper
- * @throws Exception
- */
- public function updateWarehouse($params, $where)
- {
- $dbResult = $this->objDWarehouse->update($params,$where);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 仓库列表
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getAllWarehouse($selectParams)
- {
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- if(isset($selectParams['shopId'])){
- // 如果多门店/多商户只查看自己店铺对应仓库
- $objMShop = new MShop($this->enterpriseId, 1);
- $result = $objMShop->getShopById($selectParams['shopId']);
- if(!$result->isSuccess()){
- return ResultWrapper::fail('查询店铺对应仓库数据为空', ErrorCode::$contentNotExists);
- }
- $shopData = $result->getData();
- $warehouseId = $shopData['warehouseId'];
- if(!is_numeric($warehouseId)){
- $warehouseId = explode(",", $warehouseId);
- }
- $dbResult = $this->objDWarehouse->select(['id'=>$warehouseId,'deleteStatus' => StatusCode::$standard], '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- $total = count($dbResult);
- }else{
- // 企业后台查看所有仓库
- $sql = 'deleteStatus = '.StatusCode::$standard;
- isset($selectParams['id']) && $sql .= ' and id != '.$selectParams['id'];
- isset($selectParams['enableStatus']) && $sql .= ' and enableStatus = '.$selectParams['enableStatus'];
- $dbResult = $this->objDWarehouse->select($sql, '*', 'createTime desc', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- $sql = 'select count(id) count from '.$this->objDWarehouse->get_Table().' where '.$sql;
- $countData = $this->objDWarehouse->query($sql);
- $total = array_shift($countData)['count'];
- }
- $return = [
- 'data' => self::format($dbResult),
- 'total' => ($total) ? intval($total) : 0,
- ];
- if($return === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }else{
- return ResultWrapper::success($return);
- }
- }
- public function format($data) {
- $objMSysAreaChina = new MSysAreaChina();
- foreach ($data as &$value) {
- $value['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $value['id'], StatusCode::$code['warehouse']['length']);
- $areaName = $objMSysAreaChina->getNameByCode([
- $value['provinceCode'],
- $value['cityCode'],
- $value['districtCode']
- ]);
- $value['area']['provinceName'] = isset($areaName[$value['provinceCode']]) ? $areaName[$value['provinceCode']] : '';
- $value['area']['cityName'] = isset($areaName[$value['cityCode']]) ? $areaName[$value['cityCode']] : '';
- $value['area']['districtName'] = isset($areaName[$value['districtCode']]) ? $areaName[$value['districtCode']] : '';
- $value['area']['contactAddress'] = $value['contactAddress'];
- }
- return $data;
- }
- /**
- * 仓库详情
- * @param $params
- * @return ResultWrapper
- */
- public function getWarehouseInfo($params)
- {
- $dbResult = $this->objDWarehouse->get($params);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }else{
- if(empty($dbResult)){
- return ResultWrapper::fail('没有仓库数据', ErrorCode::$contentNotExists);
- }
- $dbResult['warehouseCode'] = createCode(StatusCode::$code['warehouse']['prefix'], $dbResult['id'], StatusCode::$code['warehouse']['length']);
- return ResultWrapper::success($dbResult);
- }
- }
- /**
- * 根据ids数组获取name
- * @param $ids
- * @return array|ResultWrapper
- */
- public function getNameByIds($ids)
- {
- $where = ['id' => $ids];
- $dbResult = $this->objDWarehouse->select($where, 'id,warehouseName');
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- $returnData = [];
- foreach($dbResult as $key => $value){
- $returnData[$value['id']]['id'] = $value['id'];
- $returnData[$value['id']]['warehouseName'] = $value['warehouseName'];
- }
- if(empty($dbResult)){
- return ResultWrapper::fail('仓库数据为空', ErrorCode::$dberror);
- }
- return ResultWrapper::success($returnData);
- }
- /**
- * 获取仓库数据
- * @param array $where
- * @param string $field
- * @return ResultWrapper
- */
- public function selectWarehouse($where = [], $field = '*')
- {
- $dbResult = $this->objDWarehouse->select($where, $field);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取仓库数据
- * @param array $where
- * @param string $field
- * @return ResultWrapper
- */
- public function getWarehouse($where = [], $field = '*')
- {
- $dbResult = $this->objDWarehouse->get($where, $field);
- if($dbResult === false){
- return ResultWrapper::fail($this->objDWarehouse->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 查询仓库名称
- * @param $id
- */
- public function getWarehouseName($id)
- {
- $objGoodsBasicRelevantCache = new GoodsBasicRelevant($this->enterpriseId);
- $result = $objGoodsBasicRelevantCache->getNameByWarehouseId($id);
- $warehouseName = '';
- if(!$result){
- $dbResult = $this->objDWarehouse->get(['id' => $id]);
- if($dbResult){
- $warehouseName = isset($dbResult['warehouseName']) ? $dbResult['warehouseName'] : '';
- $objGoodsBasicRelevantCache->cacheWarehouseIdRelationName($id, $dbResult);
- }
- }else{
- $warehouseName = $result;
- }
- return $warehouseName;
- }
- }
|