IdentifyHolidaySetModel.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /**
  3. * IdentifyHolidaySetModel.php UTF-8
  4. * 节假日设置
  5. * http://doc.1tsdk.com/170?page_id=11228
  6. *
  7. * @date : 2019/12/4 16:11
  8. *
  9. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  10. * @author : chenbingling <cbl@huosdk.com>
  11. * @version : HUOSDK 8.5
  12. */
  13. namespace huoIdentify\model;
  14. use huo\model\common\CommonModel;
  15. use huolib\constant\CacheConst;
  16. use huolib\constant\CommonConst;
  17. use think\Cache;
  18. class IdentifyHolidaySetModel extends CommonModel {
  19. protected $name = 'identify_holiday_set';
  20. protected $pk = 'id';
  21. protected $cache_key_prefix = CacheConst::CACHE_IDENTIFY_HOLIDAY_SET_PREFIX;
  22. /* 开启自动写入时间戳字段 */
  23. protected $autoWriteTimestamp = true;
  24. protected $type = ['holiday' => 'array', 'workday' => 'array'];
  25. /**
  26. * 获取单条记录缓存key
  27. *
  28. * @param int $id ID
  29. *
  30. * @return string :string
  31. */
  32. protected function getSingleCacheKey($id) {
  33. return $this->cache_key_prefix.$id;
  34. }
  35. /**
  36. * 根据年份获取缓存key
  37. *
  38. * @param int $year 年份
  39. *
  40. * @return string :string
  41. */
  42. protected function getCacheKeyByYear($year) {
  43. return $this->cache_key_prefix.'year_'.$year;
  44. }
  45. /**
  46. * 添加数据
  47. *
  48. * @param array $data 需要添加的数据
  49. *
  50. * @return false|int 添加失败返回 false 添加成功 返回添加的ID
  51. */
  52. public function addData($data) {
  53. $_data = $data;
  54. $_id = parent::addData($_data);
  55. if (false === $_id) {
  56. return false;
  57. }
  58. return $_id;
  59. }
  60. /**
  61. * 通过ID获取信息
  62. *
  63. * @param int $id 主键ID
  64. *
  65. * @return array
  66. */
  67. public function getInfoById($id) {
  68. /* 缓存操作 */
  69. $_single_cache_key = $this->getSingleCacheKey($id);
  70. $_data = Cache::get($_single_cache_key);
  71. if (!empty($_data)) {
  72. return $_data;
  73. }
  74. $_data = parent::getInfoById($id);
  75. if (empty($_data)) {
  76. return [];
  77. }
  78. Cache::set($_single_cache_key, $_data);
  79. return $_data;
  80. }
  81. /**
  82. * 更新单条数据
  83. *
  84. * @param array $data 数据
  85. * @param int $id ID
  86. *
  87. * @return bool
  88. */
  89. public function updateData($data, $id) {
  90. $_map[$this->pk] = $id;
  91. $_data = $data;
  92. $_rs = $this->allowField(true)->isUpdate(true)->save($_data, $_map);
  93. if (false === $_rs) {
  94. return false;
  95. }
  96. /* 缓存操作 */
  97. $_single_cache_key = $this->getSingleCacheKey($id);
  98. Cache::rm($_single_cache_key);
  99. return true;
  100. }
  101. /**
  102. * 删除单条数据
  103. *
  104. * @param int $id ID
  105. * @param bool $is_complete 是否完成删除
  106. *
  107. * @return bool
  108. */
  109. public function deleteData($id, $is_complete = true) {
  110. $_old_data = $this->getInfoById($id);
  111. $_rs = parent::deleteData($id, $is_complete);
  112. if (false == $_rs) {
  113. return false;
  114. }
  115. /* 缓存操作 */
  116. $_single_cache_key = $this->getSingleCacheKey($id);
  117. Cache::rm($_single_cache_key);
  118. /* 删除年份 缓存 */
  119. $_cache_key = $this->getCacheKeyByYear($_old_data['year']);
  120. Cache::rm($_cache_key);
  121. return $_rs;
  122. }
  123. /**
  124. * 根据年份获取id
  125. *
  126. * @param int $year 年份
  127. *
  128. * @return int
  129. */
  130. public function getIdByYear($year) {
  131. $_cache_key = $this->getCacheKeyByYear($year);
  132. $_id = Cache::get($_cache_key);
  133. if (!empty($_id)) {
  134. return $_id;
  135. }
  136. $_map = [
  137. 'year' => $year
  138. ];
  139. $_id = $this->where($_map)->value('id');
  140. if (empty($_id)) {
  141. return CommonConst::CONST_ZERO;
  142. }
  143. Cache::set($_cache_key, $_id);
  144. return $_id;
  145. }
  146. /**
  147. * 根据年份获取实名信息
  148. *
  149. * @param int $year 年份
  150. *
  151. * @return array
  152. */
  153. public function getInfoByYear($year) {
  154. $_id = $this->getIdByYear($year);
  155. return $this->getInfoById($_id);
  156. }
  157. /**
  158. * 获取列表
  159. *
  160. * @param array $param 查找参数
  161. * @param string $page 分页
  162. * @param string $order 排序
  163. *
  164. * @return array
  165. */
  166. public function getList($param, $page = '1,10', $order = '-year') {
  167. $_map = [];
  168. if (!empty($param['year'])) {
  169. $_map['year'] = $param['year'];
  170. }
  171. $_count = $this->where($_map)->count('id');
  172. if (empty($_count)) {
  173. return [
  174. 'count' => CommonConst::CONST_ZERO,
  175. 'list' => [],
  176. ];
  177. }
  178. $_order = $this->orderFilter($order);
  179. $_list = $this->where($_map)->order($_order)->page($page)->select();
  180. if (is_object($_list)) {
  181. $_list = $_list->toArray();
  182. }
  183. foreach ($_list as $_k => $_v) {
  184. $_list[$_k]['holiday'] = array_to_str($_v['holiday']);
  185. $_list[$_k]['workday'] = array_to_str($_v['workday']);
  186. }
  187. return [
  188. 'count' => $_count,
  189. 'list' => $_list,
  190. ];
  191. }
  192. }