Template.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 上海牛之云网络科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
  9. * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
  10. * =========================================================
  11. */
  12. namespace app\model\diy;
  13. use app\model\BaseModel;
  14. use app\model\web\DiyView;
  15. /**
  16. * 模板页面类型、模板组
  17. */
  18. class Template extends BaseModel
  19. {
  20. /**
  21. * 添加自定义模板类型
  22. * @param $data
  23. * @return array
  24. */
  25. public function addTemplate($data)
  26. {
  27. $res = model('diy_template')->add($data);
  28. if ($res) {
  29. return $this->success($res);
  30. } else {
  31. return $this->error($res);
  32. }
  33. }
  34. /**
  35. * 添加多个自定义模板类型
  36. * @param $data
  37. * @return array
  38. */
  39. public function addTemplateList($data)
  40. {
  41. $res = model('diy_template')->addList($data);
  42. if ($res) {
  43. return $this->success($res);
  44. } else {
  45. return $this->error($res);
  46. }
  47. }
  48. /**
  49. * 修改自定义模板类型
  50. * @param $data
  51. * @param $condition
  52. * @return array
  53. */
  54. public function editTemplate($data, $condition)
  55. {
  56. $res = model('diy_template')->update($data, $condition);
  57. if ($res) {
  58. return $this->success($res);
  59. } else {
  60. return $this->error($res);
  61. }
  62. }
  63. /**
  64. * 删除自定义模板类型
  65. * @param $condition
  66. * @return array
  67. */
  68. public function deleteTemplate($condition)
  69. {
  70. $res = model('diy_template')->delete($condition);
  71. if ($res) {
  72. return $this->success($res);
  73. } else {
  74. return $this->error($res);
  75. }
  76. }
  77. /**
  78. * 获取模板页面类型数量
  79. * @param $condition
  80. * @return array
  81. */
  82. public function getTemplateCount($condition)
  83. {
  84. $res = model('diy_template')->getCount($condition);
  85. return $this->success($res);
  86. }
  87. /**
  88. * 模板类型信息
  89. * @param array $condition
  90. * @param string $field
  91. * @return array
  92. */
  93. public function getTemplateInfo($condition = [], $field = '*')
  94. {
  95. $info = model('diy_template')->getInfo($condition, $field);
  96. return $this->success($info);
  97. }
  98. /**
  99. * 模板类型列表
  100. * @param array $condition
  101. * @param string $field
  102. * @param string $order
  103. * @return array
  104. */
  105. public function getTemplateList($condition = [], $field = '*', $order = 'sort asc')
  106. {
  107. $list = model('diy_template')->getList($condition, $field, $order);
  108. return $this->success($list);
  109. }
  110. /**
  111. * 模板类型分页列表
  112. * @param array $condition
  113. * @param int $page
  114. * @param int $page_size
  115. * @param string $order
  116. * @param string $field
  117. * @return array
  118. */
  119. public function getTemplatePageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $order = '', $field = '*')
  120. {
  121. $list = model('diy_template')->pageList($condition, $field, $order, $page, $page_size);
  122. return $this->success($list);
  123. }
  124. /**
  125. * 添加模板组
  126. * @param $data
  127. * @return array
  128. */
  129. public function addTemplateGoods($data)
  130. {
  131. $data[ 'create_time' ] = time();
  132. $res = model('diy_template_goods')->add($data);
  133. return $this->success($res);
  134. }
  135. /**
  136. * 编辑模板组
  137. * @param $data
  138. * @param $condition
  139. * @return array
  140. */
  141. public function editTemplateGoods($data, $condition)
  142. {
  143. $data[ 'modify_time' ] = time();
  144. $res = model('diy_template_goods')->update($data, $condition);
  145. return $this->success($res);
  146. }
  147. /**
  148. * 删除模板组
  149. * @param $condition
  150. * @return array
  151. */
  152. public function deleteTemplateGoods($condition)
  153. {
  154. $goods_ids = model('diy_template_goods')->getColumn($condition, 'goods_id');
  155. $res = model('diy_template_goods')->delete($condition);
  156. if (!empty($goods_ids)) {
  157. $count = model('site_diy_template')->getCount([ [ 'template_goods_id', 'in', $goods_ids ] ]);
  158. if ($count) return $this->error('', '模板正在使用中不可删除');
  159. model('diy_template_goods_item')->delete([ [ 'goods_id', 'in', $goods_ids ] ]);
  160. }
  161. return $this->success($res);
  162. }
  163. /**
  164. * 获取模板组数量
  165. * @param $condition
  166. * @return array
  167. */
  168. public function getTemplateGoodsCount($condition)
  169. {
  170. $res = model('diy_template_goods')->getCount($condition);
  171. return $this->success($res);
  172. }
  173. /**
  174. * 获取模板组
  175. * @param $condition
  176. * @param string $field
  177. * @return array
  178. */
  179. public function getTemplateGoodsInfo($condition, $field = '*')
  180. {
  181. $info = model('diy_template_goods')->getInfo($condition, $field);
  182. return $this->success($info);
  183. }
  184. /**
  185. * 获取一条模板组
  186. * @param $condition
  187. * @param string $field
  188. * @param string $order
  189. * @return array
  190. */
  191. public function getFirstTemplateGoods($condition, $field = '*', $order = '')
  192. {
  193. $info = model('diy_template_goods')->getFirstData($condition, $field, $order);
  194. return $this->success($info);
  195. }
  196. /**
  197. * 模板组列表
  198. * @param array $condition
  199. * @param string $field
  200. * @param string $order
  201. * @return array
  202. */
  203. public function getTemplateGoodsList($condition = [], $field = '*', $order = '')
  204. {
  205. $res = model('diy_template_goods')->getList($condition, $field, $order);
  206. return $this->success($res);
  207. }
  208. /**
  209. * 模板组列表
  210. * @param array $condition
  211. * @param int $page
  212. * @param int $page_size
  213. * @param string $field
  214. * @param string $order
  215. * @param string $alias
  216. * @param array $join
  217. * @return array
  218. */
  219. public function getTemplateGoodsPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = 'create_time desc', $alias = '', $join = [])
  220. {
  221. $res = model('diy_template_goods')->pageList($condition, $field, $order, $page, $page_size, $alias, $join);
  222. return $this->success($res);
  223. }
  224. /**
  225. * 添加模板页面
  226. * @param $data
  227. * @return array
  228. */
  229. public function addTemplateGoodsItem($data)
  230. {
  231. $data[ 'create_time' ] = time();
  232. $res = model('diy_template_goods_item')->add($data);
  233. return $this->success($res);
  234. }
  235. /**
  236. * 添加多个模板页面
  237. * @param $data
  238. * @return array
  239. */
  240. public function addTemplateGoodsItemList($data)
  241. {
  242. $res = model('diy_template_goods_item')->addList($data);
  243. return $this->success($res);
  244. }
  245. /**
  246. * 编辑模板页面
  247. * @param $data
  248. * @param $condition
  249. * @return array
  250. */
  251. public function editTemplateGoodsItem($data, $condition)
  252. {
  253. $data[ 'modify_time' ] = time();
  254. $res = model('diy_template_goods_item')->update($data, $condition);
  255. return $this->success($res);
  256. }
  257. /**
  258. * 删除模板页面
  259. * @param $condition
  260. * @return array
  261. */
  262. public function deleteTemplateGoodsItem($condition)
  263. {
  264. $res = model('diy_template_goods_item')->delete($condition);
  265. return $this->success($res);
  266. }
  267. /**
  268. * 获取模板页面
  269. * @param $condition
  270. * @param string $field
  271. * @param string $alias
  272. * @param array $join
  273. * @return array
  274. */
  275. public function getTemplateGoodsItemInfo($condition, $field = '*', $alias = '', $join = [])
  276. {
  277. $info = model('diy_template_goods_item')->getInfo($condition, $field, $alias, $join);
  278. return $this->success($info);
  279. }
  280. /**
  281. * 模板页面列表
  282. * @param array $condition
  283. * @param int $page
  284. * @param int $page_size
  285. * @param string $order
  286. * @param string $field
  287. * @return array
  288. */
  289. public function getTemplateGoodsItemList($condition = [], $field = '*', $order = 'create_time desc')
  290. {
  291. $list = model('diy_template_goods_item')->getList($condition, $field, $order);
  292. return $this->success($list);
  293. }
  294. /**
  295. * 模板页面分页列表
  296. * @param array $condition
  297. * @param int $page
  298. * @param int $page_size
  299. * @param string $field
  300. * @param string $order
  301. * @return array
  302. */
  303. public function getTemplateGoodsItemPageList($condition = [], $page = 1, $page_size = PAGE_LIST_ROWS, $field = '*', $order = 'create_time desc')
  304. {
  305. $list = model('diy_template_goods_item')->pageList($condition, $field, $order, $page, $page_size);
  306. return $this->success($list);
  307. }
  308. /**
  309. * 使用模板
  310. * @param $params
  311. * @return array
  312. */
  313. public function useTemplate($params)
  314. {
  315. try {
  316. model('diy_template_goods')->startTrans();
  317. $template_goods_info = model('diy_template_goods')->getInfo([ [ 'goods_id', '=', $params[ 'goods_id' ] ] ], 'goods_id,name,addon_name');
  318. if (empty($template_goods_info)) {
  319. return $this->error('', '模板组不存在');
  320. }
  321. // 添加店铺模板组关联关系
  322. $count = $this->getSiteDiyTemplateCount([ [ 'site_id', '=', $params[ 'site_id' ] ], [ 'template_goods_id', '=', $params[ 'goods_id' ] ] ])[ 'data' ];
  323. if ($count == 0) {
  324. $this->addSiteDiyTemplate([
  325. 'name' => $template_goods_info[ 'name' ],
  326. 'site_id' => $params[ 'site_id' ],
  327. 'template_goods_id' => $template_goods_info[ 'goods_id' ],
  328. 'addon_name' => $template_goods_info[ 'addon_name' ]
  329. ]);
  330. } else {
  331. $this->modifySiteDiyTemplateIsDefault([
  332. 'site_id' => $params[ 'site_id' ],
  333. 'template_goods_id' => $template_goods_info[ 'goods_id' ]
  334. ]);
  335. }
  336. $diy_view_model = new DiyView();
  337. // 查询模板页面列表,遍历添加到站点页面中
  338. $item_list = $this->getTemplateGoodsItemList([
  339. [ 'goods_id', '=', $params[ 'goods_id' ] ]
  340. ], 'goods_item_id, goods_id, title, name, value, addon_name')[ 'data' ];
  341. if (!empty($item_list)) {
  342. foreach ($item_list as $k => $v) {
  343. // 查询页面类型
  344. $template_info = $this->getTemplateInfo([
  345. [ 'name', '=', $v[ 'name' ] ]
  346. ], 'name,title')[ 'data' ];
  347. $type = 'DIY_PAGE';
  348. $type_name = '自定义页面';
  349. if (!empty($template_info)) {
  350. $type = $template_info[ 'name' ];
  351. $type_name = $template_info[ 'title' ];
  352. }
  353. $site_diy_view_info = $diy_view_model->getSiteDiyViewInfo([
  354. [ 'site_id', '=', $params[ 'site_id' ] ],
  355. [ 'name', '=', $v[ 'name' ] ],
  356. [ 'template_id', '=', $v[ 'goods_id' ] ],
  357. [ 'template_item_id', '=', $v[ 'goods_item_id' ] ],
  358. [ 'addon_name', '=', $v[ 'addon_name' ] ],
  359. [ 'type', '=', $type ],
  360. ], 'id')[ 'data' ];
  361. $site_diy_data = [
  362. 'site_id' => $params[ 'site_id' ],
  363. 'name' => $v[ 'name' ], // 模板标识
  364. 'title' => $v[ 'title' ], // 模板名称
  365. 'template_id' => $v[ 'goods_id' ], // 所属模板id
  366. 'template_item_id' => $v[ 'goods_item_id' ], // 模板页面id
  367. 'value' => $v[ 'value' ], // 模板数据,json格式
  368. 'addon_name' => $v[ 'addon_name' ],
  369. 'type' => $type,
  370. 'type_name' => $type_name,
  371. 'is_default' => 1
  372. ];
  373. // 清除默认页面
  374. $diy_view_model->editSiteDiyView([ 'is_default' => 0 ], [
  375. [ 'site_id', '=', $params[ 'site_id' ] ],
  376. [ 'name', '=', $v[ 'name' ] ]
  377. ]);
  378. // 检测模板页面是否存在,有则改,无则加
  379. if (!empty($site_diy_view_info)) {
  380. // 修改相同页面的默认标识
  381. $site_diy_data[ 'modify_time' ] = time();
  382. $diy_view_model->editSiteDiyView($site_diy_data, [ [ 'id', '=', $site_diy_view_info[ 'id' ] ] ]);
  383. } else {
  384. $site_diy_data[ 'create_time' ] = time();
  385. $diy_view_model->addSiteDiyView($site_diy_data);
  386. }
  387. }
  388. }
  389. // 累加模板组使用次数
  390. model('diy_template_goods')->setInc([ [ 'goods_id', '=', $params[ 'goods_id' ] ] ], 'use_num');
  391. model('diy_template_goods')->commit();
  392. return $this->success();
  393. } catch (\Exception $e) {
  394. model('diy_template_goods')->rollback();
  395. return $this->error('', 'File:' . $e->getFile() . ',Line:' . $e->getLine() . ',Message:' . $e->getMessage() . ',Code:' . $e->getCode());
  396. }
  397. }
  398. /**
  399. * 添加店铺关联模板组
  400. * @param $data
  401. * @return array
  402. */
  403. public function addSiteDiyTemplate($data)
  404. {
  405. $data[ 'create_time' ] = time();
  406. $data[ 'is_default' ] = 1; // 设置店铺默认模板
  407. model('site_diy_template')->update([ 'is_default' => 0 ], [ [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  408. $res = model('site_diy_template')->add($data);
  409. return $this->success($res);
  410. }
  411. /**
  412. * 修改店铺关联模板组
  413. * @param $data
  414. * @param $condition
  415. * @return array
  416. */
  417. public function editSiteDiyTemplate($data, $condition)
  418. {
  419. $res = model('site_diy_template')->update($data, $condition);
  420. return $this->success($res);
  421. }
  422. /**
  423. * 添加店铺关联模板组
  424. * @param $data
  425. * @return array
  426. */
  427. public function modifySiteDiyTemplateIsDefault($data)
  428. {
  429. model('site_diy_template')->update([ 'is_default' => 0 ], [ [ 'site_id', '=', $data[ 'site_id' ] ] ]);
  430. $res = model('site_diy_template')->update([ 'is_default' => 1 ], [ [ 'site_id', '=', $data[ 'site_id' ] ], [ 'template_goods_id', '=', $data[ 'template_goods_id' ] ] ]);
  431. return $this->success($res);
  432. }
  433. /**
  434. * 删除店铺关联模板组
  435. * @param $condition
  436. * @return array
  437. */
  438. public function deleteSiteDiyTemplate($condition)
  439. {
  440. $res = model('site_diy_template')->delete($condition);
  441. return $this->success($res);
  442. }
  443. /**
  444. * 查询店铺关联模板组数量
  445. * @param $condition
  446. * @return array
  447. */
  448. public function getSiteDiyTemplateCount($condition)
  449. {
  450. $res = model('site_diy_template')->getCount($condition);
  451. return $this->success($res);
  452. }
  453. /**
  454. * 查询店铺关联模板组信息
  455. * @param $condition
  456. * @param $field
  457. * @return array
  458. */
  459. public function getSiteDiyTemplateInfo($condition, $field = 'id,name,template_goods_id,is_default')
  460. {
  461. $res = model('site_diy_template')->getInfo($condition, $field);
  462. return $this->success($res);
  463. }
  464. /**
  465. * 店铺关联模板组列表
  466. * @param array $condition
  467. * @param int $page
  468. * @param int $page_size
  469. * @param string $order
  470. * @param string $field
  471. * @return array
  472. */
  473. public function getSiteDiyTemplateList($condition = [], $field = '*', $order = 'create_time desc')
  474. {
  475. $list = model('site_diy_template')->getList($condition, $field, $order);
  476. return $this->success($list);
  477. }
  478. }