AddressService.php 4.5 KB
<?php
/**
+-----------------------------------------------------------------------------------------------------------------------
 * 逻辑层:用户收货地址 服务类,处理业务逻辑
+-----------------------------------------------------------------------------------------------------------------------
 *
 * PHP version 7
 *
 * @category  App\Services\User
 * @package   App\Services\User
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年06月18日 10:38:01
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
namespace App\Services\User;

use App\Models\User\Address;
use App\Models\User\SupplierDyXcxUser;
use App\Models\User\SupplierXcxUser;
use App\Services\BaseService;

/**
 * Class UserAddressService.
 *
 * @category  App\Services\User
 * @package   App\Services\User
 * @author    Richer <yangzi1028@163.com>
 * @date      2020年06月18日 10:38:01
 * @copyright 2021-2022 Richer (http://www.Richer.com/)
 * @license   http://www.Richer.com/ License
 * @link      http://www.Richer.com/
 */
class AddressService extends BaseService
{
    /**
     * UserAddressService constructor.
     *
     * @param Address $model 注入模型
     */
    public function __construct(Address $model)
    {
         // 执行父类构造方法
        parent::__construct($model);
    }

    /**
     * Display a listing of the resource.
     *
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Support\Collection
     */
    public function list()
    {
        // 当前登录用户的角色
        $list = $this->getLoginUser()->addresses()->orderByDesc('default')->orderBy('sort')->get();

        return  $list;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request 请求
     *
     * @return bool|\Illuminate\Http\Response
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function store($request)
    {
        $user = $this->getLoginUser();
        // 新增的数据设置为私有
        $request['province'] =  $this->getRegionNameByCode($request->province_code);
        $request['city'] =  $this->getRegionNameByCode($request->city_code);
        $request['district'] =  $this->getRegionNameByCode($request->district_code);
        $request['user_id'] = optional($user)->id ?: 0;
        if ($user->appid && get_class($user) == SupplierXcxUser::class) {
            $request['appid'] = $user->appid;
        }
        if ($user->appid && get_class($user) == SupplierDyXcxUser::class) {
            $request['dy_appid'] = $user->appid;
        }
        $result =  $this->create($request);

        if ($result == false) {
            return false;
        }

        // 如果设置了 default 则需要将其他的数据设置为 0
        if ($request->default == 1) {
            $this->getLoginUser()->addresses()->where('id', '<>', $result->id)->update(['default' => 0]);
        }
        return $result;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param Request $request 请求
     * @param integer $id      主键id
     *
     * @return \App\Services\Response|bool
     *
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
     */
    public function update($request, $id)
    {
        // 新增的数据设置为私有
        $request['province'] =  $this->getRegionNameByCode($request->province_code);
        $request['city'] =  $this->getRegionNameByCode($request->city_code);
        $request['district'] =  $this->getRegionNameByCode($request->district_code);

        $result =  parent::update($request, $id);

        if ($result == false) {
            return false;
        }

        // 如果设置了 default 则需要将其他的数据设置为 0
        if ($request->default == 1) {
            $this->getLoginUser()->addresses()->where('id', '<>', $id)->where('default', 1)->update(['default' => 0]);
        }
        return $result;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param integer $id 主键id
     *
     * @return \App\Services\Response|bool
     */
    public function setDefault($id)
    {
        $model = $this->model->findOrFail($id);
        $model->default = 1;
        $result = $model->save();
        if ($result == false) {
            return false;
        }

        $this->getLoginUser()->addresses()->where('id', '<>', $id)->where('default', 1)->update(['default' => 0]);

        return $result;
    }
}