/**
  * 选择地区
  * @return \Illuminate\Http\JsonResponse
  * @throws DeepInException
  */
 public function selectLocal()
 {
     $id = intval(Input::get("id"));
     $isCheck = intval(Input::get("checked"));
     //0取消选择  1选择
     $local = Input::get("local", null);
     if (empty($local)) {
         throw new DeepInException("地区不能为空~!");
     }
     $isCheck = $isCheck == 1 || 0;
     $app = $this->find($id);
     //应用对象
     if ($isCheck == 1) {
         //选在语言
         $appLocal = ShopAppsLocal::whereRaw("appid=:appid and local=:local", array(":appid" => $app->appId(), ":local" => $local))->first();
         if (empty($appLocal)) {
             $appLocal = new ShopAppsLocal();
             $appLocal->appId($id);
             $appLocal->local($local);
             if ($appLocal->save() == false) {
                 throw new DeepInException("操作失败~!");
             }
         }
     } else {
         //取消选择
         //            $this->checkLocal($app,$local);
         $this->checkAppReferred($app->appId(), $local);
         $affectedRow = ShopAppsLocal::whereRaw("appid=:appid and local=:local", array(":appid" => $app->appId(), ":local" => $local))->delete();
         if ($affectedRow < 1) {
             throw new DeepInException("取消选择失败~!");
         }
     }
     return $this->successJSON();
 }
 /**
  * 专题的应用的地区
  * @param $appId
  * @return mixed
  */
 protected function getAppLocals($appId)
 {
     $list = ShopAppsLocal::whereRaw("appid=:appid", array(":appid" => $appId))->get();
     return $list;
 }
 /**
  * 集合列表页面显示的数据 [选择的应用,和还未被选在的应用]
  * @param $type
  * @param $id
  * @return \Illuminate\View\View
  * @throws DeepInException
  * @throws DeepInHtmlException
  */
 public function index($type, $id)
 {
     if ($this->checkAppList($type, $id) == false) {
         //错误的type和id
         throw new DeepInHtmlException("错误的参数~!");
     }
     if ($type == 1) {
         //栏目
         $column = ShopColumn::find($id);
         if (!$column instanceof ShopColumn) {
             throw new DeepInException("{$id}栏目不存在");
         }
         $local = $column->local();
     } else {
         //专题
         $topic = ShopTopic::find($id);
         if (!$topic instanceof ShopTopic) {
             throw new DeepInException("{$id}专题不存在");
         }
         $local = $topic->local();
     }
     $canDisplayApps = array();
     $appLocals = ShopAppsLocal::whereRaw("local=:local", array(":local" => $local))->get();
     foreach ($appLocals as $v) {
         if ($v instanceof ShopAppsLocal) {
             $canDisplayApps[] = $v->appId();
         }
     }
     //指定列表下的所有的应用列表集合
     $list = $this->findByTypeAndItem($type, $id);
     $ids = array();
     $newList = array();
     //最终已选择信息的组合
     foreach ($list as $v) {
         $ids[] = $v->appId();
         $newList[$v->appId] = $v->toArray();
     }
     unset($list);
     //删掉老数据
     $pageSize = 16;
     $packageId = \Input::get("packageid", null);
     //查询未被选择应用
     if (empty($ids) == false) {
         //还未被选选择的应用
         $where = "appid not in (" . implode(",", $ids) . ") and inuse=1";
         $bind = array();
         if (empty($packageId) == false) {
             $where .= " and packageid LIKE \"%{$packageId}%\"";
         }
         $unSelectApps = ShopApps::whereRaw($where, $bind)->whereIn("appid", $canDisplayApps)->paginate($pageSize);
         $selectedApps = ShopApps::whereRaw("appid  in (" . implode(",", $ids) . ")", array())->get();
     } else {
         $where = " inuse=1 ";
         $bind = array();
         if (empty($packageId) == false) {
             $where .= " and packageid LIKE \"%{$packageId}%\"";
         }
         $unSelectApps = ShopApps::whereRaw($where, $bind)->whereIn("appid", $canDisplayApps)->paginate($pageSize);
         $selectedApps = array();
     }
     //两者的数据合并
     foreach ($selectedApps as $v) {
         $v = $v->toArray();
         $newList[$v["appId"]] = array_merge($v, $newList[$v["appId"]]);
     }
     //排序
     usort($newList, function ($a, $b) {
         $diff = intval($a["pos"]) - intval($b["pos"]);
         if ($diff == 0) {
             return 0;
         }
         if ($diff < 0) {
             return 1;
         }
         return -1;
     });
     unset($selectedApps);
     return view("admin.applist.index", array("unselect" => $unSelectApps, "selected" => $newList, "type" => $type, "id" => $id));
 }