| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\AddressModel;
- /**
- * 收货地址管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class AddressService
- * @package App\Services
- */
- class AddressService extends BaseService
- {
- protected static $instance = null;
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * AddressService constructor.
- */
- public function __construct()
- {
- $this->model = new AddressModel();
- }
- /**
- * 静态入口
- * @return ArticleService|null
- */
- public static function make(){
- if(!self::$instance){
- self::$instance = new ArticleService();
- }
- return self::$instance;
- }
- /**
- * 获取列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getDataList($params)
- {
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $dataList = $this->model::from('member_address as a')
- ->leftJoin('city as c1', 'c1.citycode', '=', 'a.province_id')
- ->leftJoin('city as c2', 'c2.citycode', '=', 'a.city_id')
- ->leftJoin('city as c3', 'c3.citycode', '=', 'a.district_id')
- ->where(function ($query) use ($params) {
- $query->where(['a.mark'=> 1,'a.status'=> 1]);
- $userId = isset($params['user_id']) ? $params['user_id'] : 0;
- if ($userId > 0) {
- $query->where('a.user_id', $userId);
- }
- })
- ->select(['a.id', 'a.user_id', 'c1.name as province_name','c2.name as city_name','c3.name as district_name', 'a.realname', 'a.mobile', 'a.is_default', 'a.status', 'a.create_time', 'a.update_time', 'a.address'])
- ->orderBy('a.is_default', 'asc')
- ->orderBy('a.create_time', 'desc')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- foreach ($dataList['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- $item['address_text'] = $item['province_name'].$item['city_name'].$item['district_name'];
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function edit()
- {
- $data = request()->all();
- $data['update_time'] = time();
- $data['publish_at'] = isset($data['publish_at']) && $data['publish_at']? $data['publish_at'] : date('Y-m-d H:i:s');
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 保存添加数据
- * @param $params
- * @return array
- */
- public function save($params){
- $cityCodes = isset($params['cityCodes'])? $params['cityCodes'] : [];
- $cityCodes = $cityCodes? array_filter($cityCodes) : [];
- if(empty($cityCodes)){
- return message('请选择省市', false);
- }
- $data = [
- 'user_id' => isset($params['user_id'])? $params['user_id'] : 0,
- 'realname' => isset($params['realname'])? $params['realname'] : '',
- 'mobile' => isset($params['mobile'])? $params['mobile'] : '',
- 'province_id' => isset($cityCodes[0])? $cityCodes[0] : '',
- 'city_id' => isset($cityCodes[1])? $cityCodes[1] : '',
- 'district_id' => isset($cityCodes[2])? $cityCodes[2] : '',
- 'address' => isset($params['address'])? trim($params['address']) : '',
- 'is_default'=>isset($params['is_default'])? intval($params['is_default']) : 2,
- 'create_time'=> time()
- ];
- $data['update_time'] = time();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 获取默认地址
- * @param $userId
- * @return array
- */
- public function getDefault($userId){
- $info = $this->model::from('member_address as a')
- ->leftJoin('city as c1', 'c1.citycode', '=', 'a.province_id')
- ->leftJoin('city as c2', 'c2.citycode', '=', 'a.city_id')
- ->leftJoin('city as c3', 'c3.citycode', '=', 'a.district_id')
- ->where(['a.user_id'=> $userId,'a.maek'=> 1,'a.status'=> 1])
- ->select(['a.id', 'a.user_id', 'c1.name as province_name','c2.name as city_name','c3.name as district_name', 'a.realname', 'a.mobile', 'a.is_default', 'a.status', 'a.create_time', 'a.update_time', 'a.address'])
- ->orderBy('a.is_default', 'asc')
- ->orderBy('a.create_time', 'desc')
- ->first();
- $info = $info? $info->toArray() : [];
- if($info){
- $info['create_time'] = $info['create_time'] ? datetime($info['create_time'],'Y-m-d H:i:s') : '';
- $info['address_text'] = $info['province_name'].$info['city_name'].$info['district_name'];
- }
- return $info;
- }
- }
|