| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- // +----------------------------------------------------------------------
- // | Laravel框架 [ Laravel ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 Laravel研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: wesmiler <12345678@qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services;
- use App\Models\MasterModel;
- use App\Models\MemberModel;
- use App\Models\SignCatesModel;
- use App\Models\SignsModel;
- use App\Models\SiyuanModel;
- use App\Models\TradeModel;
- use App\Models\YigongModel;
- /**
- * 打卡签到管理-服务类
- * @author wesmiler
- * @since 2020/11/11
- * Class SignsService
- * @package App\Services
- */
- class SignsService extends BaseService
- {
- /**
- * 构造函数
- * @author wesmiler
- * @since 2020/11/11
- * SignsService constructor.
- */
- public function __construct()
- {
- $this->model = new SignsModel();
- }
- /**
- * 获取列表
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function getList()
- {
- $params = request()->all();
- $page = isset($params['pageSize']) ? intval($params['pageSize']) : PAGE;
- $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : PERPAGE;
- $dataList = $this->model::from('signs as s')
- ->leftJoin('sign_cates as sc', 'sc.id', '=', 's.cate_id')
- ->leftJoin('member as m', 'm.id', '=', 's.user_id')
- ->where(function ($query) use ($params) {
- $query->where('s.mark', 1);
- $type = isset($params['type'])? $params['type'] : 0;
- if($type>0){
- $query->where('s.type', $type);
- }
- $status = isset($params['status']) ? $params['status'] : 0;
- if ($status > 0) {
- $query->where('s.status', $status);
- } else {
- $query->whereIn('s.status', [1, 2]);
- }
- })
- ->where(function ($query) use ($params) {
- $keyword = isset($params['keyword']) ? trim($params['keyword']) : '';
- if (!empty($keyword)) {
- $query->where('m.nickname','like',"%{$keyword}%");
- }
- })
- ->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'])
- ->orderBy('s.sign_at', 'desc')
- ->orderBy('s.cate_id')
- ->paginate($pageSize);
- $dataList = $dataList ? $dataList->toArray() : [];
- if ($dataList) {
- foreach ($dataList['data'] as &$item) {
- $item['create_time'] = $item['create_time'] ? datetime($item['create_time'],'Y-m-d H:i:s') : '';
- }
- unset($item);
- }
- return [
- 'code' => 0,
- 'success'=> true,
- 'msg' => '操作成功',
- 'count' => isset($dataList['total']) ? $dataList['total'] : 0,
- 'data' => isset($dataList['data']) ? $dataList['data'] : 0,
- ];
- }
- /**
- * 添加或编辑
- * @return array
- * @since 2020/11/11
- * @author wesmiler
- */
- public function edit()
- {
- $data = request()->all();
- $data['update_time'] = time();
- return parent::edit($data); // TODO: Change the autogenerated stub
- }
- /**
- * 打卡
- * @param $userId
- * @return array
- */
- public function submit($userId){
- $params = request()->all();
- $type = isset($params['type'])? $params['type'] : 0;
- $cateId = isset($params['cate_id'])? $params['cate_id'] : 0;
- $siyuanId = isset($params['siyuan_id'])? $params['siyuan_id'] : 0;
- if(!in_array($type, [1,2])){
- return message('打卡类型参数错误', false);
- }
- $cateInfo = SignCatesModel::where(['id'=> $cateId,'mark'=> 1,'status'=> 1])
- ->select(['id','name'])
- ->first();
- if($cateId <=0 || !$cateInfo){
- return message('打卡分类参数错误或不存在', false);
- }
- $siyuanInfo = SiyuanModel::where(['id'=> $siyuanId,'mark'=> 1,'status'=> 1])
- ->select(['id','title','status'])
- ->first();
- if(!$siyuanInfo){
- return message('打卡寺院不存在或不可操作', false);
- }
- // 验证用户是否可操作
- $memberInfo = MemberModel::where(['id' => $userId, 'mark' => 1, 'status' => 1])
- ->select('id', 'openid', 'nickname','coupon','salary')
- ->first();
- if (!$memberInfo) {
- return message('您的账号已被冻结或不可操作,请联系客服', false);
- }
- if($type == 1){
- $info = MasterModel::where(['user_id'=> $userId,'mark'=> 1,'status'=> 1])
- ->select('id', 'realname','status')
- ->first();
- if(!$info){
- return message('您还不是僧人或法师,请先到平台申请入驻', false);
- }
- }else if($type == 2){
- $info = YigongModel::where(['user_id'=> $userId,'mark'=> 1,'status'=> 2])
- ->select('id', 'realname','status')
- ->first();
- if(!$info){
- return message('您还不是义工,请先到平台申请入驻', false);
- }
- }
- if(time() < strtotime(date('Y-m-d 08:00')) || time() > strtotime(date('Y-m-d 20:00'))){
- return message('每天8:00~20:00时间段才可打卡,请联系客服', false);
- }
- // 验证今天是否签到过
- $checkInfo = [];
- $where = ['type'=> $type,'user_id'=> $userId,'mark'=> 1,'status'=> 1];
- if(in_array($cateId, [1,2])){
- $checkInfo = SignsModel::where($where)->whereIn('cate_id',[1,2])
- ->where('sign_at','>=', date('Y-m-d'))
- ->select(['id','sign_at','cate_id','siyuan_id'])
- ->orderBy('sign_at','desc')
- ->first();
- if($checkInfo && $checkInfo->cate_id == $cateId){
- return message('您最近已经打过'.$cateInfo->name.'卡,请先联系客服', false);
- }
- if($cateId == 2){
- if(!$checkInfo){
- return message('请先完成上班打卡,才能打下班卡', false);
- }
- $siyuanInfo = SiyuanModel::where(['id'=> $checkInfo->siyuan_id,'mark'=> 1,'status'=> 1])
- ->select(['id','title','status'])
- ->first();
- if($checkInfo->siyuan_id != $siyuanId){
- return message('打卡寺院与今日最近一次['.$siyuanInfo->title.']不相同,请联系客服', false);
- }
- }
- }else if(in_array($cateId, [3,4])){
- $checkInfo = SignsModel::where($where)->whereIn('cate_id',[3,4])
- ->where('sign_at','>=', date('Y-m-d'))
- ->select(['id','sign_at','cate_id','siyuan_id'])
- ->orderBy('sign_at','desc')
- ->first();
- if($checkInfo && $checkInfo->cate_id == $cateId){
- return message('您最近已经打过'.$cateInfo->name.'卡,请先联系客服', false);
- }
- if($cateId == 4){
- if(!$checkInfo){
- return message('请先完成上课打卡,才能打下课卡', false);
- }
- $siyuanInfo = SiyuanModel::where(['id'=> $checkInfo->siyuan_id,'mark'=> 1,'status'=> 1])
- ->select(['id','title','status'])
- ->first();
- if($checkInfo->siyuan_id != $siyuanId){
- return message('打卡寺院与今日最近一次['.$siyuanInfo->title.']不相同,请联系客服', false);
- }
- }
- }
- // 每天同类型打卡最多次数
- $signCount = ConfigService::make()->getConfigByCode('sign_count');
- $signCount = $signCount>0? $signCount : 1;
- $checkCount = SignsModel::where($where)->where('cate_id', $cateId)
- ->where('sign_at','>=', date('Y-m-d'))
- ->count('id');
- if($checkCount >= $signCount){
- return message(($type==1? '法师/僧人':'义工')."{$cateInfo->name}打卡每天最多{$signCount}次,请每天再来", false);
- }
- // 打卡处理
- $signCheck = ConfigService::make()->getConfigByCode('sign_check');
- $signCheck = $signCheck? $signCheck : 1;
- \DB::beginTransaction();
- $data = [
- 'user_id'=> $userId,
- 'cate_id'=> $cateId,
- 'type'=> $type,
- 'siyuan_id'=> $siyuanId,
- 'sign_at'=> date('Y-m-d H:i'),
- 'description'=> isset($params['description'])? trim($params['description']) : '',
- 'update_time'=> time(),
- 'create_time'=> time(),
- 'status'=> in_array($cateId, [1,2])? $signCheck : 1,
- ];
- if(!$sid = $this->model::insertGetId($data)){
- \DB::rollBack();
- return message('打卡失败', false);
- }
- // 义工上班结算
- $signTime = 0;
- $yigongSalary = 0;
- $isSettle = false;
- $signSettle = ConfigService::make()->getConfigByCode('sign_settle');
- $signSettle = $signSettle>0? $signSettle : false;
- if($type == 2 && $cateId == 2 && $signSettle==1){
- $yigongSalary = ConfigService::make()->getConfigByCode('yg_salary');
- $signStartTime = isset($checkInfo['sign_at']) && $checkInfo['sign_at']? strtotime($checkInfo['sign_at']) : 0;
- $signEndTIme = strtotime(date('Y-m-d H:i'));
- $signTime = moneyFormat(($signEndTIme - $signStartTime)/3600, 2);
- $signTime = $signTime>=intval($signTime)+0.5? intval($signTime)+0.5 : intval($signTime);
- $salary = moneyFormat($signTime * $yigongSalary, 2);
- if($salary>0){
- if(!MemberModel::where(['id'=> $userId,'mark'=> 1])->increment('salary',$salary)){
- \DB::rollBack();
- return message('打卡工资结算失败', false);
- }
- $data = [
- 'user_id'=> $userId,
- 'type'=> 7,
- 'coin_type'=> 2,
- 'pay_type'=> 1,
- 'money'=> $salary,
- 'change_type'=> 1,
- 'balance'=> $memberInfo->salary? $memberInfo->salary : 0,
- 'create_time'=> time(),
- 'remark'=> "打卡工资结算{$signTime}小时",
- 'status'=> 1
- ];
- if(!TradeModel::insertGetId($data)){
- \DB::rollBack();
- return message('打卡工资结算处理失败', false);
- }
- $isSettle = true;
- }
- }
- \DB::commit();
- if($type == 2 && $cateId == 2 && $isSettle) {
- $this->model::where(['id'=> $sid,'user_id'=> $userId])->update(['is_settle'=> 1,'remark'=>"自动结算{$signTime}小时工资成功,{$yigongSalary}/小时"]);
- }
- return message('打卡成功', true);
- }
- }
|