286 lines
11 KiB
PHP
286 lines
11 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\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('该账号已经注册过企业');
|
||
// }
|
||
$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_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);
|
||
Db::commit();
|
||
return true;
|
||
} catch (\Exception $e) {
|
||
Db::rollback();
|
||
self::setError($e->getMessage());
|
||
return false;
|
||
}
|
||
}
|
||
|
||
|
||
/**
|
||
* @notes 编辑
|
||
* @param array $params
|
||
* @return bool
|
||
* @author likeadmin
|
||
* @date 2023/07/15 14:43
|
||
*/
|
||
public static function edit(array $params): bool
|
||
{
|
||
Db::startTrans();
|
||
try {
|
||
$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_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'])){
|
||
$wehre[]=['contract_type','=',$params['contract_type']];
|
||
}
|
||
$data['contract'] = Contract::where($wehre)->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 getAllCompanyIds()
|
||
{
|
||
$companyId = request()->adminInfo['company_id'] ?? 0;
|
||
$companyIds[] = $companyId;
|
||
$companies = loopGetChild(Company::class, $companyId, 'level_one', 'id,level_one');
|
||
if (!empty($companies)) {
|
||
$companyIds = array_merge(array_column($companies, 'id'), $companyIds);
|
||
}
|
||
return array_filter($companyIds);
|
||
}
|
||
}
|