MShippingAddress.Class.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * 客户收货地址管理Model
  4. * Created by PhpStorm.
  5. * User: 小威
  6. * Date: 2019/10/31
  7. * Time: 11:30
  8. */
  9. namespace JinDouYun\Model\Customer;
  10. use JinDouYun\Controller\Customer\Customer;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Core\StatusCode;
  13. use Mall\Framework\Core\ResultWrapper;
  14. use JinDouYun\Dao\Customer\DShippingAddress;
  15. use JinDouYun\Model\Common\MCommon;
  16. class MShippingAddress
  17. {
  18. private $objDShippingAddress;
  19. public function __construct($enterpriseId)
  20. {
  21. $this->objDShippingAddress = new DShippingAddress('default');
  22. $this->objDShippingAddress->setTable($this->objDShippingAddress->get_Table().'_'.$enterpriseId);
  23. }
  24. /**
  25. * 客户收货地址添加
  26. * @param $params
  27. * @return ResultWrapper
  28. */
  29. public function addShippingAddress($params)
  30. {
  31. //查询表里是否有默认地址
  32. $selectParams = [
  33. 'customerId' => $params['customerId'],
  34. 'deleteStatus' => StatusCode::$standard,
  35. 'defaultStatus' => StatusCode::$standard
  36. ];
  37. $total = $this->getCount($selectParams);
  38. if(!$total) {
  39. //没有的话 新增这条就为默认地址
  40. $params['defaultStatus'] = StatusCode::$standard;
  41. } else {
  42. // 有默认地址,并且要设置当前的为默认,就把表里所有收货地址的默认状态删除
  43. if($params['defaultStatus'] == StatusCode::$standard){
  44. self::updateAddressDefaultStatus($params['customerId']);
  45. }
  46. }
  47. $params['createTime'] = $params['updateTime'];
  48. $dbResult = $this->objDShippingAddress->insert($params);
  49. if($dbResult === false){
  50. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  51. }else{
  52. return ResultWrapper::success($dbResult);
  53. }
  54. }
  55. /**
  56. * 删
  57. */
  58. /**
  59. * 客户收货地址删除
  60. * @param $id
  61. * @return ResultWrapper
  62. */
  63. public function deleteShippingAddress($id)
  64. {
  65. $params = [
  66. 'deleteStatus' => StatusCode::$delete,
  67. 'updateTime' => time(),
  68. ];
  69. $dbResult = $this->objDShippingAddress->update($params, $id);
  70. if($dbResult === false){
  71. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  72. }else{
  73. return ResultWrapper::success($dbResult);
  74. }
  75. }
  76. /**
  77. * 改
  78. */
  79. /**
  80. * 客户收货地址修改
  81. * @param $params
  82. * @param $id
  83. * @return ResultWrapper
  84. */
  85. public function updateShippingAddress($params, $id)
  86. {
  87. //如果设置默认状态 就把表里所有customerId为他的默认地址取消
  88. if($params['defaultStatus'] == StatusCode::$standard){
  89. self::updateAddressDefaultStatus($params['customerId']);
  90. }
  91. $dbResult = $this->objDShippingAddress->update($params,$id);
  92. if($dbResult === false){
  93. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  94. }else{
  95. return ResultWrapper::success($dbResult);
  96. }
  97. }
  98. /**
  99. * 查
  100. */
  101. /**
  102. * 客户收货地址列表
  103. * @param $params
  104. * @return ResultWrapper
  105. */
  106. public function getAllShippingAddress($selectParams)
  107. {
  108. $limit = $selectParams['limit'];
  109. unset($selectParams['limit']);
  110. $offset = $selectParams['offset'];
  111. unset($selectParams['offset']);
  112. $selectParams['deleteStatus'] = StatusCode::$standard;
  113. $dbResult = $this->objDShippingAddress->select($selectParams, '*', 'createTime desc', $limit, $offset);
  114. if ($dbResult === false) {
  115. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  116. }
  117. $MCommon = new MCommon();
  118. $dbResult = $MCommon->formatAddress($dbResult);
  119. $return = [
  120. 'data' => $dbResult,
  121. 'total' => $this->getCount($selectParams),
  122. ];
  123. if($return === false){
  124. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  125. }else{
  126. return ResultWrapper::success($return);
  127. }
  128. }
  129. /**
  130. * 获取地址总数
  131. * @param array $selectParams
  132. * @return int
  133. */
  134. public function getCount($selectParams = []) {
  135. $total = $this->objDShippingAddress->count($selectParams);
  136. return ($total) ? intval($total) : 0;
  137. }
  138. /**
  139. * 客户收货地址详情
  140. * @param $params
  141. * @return ResultWrapper
  142. */
  143. public function getShippingAddressInfo($params)
  144. {
  145. $dbResult = $this->objDShippingAddress->get($params);
  146. if($dbResult === false){
  147. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  148. }else{
  149. return ResultWrapper::success($dbResult);
  150. }
  151. }
  152. /**
  153. * 修改收货地址默认状态(全部设成默认)
  154. * @param $customerId
  155. * @return ResultWrapper
  156. */
  157. public function updateAddressDefaultStatus($customerId)
  158. {
  159. $dbResult = $this->objDShippingAddress->update(['defaultStatus' => StatusCode::$delete], ['customerId' => $customerId]);
  160. if($dbResult === false){
  161. return ResultWrapper::fail($this->objDShippingAddress->error(), ErrorCode::$dberror);
  162. }else{
  163. return ResultWrapper::success($dbResult);
  164. }
  165. }
  166. }