<?php
namespace app\api\logic;
use app\common\logic\BaseLogic;
use app\common\model\article\Article;
use app\common\model\decorate\DecoratePage;
use app\common\model\decorate\DecorateTabbar;
use app\common\model\setting\PostageRegion;
use app\common\model\user\UserAddress;
use app\common\service\ConfigService;
use app\common\service\FileService;
use think\facade\Db;

/**
 * UserAddressLogic
 * Class UserAddressLogic
 * @package app\api\logic
 */
class UserAddressLogic extends BaseLogic
{
    /**
     * 获取用户地址列表
     * @return array
     */
    public static function getAddressList($user_id)
    {
        return UserAddress::where('user_id',$user_id)->select()->toArray();
    }

    /**
     * @notes 添加
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/18 13:51
     */
    public static function add(array $params): bool
    {
        Db::startTrans();
        try {
            UserAddress::create([
                'user_id' => $params['user_id'],
                'area' => $params['area'],
                'address' => $params['address'],
                'house_number' => $params['house_number'],
                'contact_number' => $params['contact_number'],
                'contact_people' => $params['contact_people'],
                'lon'=>$params['lon'],
                'lat'=>$params['lat']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/18 13:51
     */
    public static function edit(array $params): bool
    {
        Db::startTrans();
        try {
            UserAddress::where(['id'=> $params['id'],'user_id'=>$params['user_id']])->update([
                'user_id' => $params['user_id'],
                'area' => $params['area'],
                'address' => $params['address'],
                'house_number' => $params['house_number'],
                'contact_number' => $params['contact_number'],
                'contact_people' => $params['contact_people'],
                'lon'=>$params['lon'],
                'lat'=>$params['lat']
            ]);

            Db::commit();
            return true;
        } catch (\Exception $e) {
            Db::rollback();
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 删除
     * @param array $params
     * @return bool
     * @author likeadmin
     * @date 2024/07/18 13:51
     */
    public static function delete(array $params): bool
    {
        return UserAddress::where(['id'=> $params['id'],'user_id'=>$params['user_id']])->delete();
    }


    /**
     * @notes 获取详情
     * @param $params
     * @return array
     * @author likeadmin
     * @date 2024/07/18 13:51
     */
    public static function detail($params): array
    {
        return UserAddress::where(['id'=> $params['id'],'user_id'=>$params['user_id']])->findOrEmpty()->toArray();
    }
}