2023-01-09 16:41:41 +08: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 ;
2023-01-10 14:56:58 +08:00
use app\model\AccountDimission ;
2023-01-09 16:41:41 +08: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 17:41:04 +08:00
use think\facade\Config ;
2023-01-09 16:41:41 +08: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' ,
2023-02-09 09:51:04 +08:00
'worksite_id|工地' => 'require|number|gt:0' ,
2023-01-10 14:56:58 +08:00
'bank_card_img|银行卡拍照' => 'require' ,
'id_front|身份证正面' => 'require' ,
'id_back|身份证反面' => 'require' ,
2023-02-09 11:08:13 +08:00
'outsource_id|班组' => 'require' ,
2023-01-10 14:56:58 +08:00
'address_now|现住址' => 'require' ,
2023-01-09 16:41:41 +08:00
];
$message = [
'worksite_id.number' => '工地必选' ,
2023-02-09 09:51:04 +08:00
'worksite_id.gt' => '工地不存在' ,
2023-01-09 16:41:41 +08:00
'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' ,
2023-01-10 14:56:58 +08:00
'bank_name' , 'card_number' , 'position' , 'worksite_id' , 'certificate' , 'address_now' , 'province' , 'city' , 'area' ,
2023-02-09 11:08:13 +08:00
'id_front' , 'id_back' , 'bank_card_img' , 'work_experience' , 'outsource_id'
2023-01-09 16:41:41 +08: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' ];
}
}
2023-01-15 10:57:08 +08:00
if ( ! $checkLog = CheckLog :: where ( 'account_id' , $accountId ) -> where ( 'status' , CheckLog :: COMMON_OFF )
2023-01-09 16:41:41 +08:00
-> 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 {
2023-01-12 18:24:33 +08:00
if ( $params [ 'field' ] == 'address' ) {
//省市区单独处理 使用-分割 如 四川-成都-成华区
$arr = explode ( '-' , $params [ 'value' ]);
$customer -> save ([
'province' => $arr [ 0 ] ? ? '' ,
2023-01-14 14:24:14 +08:00
'city' => $arr [ 1 ] ? ? '' ,
'area' => $arr [ 2 ] ? ? '' ,
2023-01-12 18:24:33 +08:00
]);
} else {
$customer -> save ([
$params [ 'field' ] => $params [ 'value' ]
]);
}
2023-01-09 16:41:41 +08:00
}
} catch ( Exception $e ) {
2023-01-12 18:24:33 +08:00
Log :: error ( '资料修改失败' . $e -> getMessage ());
2023-01-09 16:41:41 +08: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' )
2023-01-14 14:24:14 +08:00
-> 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,sum(pml.paid_amount) as paid_amount' );
2023-01-09 16:41:41 +08:00
$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 ( 'time' , 'desc' ) -> select ();
2023-01-14 14:24:14 +08:00
$res [ 'list' ] -> each ( function ( $item ) {
$item -> date = $item [ 'year' ] . '年' . $item [ 'month' ] . '月' ;
// $item->done = $monthPay[$item->time] ?? 0;
2023-01-09 16:41:41 +08:00
$item -> status = 0 ;
$item -> status_text = '待发放' ;
2023-01-14 14:24:14 +08:00
if ( $item [ 'amount' ] == $item [ 'paid_amount' ] && $item [ 'amount' ] > 0 ) {
$item -> status_text = '完全发放' ;
2023-01-09 16:41:41 +08:00
$item -> status = 1 ;
2023-01-14 14:24:14 +08:00
}
if ( $item [ 'amount' ] > $item [ 'paid_amount' ] && $item [ 'paid_amount' ] > 0 ) {
$item -> status_text = '部分发放' ;
$item -> status = 2 ;
2023-01-09 16:41:41 +08:00
}
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 );
}
2023-01-10 14:56:58 +08:00
/**
* 申请离职
*/
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 ();
}
2023-01-09 16:41:41 +08:00
}