AgreementServices.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\other;
  12. use app\dao\other\AgreementDao;
  13. use app\services\BaseServices;
  14. use think\exception\ValidateException;
  15. /**
  16. * Class AgreementServices
  17. * @package app\services\other
  18. * @mixin AgreementDao
  19. */
  20. class AgreementServices extends BaseServices
  21. {
  22. public function __construct(AgreementDao $dao)
  23. {
  24. $this->dao = $dao;
  25. }
  26. /**
  27. * 修改协议内容
  28. * @param array $data
  29. * @param int $id
  30. * @return bool|\crmeb\basic\BaseModel
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\DbException
  33. * @throws \think\db\exception\ModelNotFoundException
  34. */
  35. public function saveAgreement(array $data, int $id = 0)
  36. {
  37. if (!$data) return false;
  38. if (!isset($data['type']) || !$data['type'] || $data['type'] == 0) throw new ValidateException('协议类型缺失');
  39. if (!isset($data['title']) || !$data['title']) throw new ValidateException('请填写协议名称');
  40. if (!isset($data['content']) || !$data['content']) throw new ValidateException('请填写协议内容');
  41. if (!$id){
  42. $getOne = $this->getAgreementBytype($data['type']);
  43. if ($getOne) throw new ValidateException('该类型协议已经存在');
  44. }
  45. return $this->dao->saveAgreement($data, $id);
  46. }
  47. /**
  48. * 获取会员协议
  49. * @param $type
  50. * @return array|\think\Model|null
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function getAgreementBytype($type)
  56. {
  57. if (!$type) return [];
  58. $data = $this->dao->getOne(['type' => $type]);
  59. return $data ? $data->toArray() : [];
  60. }
  61. }