onlineUserId = $onlineUserId; $this->onlineEnterpriseId = $onlineEnterpriseId; $this->objDOrderGoods = new DOrderGoods('default'); self::orderGoodsSubTable(); } /** * @param $enterpriseId * @param $userId * @return string * @throws \Exception */ public function orderGoodsSubTable() { $tableName = $this->objDOrderGoods->getTableName($this->objDOrderGoods->get_Table() . '_' . $this->onlineEnterpriseId, $this->onlineUserId, $this->cutTable); $this->objDOrderGoods->setTable($tableName); } public function getTable() { $tableName = $this->objDOrderGoods->get_Table(); return $tableName; } /** * 添加订单商品信息 * @param $params * @return ResultWrapper * @throws \Exception */ public function addOrderGoods($params) { $dbResult = $this->objDOrderGoods->insert($params); if ($dbResult === false) { return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror); } else { return ResultWrapper::success($dbResult); } } /** * 通过订单id,获取订单的商品详情 * @param $selectParams * @return ResultWrapper * @throws \Exception */ public function getOrderGoodsByOrderIds($selectParams) { $selectParams['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDOrderGoods->select($selectParams); if ($dbResult === false) { return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror); } else { $goodsResult = self::formatGoodsBasic($dbResult); if (!$goodsResult->isSuccess()){ return ResultWrapper::fail($goodsResult->getData(),$goodsResult->getErrorCode()); } $dbResult = $goodsResult->getData(); return ResultWrapper::success(self::formatSku($dbResult)); } } /** * Doc: (des="格式化sku") * User: XMing * Date: 2020/11/26 * Time: 4:27 下午 * @param array $params * @param string $materielIdKey * @param string $skuIdKey * @return ResultWrapper * @throws \Exception */ public function formatSkuValue(array $params,$materielIdKey = 'goodsBasicId',$skuIdKey = 'skuId',$shopId = 0): ResultWrapper { $materielIds = []; foreach ($params as $param){ $materielIds[] = $param[$materielIdKey]; } $objMSku = new MSku($this->onlineUserId, $this->onlineEnterpriseId); //获取所有skuIds $modelResult = $objMSku->getSkuDataByGoodsIds($materielIds); if (!$modelResult->isSuccess()) { return ResultWrapper::fail($modelResult->getData(), $modelResult->getErrorCode()); } $skuData = $modelResult->getData(); unset($modelResult); $objDGoods = new DGoods(); $objDGoods->setTable('qianniao_goods_'.$this->onlineEnterpriseId); foreach($params as &$value){ $value['skuValue'] = ''; $value['skuNum'] = '0'; // 获取商品信息 if($shopId != 0){ $goodsDate = $objDGoods->get(['shopId'=>$shopId,'basicGoodsId'=>$value['materielId']]); if ($goodsDate === false) { return ResultWrapper::fail($objDGoods->error(), ErrorCode::$dberror); } // 负库存销售字段 $value['isDistribution'] = $goodsDate['isDistribution'] ? $goodsDate['isDistribution'] : StatusCode::$delete; } //取出sku数据 if(isset($skuData[$value[$materielIdKey]]) && isset($skuData[$value[$materielIdKey]][$value[$skuIdKey]])){ //获取当前单位sku $selectSkuData = $skuData[$value[$materielIdKey]][$value[$skuIdKey]]; $value['isNew'] = $selectSkuData['isNew']; $value['isMaster'] = $selectSkuData['isMaster']; $skuParam = []; $masterSku = [];//主单位 //获取所有sku foreach($skuData[$value[$materielIdKey]] as $v){ if($v['isMaster'] == StatusCode::$delete){ if($selectSkuData['specType'] == 2){ //有属性 比对哈希 找出同类型单位 if($v['specGroupHash'] == $selectSkuData['specGroupHash']){ $skuParam[] = $v; } }else{ //无属性 都是同类型单位 $skuParam[] = $v; } }else{ $v['conversion'] = 1; $skuParam[] = $v; $masterSku = $v; } } if(!empty($skuParam)){ //重新排序sku $conversions = array_column($skuParam,'conversion'); if($masterSku['isNew'] == StatusCode::$standard){ $order = SORT_DESC; }else{ $order = SORT_ASC; } array_multisort($conversions,$order,$skuParam); //最小单位 $smallSku = array_pop($skuParam); $skuParam[] = $smallSku; //主单位换算比例修改 if($selectSkuData['isMaster'] == StatusCode::$standard){ $selectSkuData['conversion'] = 1; } foreach($skuParam as $sku){ //拼接比例 if($sku['isMaster'] == StatusCode::$delete){ if($masterSku['isNew'] == StatusCode::$standard) { $skuValues = '1'.$sku['unitName'].'='.floatval($sku['conversion']).$masterSku['unitName']; }else{ $skuValues = '1'.$masterSku['unitName'].'='.floatval($sku['conversion']).$sku['unitName']; } $value['skuValue'] .= (empty($value['skuValue']) ? '':'/').$skuValues; } } } } } return ResultWrapper::success($params); } /** * @param array $data * @return ResultWrapper * @throws \Exception */ public function formatGoodsBasic(array $data) { if (empty($data)){ return ResultWrapper::success([]); } $objMGoodsBasic = new MGoodsBasic($this->onlineUserId,$this->onlineEnterpriseId); $goodsBasicRes = $objMGoodsBasic->getBasicInfoByIds(['id'=>array_column($data,'goodsBasicId')]); if (!$goodsBasicRes->isSuccess()){ return ResultWrapper::fail($goodsBasicRes->getData(),$goodsBasicRes->getErrorCode()); } $goodsBasic = $goodsBasicRes->getData(); $map = []; foreach ($goodsBasic as &$row){ $map[$row['id']] = $row; } foreach ($data as $key => $item){ $data[$key]['storage'] = ''; if (!isset($map[$item['goodsBasicId']])){ continue; } $extends = empty($map[$item['goodsBasicId']]['extends']) ? [] : json_decode($map[$item['goodsBasicId']]['extends'],true); $data[$key]['storage'] = isset($extends['storage']) ? $extends['storage'] : ''; } $formatResult = self::formatSkuValue($data); if (!$formatResult->isSuccess()){ return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode()); } $data = $formatResult->getData(); return ResultWrapper::success($data); } /** * @param $data * @return mixed * @throws \Exception */ public function formatSku($data) { if (empty($data)){ return $data; } $allSkuMapping = []; $objMSku = new MSku($this->onlineUserId,$this->onlineEnterpriseId); $skuResult = $objMSku->getSpecNameBySkuId(array_values(array_unique(array_column($data,'skuId')))); if ($skuResult->isSuccess()){ $allSkuMapping = $skuResult->getData(); } foreach ($data as $key => $val){ $data[$key]['unitName'] = isset($allSkuMapping[$val['skuId']]) ? $allSkuMapping[$val['skuId']]['unitName'] : ''; $data[$key]['specGroup'] = isset($allSkuMapping[$val['skuId']]) ? $allSkuMapping[$val['skuId']]['specGroup'] : []; if (empty($data[$key]['specGroup'])){ $data[$key]['specType'] = StatusCode::$specType['single']; }else{ $data[$key]['specType'] = StatusCode::$specType['multiple']; } } return $data; } /** * 修改订单详情 * @param $updateData * @param $where * @return ResultWrapper */ public function updateDetails($updateData, $where) { $dbResult = $this->objDOrderGoods->update($updateData, $where); if($dbResult === false){ return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * @param $where * @param string $field * @return ResultWrapper */ public function getDetails($where,$field='*') { $dbResult = $this->objDOrderGoods->select($where); if($dbResult === false){ return ResultWrapper::fail($this->objDOrderGoods->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * Doc: (des="编辑商品") * User: XMing * Date: 2020/11/5 * Time: 11:37 上午 * @param array $params * @param int $orderId * @return ResultWrapper * @throws \Exception */ public function edit(array $params,int $orderId,$no): ResultWrapper { $lists = $this->objDOrderGoods->select(['orderId'=>$orderId,'deleteStatus' => StatusCode::$standard]); if ($lists === false){ return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$dberror); } if (empty($lists)){ return ResultWrapper::fail('商品数据为空',ErrorCode::$paramError); } if (!isset($params['goodsData']) || empty($params['goodsData'])){ return ResultWrapper::fail('订单商品数据为空',ErrorCode::$paramError); } //构建锁定数据 $lock = []; foreach ($lists as $v){ $lock[] = [ 'originId' => $orderId, 'shopId' => getArrayItem($v, 'shopId', 0), 'materielId' => getArrayItem($v, 'goodsBasicId', 0), 'materielCode' => getArrayItem($v, 'goodsCode', ''), 'lockingNum' => getArrayItem($v, 'buyNum', 0), 'originNo' => $no, 'source' => StatusCode::$orderType['saleOrder'], 'sourceNo' => $no, 'operatorId' => $this->onlineUserId, 'operatorName' => getArrayItem($v, 'name', ''), 'skuId' => getArrayItem($v, 'skuId', 0), 'goodsName' => getArrayItem($v, 'goodsName', ''), 'unitName' => getArrayItem($v, 'unitName', ''), 'specGroup' => getArrayItem($v, 'specGroup', null), ]; } $shopId = 0; $haveIds = [];//已存在的id $map = []; foreach ($lists as $list){ $haveIds[] = $list['id']; $map[$list['id']] = $list; $shopId = $list['shopId']; } $objMShop = new MShop($this->onlineEnterpriseId,$this->onlineUserId); $shopResult = $objMShop->getShopInfo($shopId); if (!$shopResult->isSuccess()){ return ResultWrapper::fail($shopResult->getData(),$shopResult->getErrorCode()); } $shop = $shopResult->getData(); if (empty($shop)){ return ResultWrapper::fail('获取店铺信息失败',ErrorCode::$paramError); } $update = []; $insert = []; $objMSku = new MSku($this->onlineUserId,$this->onlineEnterpriseId); foreach ($params['goodsData'] as $item){ if($item['isEq'] ==StatusCode::$standard ){ //超码商品 $basicGoodsId = isset($item['basicGoodsId']) ? $item['basicGoodsId'] : $item['goodsBasicId']; $masterSkuResult = $objMSku->getMasterById($basicGoodsId); if (!$masterSkuResult->isSuccess()){ return ResultWrapper::fail($masterSkuResult->getData(),$masterSkuResult->getErrorCode()); } $masterSku = $masterSkuResult->getData(); if (empty($masterSku)){ return ResultWrapper::fail('超码商品单位获取异常',ErrorCode::$paramError); } $masterSku = $masterSkuResult->getData(); //抄码商品为了支持主单位购买 不进行换算 if($item['skuId'] != $masterSku['id']){ //辅单位 $conversion = $item['conversion']; $item['otherNum'] = $item['buyNum']; $u_1_buy = $item['buyNum'];//辅单位购买数量 $item['buyNum'] = bcmul($item['buyNum'],$conversion,2); $item['salePrice'] = bcdiv($item['salePrice'],$conversion,2); $u = isset($masterSku['unitName']) ? $masterSku['unitName'] : '';//主单位名字 $u_1 = $item['unitName'];//辅单位名字 $uId_1 = $item['skuId'];//辅单位id $uId = $masterSku['id'];//主单位id }else{ $skuData = $objMSku->getSkuData(['goodsId' => $basicGoodsId, 'deleteStatus' => StatusCode::$standard, 'isMaster' => StatusCode::$delete]); if(empty($skuData)){ return ResultWrapper::fail('超码商品单位获取异常',ErrorCode::$paramError); // parent::sendOutput('超码商品辅单位获取异常',ErrorCode::$paramError); } $conversion = $skuData['conversion']; $u = isset($masterSku['unitName']) ? $masterSku['unitName'] : '';//主单位名字 $u_1 = $skuData['unitName'];//辅单位名字 $uId_1 = $skuData['skuId'];//辅单位id $uId = $masterSku['id'];//主单位id // $u_1_buy = bcdiv($item['buyNum'], $conversion, 0);//辅单位购买数量 $u_1_buy = isset($item['otherNum']) && !empty($item['otherNum']) ? $item['otherNum'] : 0; } $extends = ['uId' => $uId, 'uId_1' => $uId_1, 'u' => $u, 'u_1' => $u_1,'u_1_buy'=> $u_1_buy,'conversion' => $conversion]; $item['extends'] = json_encode($extends); } $lockInventoryData[] = [ 'materielId' => $item['materielId'], 'materielCode' => $item['code'], 'lockingNum' => $item['buyNum'], 'originId' => $orderId, 'originNo' => $no, 'source' => StatusCode::$orderType['saleOrder'], 'sourceNo' => $no, 'operatorId' => $params['customerId'], 'operatorName' => $params['customerName'], 'skuId' => $item['skuId'], ]; if (!isset($item['id']) || empty($item['id'])){ //提交的数据中没有id 新增加的商品 $item['no'] = $no; $item['orderId'] = $orderId; $insert[] = self::buildOrderGoodsData($item); continue; } if (in_array($item['id'],$haveIds)){ //修改了此条商品数据 $id = $item['id']; $item['no'] = $no; $item = self::buildOrderGoodsData($item); $item['id'] = $id; $update[] = $item; unset($map[$item['id']]); continue; } } $delete = []; foreach (array_values($map) as $value){ $delete[] = $value['id']; } if (!empty($insert)){ $result = $this->objDOrderGoods->insert($insert,true); if ($result === false){ return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$paramError); } } if(!empty($update)){ foreach ($update as $value){ $id = $value['id']; unset($value['id']); if (empty($id)){ return ResultWrapper::fail('更新订单商品数据失败',ErrorCode::$paramError); } $value['updateTime'] = time(); $result = $this->objDOrderGoods->update($value,$id); if ($result === false){ return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$paramError); } } } if (!empty($delete)){ $result = $this->objDOrderGoods->update(['deleteStatus' => StatusCode::$delete,'updateTime' => time()],['id' => $delete]); if ($result === false){ return ResultWrapper::fail($this->objDOrderGoods->error(),ErrorCode::$paramError); } } if (empty($lockInventoryData)){ return ResultWrapper::fail('锁定数据构建失败',ErrorCode::$paramError); } //锁定库存商品数量 $objMInventory = new MInventory($this->onlineEnterpriseId, $this->onlineUserId); $lockInventoryResult = $objMInventory->updateLockInventory($params['shopId'],$lockInventoryData); if (!$lockInventoryResult->isSuccess()) { //TODO(由于跨库不能支持事务所以有了这段奇怪的代码) //所以这里失败,重新锁定原来解锁的库存 $lockInventoryRes = $objMInventory->updateLockInventory($params['shopId'],$lock); if (!$lockInventoryRes->isSuccess()){ Logger::logs(E_USER_ERROR,'订单号:'.$no.',企业:'.$this->onlineEnterpriseId,__CLASS__,__LINE__,$lockInventoryRes->getData()); } return ResultWrapper::fail($lockInventoryResult->getData(), $lockInventoryResult->getErrorCode()); } return ResultWrapper::success(true); } /** * Doc: (des="构建商品数据") * User: XMing * Date: 2020/11/4 * Time: 9:38 上午 * @param array $params * @return array */ public static function buildOrderGoodsData(array $params):array { return [ "no" => isset($params['no']) ? $params['no'] : '', //char(25) NOT NULL COMMENT '订单编号', "orderId" => isset($params['orderId']) ? $params['orderId'] : 0, //int(10) NOT NULL DEFAULT '0' COMMENT '订单Id', "goodsCode" => isset($params['code']) ? $params['code'] : '', //varchar(255) NOT NULL DEFAULT '' COMMENT '商品编号', "goodsId" => isset($params['goodsId']) ? $params['goodsId'] : 0, //int(10) NOT NULL DEFAULT '0' COMMENT '商品Id', "goodsName" => isset($params['goodsName']) ? $params['goodsName'] : '', //varchar(50) NOT NULL DEFAULT '' COMMENT '商品名称', "shopId" => isset($params['shopId']) ? $params['shopId'] : 0, //int(10) NOT NULL DEFAULT '0' COMMENT '商铺Id', "shopName" => isset($params['shopName']) ? $params['shopName'] : '', //varchar(50) NOT NULL DEFAULT '' COMMENT '商铺名称', "merchantId" => isset($params['merchantId']) ? $params['merchantId'] : 0, //int(10) DEFAULT NULL COMMENT '商户id', "originPrice" => isset($params['salePrice']) ? $params['salePrice'] : 0, //decimal(12,2) DEFAULT '0.00' COMMENT '原始单价', "price" => isset($params['salePrice']) ? $params['salePrice'] : 0, //decimal(12,2) DEFAULT '0.00' COMMENT '商品单价', "buyNum" => isset($params['buyNum']) ? $params['buyNum'] : 0, "otherNum" => isset($params['otherNum']) ? $params['otherNum'] : 0, //decimal(10,4) NOT NULL DEFAULT '0.0000' COMMENT '购买数量', "preferential" => isset($params['preferential']) ? $params['preferential'] : 0, //decimal(12,2) DEFAULT '0.00' COMMENT '优惠券优惠金额', "totalMoney" => isset($params['subMoney']) ? $params['subMoney'] : 0, //decimal(12,2) DEFAULT '0.00' COMMENT '小计金额', "deliverNum" => isset($params['buyNum']) ? $params['buyNum'] : 0, //decimal(10,4) NOT NULL DEFAULT '0.0000' COMMENT '发货数量', "deleteStatus" => StatusCode::$standard, //tinyint(3) NOT NULL DEFAULT '5' COMMENT '是否删除 默认5 正常 4删除', "returnStatus" => isset($params['returnStatus']) ? $params['returnStatus'] : StatusCode::$orderReturn['notReturn'], //tinyint(3) DEFAULT '0' COMMENT '退货状态 0:未退货 1:已退货', "createTime" => time(), //int(10) NOT NULL DEFAULT '0' COMMENT '创建时间', "updateTime" => time(), //int(10) NOT NULL DEFAULT '0' COMMENT '更新时间', "preferentialActivityId" => isset($params['preferentialActivityId']) ? $params['preferentialActivityId'] : 0, //int(10) DEFAULT '0' COMMENT '店铺优惠活动id', "skuId" => isset($params['skuId']) ? $params['skuId'] : 0, //int(10) DEFAULT NULL COMMENT 'skuid', "goodsBasicId" => isset($params['materielId']) ? $params['materielId'] : 0, //int(10) DEFAULT NULL COMMENT 'goodsBasicId', "unitName" => isset($params['unitName']) ? $params['unitName'] : 0, //varchar(50) DEFAULT NULL COMMENT '单位名称', "conversion" => isset($params['conversion']) ? $params['conversion'] : 0, //decimal(10,2) DEFAULT NULL COMMENT '换算比率', "outCostPrice" => isset($params['outCostPrice']) ? $params['outCostPrice'] : 0, //decimal(10,2) DEFAULT NULL COMMENT '成本', "vipDiscount" => isset($params['vipDiscount']) ? $params['vipDiscount'] : 0, //decimal(12,2) DEFAULT '0.00' COMMENT '会员卡优惠金额', "categoryPath" => isset($params['categoryPath']) ? $params['categoryPath'] : '', //varchar(50) DEFAULT '' COMMENT '分类路径', "brandId" => isset($params['brandId']) ? $params['brandId'] : 0, //int(5) DEFAULT '0' COMMENT '品牌id', "specGroup" => isset($params['specGroup']) ? json_encode($params['specGroup']) : null, //json DEFAULT NULL COMMENT '规格明细', "goodsImages" => isset($params['goodsImages']) ? $params['goodsImages'] : '', //varchar(255) DEFAULT '' COMMENT '商品图', "categoryName" => isset($params['categoryName']) ? $params['categoryName'] : '', //varchar(50) DEFAULT '' COMMENT '分类名称', "brandName" => isset($params['brandName']) ? $params['brandName'] : '', //varchar(50) DEFAULT '' COMMENT '品牌名称', "barCode" => isset($params['barCode']) ? $params['barCode'] : '', //varchar(50) DEFAULT '' COMMENT '商品条码', "storageCode" => isset($params['storageCode']) ? $params['storageCode'] : '', //varchar(50) DEFAULT '' COMMENT '商品货架编码', "activityExt" => isset($params['activityExt']) ? json_encode($params['activityExt']) : null, //json DEFAULT NULL COMMENT '商品活动数据', "expressMoney" => isset($params['expressMoney']) ? $params['expressMoney'] : 0, //decimal(10,2) DEFAULT '0.00' COMMENT '运费', 'outNum' => 0, 'outOfNum' => isset($params['buyNum']) ? $params['buyNum'] : 0, //isEq 'isEq' => isset($params['isEq']) ? $params['isEq'] : 4, //抄吗商品 'extends' => isset($params['extends']) ? $params['extends'] : null, ]; } }