PaywayModel.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. /**
  3. * PaywayModel.php UTF-8
  4. * 支付方式
  5. *
  6. * @date : 2018/5/25 16:44
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huo\model\conf;
  13. use huo\model\common\CommonModel;
  14. use huolib\constant\CacheConst;
  15. use huolib\constant\CommonConst;
  16. use think\Cache;
  17. class PaywayModel extends CommonModel {
  18. protected $name = 'payway';
  19. protected $pk = 'id';
  20. protected $type = ['config' => 'array'];
  21. protected $cache_key_prefix = CacheConst::CACHE_PAY_WAY_PREFIX;
  22. protected $cache_tag = CacheConst::CACHE_PAY_WAY_TAG;
  23. /**
  24. * 获取单条记录缓存key
  25. *
  26. * @param int $id ID
  27. *
  28. * @return string :string
  29. */
  30. protected function getSingleCacheKey($id) {
  31. return $this->cache_key_prefix.$id;
  32. }
  33. /**
  34. * 根据支付方式获取缓存key
  35. *
  36. * @param string $payname 支付方式
  37. *
  38. * @return string :string
  39. */
  40. protected function getCacheKeyByPayname($payname) {
  41. return $this->cache_key_prefix.$payname;
  42. }
  43. /**
  44. * 添加数据
  45. *
  46. * @param array $data 需要添加的数据
  47. *
  48. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  49. */
  50. public function addData($data) {
  51. if (empty($data)) {
  52. return false;
  53. }
  54. $_data = $data;
  55. $_model = new static();
  56. $_rs = $_model->allowField(true)->isUpdate(false)->save($_data, []);
  57. if (false !== $_rs) {
  58. return $_model->getLastInsID();
  59. }
  60. Cache::clear($this->cache_tag);
  61. return false;
  62. }
  63. /**
  64. * 通过ID获取信息
  65. *
  66. * @param int $id 主键ID
  67. *
  68. * @return array
  69. */
  70. public function getInfoById($id) {
  71. /* 缓存操作 */
  72. $_single_cache_key = $this->getSingleCacheKey($id);
  73. $_data = Cache::get($_single_cache_key);
  74. if (!empty($_data)) {
  75. return $_data;
  76. }
  77. $_data = parent::getInfoById($id);
  78. if (empty($_data)) {
  79. return [];
  80. }
  81. Cache::set($_single_cache_key, $_data);
  82. return $_data;
  83. }
  84. /**
  85. * 更新单条数据
  86. *
  87. * @param array $data 数据
  88. * @param int $id ID
  89. *
  90. * @return bool
  91. */
  92. public function updateData($data, $id) {
  93. $_data = $data;
  94. $_map[$this->pk] = $id;
  95. $_model = new static();
  96. $_rs = $_model->isUpdate(true)->save($_data, $_map);
  97. if (false === $_rs) {
  98. return false;
  99. }
  100. /* 缓存操作 */
  101. $_single_cache_key = $this->getSingleCacheKey($id);
  102. Cache::rm($_single_cache_key);
  103. Cache::clear($this->cache_tag);
  104. return true;
  105. }
  106. /**
  107. * 删除单条数据
  108. *
  109. * @param int $id ID
  110. * @param bool $is_complete 是否完成删除
  111. *
  112. * @return bool
  113. */
  114. public function deleteData($id, $is_complete = true) {
  115. $_old_data = $this->getInfoById($id);
  116. $_map[$this->pk] = $id;
  117. if (true == $is_complete) {
  118. /* 彻底删除 */
  119. $_rs = $this->where($_map)->delete();
  120. } else {
  121. $_data['is_delete'] = CommonConst::CONST_DELETED;
  122. $_data['delete_time'] = time();
  123. $_rs = self::updateData($_data, $id);
  124. }
  125. if (false === $_rs) {
  126. return false;
  127. }
  128. /* 缓存操作 */
  129. $_single_cache_key = $this->getSingleCacheKey($id);
  130. Cache::rm($_single_cache_key);
  131. /* 删除缓存 */
  132. $_cache_key = $this->getCacheKeyByPayname($_old_data['payname']);
  133. Cache::rm($_cache_key);
  134. Cache::clear($this->cache_tag);
  135. return $_rs;
  136. }
  137. /**
  138. * 根据payname 获取ID
  139. *
  140. * @param $payname
  141. *
  142. * @return mixed
  143. */
  144. public function getIdByPayname($payname) {
  145. $_cache_key = $this->getCacheKeyByPayname($payname);
  146. $_id = Cache::get($_cache_key);
  147. if (!empty($_id)) {
  148. return $_id;
  149. }
  150. $_map = [
  151. 'payname' => $payname
  152. ];
  153. $_id = $this->where($_map)->value('id');
  154. if (empty($_id)) {
  155. return CommonConst::CONST_ZERO;
  156. }
  157. Cache::set($_cache_key, $_id);
  158. return $_id;
  159. }
  160. /**
  161. * 根据payname 获取信息
  162. *
  163. * @param $payname
  164. *
  165. * @return mixed
  166. */
  167. public function getInfoByPayname($payname) {
  168. $_id = $this->getIdByPayname($payname);
  169. return $this->getInfoById($_id);
  170. }
  171. /**
  172. * 通过PayName获取列表
  173. *
  174. * @param int $status
  175. * @param int $is_config
  176. * @param string $code
  177. * @param int $id
  178. * @param null $parent_id
  179. *
  180. * @return array
  181. */
  182. public function getPayWaysByPayName($status = 2, $is_config = 0, $code = '', $id = 0, $parent_id = null) {
  183. $_map = [];
  184. if (!empty($status)) {
  185. $_map['status'] = $status;
  186. }
  187. if (!empty($id)) {
  188. $_map['id'] = $id;
  189. }
  190. if (!empty($is_config)) {
  191. $_map['is_config'] = $is_config;
  192. }
  193. if (!empty($parent_id)) {
  194. $_map['parent_id'] = $parent_id;
  195. }
  196. if (!empty($code)) {
  197. $_map['code'] = $code;
  198. }
  199. $_tag = $this->cache_tag;
  200. $_cache_key = md5($_tag.json_encode($_map));
  201. $_field = 'realname';
  202. $_payways = $this->where($_map)
  203. ->cache($_cache_key, CommonConst::CONST_DAY_SECONDS, $_tag)
  204. ->column($_field, 'payname');
  205. return $_payways;
  206. }
  207. }