Address.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services\customer;
  10. //use fecshop\models\mysqldb\customer\Address as MyAddress;
  11. use fecshop\services\Service;
  12. use Yii;
  13. /**
  14. * Address child services.
  15. * @author Terry Zhao <2358269014@qq.com>
  16. * @since 1.0
  17. */
  18. class Address extends Service
  19. {
  20. protected $currentCountry;
  21. protected $currentState;
  22. protected $_addressModelName = '\fecshop\models\mysqldb\customer\Address';
  23. protected $_addressModel;
  24. public function init()
  25. {
  26. parent::init();
  27. list($this->_addressModelName, $this->_addressModel) = \Yii::mapGet($this->_addressModelName);
  28. }
  29. protected function actionGetPrimaryKey()
  30. {
  31. return 'address_id';
  32. }
  33. /**
  34. * @param $primaryKey | Int
  35. * @return Object(MyCoupon)
  36. * 通过id找到customer address的对象
  37. */
  38. protected function actionGetByPrimaryKey($primaryKey)
  39. {
  40. $one = $this->_addressModel->findOne($primaryKey);
  41. $primaryKey = $this->getPrimaryKey();
  42. if ($one[$primaryKey]) {
  43. return $one;
  44. } else {
  45. return new $this->_addressModelName();
  46. }
  47. }
  48. /**
  49. * @param $address_id | Int , address表的id
  50. * @param $customer_id | Int , 用户id
  51. * 在这里在主键查询的同时,加入customer_id,这样查询的肯定是这个用户的,
  52. * 这样就防止有的用户去查询其他用户的address信息。
  53. */
  54. protected function actionGetAddressByIdAndCustomerId($address_id, $customer_id)
  55. {
  56. $primaryKey = $this->getPrimaryKey();
  57. $one = $this->_addressModel->findOne([
  58. $primaryKey => $address_id,
  59. 'customer_id' => $customer_id,
  60. ]);
  61. if ($one[$primaryKey]) {
  62. return $one;
  63. } else {
  64. return false;
  65. }
  66. }
  67. /**
  68. * @param $filter|array
  69. * @return Array;
  70. * 通过过滤条件,得到coupon的集合。
  71. * example filter:
  72. * [
  73. * 'numPerPage' => 20,
  74. * 'pageNum' => 1,
  75. * 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ],
  76. * 'where' => [
  77. * ['>','price',1],
  78. * ['<=','price',10]
  79. * ['sku' => 'uk10001'],
  80. * ],
  81. * 'asArray' => true,
  82. * ]
  83. */
  84. protected function actionColl($filter = '')
  85. {
  86. $query = $this->_addressModel->find();
  87. $query = Yii::$service->helper->ar->getCollByFilter($query, $filter);
  88. $coll = $query->all();
  89. return [
  90. 'coll' => $coll,
  91. 'count'=> $query->limit(null)->offset(null)->count(),
  92. ];
  93. }
  94. /**
  95. * @return Array
  96. * 得到当前用户的所有货运地址数组
  97. */
  98. protected function actionCurrentAddressList()
  99. {
  100. $arr = [];
  101. if (!Yii::$app->user->isGuest) {
  102. $identity = Yii::$app->user->identity;
  103. $customer_id = $identity['id'];
  104. if ($customer_id) {
  105. $filter = [
  106. 'numPerPage' => 30,
  107. 'pageNum' => 1,
  108. 'orderBy' => ['updated_at' => SORT_DESC],
  109. 'where' => [
  110. ['customer_id' => $customer_id],
  111. ],
  112. 'asArray' => true,
  113. ];
  114. $coll = $this->coll($filter);
  115. $ii = 0;
  116. if (is_array($coll['coll']) && !empty($coll['coll'])) {
  117. foreach ($coll['coll'] as $one) {
  118. $address_id = $one['address_id'];
  119. $first_name = $one['first_name'];
  120. $last_name = $one['last_name'];
  121. $email = $one['email'];
  122. $telephone = $one['telephone'];
  123. $street1 = $one['street1'];
  124. $street2 = $one['street2'];
  125. $is_default = $one['is_default'];
  126. $city = $one['city'];
  127. $state = $one['state'];
  128. $zip = $one['zip'];
  129. $area = $one['area'];
  130. $country = Yii::$service->helper->country->getCountryNameByKey($one['country']);
  131. $state = Yii::$service->helper->country->getStateByContryCode($one['country'],$one['state']);
  132. $address_info = [
  133. 'address_id' => $address_id,
  134. 'first_name' => $first_name,
  135. 'last_name' => $last_name,
  136. 'email' => $email,
  137. 'telephone' => $telephone,
  138. 'street1' => $street1,
  139. 'is_default' => $is_default,
  140. 'city' => $city,
  141. 'state' => $state,
  142. 'zip' => $zip,
  143. 'area' => $area,
  144. ];
  145. //$state = Yii::$service->helper->country->getStateByContryCode($one['country'],$one['state']);
  146. $str = $first_name.' '.$last_name.' '.$email.' '.
  147. $street1.' '.$street2.' '.$city.' '.$state.' '.$country.' '.
  148. $zip.' '.$telephone;
  149. if ($is_default == 1) {
  150. $ii = 1;
  151. }
  152. $arr[$address_id] = [
  153. 'address' => $str,
  154. 'is_default'=>$is_default,
  155. 'address_info' => $address_info,
  156. ];
  157. }
  158. if (!$ii) {
  159. // 如果没有默认的地址,则取第一个当默认
  160. foreach ($arr as $k=>$v) {
  161. $arr[$k]['is_default'] = 1;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. }
  168. return $arr;
  169. }
  170. /**
  171. * @param $one|array , 保存的address数组
  172. * @return int 返回保存的 address_id 的值。
  173. */
  174. protected function actionSave($one)
  175. {
  176. if (!is_array($one) || empty($one)) {
  177. Yii::$service->helper->errors->add('address data is empty');
  178. return false;
  179. }
  180. $primaryKey = $this->getPrimaryKey();
  181. $primaryVal = isset($one[$primaryKey]) ? $one[$primaryKey] : '';
  182. if ($primaryVal) {
  183. $model = $this->_addressModel->findOne($primaryVal);
  184. if (!$model) {
  185. Yii::$service->helper->errors->add('address {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]);
  186. return false;
  187. }
  188. } else {
  189. $model = new $this->_addressModelName();
  190. $model->created_at = time();
  191. }
  192. $model->attributes = $one;
  193. // 规则验证
  194. if ($model->validate()) {
  195. $model->updated_at = time();
  196. // 保存地址。
  197. $model = Yii::$service->helper->ar->save($model, $one);
  198. if (!$model) {
  199. return false;
  200. }
  201. } else {
  202. $errors = $model->errors;
  203. Yii::$service->helper->errors->addByModelErrors($errors);
  204. return false;
  205. }
  206. $primaryVal = $model[$primaryKey];
  207. if ($one['is_default'] == 1) {
  208. $customer_id = $one['customer_id'];
  209. if ($customer_id && $primaryVal) {
  210. $this->_addressModel->updateAll(
  211. ['is_default'=>2], // $attributes
  212. 'customer_id = :customer_id and '.$primaryKey.' != :primaryVal ', // $condition
  213. [
  214. 'customer_id' => $customer_id,
  215. 'primaryVal' => $primaryVal,
  216. ]
  217. );
  218. }
  219. }
  220. return $primaryVal;
  221. }
  222. /**
  223. * @param $ids | Int or Array
  224. * @return bool
  225. * 如果传入的是id数组,则删除多个address,如果传入的是Int,则删除一个address
  226. * 删除address的同时,删除掉购物车中的address_id
  227. * 删除address的同时,如果删除的是default address,那么重新找出来一个address作为default address并保存到表中。
  228. */
  229. protected function actionRemove($ids, $customer_id)
  230. {
  231. if (!$ids) {
  232. Yii::$service->helper->errors->add('remove id is empty');
  233. return false;
  234. }
  235. if (is_array($ids) && !empty($ids)) {
  236. foreach ($ids as $id) {
  237. $model = $this->_addressModel->findOne($id);
  238. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  239. if ($customer_id) {
  240. if ($model['customer_id'] == $customer_id) {
  241. $this->removeCartAddress($model['customer_id'], $id);
  242. $model->delete();
  243. } else {
  244. Yii::$service->helper->errors->add('remove address is not current customer address');
  245. }
  246. }
  247. //} else {
  248. // $this->removeCartAddress($model['customer_id'], $id);
  249. // $model->delete();
  250. //}
  251. } else {
  252. Yii::$service->helper->errors->add('Address Remove Errors:ID {id} is not exist', ['id' => $id]);
  253. return false;
  254. }
  255. }
  256. } else {
  257. $id = $ids;
  258. $model = $this->_addressModel->findOne($id);
  259. if (isset($model[$this->getPrimaryKey()]) && !empty($model[$this->getPrimaryKey()])) {
  260. if ($customer_id) {
  261. if ($model['customer_id'] == $customer_id) {
  262. $this->removeCartAddress($model['customer_id'], $id);
  263. $model->delete();
  264. } else {
  265. Yii::$service->helper->errors->add('remove address is not current customer address');
  266. }
  267. }
  268. //} else {
  269. // $this->removeCartAddress($model['customer_id'], $id);
  270. // $model->delete();
  271. //}
  272. } else {
  273. Yii::$service->helper->errors->add('Address Remove Errors:ID:{id} is not exist', ['id'=> $id]);
  274. return false;
  275. }
  276. }
  277. // 查看是否有默认地址?如果该用户存在记录,但是没有默认地址,
  278. // 则查找用户是否存在非默认地址,如果存在,则取一个设置为默认地址
  279. $addressOne = $this->_addressModel->find()->asArray()
  280. ->where(['customer_id' => $customer_id, 'is_default' => 1])
  281. ->one();
  282. if (!$addressOne['address_id']) {
  283. $assOne = $this->_addressModel->find()
  284. ->where(['customer_id' => $customer_id])
  285. ->one();
  286. if ($assOne['address_id']) {
  287. $assOne->is_default = 1;
  288. $assOne->updated_at = time();
  289. $assOne->save();
  290. }
  291. }
  292. return true;
  293. }
  294. /**
  295. * @param $customer_id | Int ,
  296. * @param $address_id | Int,address id
  297. * 删除购物车中的address部分。
  298. */
  299. protected function removeCartAddress($customer_id, $address_id)
  300. {
  301. $cart = Yii::$service->cart->quote->getCartByCustomerId($customer_id);
  302. if (isset($cart['customer_address_id']) && !empty($cart['customer_address_id'])) {
  303. if ($cart['customer_address_id'] == $address_id) {
  304. $cart->customer_address_id = '';
  305. $cart->save();
  306. }
  307. }
  308. }
  309. /**
  310. * @param $customer_id | int 用户id
  311. * @param $address_id | int 用户地址id
  312. * @return boolean
  313. * 将某个address_id对应的地址数据,设置成默认地址
  314. */
  315. public function setDefault($customer_id, $address_id)
  316. {
  317. $primaryKey = $this->getPrimaryKey();
  318. // 当前的设置成1
  319. $address_one = $this->_addressModel->findOne([
  320. 'customer_id' => $customer_id,
  321. $primaryKey => $address_id,
  322. ]);
  323. if (!$address_one[$primaryKey]) {
  324. Yii::$service->helper->errors->add('customer address is empty');
  325. return false;
  326. }
  327. $address_one->is_default = 1;
  328. $address_one->updated_at = time();
  329. $address_one->save();
  330. // 将其他的设置成2
  331. $this->_addressModel->updateAll(
  332. ['is_default'=>2], // $attributes
  333. 'customer_id = :customer_id and '.$primaryKey.' != :primaryVal ', // $condition
  334. [
  335. 'customer_id' => $customer_id,
  336. 'primaryVal' => $address_id,
  337. ]
  338. );
  339. return true;
  340. }
  341. public function getDefualtAddressId($customer_id = '')
  342. {
  343. if(!$customer_id){
  344. $identity = Yii::$app->user->identity;
  345. $customer_id = $identity['id'];
  346. }
  347. if (!$customer_id) {
  348. return null;
  349. }
  350. $addressOne = $this->_addressModel->find()->asArray()
  351. ->where(['customer_id' => $customer_id,'is_default' => 1])
  352. ->one();
  353. if($addressOne['address_id']){
  354. return $addressOne['address_id'];
  355. }
  356. return null;
  357. }
  358. /*
  359. * @param $customer_id | int 用户的id
  360. * @return Array Or ''
  361. * 得到customer的默认地址。
  362. */
  363. /*
  364. protected function actionGetDefaultAddress($customer_id = ''){
  365. if(!$customer_id){
  366. $identity = Yii::$app->user->identity;
  367. $customer_id = $identity['id'];
  368. }
  369. if($customer_id ){
  370. $addressOne = $this->_addressModel->find()->asArray()
  371. ->where(['customer_id' => $customer_id,'is_default' => 1])
  372. ->one();
  373. if($addressOne['address_id']){
  374. return $addressOne;
  375. }else{
  376. $assOne = $this->_addressModel->find()->asArray()
  377. ->where(['customer_id' => $customer_id])
  378. ->one();
  379. if($assOne['address_id']){
  380. return $assOne;
  381. }
  382. }
  383. }
  384. }
  385. */
  386. }