FormInfoService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <?php
  2. namespace App\Form\Services;
  3. use App\Exceptions\ApiException;
  4. use App\Services\CommonUserBaseService;
  5. use App\Form\Facades\FormItemDetailFacade;
  6. use App\Form\Facades\FormItemFacade;
  7. use App\Form\Facades\FormRecordFacade;
  8. use App\Website\Facades\WebsitePageFacade;
  9. class FormInfoService extends CommonUserBaseService
  10. {
  11. protected $cache = true;
  12. protected $cacheBucket = 'FormInfo:';
  13. protected $tokenBucket = 'Token:';
  14. protected $activeBucket = "Active:";
  15. const DEF_FORM_NAME_EN='表单';
  16. public function getList($params)
  17. {
  18. $pageSize = !empty($params['page_size']) ? $params['page_size'] : 10; //页面大小,不传默认一页10条记录
  19. $pageNo = !empty($params['page']) ? $params['page'] : 1; //页码,不传默认第1页
  20. $skip = ($pageNo - 1) * $pageSize; //页面记录的开始位置,即偏移量
  21. $query = $this->getModel()->alias('a');
  22. if(!empty($params['status'])){
  23. $query= $query->where('a.status','=',$params['status']);
  24. }else{
  25. $query= $query->where('a.status','<',2);
  26. }
  27. if(!empty($params['keyword'])){
  28. $query= $query->where('a.name','like','%'.$params['keyword'].'%');
  29. }
  30. if(!empty($params['date'])){
  31. $date=$params['date'];
  32. $beginTime=$date.' 00:00:00';
  33. $endTime=$date.' 23:59:59';
  34. $query=$query->whereBetween('a.create_time',[$beginTime,$endTime ]);
  35. }
  36. $totalCount=$query->count();
  37. $data=$query->selectRaw('a.*,0 as count,0 as not_read_count')
  38. ->skip($skip)
  39. ->limit($pageSize)
  40. ->orderByDesc('a.data_update_time')
  41. ->orderByDesc('a.create_time')
  42. ->get()->toArray();
  43. $resultData = [];
  44. if(!empty($data)){
  45. $resultData = mapByKey($data, 'id');
  46. $formIds = array_keys($resultData);
  47. $recordCountData = $this->getFormRecordCount($formIds);
  48. $recordNotReadCountData = $this->getFormRecordNotReadCount($formIds);
  49. $resultData = combineArray($resultData, $recordCountData, 'id');
  50. $resultData = combineArray($resultData, $recordNotReadCountData, 'id');
  51. $resultData = array_values($resultData);
  52. }
  53. $result = buildPage($resultData, $skip, $pageNo, $pageSize, $totalCount);
  54. return $result;
  55. }
  56. public function setDataUpdateTime($formId){
  57. $nowTime=nowTime();
  58. $ret= $this->model->where(['id'=>$formId])->update(['data_update_time'=>$nowTime,'update_time'=>$nowTime]);
  59. return $ret;
  60. }
  61. public function setFormIdsUpdateTime($formIds){
  62. $nowTime=nowTime();
  63. $ret= $this->model->whereIn('id',$formIds)->update(['data_update_time'=>$nowTime,'update_time'=>$nowTime]);
  64. return $ret;
  65. }
  66. private function getFormRecordCount($formIds)
  67. {
  68. $formRecordModel = FormRecordFacade::getModel();
  69. $data= $formRecordModel->whereIn('form_id',$formIds)
  70. ->where('status','<',2)
  71. ->selectRaw('form_id as id, count(1) as count')
  72. ->groupBy('form_id')->get()->toArray();
  73. return $data;
  74. }
  75. /**
  76. * 获取未读询盘数据数量
  77. * */
  78. private function getFormRecordNotReadCount($formIds)
  79. {
  80. $formRecordModel = FormRecordFacade::getModel();
  81. $data= $formRecordModel->whereIn('form_id',$formIds)
  82. ->where('is_read',0)
  83. ->where('status','<',2)
  84. ->selectRaw('form_id as id, count(0) as not_read_count')
  85. ->groupBy('form_id')->get()->toArray();
  86. return $data;
  87. }
  88. /**
  89. * 获取未读询盘数据总数量
  90. * */
  91. public function getCompanyFormRecordNotReadCount()
  92. {
  93. $formRecordModel = FormRecordFacade::getModel();
  94. $result= $formRecordModel->alias('a')
  95. ->where('a.is_read',0)
  96. ->where('b.status','=',0)
  97. ->selectRaw('count(0) as not_read_count')
  98. ->first()->toArray();
  99. return $result;
  100. }
  101. /**
  102. * 获取表单数据
  103. * @param $id
  104. * @return mixed
  105. */
  106. public function getData($id)
  107. {
  108. $form = $this->findOneBy(['id' => $id, 'status' => 0], 'id,name');
  109. $form['items'] = FormItemFacade::getData($id);
  110. return $form;
  111. }
  112. /**
  113. * 添加表单信息
  114. * @param array $params
  115. * @param null $userId
  116. * @return mixed
  117. */
  118. public function addData($params = [], $userId = null)
  119. {
  120. $userId = $userId ?? $this->getAuthUserId();
  121. $data= $this->buildFormData($params,$userId);
  122. if(!empty($data['id'])){
  123. $id = $data['id'];
  124. $this->model->where('id','=',$id)->update($data);
  125. } else {
  126. $id = $this->save($data)->id;
  127. }
  128. if(!empty($params['form_items'])){
  129. FormItemFacade::addList($id, $params['form_items'], $userId);
  130. }
  131. return $id;
  132. }
  133. private function buildFormData($params,$userId = null){
  134. $userId = $userId ?? $this->getAuthUserId();
  135. $add = [];
  136. $add['update_time']=nowTime();
  137. if(!empty($params['id'])){
  138. $add['id']=$params['id'];
  139. if(isset($params['name'])){
  140. $add['name'] = $params['name'];
  141. if(empty($params['name'])){
  142. throw new ApiException(12001);
  143. }
  144. $nameUnique =$this->model->checkNameUnique($params['name'],$params['id']);
  145. if (!$nameUnique) {
  146. throw new ApiException(10018, ['name' => $params['name']]);
  147. }
  148. }
  149. }else{
  150. if(empty($params['name'])){
  151. throw new ApiException(12001);
  152. }
  153. $nameUnique =$this->model->checkNameUnique($params['name'],'');
  154. if (!$nameUnique) {
  155. throw new ApiException(10018, ['name' => $params['name']]);
  156. }
  157. $add['name'] = $params['name'];
  158. $add['user_id']=$userId;
  159. }
  160. return $add;
  161. }
  162. public function setFormByEditor($param, $userId = null)
  163. {
  164. $formParams = $param ?? [];
  165. $formParams['name'] = $formParams['form_name'] ?? '';
  166. $formParams['id'] = $formParams['form_id'] ?? '';
  167. $return = $this->addData($formParams, true,$userId);
  168. return $return;
  169. }
  170. /**
  171. * 处理页面保存的表单信息
  172. * @param $element
  173. * @param null $userId
  174. * @return mixed
  175. */
  176. public function processElement($element, $userId = null)
  177. {
  178. $userId = $userId ?? $this->getAuthUserId();
  179. foreach ($element as &$el){
  180. if(isset($el['type']) && isset($el['value'])){
  181. //如果组件类型是表单
  182. if($el['type'] == 'form' || $el['type'] == 'contactForm'){
  183. $el['value'] = $this->setFormByEditor($el['value'], $userId);
  184. } else if($el['type'] == 'itemList' || $el['type'] == 'item'){
  185. foreach ($el['value'] as &$elm){
  186. $elm = $this->processElement($elm, $userId);
  187. }
  188. }
  189. }
  190. }
  191. return $element;
  192. }
  193. /**
  194. * 添加表单项,单条
  195. * @param array $params
  196. * @return mixed
  197. */
  198. public function addItem($params = [])
  199. {
  200. $userId=$this->getAuthUserId();
  201. $formId = $params['form_id'] ?? '';
  202. $id=FormItemFacade::addData($params['form_id'] ?? '', $params);
  203. if($id&&!empty($params['content'])){
  204. FormItemDetailFacade::addList($formId,$id, $params['content'], $userId);
  205. }
  206. return $id;
  207. }
  208. /**
  209. * 删除表单项,单条
  210. * */
  211. public function delItem($params){
  212. $formId = $params['form_id'] ?? '';
  213. $itemId = $params['id'] ?? 0;
  214. $ret=FormItemFacade::delData($formId,$itemId);
  215. return $ret;
  216. }
  217. /**
  218. * 表单项排序
  219. * @param $id
  220. * @return mixed
  221. */
  222. public function sortItem($id)
  223. {
  224. return FormItemFacade::sortItem($id);
  225. }
  226. /**
  227. * 表单项详情排序
  228. * @param $id
  229. * @return mixed
  230. */
  231. public function sortItemDetail($id)
  232. {
  233. return FormItemDetailFacade::sortItemDetails($id);
  234. }
  235. public function setIsRemove($id){
  236. if(!is_array($id)){
  237. $id=[$id];
  238. }
  239. $ret= $this->model->whereIn('id',$id)->update(['is_remove'=>1]);
  240. return $ret;
  241. }
  242. /**
  243. * 禁用或删除表单
  244. * */
  245. public function stopAndDelFormById($id,$status=1){
  246. $model = $this->getModel();
  247. $updateData=[];
  248. $updateData['status']=$status;
  249. $updateData['update_time']=nowTime();
  250. $ret= $model->where('id','=',$id)->update($updateData);
  251. return $ret;
  252. }
  253. /**
  254. * 当有表单提交的时候重新激活禁用状态的表单
  255. * */
  256. public function reActivateFormById($id){
  257. $model = $this->getModel();
  258. $updateData=[];
  259. $updateData['status']=0;
  260. $updateData['update_time']=nowTime();
  261. $ret= $model->where('id','=',$id)->where('status','=','1')->update($updateData);
  262. return $ret;
  263. }
  264. /**
  265. * 获取对应公司下询盘列表
  266. * */
  267. public function correspondCompanyFormList($params)
  268. {
  269. $pageSize = !empty($params['page_size']) ? $params['page_size'] : 10; //页面大小,不传默认一页10条记录
  270. $pageNo = !empty($params['page']) ? $params['page'] : 1; //页码,不传默认第1页
  271. $skip = ($pageNo - 1) * $pageSize; //页面记录的开始位置,即偏移量
  272. $model = $this->getModel();
  273. $query= $model->alias('a')
  274. ->leftJoin('website_page as b', 'a.website_page_id', '=', 'b.id')
  275. ->where('a.status','=',0);
  276. if(!empty($params['keyword'])){
  277. $query= $query->where('a.name','like','%'.$params['keyword'].'%');
  278. }
  279. if(!empty($params['date'])){
  280. $date=$params['date'];
  281. $beginTime=$date.' 00:00:00';
  282. $endTime=$date.' 23:59:59';
  283. $query=$query->whereBetween('a.create_time',[$beginTime,$endTime ]);
  284. }
  285. $totalCount=$query->count();
  286. $data=$query->selectRaw('a.*,b.page_name,0 as count,0 as not_read_count,b.urla,b.link,c.name as website_name ')
  287. ->skip($skip)
  288. ->limit($pageSize)
  289. ->orderByDesc('a.data_update_time')
  290. ->orderByDesc('a.create_time')
  291. ->get()->toArray();
  292. $resultData = [];
  293. if(!empty($data)){
  294. $resultData = mapByKey($data, 'id');
  295. $formIds = array_keys($resultData);
  296. $recordCountData = $this->getFormRecordCount($formIds);
  297. $recordNotReadCountData = $this->getFormRecordNotReadCount($formIds);
  298. $resultData = combineArray($resultData, $recordCountData, 'id');
  299. $resultData = combineArray($resultData, $recordNotReadCountData, 'id');
  300. $resultData = array_values($resultData);
  301. }
  302. $result = buildPage($resultData, $skip, $pageNo, $pageSize, $totalCount);
  303. return $result;
  304. }
  305. }