<?php

namespace app\admin\controller\medical;

use app\admin\validate\medical\ShopValidate;
use app\controller\Curd;
use app\model\MedicalShop;
use support\exception\BusinessException;
use support\Request;
use support\Response;

class ShopController extends Curd
{
    public function __construct()
    {
        $this->model = new MedicalShop();
        $this->validate = true;
        $this->validateClass = new ShopValidate();
    }

    /**
     * @Desc 列表
     * @Author Gorden
     * @Date 2024/3/1 9:15
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function select(Request $request): Response
    {
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $order = $request->get('order', 'desc');
        $field = $field ?? 'shop_addTime';
        $where['shop_is_del'] = 0;
        $query = $this->doSelect($where, $field, $order);
        return $this->doFormat($query, $format, $limit);
    }

    public function selectList()
    {
        $class = get_class($this->model);
        $data = $class::where('shop_status', 1)
            ->select('shop_id', 'shop_name')
            ->get()
            ->toArray();

        return json_success('', $data);
    }


    /**
     * @Desc 插入数据处理
     * @Author Gorden
     * @Date 2024/3/19 13:37
     *
     * @param Request $request
     * @return array
     * @throws \support\exception\BusinessException
     */
    protected function insertInput(Request $request): array
    {
        $data = $this->inputFilter($request->post());

        $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
        if ($data['shop_business_hours']){
            $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
            $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
            $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
        }
        return $data;
    }

    protected function updateInput(Request $request): array
    {
        $primary_key = $this->model->getKeyName();
        $id = $request->post($primary_key);
        $data = $this->inputFilter($request->post());
        $model = $this->model->find($id);
        if (!$model) {
            throw new BusinessException('记录不存在', 2);
        }
        unset($data[$primary_key]);
        // 头像
        $data['shop_logo'] = str_replace(getenv("STORAGE_DOMAIN"), '', $data['shop_logo']);
        if ($data['shop_business_hours']){
            $shopBusinessHoursStart = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[0]));
            $shopBusinessHoursEnd = date('H:i', strtotime(explode(',', $data['shop_business_hours'])[1]));
            $data['shop_business_hours'] = $shopBusinessHoursStart . '~' . $shopBusinessHoursEnd;
        }

        return [$id, $data];
    }

    public function afterQuery($items)
    {
        foreach ($items as &$item) {
            $item->shop_logo = getenv('STORAGE_DOMAIN').$item->shop_logo;
            $item->timeFormat = '';
            if (!empty($item->shop_business_hours)){
                $shopBusinessHoursArr = explode('~', $item->shop_business_hours);
                foreach ($shopBusinessHoursArr as $key => $v){
                    $item->timeFormat .= $v.'~';
                    $shopBusinessHoursArr[$key] = date("Y-m-d\TH:i:s\Z",strtotime(date('Y-m-d ').$v)-8*3600);
                }
                $item->timeFormat = rtrim($item->timeFormat,'~');
                $item->shop_business_hours = $shopBusinessHoursArr;
            }

            $item->shop_label = !empty($item->shop_label) ? explode(',',$item->shop_label) : [];
        }
        return $items;
    }

    /**
     * @Desc 删除
     * @Author Gorden
     * @Date 2024/3/1 9:14
     *
     * @param Request $request
     * @return Response
     * @throws \support\exception\BusinessException
     */
    public function delete(Request $request): Response
    {
        $ids = $this->deleteInput($request);
        $this->doSoftDelete($ids, ['shop_is_del' => 1]);

        return json_success('success');
    }
}