123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- /**
- * 客户收货地址管理Model
- * Created by PhpStorm.
- * User: 小威
- * Date: 2019/10/31
- * Time: 11:30
- */
- namespace JinDouYun\Model\Customer;
- use JinDouYun\Controller\Customer\Customer;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\StatusCode;
- use Mall\Framework\Core\ResultWrapper;
- use JinDouYun\Dao\Customer\DShippingAddress;
- use JinDouYun\Model\Common\MCommon;
- class MShippingAddress
- {
- private $objDShippingAddress;
- public function __construct($enterpriseId)
- {
- $this->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);
- }
- }
- }
|