SignsService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Laravel框架 [ Laravel ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 Laravel研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: wesmiler <12345678@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\MasterModel;
  13. use App\Models\MemberModel;
  14. use App\Models\SignCatesModel;
  15. use App\Models\SignsModel;
  16. use App\Models\SiyuanModel;
  17. use App\Models\TradeModel;
  18. use App\Models\YigongModel;
  19. /**
  20. * 打卡签到管理-服务类
  21. * @author wesmiler
  22. * @since 2020/11/11
  23. * Class SignsService
  24. * @package App\Services
  25. */
  26. class SignsService extends BaseService
  27. {
  28. /**
  29. * 构造函数
  30. * @author wesmiler
  31. * @since 2020/11/11
  32. * SignsService constructor.
  33. */
  34. public function __construct()
  35. {
  36. $this->model = new SignsModel();
  37. }
  38. /**
  39. * 获取列表
  40. * @return array
  41. * @since 2020/11/11
  42. * @author wesmiler
  43. */
  44. public function getList()
  45. {
  46. $params = request()->all();
  47. $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
  48. $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
  49. $dataList = $this->model::from('signs as s')
  50. ->leftJoin('sign_cates as sc', 'sc.id', '=', 's.cate_id')
  51. ->leftJoin('member as m', 'm.id', '=', 's.user_id')
  52. ->where(function ($query) use ($params) {
  53. $query->where('s.mark', 1);
  54. $type = isset($params['type'])? $params['type'] : 0;
  55. if($type>0){
  56. $query->where('s.type', $type);
  57. }
  58. $status = isset($params['status']) ? $params['status'] : 0;
  59. if ($status > 0) {
  60. $query->where('s.status', $status);
  61. } else {
  62. $query->whereIn('s.status', [1, 2]);
  63. }
  64. })
  65. ->where(function ($query) use ($params) {
  66. $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
  67. if (!empty($keyword)) {
  68. $query->where('m.nickname','like',"%{$keyword}%");
  69. }
  70. })
  71. ->select(['s.id', 's.user_id','s.cate_id', 'sc.name as cate_name', 'm.nickname', 's.sign_at', 's.type', 's.status', 's.create_time', 's.update_time', 's.description'])
  72. ->orderBy('s.sign_at', 'desc')
  73. ->orderBy('s.cate_id')
  74. ->paginate($pageSize);
  75. $dataList = $dataList ? $dataList->toArray() : [];
  76. if ($dataList) {
  77. foreach ($dataList['data'] as &$item) {
  78. $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
  79. }
  80. unset($item);
  81. }
  82. return [
  83. 'code' => 0,
  84. 'success'=> true,
  85. 'msg' => '操作成功',
  86. 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
  87. 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
  88. ];
  89. }
  90. /**
  91. * 添加或编辑
  92. * @return array
  93. * @since 2020/11/11
  94. * @author wesmiler
  95. */
  96. public function edit()
  97. {
  98. $data = request()->all();
  99. $data['update_time'] = time();
  100. return parent::edit($data); // TODO: Change the autogenerated stub
  101. }
  102. /**
  103. * 打卡
  104. * @param $userId
  105. * @return array
  106. */
  107. public function submit($userId){
  108. $params = request()->all();
  109. $type = isset($params['type'])? $params['type'] : 0;
  110. $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
  111. $siyuanId = isset($params['siyuan_id'])? $params['siyuan_id'] : 0;
  112. if(!in_array($type, [1,2])){
  113. return message('打卡类型参数错误', false);
  114. }
  115. $cateInfo = SignCatesModel::where(['id'=> $cateId,'mark'=> 1,'status'=> 1])
  116. ->select(['id','name'])
  117. ->first();
  118. if($cateId <=0 || !$cateInfo){
  119. return message('打卡分类参数错误或不存在', false);
  120. }
  121. $siyuanInfo = SiyuanModel::where(['id'=> $siyuanId,'mark'=> 1,'status'=> 1])
  122. ->select(['id','title','status'])
  123. ->first();
  124. if(!$siyuanInfo){
  125. return message('打卡寺院不存在或不可操作', false);
  126. }
  127. // 验证用户是否可操作
  128. $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1])
  129. ->select('id', 'openid', 'nickname','coupon','salary')
  130. ->first();
  131. if (!$memberInfo) {
  132. return message('您的账号已被冻结或不可操作,请联系客服', false);
  133. }
  134. if($type == 1){
  135. $info = MasterModel::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])
  136. ->select('id', 'realname','status')
  137. ->first();
  138. if(!$info){
  139. return message('您还不是僧人或法师,请先到平台申请入驻', false);
  140. }
  141. }else if($type == 2){
  142. $info = YigongModel::where(['user_id'=> $userId,'mark'=> 1,'status'=> 2])
  143. ->select('id', 'realname','status')
  144. ->first();
  145. if(!$info){
  146. return message('您还不是义工,请先到平台申请入驻', false);
  147. }
  148. }
  149. if(time() < strtotime(date('Y-m-d 08:00')) || time() > strtotime(date('Y-m-d 20:00'))){
  150. return message('每天8:00~20:00时间段才可打卡,请联系客服', false);
  151. }
  152. // 验证今天是否签到过
  153. $checkInfo = [];
  154. $where = ['type'=> $type,'user_id'=> $userId,'mark'=> 1,'status'=> 1];
  155. if(in_array($cateId, [1,2])){
  156. $checkInfo = SignsModel::where($where)->whereIn('cate_id',[1,2])
  157. ->where('sign_at','>=', date('Y-m-d'))
  158. ->select(['id','sign_at','cate_id','siyuan_id'])
  159. ->orderBy('sign_at','desc')
  160. ->first();
  161. if($checkInfo && $checkInfo->cate_id == $cateId){
  162. return message('您最近已经打过'.$cateInfo->name.'卡,请先联系客服', false);
  163. }
  164. if($cateId == 2){
  165. if(!$checkInfo){
  166. return message('请先完成上班打卡,才能打下班卡', false);
  167. }
  168. $siyuanInfo = SiyuanModel::where(['id'=> $checkInfo->siyuan_id,'mark'=> 1,'status'=> 1])
  169. ->select(['id','title','status'])
  170. ->first();
  171. if($checkInfo->siyuan_id != $siyuanId){
  172. return message('打卡寺院与今日最近一次['.$siyuanInfo->title.']不相同,请联系客服', false);
  173. }
  174. }
  175. }else if(in_array($cateId, [3,4])){
  176. $checkInfo = SignsModel::where($where)->whereIn('cate_id',[3,4])
  177. ->where('sign_at','>=', date('Y-m-d'))
  178. ->select(['id','sign_at','cate_id','siyuan_id'])
  179. ->orderBy('sign_at','desc')
  180. ->first();
  181. if($checkInfo && $checkInfo->cate_id == $cateId){
  182. return message('您最近已经打过'.$cateInfo->name.'卡,请先联系客服', false);
  183. }
  184. if($cateId == 4){
  185. if(!$checkInfo){
  186. return message('请先完成上课打卡,才能打下课卡', false);
  187. }
  188. $siyuanInfo = SiyuanModel::where(['id'=> $checkInfo->siyuan_id,'mark'=> 1,'status'=> 1])
  189. ->select(['id','title','status'])
  190. ->first();
  191. if($checkInfo->siyuan_id != $siyuanId){
  192. return message('打卡寺院与今日最近一次['.$siyuanInfo->title.']不相同,请联系客服', false);
  193. }
  194. }
  195. }
  196. // 每天同类型打卡最多次数
  197. $signCount = ConfigService::make()->getConfigByCode('sign_count');
  198. $signCount = $signCount>0? $signCount : 1;
  199. $checkCount = SignsModel::where($where)->where('cate_id', $cateId)
  200. ->where('sign_at','>=', date('Y-m-d'))
  201. ->count('id');
  202. if($checkCount >= $signCount){
  203. return message(($type==1? '法师/僧人':'义工')."{$cateInfo->name}打卡每天最多{$signCount}次,请每天再来", false);
  204. }
  205. // 打卡处理
  206. $signCheck = ConfigService::make()->getConfigByCode('sign_check');
  207. $signCheck = $signCheck? $signCheck : 1;
  208. \DB::beginTransaction();
  209. $data = [
  210. 'user_id'=> $userId,
  211. 'cate_id'=> $cateId,
  212. 'type'=> $type,
  213. 'siyuan_id'=> $siyuanId,
  214. 'sign_at'=> date('Y-m-d H:i'),
  215. 'description'=> isset($params['description'])? trim($params['description']) : '',
  216. 'update_time'=> time(),
  217. 'create_time'=> time(),
  218. 'status'=> in_array($cateId, [1,2])? $signCheck : 1,
  219. ];
  220. if(!$sid = $this->model::insertGetId($data)){
  221. \DB::rollBack();
  222. return message('打卡失败', false);
  223. }
  224. // 义工上班结算
  225. $signTime = 0;
  226. $yigongSalary = 0;
  227. $isSettle = false;
  228. $signSettle = ConfigService::make()->getConfigByCode('sign_settle');
  229. $signSettle = $signSettle>0? $signSettle : false;
  230. if($type == 2 && $cateId == 2 && $signSettle==1){
  231. $yigongSalary = ConfigService::make()->getConfigByCode('yg_salary');
  232. $signStartTime = isset($checkInfo['sign_at']) && $checkInfo['sign_at']? strtotime($checkInfo['sign_at']) : 0;
  233. $signEndTIme = strtotime(date('Y-m-d H:i'));
  234. $signTime = moneyFormat(($signEndTIme - $signStartTime)/3600, 2);
  235. $signTime = $signTime>=intval($signTime)+0.5? intval($signTime)+0.5 : intval($signTime);
  236. $salary = moneyFormat($signTime * $yigongSalary, 2);
  237. if($salary>0){
  238. if(!MemberModel::where(['id'=> $userId,'mark'=> 1])->increment('salary',$salary)){
  239. \DB::rollBack();
  240. return message('打卡工资结算失败', false);
  241. }
  242. $data = [
  243. 'user_id'=> $userId,
  244. 'type'=> 7,
  245. 'coin_type'=> 2,
  246. 'pay_type'=> 1,
  247. 'money'=> $salary,
  248. 'change_type'=> 1,
  249. 'balance'=> $memberInfo->salary? $memberInfo->salary : 0,
  250. 'create_time'=> time(),
  251. 'remark'=> "打卡工资结算{$signTime}小时",
  252. 'status'=> 1
  253. ];
  254. if(!TradeModel::insertGetId($data)){
  255. \DB::rollBack();
  256. return message('打卡工资结算处理失败', false);
  257. }
  258. $isSettle = true;
  259. }
  260. }
  261. \DB::commit();
  262. if($type == 2 && $cateId == 2 && $isSettle) {
  263. $this->model::where(['id'=> $sid,'user_id'=> $userId])->update(['is_settle'=> 1,'remark'=>"自动结算{$signTime}小时工资成功,{$yigongSalary}/小时"]);
  264. }
  265. return message('打卡成功', true);
  266. }
  267. }