<?php


namespace App\Form\Services;

use App\Services\CommonUserBaseService;

class FormItemDetailService extends CommonUserBaseService
{
    protected $cache = true;

    protected $cacheBucket = 'FormItemDetail:';

    protected $tokenBucket = 'Token:';

    protected $activeBucket = "Active:";

    /**
     * 获取表单项详情数据
     * @param $formId
     * @param null $itemId
     * @param null $formItemDetailId
     * @return array
     */
    public function getData($formId, $itemId = null, $formItemDetailId = null)
    {
        $map = [];
        $map['form_id'] = $formId;
        if(isset($itemId)){
            $map['form_item_id'] = $itemId;
        }
        if(isset($formItemDetailId)){
            $formItemDetailId = is_array($formItemDetailId) ? $formItemDetailId : explode(',', $formItemDetailId);
            $map['id'] = array(['in', $formItemDetailId]);
        }
        $map['status'] = $this->model::STATUS_ENABLED;
        return $this->model->buildQuery($map)->orderBy('sort', 'desc')
          ->selectRaw('id,content,sort')
          ->get()->toArray();
    }

    /**
     * 添加单项数据
     * @param $formId
     * @param $formItemId
     * @param $item
     * @param null $userId
     * @return mixed
     */
    public function addData($formId, $formItemId, $item,  $userId = null)
    {
        $userId = $userId ?? $this->getAuthUserId();
        $add = [];
        $add['form_id'] = $formId;
        $add['form_item_id'] = $formItemId;
        $add['content'] = $item['content'] ?? '';
        if(isset($item['sort'])){
            $add['sort'] = $item['sort'];
        }
        if(!empty($item['id']) && $item['id'] > 0){
            $id = $item['id'];
            $add['status'] = $this->model::STATUS_ENABLED;
            $this->updateBy([
                'id' => $id,
            ], $add);
        } else {
            $add['user_id'] = $userId;
            $id = $this->save($add)->id;
        }
        return $id;
    }

    /**
     * 添加多项数据
     * @param $formId
     * @param $formItemId
     * @param array $items
     * @param null $userId
     * @return array
     */
    public function addList($formId, $formItemId, $items = [],  $userId = null)
    {
        if(!empty($items)){
            $sort = 1;
            foreach($items as $i => $item){
                $item['sort'] = $sort++;
                $item['id'] = $this->addData($formId, $formItemId, $item, $userId);
                $items[$i] = $item;
            }
            $ids = array_column($items, 'id');
            $allIds = $this->getData($formId, $formItemId);
            $allIds = array_column($allIds, 'id');
            $delIds = array_diff($allIds, $ids);
            if(!empty($delIds)){
                //删除已取消的数据
                $this->delItemDetails($formId, $formItemId, $delIds);
            }
        }
        return $items;
    }

    /**
     * 排序
     * @param $ids
     * @return int
     */
    public function sortItemDetails($ids)
    {
        $sort = 1;
        foreach ($ids as $id){
            $this->updateBy([
                'id' => $id,
                'status' => $this->model::STATUS_ENABLED
            ], ['sort' => $sort++]);
        }
        return $sort;
    }

    /**
     * 删除表单项目
     * @param $formId
     * @param null $itemId
     * @param null $formItemDetailId
     * @return bool
     */
    public function delItemDetails($formId, $itemId = null, $formItemDetailId = null)
    {
        $map = [];
        $map['form_id'] = $formId;
        //删除整个表单项
        if(isset($itemId)){
            $itemId = is_array($itemId) ? $itemId : explode(',', $itemId);
            $map['form_item_id'] = array(['in', $itemId]);
        }
        //删除单个表单项
        if(isset($formItemDetailId)){
            $formItemDetailId = is_array($formItemDetailId) ? $formItemDetailId : explode(',', $formItemDetailId);
            $map['id'] = array(['in', $formItemDetailId]);
        }
        return $this->updateBy($map,
            ['status' => $this->model::STATUS_DELETED]
        );
    }
}
