WokerTask/app/common/logic/CompanyLogic.php

456 lines
19 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | likeadmin快速开发前后端分离管理后台PHP版
// +----------------------------------------------------------------------
// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
// | 开源版本可自由商用可去除界面版权logo
// | gitee下载https://gitee.com/likeshop_gitee/likeadmin
// | github下载https://github.com/likeshop-github/likeadmin
// | 访问官网https://www.likeadmin.cn
// | likeadmin团队 版权所有 拥有最终解释权
// +----------------------------------------------------------------------
// | author: likeadminTeam
// +----------------------------------------------------------------------
namespace app\common\logic;
use app\adminapi\logic\auth\AdminLogic;
use app\common\model\auth\AdminRole;
use app\common\model\Company;
use app\common\logic\BaseLogic;
use app\common\model\auth\Admin;
use app\common\model\contract\Contract;
use app\common\model\user\User;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Db;
use app\adminapi\validate\CompanyValidate;
use app\api\controller\JunziqianController;
use app\api\logic\SmsLogic;
use app\common\model\task_scheduling\TaskScheduling;
use app\common\service\FileService;
use think\facade\Config;
/**
* 公司逻辑
* Class CompanyLogic
* @package app\adminapi\logic
*/
class CompanyLogic extends BaseLogic
{
/**
* @notes 添加
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/15 14:43
*/
public static function add(array $params): bool
{
unset($params['id']);
Db::startTrans();
try {
// $exist = Company::where('admin_id', $params['admin_id'])->find();
// if (!empty($exist)) {
// throw new ValidateException('该账号已经注册过企业');
// }
// 手机号不能重复
$companyInfo = Company::where(['master_phone'=>$params['master_phone']])->whereNull('delete_time')->find();
if (!empty($companyInfo)) {
throw new Exception('手机号已存在');
}
// 社会统一信用代码不能重复
$companyInfo = Company::where(['organization_code'=>$params['organization_code']])->whereNull('delete_time')->find();
if (!empty($companyInfo)) {
throw new Exception('社会代码已存在');
}
$arr=[
'company_name' => $params['company_name'],
'organization_code' => $params['organization_code'],
'province' => $params['province']>0?$params['province']:0,
'city' => $params['city']>0?$params['city']:0,
'area' => $params['area']>0?$params['area']:0,
'street' => $params['street']>0?$params['street']:0,
'village' => $params['village']>0?$params['village']:0,
'brigade' => $params['brigade']>0?$params['brigade']:0,
'address' => $params['address'],
'company_type' => $params['company_type'],
'master_name' => $params['master_name'],
'master_id_card' => $params['id_card'], // 主联系人证件号 todo DDL 新增字段
'master_position' => $params['master_position'],
'master_phone' => $params['master_phone'],
'master_email' => substr(md5(uniqid()),rand(0, 22),10)."@lihai.com", // 随机邮箱
'other_contacts' => $params['other_contacts'],
'area_manager' => $params['area_manager'] ?? 0,
'qualification' => $params['qualification'],
'responsible_area' => $params['responsible_area'],
'admin_id' => 0,
'is_contract' => 1, // todo 暂时默认为已签约
'is_authentication' => 1, // todo 暂时默认为已认证
];
$data = Company::create($arr);
$passwordSalt = Config::get('project.unique_identification');
$defaultAvatar = config('project.default_image.admin_avatar');
$admin=$arr;
$admin['id_card']=$params['id_card'];
$admin['area_manager']=$params['area_manager'] ?? 0;
$admin['sex']=$params['sex'];
$admin['name']=$params['master_name'];
$admin['account']=$params['master_phone'];
$admin['avatar'] = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
$admin['phone']=$params['master_phone'];
$admin['password'] = create_password(123456, $passwordSalt);
$admin_id=Admin::strict(false)->insertGetId($admin);
// 镇合伙人公司
if($params['company_type']==16){
AdminLogic::insertRole($admin_id, [3]);
$admin['group_id']=15;
}elseif($params['company_type']==17){
//村管理公司
AdminLogic::insertRole($admin_id, [4]);
$admin['group_id']=3;
}
elseif ($params['company_type']==18) {
//小组服务公司
AdminLogic::insertRole($admin_id, [6]);
$village = Db::name('geo_village')->where('village_code', $params['village'])->find();
// 判断字符串是否包含子串
if (mb_strpos($village['village_name'], '社区') !== false) {
$admin['group_id']=18; // 社区队长
} else {
$admin['group_id']=2; // 生产队队长
}
} elseif ($params['company_type']==41) {
//镇农科公司
AdminLogic::insertRole($admin_id, [7]); //后台角色
$admin['group_id']=4; // 前台角色
}
$userSn = User::createUserSn();
$admin['admin_id']=$admin_id;
$admin['sn']=$userSn;
$admin['sechannel']=4;
$admin['company_id']=$data['id'];
$admin['real_name']=$params['master_name'];
$admin['nickname']=$params['master_name'];
$admin['mobile']=$params['master_phone'];
$admin['phone']=$params['master_phone'];
$admin['create_time']=time();
$admin['update_time']=time();
$user_id = User::strict(false)->insertGetId($admin);
Company::where('id',$data['id'])->update(['admin_id'=>$admin_id,'user_id'=>$user_id]);
// 平台公司没有合同和发布业务
if ($params['company_type'] != 30){
$scheduling=[
'create_user_id'=>$params['area_manager'] ?? 0,
'company_id'=>$data['id'],
'company_type'=>$params['company_type'],
];
TaskScheduling::create($scheduling);
}
// 企业人脸校验上传
// self::originationFaceCreate($data);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage());
return false;
}
}
public static function originationFaceCreate($data)
{
// 人脸识别校验上传
$organizationFaceCreateData = [
'company_name' => $data['company_name'],
'organization_code' => $data['organization_code'],
'master_name' => $data['master_name'],
'master_id_card' => $data['master_id_card'],
'master_email' => substr(uniqid(),3,5).$data['master_email'],
'master_phone' => $data['master_phone'],
'id' => $data['id'],
];
// 公司数据合法性校验
$checkCompany = self::companyCheck($organizationFaceCreateData);
if ($checkCompany !== true){
return $checkCompany;
}
$res = app(JunziqianController::class)->organizationFaceCreate($organizationFaceCreateData);
if ($res->success == true && !empty($res->data)) {
// 保存人脸识别地址 todo 新增字段的DDL
Company::where('id',$data['id'])->update(['face_create_url'=>$res->data]);
// 发送短信给法人
$sms = [
'mobile' => $data['master_phone'],
'name' => $data['master_name'],
'code' => 'api/Hetong/toFaceCreate?id=' . $data['id'],
'scene' => 'FACE_CREATE',
'type' => ''
];
$smsRes = SmsLogic::contractUrl($sms);
if ($smsRes != true) {
return SmsLogic::getError();
}
} else {
return $res->msg;
}
return true;
}
private static function companyCheck($company) {
if (empty($company)) {
return self::setError("公司不存在");
}
if(empty($company['master_id_card'])) {
return self::setError("主联系人身份证为空,无法进行人脸识别");
}
if(empty($company['master_email'])) {
return self::setError("主联系人邮箱为空,无法进行人脸识别");
}
return true;
}
/**
* @notes 编辑
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/15 14:43
*/
public static function edit(array $params): bool
{
Db::startTrans();
try {
// 手机号不能重复
$companyInfo = Company::where(['master_phone'=>$params['master_phone']])->where('id','<>',$params['id'])->whereNull('delete_time')->find();
if (!empty($companyInfo)) {
Db::rollback();
throw new Exception('手机号已存在');
}
// 社会代码不能重复
$companyInfo = Company::where(['organization_code'=>$params['organization_code']])->where('id','<>',$params['id'])->whereNull('delete_time')->find();
if (!empty($companyInfo)) {
Db::rollback();
throw new Exception('社会代码已存在');
}
$arr=[
'company_name' => $params['company_name'],
'organization_code' => $params['organization_code'],
'province' => $params['province'],
'city' => $params['city'],
'area' => $params['area'],
'street' => $params['street'],
'village' => $params['village'],
'brigade' => $params['brigade'],
'address' => $params['address'],
'company_type' => $params['company_type'],
'master_name' => $params['master_name'],
'master_id_card' => $params['id_card'], // 主联系人证件号
'master_position' => $params['master_position'],
'master_phone' => $params['master_phone'],
'master_email' => $params['master_email'],
'other_contacts' => $params['other_contacts'],
'area_manager' => $params['area_manager'] ?? 0,
'qualification' => $params['qualification'],
'responsible_area' => $params['responsible_area'],
];
$defaultAvatar = config('project.default_image.admin_avatar');
$admin=$arr;
$account=$params['master_phone'];
$admin['id_card']=$params['id_card'];
$admin['area_manager']=$params['area_manager'];
$admin['sex']=$params['sex'];
$admin['name']=$params['master_name'];
$admin['account']=$params['master_phone'];
$admin['avatar'] = !empty($params['avatar']) ? FileService::setFileUrl($params['avatar']) : $defaultAvatar;
$admin['phone']=$params['master_phone'];
// 如果更改了公司类型,则更改公司后台账号和前台账号的角色,以及任务安排
$editCompany = Company::where(['id'=>$params['id']])->find();
if($editCompany['company_type'] != $params['company_type']) {
if($editCompany['company_type'] == 30){
// 平台公司 删除用户角色和任务安排 修改用户角色
Db::rollback();
throw new Exception('不可将已创建的平台公司变更为其他类型公司');
}
if($params['company_type'] == 30){
// 平台公司 删除用户角色和任务安排 修改用户角色
Db::rollback();
throw new Exception('不可将已创建的企业变更为平台公司');
}
if($editCompany['company_type']==16){
// 镇合伙人公司
$oldAdminRoleId = 3;
} elseif ($editCompany['company_type']==17){
//村管理公司
$oldAdminRoleId = 4;
} elseif ($editCompany['company_type']==18) {
//小组服务公司
$oldAdminRoleId = 6;
} elseif ($editCompany['company_type']==41) {
//镇农科公司 总负责人
$oldAdminRoleId = 7; //后台角色
}
if($params['company_type']==16){
$adminRoleId = 3;
$userGroupId = 15;
} elseif ($params['company_type']==17){
$adminRoleId = 4;
$userGroupId = 3;
} elseif ($params['company_type']==18) {
$adminRoleId = 6;
$userGroupId = 5;
} elseif ($params['company_type']==41) {
$adminRoleId = 7; //后台角色
$userGroupId = 4; // 前台角色
}
// 更新后台账户角色
$adminRoleModel = AdminRole::where(['admin_id'=>$params['admin_id'], 'role_id'=>$oldAdminRoleId])->findOrEmpty();
if (!empty($adminRoleModel)) {
$adminRoleModel->save(['role_id'=>$adminRoleId]);
}
// 更新前台账户角色
$userModel = User::where(['admin_id'=>$params['admin_id'], 'company_id'=>$editCompany['id']])->findOrEmpty();
if (!empty($userModel)) {
$userModel->save(['group_id'=>$userGroupId]);
}
// 更新任务安排公司类型
$taskScheduleModel = TaskScheduling::where(['company_id'=>$params['id'], 'company_type'=>$editCompany['company_type']])->findOrEmpty();
if (!empty($taskScheduleModel)) {
$taskScheduleModel->save(['company_type'=>$params['company_type']]);
}
}
Admin::strict(false)->where('id',$params['admin_id'])->update($admin);
$admin_id=Admin::where('id',$params['admin_id'])->value('id');
// if($params['company_type']==16){
// AdminLogic::insertRole($admin_id, [3]);
// }
$user_id=User::where(['admin_id'=>$params['admin_id'], 'company_id'=> $params['id']])->value('id');
$arr['admin_id']=$admin_id;
$arr['user_id']=$user_id;
Company::where('id', $params['id'])->update($arr);
$admin['admin_id']=$admin_id;
$admin['sechannel']=4;
// $admin['company_id']=$data['id'];
$admin['real_name']=$params['master_name'];
$admin['nickname']=$params['master_name'];
$admin['mobile']=$params['master_phone'];
$admin['phone']=$params['master_phone'];
User::strict(false)->where('id', $user_id)->update($admin);
Db::commit();
return true;
} catch (\Exception $e) {
Db::rollback();
self::setError($e->getMessage(), $e->getFile());
return false;
}
}
/**
* @notes 删除
* @param array $params
* @return bool
* @author likeadmin
* @date 2023/07/15 14:43
*/
public static function delete(array $params): bool
{
return Company::destroy($params['id']);
}
/**
* @notes 获取详情
* @param $params
* @return array
* @author likeadmin
* @date 2023/07/15 14:43
*/
public static function detail($params): array
{
$data = Company::findOrEmpty($params['id'])->toArray();
if ($data) {
$data['other_contacts'] = json_decode($data['other_contacts'], true);
$data['qualification'] = json_decode($data['qualification'], true);
if($data['qualification'] && isset($data['qualification']['other_qualifications'])){
$data['qualification']['other_qualifications']=json_decode($data['qualification']['other_qualifications'],true);
}
$find= Admin::where('id', $data['admin_id'])->field('avatar,id_card')->find();
if($find){
$data['avatar']=$find['avatar'];
$data['id_card']=$find['id_card'];
}else{
$data['avatar']='';
$data['id_card']='';
}
$data['area_manager_name'] = Db::name('admin')->where('id', $data['area_manager'])->value('name');
$data['responsible_area'] = explode(',', $data['responsible_area']);
$where[]=['party_b','=', $data['id']];
if(isset($params['contract_type'])){
$where[]=['contract_type','=',$params['contract_type']];
}
$data['contract'] = Contract::where($where)->with(['party_a_info', 'contractType'])->find();
}
return $data;
}
public static function contract($companyId, $params)
{
$model = Contract::where(['party_b' => $companyId])->find();
if (empty($model)) {
$model = new Contract();
$model->contract_no = time();
$model->create_time = time();
$model->type = 1;
}
$model->update_time = time();
$model->party_a = $params['contract']['party_a'] ?? 0;
$model->contract_type = $params['contract']['contract_type'] ?? 0;
$model->file = $params['contract']['file'] ?? '';
$model->party_b = $companyId;
$model->save();
return $model->id;
}
public static function getList()
{
return Company::field(['id', 'company_name', 'province', 'city', 'area', 'street', 'village', 'brigade'])->select()->toArray();
}
public static function getPartyA($companyId)
{
$company = Company::where(['id' => $companyId])->find();
$contract = Contract::where(['party_b'=>$company['id']])->find();
if ($contract) {
$partyA = Company::where(['id'=>$contract['party_a']])->find()->toArray();
return $partyA;
} else {
self::setError("该公司未与上级公司签约,无法充值押金");
return false;
}
}
}