SignLogic.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop100%开源免费商用商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | 商业版本务必购买商业授权,以免引起法律纠纷
  8. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  9. // | gitee下载:https://gitee.com/likeshop_gitee
  10. // | github下载:https://github.com/likeshop-github
  11. // | 访问官网:https://www.likeshop.cn
  12. // | 访问社区:https://home.likeshop.cn
  13. // | 访问手册:http://doc.likeshop.cn
  14. // | 微信公众号:likeshop技术社区
  15. // | likeshop团队 版权所有 拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshopTeam
  18. // +----------------------------------------------------------------------
  19. namespace app\adminapi\logic\sign;
  20. use app\common\enum\AccountLogEnum;
  21. use app\common\enum\SignEnum;
  22. use app\common\enum\YesNoEnum;
  23. use app\common\logic\BaseLogic;
  24. use app\common\model\AccountLog;
  25. use app\common\model\SignDaily;
  26. use app\common\model\SignLog;
  27. use app\common\service\ConfigService;
  28. use app\common\service\FileService;
  29. use think\facade\Config;
  30. use think\facade\Db;
  31. /**
  32. * 签到逻辑层
  33. * Class SignLogic
  34. * @package app\adminapi\logic
  35. */
  36. class SignLogic extends BaseLogic
  37. {
  38. /**
  39. * @notes 获取签到规则
  40. * @return array
  41. * @throws \think\db\exception\DataNotFoundException
  42. * @throws \think\db\exception\DbException
  43. * @throws \think\db\exception\ModelNotFoundException
  44. * @author Tab
  45. * @date 2021/8/16 9:38
  46. */
  47. public static function getConfig()
  48. {
  49. $daily = SignDaily::field('integral_status,integral')
  50. ->where('type', SignEnum::DAILY)
  51. ->findOrEmpty()
  52. ->toArray();
  53. if(empty($daily)) {
  54. $daily = [
  55. 'integral_status' => YesNoEnum::NO,
  56. 'integral' => 0
  57. ];
  58. }
  59. $continuous = SignDaily::field('id,integral_status,integral,days,sort')
  60. ->where('type', SignEnum::CONTINUOUS)
  61. ->order(['sort'=>'desc','days'=>'asc'])
  62. ->select()
  63. ->toArray();
  64. $config = [
  65. 'is_open' => ConfigService::get('sign', 'is_open', YesNoEnum::YES),
  66. 'daily' => $daily,
  67. 'continuous' => $continuous,
  68. 'remark' => ConfigService::get('sign', 'remark')
  69. ];
  70. return $config;
  71. }
  72. /**
  73. * @notes 设置签到规则
  74. * @param $params
  75. * @return bool
  76. * @author Tab
  77. * @date 2021/8/16 10:29
  78. */
  79. public static function setConfig($params)
  80. {
  81. Db::startTrans();
  82. try {
  83. // 更新状态
  84. ConfigService::set('sign', 'is_open', $params['is_open']);
  85. //删除旧的签到规则
  86. SignDaily::where('id','>',0)->delete();
  87. //添加每日签到规则
  88. SignDaily::create(['type'=>SignEnum::DAILY,'integral'=>$params['daily']['integral'],'integral_status'=>$params['daily']['integral_status'],'days'=>1]);
  89. //添加连续签到规则
  90. foreach ($params['continuous'] as $val) {
  91. SignDaily::create(['type'=>SignEnum::CONTINUOUS,'integral'=>$val['integral'],'integral_status'=>1,'days'=>$val['days'],'sort'=>$val['sort'] ?? 0]);
  92. }
  93. // 更新签到说明
  94. ConfigService::set('sign', 'remark', $params['remark']);
  95. Db::commit();
  96. return true;
  97. } catch (\Exception $e) {
  98. Db::rollback();
  99. self::setError($e->getMessage());
  100. return false;
  101. }
  102. }
  103. /**
  104. * @notes 添加连续签到规则
  105. * @param $params
  106. * @return bool
  107. * @author Tab
  108. * @date 2021/8/16 15:34
  109. */
  110. public static function add($params)
  111. {
  112. try {
  113. SignDaily::create($params);
  114. return true;
  115. } catch(\Exception $e) {
  116. self::setError($e->getMessage());
  117. return false;
  118. }
  119. }
  120. /**
  121. * @notes 编辑连续签到规则
  122. * @param $params
  123. * @return bool
  124. * @author Tab
  125. * @date 2021/8/16 15:43
  126. */
  127. public static function edit($params)
  128. {
  129. try {
  130. SignDaily::update($params);
  131. return true;
  132. } catch(\Exception $e) {
  133. self::setError($e->getMessage());
  134. return false;
  135. }
  136. }
  137. /**
  138. * @notes 删除连续签到规则
  139. * @param $params
  140. * @return bool
  141. * @author Tab
  142. * @date 2021/8/16 15:50
  143. */
  144. public static function delete($params)
  145. {
  146. try {
  147. SignDaily::destroy($params['id']);
  148. return true;
  149. } catch(\Exception $e) {
  150. self::setError($e->getMessage());
  151. return false;
  152. }
  153. }
  154. /**
  155. * @notes 查看连续签到规则详情
  156. * @param $params
  157. * @return array
  158. * @author Tab
  159. * @date 2021/8/16 11:42
  160. */
  161. public static function detail($params)
  162. {
  163. $signDialy = SignDaily::field('integral, integral_status, days, id')
  164. ->where('id', $params['id'])
  165. ->findOrEmpty()
  166. ->toArray();
  167. return $signDialy;
  168. }
  169. /**
  170. * @notes 重置说明
  171. * @author Tab
  172. * @date 2021/8/16 19:26
  173. */
  174. public static function resetRemark()
  175. {
  176. $default = Config::get('project.sign.remark');
  177. ConfigService::set('sign', 'remark', $default);
  178. }
  179. /**
  180. * @notes 签到数据中心
  181. * @return array
  182. * @author Tab
  183. * @date 2021/8/16 19:56
  184. */
  185. public static function dataCenter()
  186. {
  187. return [
  188. 'sign_data' => self::signData(),
  189. 'recent_data' => self::recentData(),
  190. 'top_data' => self::topData()
  191. ];
  192. }
  193. /**
  194. * @notes 签到数据
  195. * @return array
  196. * @author Tab
  197. * @date 2021/8/16 19:59
  198. */
  199. public static function signData()
  200. {
  201. $totalSign = SignLog::count();
  202. $totalIntegral = AccountLog::where('change_type', AccountLogEnum::INTEGRAL_INC_SIGN)->sum('change_amount');
  203. return [
  204. 'total_sign' => $totalSign,
  205. 'total_integral' => $totalIntegral
  206. ];
  207. }
  208. /**
  209. * @notes 近30天签到数据
  210. * @return array
  211. * @author Tab
  212. * @date 2021/8/16 20:50
  213. */
  214. public static function recentData()
  215. {
  216. $field = [
  217. "FROM_UNIXTIME(create_time,'%Y%m%d') as date",
  218. "count(id) as num"
  219. ];
  220. $lists = SignLog::field($field)
  221. ->group('date')
  222. ->whereMonth('create_time')
  223. ->select()
  224. ->toArray();
  225. $lists = array_column($lists, 'num', 'date');
  226. $data = [];
  227. for($i = 0; $i < 30; $i ++) {
  228. $today = new \DateTime();
  229. $targetDay = $today->add(\DateInterval::createFromDateString('-'. $i . 'day'));
  230. $targetDay = $targetDay->format('Ymd');
  231. $item['date'] = $targetDay;
  232. $item['num'] = isset($lists[$targetDay]) ? $lists[$targetDay]: 0;
  233. $data[] = $item;
  234. }
  235. return $data;
  236. }
  237. /**
  238. * @notes 签到排行榜
  239. * @return array
  240. * @author Tab
  241. * @date 2021/8/16 21:07
  242. */
  243. public static function topData()
  244. {
  245. $fieldNum = 'count(sl.id) as num, u.nickname,u.avatar';
  246. $topNum = SignLog::alias('sl')
  247. ->leftJoin('user u', 'u.id = sl.user_id')
  248. ->field($fieldNum)
  249. ->group('u.id')
  250. ->order('num', 'desc')
  251. ->limit(10)
  252. ->select()
  253. ->toArray();
  254. foreach($topNum as &$item) {
  255. $item['avatar'] = FileService::getFileUrl($item['avatar']);
  256. }
  257. $fieldAward = 'sum(al.change_amount) as amount, u.nickname,u.avatar';
  258. $topAward = AccountLog::alias('al')
  259. ->leftJoin('user u', 'u.id = al.user_id')
  260. ->field($fieldAward)
  261. ->where('change_type', AccountLogEnum::INTEGRAL_INC_SIGN)
  262. ->group('u.id')
  263. ->order('amount', 'desc')
  264. ->limit(10)
  265. ->select()
  266. ->toArray();
  267. foreach($topAward as &$item) {
  268. $item['avatar'] = FileService::getFileUrl($item['avatar']);
  269. $item['amount'] = (int)$item['amount'];
  270. }
  271. return [
  272. 'top_num' => $topNum,
  273. 'top_award' => $topAward
  274. ];
  275. }
  276. }