<?php

namespace app\common\logic;

use app\common\logic\store_order\StoreOrderLogic;
use app\common\model\system_store\SystemStoreStaff;
use app\common\service\FileService;
use Webman\Config;

class SystemStoreStaffLogic extends BaseLogic
{

    public function listByWhere($where, $field = '*')
    {
        return SystemStoreStaff::where($where)->field($field)->select()->toArray();
    }

    public function add($params)
    {
        try {
            $model = new SystemStoreStaff();
            $model->setAttrs($params);
            $model->store_id = request()->adminInfo['store_id'];
            $passwordSalt = Config::get('project.unique_identification');
            $model->pwd = create_password($params['pwd'], $passwordSalt);
            $defaultAvatar = config('project.default_image.admin_avatar');
            $model->avatar = !empty($model['avatar']) ? FileService::setFileUrl($model['avatar']) : $defaultAvatar;
            $model->save();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    public function edit($id, $params)
    {
        try {
            $model = SystemStoreStaff::find($id);
            if (empty($model)) {
                throw new \Exception('数据不存在');
            }
            $model->setAttrs($params);
            if (!empty($params['pwd'])) {
                $passwordSalt = Config::get('project.unique_identification');
                $model->pwd = create_password($params['pwd'], $passwordSalt);
            }
            $defaultAvatar = config('project.default_image.admin_avatar');
            $model->avatar = !empty($model['avatar']) ? FileService::setFileUrl($model['avatar']) : $defaultAvatar;
            $model->save();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    public function delete($id)
    {
        try {
            $model = SystemStoreStaff::find($id);
            if (empty($model)) {
                throw new \Exception('数据不存在');
            }
            $model->delete();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    public function detail($id)
    {
        $model = SystemStoreStaff::find($id);
        if (empty($model)) {
            throw new \Exception('数据不存在');
        }
        $data = $model->toArray();
        $orderLogic = new StoreOrderLogic();
        $data['cashier_order'] = $orderLogic->storeOrderCountByWhere([
            'store_id' => $data['store_id'],
            'staff_id' => $data['id'],
            'status' => [0, 1, 2],
            'paid' => 1,
            'shipping_type' => 3,
        ]);
        $data['verify_order'] = $orderLogic->storeOrderCountByWhere([
            'store_id' => $data['store_id'],
            'delivery_id' => $data['phone'],
            'status' => [1, 2],
            'paid' => 1,
            'shipping_type' => 2,
        ]);
        $data['delivery_order'] = $orderLogic->storeOrderSumByWhere([
            'store_id' => $data['store_id'],
            'delivery_id' => $data['phone'],
            'status' => 0,
            'paid' => 1,
            'shipping_type' => 1,
        ]);
        return $data;
    }

    public function status($id)
    {
        try {
            $model = SystemStoreStaff::find($id);
            if (empty($model)) {
                throw new \Exception('数据不存在');
            }
            $model->status = $model->status == 1 ? 0 : 1;
            $model->save();
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

}