<?php

namespace app\api\model;

use app\api\service\User as UserService;
use app\common\enum\RecoveryStatusEnum;
use app\common\model\RecoveryOrder as BaseRecoveryOrder;
use app\common\service\Order as OrderService;
use cores\exception\BaseException;
use think\db\exception\DbException;
use think\Paginator;

class RecoveryOrder extends BaseRecoveryOrder
{
    /**
     * 隐藏字段
     * @var array
     */
    protected $hidden = [
        'store_id',
        'update_time',
    ];

    /**
     * @notes:用户回收订单列表
     * @param array $where
     * @param int $listRows
     * @return Paginator
     * @throws BaseException
     * @throws DbException
     * @author: wanghousheng
     */
    public function getUserList(array $where = [], int $listRows = 15, array $with = []): Paginator
    {
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        if (!UserService::isStore()) {
            $where = array_merge($where, ['user_id' => $userId]);
        }
        //分销商工程师
        return $this
            ->with($with)
            ->where($where)
            ->order(['create_time' => 'desc'])
            ->paginate($listRows);
    }

    /**
     * @notes:订单详情
     * @param int $orderId
     * @return BaseRecoveryOrder|array|null
     * @throws BaseException
     * @author: wanghousheng
     */
    public function getDetails(int $orderId)
    {
        $where['order_id'] = $orderId;
        //不是店长带上 use_id
        if(!UserService::isStore()) {
            $where['user_id'] = UserService::getCurrentLoginUserId();
        }
        return static::detail($where, ['images.file']);
    }

    /**
     * @notes:添加记录
     * @param array $data
     * @param array $imageIds
     * @return bool
     * @throws BaseException
     * @author: wanghousheng
     */
    public function add(array $data, array $imageIds = []): bool
    {
        $data['order_no'] = OrderService::createOrderNo();
        $data['user_id'] = UserService::getCurrentLoginUserId();
        $data['store_id'] = self::$storeId;
        $data['create_time'] = time();
        $data['update_time'] = $data['create_time'];
        $insertId = $this->insertGetId($data);
        if ($insertId) {
            if ($imageIds) {
                RecoveryImage::increased($insertId, $imageIds);
            }
            return true;
        }
        return false;
    }

    /**
     * @notes:更新
     * @param array $data
     * @param int $orderId
     * @param array $imageIds
     * @return bool
     * @author: wanghousheng
     */
    public function edit(array $data, int $orderId, array $imageIds = []): bool
    {
        if ($this->where(['order_id' => $orderId])->save($data)) {
            if ($imageIds) {
                log_record(['name'=>'服务订单图片', 'image_ids' => $imageIds]);
                RecoveryImage::updates($orderId, $imageIds);
            }
            return true;
        }
        return false;
    }

    /**
     * @notes:取消
     * @param int $orderId
     * @return bool
     * @throws BaseException
     * @author: wanghousheng
     */
    public function cancel(int $orderId): bool
    {
        $where['order_id'] = $orderId;
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        if (!UserService::isStore()) {
            $where['user_id'] = $userId;
        }
        $info = static::detail($where);
        if (!$info->isEmpty() && $info['order_status'] == RecoveryStatusEnum::ACCEPTED) {
            $this->where(['order_id' => $orderId])->save(['order_status' => RecoveryStatusEnum::CANCEL]);
            return true;
        }
        return false;
    }

    /**
     * 获取回收单记录
     * @return int
     */
    public static function getCount($type): int {
        // 当前用户ID
        $userId = UserService::getCurrentLoginUserId();
        // 查询数据
        $query = self::where('order_status', '=', $type);
        // 非店长不带入user_id查询
        if (!UserService::isStore()) {
            $query = $query->where('user_id', '=', $userId);
        }
        return $query->count();
    }

}