345 lines
14 KiB
PHP
345 lines
14 KiB
PHP
<?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\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']])->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' => $params['master_email'],
|
||
'other_contacts' => $params['other_contacts'],
|
||
'area_manager' => $params['area_manager'] ?? 0,
|
||
'qualification' => $params['qualification'],
|
||
'responsible_area' => $params['responsible_area'],
|
||
'admin_id' => 0,
|
||
];
|
||
$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']=4;
|
||
}elseif($params['company_type']==17){
|
||
//村管理公司
|
||
AdminLogic::insertRole($admin_id, [4]);
|
||
$admin['group_id']=3;
|
||
}
|
||
elseif ($params['company_type']==18) {
|
||
//小组服务公司
|
||
AdminLogic::insertRole($admin_id, [6]);
|
||
$admin['group_id']=5;
|
||
}
|
||
$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]);
|
||
|
||
$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($data);
|
||
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'])->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'];
|
||
|
||
Admin::strict(false)->where('account',$account)->update($admin);
|
||
$admin_id=Admin::where('account',$account)->value('id');
|
||
// if($params['company_type']==16){
|
||
// AdminLogic::insertRole($admin_id, [3]);
|
||
// }
|
||
$user_id=User::where('account',$account)->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('account',$account)->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;
|
||
}
|
||
}
|