building-sign/app/controller/api/v1/Worker.php

731 lines
24 KiB
PHP
Raw Normal View History

2023-01-09 08:41:41 +00:00
<?php
namespace app\controller\api\v1;
use app\controller\api\Base;
use app\controller\manager\Clock;
use app\exception\RepositoryException;
use app\model\Account;
use app\model\AccountDimission;
2023-01-09 08:41:41 +00:00
use app\model\AccountRecord;
use app\model\AccountWorksite;
use app\model\CheckLog;
use app\model\ClockLog;
use app\model\OvertimeLog;
use app\model\PayLog;
use app\model\Worksite;
use app\repository\AccountRepository;
use app\service\File;
use app\service\Jwt;
use app\service\wx\WechatApplets;
use app\validate\User as UserValidate;
use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use Exception;
use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
2023-01-09 09:41:04 +00:00
use think\facade\Config;
2023-01-09 08:41:41 +00:00
use think\facade\Log;
use think\response\Json;
/**
* 工人相关
*
* Class Worker
* @package app\controller\api\v1
*/
class Worker extends Base
{
protected $noNeedLogin = [];
/**
* 注册工人资料
*/
public function register(): Json
{
if (!$this->request->isPost()) {
return $this->json(4000, '无效请求');
}
$accountId = $this->request->user['user_id'] ?? 0;
if (!$account = Account::findById($accountId)) {
return $this->json(6001, '请先登录');
}
if ($account['role'] != Account::COMMON_OFF) {
return $this->json(4003, '您无需提交资料');
}
if ($account['checking'] == Account::COMMON_ON) {
return $this->json(4002, '您已提交资料,等待审核中');
}
$post = input('post.');
$rules = [
'real_name|姓名' => 'require|max:50|min:2',
'mobile|手机号' => 'require',
'pay|工资' => 'require|number|min:0',
'emergency_contact|紧急联系人' => 'require',
'emergency_phone|紧急联系人电话' => 'require',
'bank_card_name|银行卡户名' => 'require',
'bank_card_number|银行卡号' => 'require',
'bank_name|开户行' => 'require|max:100',
'card_number|身份证' => 'require|max:20|min:15',
'position|岗位' => 'require|number',
'worksite_id|工地' => 'require|number',
'bank_card_img|银行卡拍照' => 'require',
'id_front|身份证正面' => 'require',
'id_back|身份证反面' => 'require',
'address_now|现住址' => 'require',
2023-01-09 08:41:41 +00:00
];
$message = [
'worksite_id.number' => '工地必选',
'worksite_id.position' => '岗位必选',
];
$validate = $this->validateByApi($post, $rules, $message);
if ($validate !== true) {
return $validate;
}
$fields = [
'real_name', 'mobile', 'pay', 'emergency_contact', 'emergency_phone', 'bank_card_name', 'bank_card_number',
'bank_name', 'card_number', 'position', 'worksite_id', 'certificate', 'address_now', 'province', 'city', 'area',
'id_front', 'id_back', 'bank_card_img', 'work_experience'
2023-01-09 08:41:41 +00:00
];
$post = array_filter($post, function ($item, $key) use ($fields) {
return in_array($key, $fields);
}, ARRAY_FILTER_USE_BOTH);
$post['account_id'] = $accountId;
$post['is_register'] = CheckLog::COMMON_ON;
$post['created_at'] = date('Y-m-d H:i:s');
try {
CheckLog::create($post);
$account->save(['checking' => Account::COMMON_ON]);
} catch (Exception $e) {
return $this->json(5000, '资料录入失败');
}
return $this->json();
}
/**
* 修改用户信息 字段区分1.负责人审核 2.不需要审核 如nickname
*/
public function updateInfo(): Json
{
try {
$params = input('post.');
$rules = [
'field|修改项' => 'require',
'value|修改内容' => 'require',
];
$accountId = $this->request->user['user_id'] ?? 0;
$validate = $this->validateByApi($params, $rules);
if ($validate !== true) {
return $validate;
}
if (!$customer = Account::findById($accountId)) {
return $this->json(4004, '用户不存在');
}
$needCheckFields = Account::needCheckFields();
// 需要审核的字段
if (in_array($params['field'], $needCheckFields)) {
// 字段值未改变
if ($params['value'] == ($customer[$params['field']] ?? '')) {
return $this->json();
}
$insert = [];
$update = [];
foreach ($needCheckFields as $field) {
$insert[$field] = $field == $params['field'] ? $params['value'] : $customer[$field];
if ($field == $params['field']) {
$update[$field] = $params['value'];
}
}
if (!$checkLog = CheckLog::where('account_id', $accountId)->where('status', CheckLog::COMMON_OFF)
->find()) {
// 没有待审核的记录则新增
$insert['created_at'] = date('Y-m-d H:i:s');
$insert['account_id'] = $accountId;
$insert['worksite_id'] = $customer['worksite_id'];
CheckLog::create($insert);
} else {
$checkLog->save($update);
}
$customer->save(['checking' => Account::COMMON_ON]);
} else {
if ($params['field'] == 'address') {
//省市区单独处理 使用-分割 如 四川-成都-成华区
$arr = explode('-', $params['value']);
$customer->save([
'province' => $arr[0] ?? '',
'city' => $arr[1] ?? '',
'area' => $arr[2] ?? '',
]);
} else {
$customer->save([
$params['field'] => $params['value']
]);
}
2023-01-09 08:41:41 +00:00
}
} catch (Exception $e) {
Log::error('资料修改失败'.$e->getMessage());
2023-01-09 08:41:41 +00:00
return $this->json(5000, '修改资料失败!'.$e->getMessage());
}
return $this->json();
}
// 我的打卡
public function clockList(): Json
{
$page = input('page/d', 1);
$size = input('size/d', 20);
$keyword = input('keyword/s');
$worksiteId = input('worksite_id/d', 0);
$begin = input('begin_at/s', '');
$end = input('end_at/s', '');
$accountId = $this->request->user['user_id'] ?? 0;
$where = [];
if (!empty($keyword)) {
$where[] = ['w.name', 'like', '%'.$keyword.'%'];
}
if ($worksiteId > 0) {
$where[] = ['cl.worksite_id', '=', $worksiteId];
}
if (!empty($begin)) {
$where[] = ['cl.created_at', '>', $begin.' 00:00:00'];
}
if (!empty($end)) {
$where[] = ['cl.created_at', '<', $end.' 23:59:59'];
}
$where[] = ['cl.account_id', '=', $accountId];
$query = \app\model\ClockLog::alias('cl')
->leftJoin('worksite w', 'w.id = cl.worksite_id')
->field('cl.*,w.name as worksite_name')
->where($where);
$total = $query->count();
$res = [
'total' => $total,
'current' => $page ?: 1,
'size' => $size ?: 20,
'list' => new Collection(),
];
if ($total > 0) {
$res['list'] = $query->page($page, $size)->order('cl.id', 'desc')->select();
$res['list']->each(function ($item) {
$item->type_text = $item->type == 'in' ? '上班' : '下班';
switch ($item->status) {
case 0:
$item->status_text = '待确认';
break;
case 1:
$item->status_text = '已确认';
break;
case -1:
$item->status_text = '不通过';
break;
}
$item->time = date('H:i:s', $item->create_time);
});
}
return $this->json(0, 'success', $res);
}
/**
* 提交加班
*/
public function overtime(): Json
{
try {
$input = input('post.');
$rules = [
'day|加班日期' => 'require|date',
'time|加班时长' => 'require|float',
'worksite_id|工地' => 'require|number',
];
$validate = $this->validateByApi($input, $rules, ['worksite_id.number' => '工地必传']);
if ($validate !== true) {
return $validate;
}
$accountId = $this->request->user['user_id'] ?? 0;
if (!$customer = Account::findById($accountId)) {
return $this->json(6001, '请先登录');
}
if ($customer['role'] != Account::ROLE_WORKER) {
return $this->json(4003, '还不是工人');
}
$time = time();
$now = date('Y-m-d H:i:s', $time);
$day = date('Ymd', strtotime($input['day']));
OvertimeLog::create([
'account_id' => $accountId,
'worksite_id' => $input['worksite_id'],
'day_text' => $input['day'],
'day' => $day,
'time' => $input['time'],
'remarks' => $input['remarks'] ?? '',
'indexs' => $accountId.'-'.$input['worksite_id'].'-'.$day,
'created_at' => $now,
'create_time' => $time,
]);
// 创建当日工资初始记录
PayLog::createWhenNotExists($accountId, $input['worksite_id'], $day);
} catch (Exception $e) {
Log::error('工人加班提交失败'.$e->getMessage());
return $this->json(5000, '加班申请失败!');
}
return $this->json();
}
// 我的加班记录
public function overtimeList(): Json
{
$page = input('page/d', 1);
$size = input('size/d', 20);
$keyword = input('keyword/s');
$worksiteId = input('worksite_id/d', 0);
$begin = input('begin_at/s', '');
$end = input('end_at/s', '');
$accountId = $this->request->user['user_id'] ?? 0;
$where = [];
if (!empty($keyword)) {
$where[] = ['w.name', 'like', '%'.$keyword.'%'];
}
if ($worksiteId > 0) {
$where[] = ['ol.worksite_id', '=', $worksiteId];
}
if (!empty($begin)) {
$where[] = ['ol.created_at', '>', $begin.' 00:00:00'];
}
if (!empty($end)) {
$where[] = ['ol.created_at', '<', $end.' 23:59:59'];
}
$where[] = ['ol.account_id', '=', $accountId];
$query = \app\model\OvertimeLog::alias('ol')
->leftJoin('worksite w', 'w.id = ol.worksite_id')
->field('ol.*,w.name as worksite_name')
->where($where);
$total = $query->count();
$res = [
'total' => $total,
'current' => $page ?: 1,
'size' => $size ?: 20,
'list' => new Collection(),
];
if ($total > 0) {
$res['list'] = $query->page($page, $size)->order('ol.id', 'desc')->select();
$res['list']->each(function ($item) {
switch ($item->status) {
case 0:
$item->status_text = '待确认';
break;
case 1:
$item->status_text = '已确认';
break;
case -1:
$item->status_text = '不通过';
break;
}
$item->create_time = date('Y年m月d日 H:i:s', $item->create_time);
$item->worktime = date('Y年m月d日', strtotime($item->day));
unset($item->check_at);
unset($item->check_by);
unset($item->created_at);
unset($item->worksite_id);
unset($item->is_statistic);
unset($item->day);
unset($item->day_text);
});
}
return $this->json(0, 'success', $res);
}
// 加班申请被打回后再次编辑
public function overtimeEdit(): Json
{
$id = input('id');
$input = input('post.');
$rules = [
'worksite_id|工地' => 'require|number',
'day|加班日期' => 'require|date',
'time|加班时长' => 'require|float',
];
$validate = $this->validateByApi($input, $rules);
if ($validate !== true) {
return $validate;
}
$accountId = $this->request->user['user_id'] ?? 0;
if (!$item = OvertimeLog::where('id', $id)->find()) {
return $this->json(4004, '记录不存在');
}
if ($item['account_id'] != $accountId) {
return $this->json(4003, '不是你提交的加班申请');
}
if ($item['status'] != OvertimeLog::STATUS_NO) {
return $this->json(4003, '当前状态不能编辑');
}
$item->save([
'day' => date('Ymd', strtotime($input['day'])),
'worksite_id' => $input['worksite_id'],
'time' => $input['time'],
'remarks' => $input['remarks'] ?? '',
'status' => OvertimeLog::COMMON_OFF,
]);
return $this->json(0, 'success');
}
// 加班申请被打回后删除
public function overtimeDel(): Json
{
$id = input('id');
$accountId = $this->request->user['user_id'] ?? 0;
if (!$item = OvertimeLog::where('id', $id)->find()) {
return $this->json(4004, '记录不存在');
}
if ($item['account_id'] != $accountId) {
return $this->json(4003, '不是你提交的加班申请');
}
if ($item['status'] != OvertimeLog::STATUS_NO) {
return $this->json(4003, '当前状态不能删除');
}
$item->delete();
return $this->json(0, 'success');
}
// 我的工资记录
public function payListMock(): Json
{
$page = input('page/d', 1);
$size = input('size/d', 20);
$accountId = $this->request->user['user_id'] ?? 0;
$where = [];
if (!empty($keyword)) {
$where[] = ['w.name', 'like', '%'.$keyword.'%'];
}
$where[] = ['cl.account_id', '=', $accountId];
// $query = \app\model\ClockLog::alias('cl')
// ->leftJoin('worksite w', 'w.id = cl.worksite_id')
// ->field('cl.*,w.name as worksite_name')
// ->where($where);
// $total = $query->count();
$total = 10;
$res = [
'total' => $total,
'current' => $page ?: 1,
'size' => $size ?: 20,
'list' => new Collection(),
];
if ($total > 0) {
$res['list'] = [
[
'status' => 0,
'status_text' => '待发放',
'date' => '2022年11月',
'base_amount' => 7500,
'overtime_amount' => 500,
'amount' => 8000,
],
[
'status' => 1,
'status_text' => '已发放',
'date' => '2022年10月',
'base_amount' => 7500,
'overtime_amount' => 500,
'amount' => 8000,
],
[
'status' => 0,
'status_text' => '已发放',
'date' => '2022年09月',
'base_amount' => 7500,
'overtime_amount' => 500,
'amount' => 8000,
],
[
'status' => 0,
'status_text' => '已发放',
'date' => '2022年08月',
'base_amount' => 7500,
'overtime_amount' => 0,
'amount' => 7500,
],
[
'status' => 0,
'status_text' => '已发放',
'date' => '2022年08月',
'base_amount' => 7500,
'overtime_amount' => 0,
'amount' => 7500,
],
[
'status' => 0,
'status_text' => '已发放',
'date' => '2022年07月',
'base_amount' => 7500,
'overtime_amount' => 0,
'amount' => 7500,
],
[
'status' => 0,
'status_text' => '已发放',
'date' => '2022年06月',
'base_amount' => 7500,
'overtime_amount' => 0,
'amount' => 7500,
],
];
}
return $this->json(0, 'success', $res);
}
// 我的工资记录
public function payList(): Json
{
$page = input('page/d', 1);
$size = input('size/d', 20);
$accountId = $this->request->user['user_id'] ?? 0;
$where = [];
$where[] = ['pml.account_id', '=', $accountId];
$query = \app\model\PayMonthLog::alias('pml')
->leftJoin('account a', 'a.id = pml.account_id')
->where($where)
->group('pml.time')
->fieldRaw('pml.time,pml.year,pml.month,sum(pml.amount) as amount,sum(pml.base_amount) as base_amount,sum(pml.overtime_amount) as overtime_amount');
$total = $query->count();
$res = [
'total' => $total,
'current' => $page ?: 1,
'size' => $size ?: 20,
'list' => new Collection(),
];
if ($total > 0) {
// 获取按月已发工资
$paidMonth = \app\model\PayMonthLog::alias('pml')
->leftJoin('account a', 'a.id = pml.account_id')
->where($where)
->where('pml.status', 1)
->group('pml.time')
->page($page, $size)
->order('time', 'desc')
->fieldRaw('sum(pml.amount) as amount,pml.time')
->select()->toArray();
$monthPay = [];
foreach ($paidMonth as $p) {
$monthPay[$p['time']] = $p['amount'];
}
$res['list'] = $query->page($page, $size)->order('time', 'desc')->select();
$res['list']->each(function ($item) use ($monthPay) {
$item->date = $item['year'].'年'.$item['month'].'月';
$item->done = $monthPay[$item->time] ?? 0;
$item->status = 0;
$item->status_text = '待发放';
if ($item->amount <= $item->done) {
$item->status = 1;
$item->status_text = '已发放';
}
unset($item->year);
unset($item->month);
unset($item->think_count);
});
}
return $this->json(0, 'success', $res);
}
// 获取审核记录
public function checkDetail(): Json
{
$id = input('id');
if (!$item = CheckLog::where('id', $id)->find()) {
return $this->json(4004, '记录不存在');
}
$item = arrayNullToString($item->toArray());
return $this->json(0, 'success', $item);
}
/**
* 申请离职
*/
public function dimission(): Json
{
try {
$accountId = $this->request->user['user_id'] ?? 0;
if (!$customer = Account::findById($accountId)) {
return $this->json(6001, '请先登录');
}
if ($customer['role'] != Account::ROLE_WORKER) {
return $this->json(4003, '不是工人');
}
if (AccountDimission::where('account_id', $accountId)->where('worksite_id', $customer['worksite_id'])->where('status', 0)->count() > 0) {
return $this->json(4001, '审核中请勿重复提交');
}
$time = time();
$now = date('Y-m-d H:i:s', $time);
AccountDimission::create([
'account_id' => $accountId,
'worksite_id' => $customer['worksite_id'],
'created_at' => $now,
]);
} catch (Exception $e) {
Log::error('申请离职提交失败'.$e->getMessage());
return $this->json(5000, '申请离职提交失败!');
}
return $this->json();
}
/**
* 提交补卡
*/
public function replenish(): Json
{
try {
$input = input('post.');
$rules = [
'day|补卡日期' => 'require|date',
'type|补卡类型' => 'require|in:morning_on,morning_off,afternoon_on,afternoon_off',
'worksite_id|工地' => 'require|number',
];
$validate = $this->validateByApi($input, $rules, ['worksite_id.number' => '工地必传', 'type.in' => '补卡类型错误']);
if ($validate !== true) {
return $validate;
}
$accountId = $this->request->user['user_id'] ?? 0;
if (!$customer = Account::findById($accountId)) {
return $this->json(6001, '请先登录');
}
if ($customer['role'] != Account::ROLE_WORKER) {
return $this->json(4003, '还不是工人');
}
$time = time();
$now = date('Y-m-d H:i:s', $time);
$day = date('Ymd', strtotime($input['day']));
2023-01-12 03:43:33 +00:00
$where = [
'account_id' => $accountId,
'type' => $input['type'],
'worksite_id' => $input['worksite_id'],
'day' => $day,
'role' => $customer['role'],
'indexs' => $accountId.'-'.$input['worksite_id'].'-'.$day,
];
if (ClockLog::where($where)->whereIn('status', [0, 1])->count() > 0) {
return $this->json(4001, '记录存在,无需补卡!');
}
$data = [
'account_id' => $accountId,
'type' => $input['type'],
'worksite_id' => $input['worksite_id'],
'created_at' => $now,
'create_time' => $time,
'day' => $day,
'role' => $customer['role'],
'is_replenish' => ClockLog::COMMON_ON,
'indexs' => $accountId.'-'.$input['worksite_id'].'-'.$day,
];
ClockLog::create($data);
// 创建当日工资初始记录
PayLog::createWhenNotExists($accountId, $input['worksite_id'], $day);
} catch (Exception $e) {
Log::error('工人补卡提交失败'.$e->getMessage());
return $this->json(5000, '补卡申请失败!');
}
return $this->json();
}
2023-01-09 08:41:41 +00:00
}