objDShippingAddress = new DShippingAddress('default'); $this->objDShippingAddress->setTable($this->objDShippingAddress->get_Table().'_'.$enterpriseId); } /** * 客户收货地址添加 * @param $params * @return ResultWrapper */ public function addShippingAddress($params) { //查询表里是否有默认地址 $selectParams = [ 'customerId' => $params['customerId'], 'deleteStatus' => StatusCode::$standard, 'defaultStatus' => StatusCode::$standard ]; $total = $this->getCount($selectParams); if(!$total) { //没有的话 新增这条就为默认地址 $params['defaultStatus'] = StatusCode::$standard; } else { // 有默认地址,并且要设置当前的为默认,就把表里所有收货地址的默认状态删除 if($params['defaultStatus'] == StatusCode::$standard){ self::updateAddressDefaultStatus($params['customerId']); } } $params['createTime'] = $params['updateTime']; $dbResult = $this->objDShippingAddress->insert($params); if($dbResult === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 删 */ /** * 客户收货地址删除 * @param $id * @return ResultWrapper */ public function deleteShippingAddress($id) { $params = [ 'deleteStatus' => StatusCode::$delete, 'updateTime' => time(), ]; $dbResult = $this->objDShippingAddress->update($params, $id); if($dbResult === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 改 */ /** * 客户收货地址修改 * @param $params * @param $id * @return ResultWrapper */ public function updateShippingAddress($params, $id) { //如果设置默认状态 就把表里所有customerId为他的默认地址取消 if($params['defaultStatus'] == StatusCode::$standard){ self::updateAddressDefaultStatus($params['customerId']); } $dbResult = $this->objDShippingAddress->update($params,$id); if($dbResult === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 查 */ /** * 客户收货地址列表 * @param $params * @return ResultWrapper */ public function getAllShippingAddress($selectParams) { $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $selectParams['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDShippingAddress->select($selectParams, '*', 'createTime desc', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); } $MCommon = new MCommon(); $dbResult = $MCommon->formatAddress($dbResult); $return = [ 'data' => $dbResult, 'total' => $this->getCount($selectParams), ]; if($return === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($return); } } /** * 获取地址总数 * @param array $selectParams * @return int */ public function getCount($selectParams = []) { $total = $this->objDShippingAddress->count($selectParams); return ($total) ? intval($total) : 0; } /** * 客户收货地址详情 * @param $params * @return ResultWrapper */ public function getShippingAddressInfo($params) { $dbResult = $this->objDShippingAddress->get($params); if($dbResult === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } /** * 修改收货地址默认状态(全部设成默认) * @param $customerId * @return ResultWrapper */ public function updateAddressDefaultStatus($customerId) { $dbResult = $this->objDShippingAddress->update(['defaultStatus' => StatusCode::$delete], ['customerId' => $customerId]); if($dbResult === false){ return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror); }else{ return ResultWrapper::success($dbResult); } } }