WokerTask/app/middleapi/controller/CompanyController.php

484 lines
17 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +08:00
<?php
namespace app\middleapi\controller;
use app\adminapi\logic\auth\AdminLogic;
use app\adminapi\logic\user\UserLogic;
use app\adminapi\validate\CompanyValidate;
use app\api\controller\JunziqianController;
use app\common\controller\BaseLikeAdminController;
use app\common\enum\user\UserTerminalEnum;
use app\common\logic\CompanyLogic;
use app\common\logic\contract\ContractLogic;
use app\common\logic\RedisLogic;
use app\common\model\auth\Admin;
use app\common\model\Company;
use app\common\model\company\CompanyAccountLog;
use app\common\model\contract\Contract;
use app\common\model\dict\DictData;
use app\common\model\task_scheduling\TaskScheduling;
use app\common\model\user\User;
use app\common\model\CompanyDepositVoucher;
use Exception;
use think\facade\Db;
use think\facade\Log;
use think\response\Json;
class CompanyController extends BaseLikeAdminController
{
//公司列表
public function lists(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['page_no','page_size','company_name','area_name','street_name','area_manager','company_type','is_contract']);
$where = [];
if(!empty($params['company_name'])){
$where[] = ['company_name','like','%'.$params['company_name'].'%'];
}
if(!empty($params['area_name'])){
$arr= Db::name('geo_area')->where('area_name','like','%'.$params['area_name'].'%')->column('area_code');
if($arr){
$where[]=['area','in',$arr];
}
}
if(!empty($params['street_name'])){
$arr= Db::name('geo_street')->where('street_name','like','%'.$params['street_name'].'%')->column('street_code');
if($arr){
$where[]=['street','in',$arr];
}
}
if(!empty($params['area_manager'])){
$arr= Admin::where('name','like','%'.$params['area_manager'].'%')->column('id');
if($arr){
$where[]=['area_manager','in',$arr];
}
}
if(!empty($params['company_type'])){
$where[] = ['company_type','=',$params['company_type']];
}
if(!empty($params['is_contract'])){
$where[] = ['is_contract','=',$params['is_contract']];
}
$pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
$pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
$data = Company::where($where)
->field(['is_authentication','id', 'id contract', 'company_name', 'organization_code', 'city', 'area', 'street', 'company_type', 'master_name', 'master_position', 'master_phone', 'master_email', 'area_manager', 'is_contract', 'deposit', 'company_money', 'shareholder_money', 'deposit_time', 'status', 'face_create_status'])
->page($pageNo, $pageSize)
->order(['id' => 'desc'])
->append(['notes'], true)
->withAttr('company_type',function($value,$data){
return Db::name('dict_data')->where('id',$value)->value('name');
})
->withAttr('area',function($value,$data){
return Db::name('geo_area')->where('area_code',$value)->value('area_name');
})
->withAttr('street',function($value,$data){
return Db::name('geo_street')->where('street_code',$value)->value('street_name');
})
->withAttr('area_manager',function($value,$data){
return Db::name('admin')->where('id',$value)->value('name');
})
->withAttr('notes',function($value,$data){
if ($data['is_authentication'] == 1) {
return Db::name('company_authentication_fail_log')->where('company_id',$data['id'])->where('log_type', 2)->order(['id'=>'desc'])->limit(1)->value('fail_reason');
} else {
return Db::name('company_authentication_fail_log')->where('company_id',$data['id'])->where('log_type', 1)->order(['id'=>'desc'])->limit(1)->value('fail_reason');
}
})->select()->toArray();
$count = Company::where($where)->count();
$result = [
'lists' => $data,
'count' => $count,
'page_no' => $pageNo,
'page_size' => $pageSize
];
return $this->success('请求成功',$result);
}
//公司详情
public function detail(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
$result = CompanyLogic::detail($params);
return $this->data($result);
}
//公司删除
public function delete(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
$admin_id = Company::where('id', $params['id'])->value('admin_id');
User::where('company_id', $params['id'])->update(['delete_time' => time()]);
TaskScheduling::where('company_id', $params['id'])->update(['delete_time' => time()]);
AdminLogic::delete(['id' => $admin_id]);
CompanyLogic::delete($params);
return $this->success('删除成功', [], 1, 1);
}
//添加公司
public function create(): Json
{
$params = (new CompanyValidate())->post()->goCheck('add');
$params['other_contacts'] = json_encode($params['other_contacts']);
$params['qualification'] = json_encode($params['qualification']);
if (isset($params['responsible_area'])) {
$params['responsible_area'] = implode(',', $params['responsible_area']);
}
$result = CompanyLogic::add($params);
if (true === $result) {
return $this->success('添加成功', [], 1, 1);
}
return $this->fail(CompanyLogic::getError());
}
//修改公司
public function edit(): Json
{
$params = (new CompanyValidate())->post()->goCheck('edit');
$params['other_contacts'] = json_encode($params['other_contacts']);
$params['qualification'] = json_encode($params['qualification']);
if (isset($params['responsible_area'])) {
$params['responsible_area'] = implode(',', $params['responsible_area']);
}
$result = CompanyLogic::edit($params);
if (true === $result) {
return $this->success('编辑成功', [], 1, 1);
}
return $this->fail(CompanyLogic::getError());
}
//公司认证
public function enterpriseCertification(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['id']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
$company = Db::name('company')->where('id', $params['id'])->find();
$qualification = json_decode($company['qualification'], true);
if ($company && $qualification['business_license']) {
$data = [
'name' => $company['company_name'],
'organization_code' => $company['organization_code'],
'business_license' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/561f8202305171526091317.png', //$qualification['business_license'],
'master_name' => $company['master_name'],
'master_email' => $company['master_email'],
'master_phone' => $company['master_phone'],
'master_id_card' => $company['master_id_card'],
'id' => $company['id'],
];
$res = app(JunziqianController::class)->EnterpriseCertification($data);
Log::info(['企业认证同步结果',$res]);
if ($res->success) {
if ($company['company_type'] == 30) {
// 平台公司不用初始化生成合同 合同签约暂不用人脸识别,预留人脸采集状态为已采集
Db::name('company')->where('id', $params['id'])->update([ 'is_contract'=>1,'face_create_status'=>1]);
} else {
Db::name('company')->where('id', $params['id'])->update([ 'face_create_status'=>1]);
}
// 加入缓存中is_callback用于判断是否获取到异步通知
RedisLogic::getInstance()->set('authentication_company_id_'.$company['id'], json_encode(['company_id'=>$company['id'],'is_callback'=>0, 'timing'=>time()]));
return $this->success('系统认证中,请稍后刷新页面查看', ['email' => $res->data], 1, 1);
} else {
return $this->fail($res->msg);
}
} else {
return $this->fail("公司不存在");
}
}
//生成合同
public function generateContract(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['id','party_a','contract_type']);
if(empty($params['id']) || empty($params['party_a']) || empty($params['contract_type'])){
return $this->fail('缺少必要参数');
}
$area_manager = Company::where('id', $params['party_a'])->value('area_manager');
$params['area_manager'] = $area_manager;
$params['type'] = 1;
$params['party_b'] = $params['id'];
unset($params['id']);
$result = ContractLogic::initiate_contract($params);
if (!empty($result) && $result['code'] == 1) {
return $this->success($result['msg'], $result['data'], 1, 1);
}
return $this->fail(ContractLogic::getError());
}
//下属公司
public function subsidiaryCompany(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params=$this->request->post(['id','page_no','page_size']);
if(empty($params['id'])){
return $this->fail('缺少必要参数');
}
$pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
$pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
$ids = Contract::where('party_a', $params['id'])->where('status', 1)->where('type', 1)->column('party_b');
if ($ids) {
$result = Company::where('id', 'in', $ids)->field('company_name,id,company_type,company_type company_type_name,area,area area_name,street,street street_name,is_contract,area_manager,area_manager area_manager_name,master_name,master_phone,is_authentication')->page($pageNo,$pageSize)->select();
$count = Company::where('id', 'in', $ids)->count();
} else {
$result = [];
$count = 0;
}
$data = [
'lists' => $result,
'count' => $count,
'page_no' => $pageNo,
'page_size' => $pageSize
];
return $this->success('success', $data);
}
//公司类型
public function companyType(): Json
{
$data = DictData::where('type_id',6)
->append(['status_desc'])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
return $this->success('请求成功',$data);
}
//合同类型
public function contractType(): Json
{
$data = DictData::where('type_id',7)
->append(['status_desc'])
->order(['sort' => 'desc', 'id' => 'desc'])
->select()
->toArray();
return $this->success('请求成功',$data);
}
public function responsibleArea(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params = $this->request->post(['key','value','company_type']);
if (empty($params['key']) || empty($params['value']) || empty($params['company_type'])) {
return $this->fail('参数错误');
}
if ($params['key'] == 'city') {
$where[] = ['area', '=', 0];
}
if ($params['value'] == '') {
return $this->fail('参数不能为空');
}
$where[] = [$params['key'], '=', $params['value']];
$where[] = ['company_type', '=', $params['company_type']];
switch ($params['key']) {
case 'city':
// $geo_area=Db::name('geo_area')->where('city_code', '=', $parmas['value'])->column('area_code');
// $where[] = ['area', 'in', $geo_area];
break;
case 'area':
$street_code = Db::name('geo_street')->where('area_code', '=', $params['value'])->column('street_code');
$where[] = ['street', 'in', $street_code];
$where[] = ['village', '=', 0];
break;
case 'street':
$street_code = Db::name('geo_village')->where('street_code', '=', $params['value'])->column('village_code');
$where[] = ['village', 'in', $street_code];
$where[] = ['brigade', '=', 0];
break;
case 'village':
// $street_code = Db::name('geo_brigade')->where('street_code', '=', $parmas['value'])->column('village_code');
$where[] = ['village', '=', $params['value']];
// $where[] = ['brigade', '=', 0];
break;
}
$res = Company::where($where)->column('responsible_area');
foreach ($res as $k => $v) {
$res[$k] = explode(',', $v);
}
$data = [];
foreach ($res as $k => $v) {
foreach ($v as $kk => $vv) {
if ($vv != '') {
$data[] = $vv;
}
}
}
return $this->success('success', array_unique($data));
}
public function getDepositRechargeTransferVoucherList(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params = $this->request->post(['page_no','page_size', 'account', 'mobile', 'company_id']);
$where = [];
if(isset($params['company_id']) && $params['company_id'] > 0){
$where[] = ['company_id', '=', $params['company_id']];
}
$pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
$pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
$lists = CompanyDepositVoucher::where($where)
->page($pageNo, $pageSize)
->order(['id' => 'desc'])
->select()
->toArray();
$count=CompanyDepositVoucher::where($where)->count();
$result = [
'lists' => $lists,
'count' => $count,
'page_no' => $pageNo,
'page_size' => $pageSize
];
return $this->success('请求成功',$result);
}
public function depositRechargeTransferVoucher(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$param = $this->request->param();
if(empty($param['company_id'])){
return $this->fail('缺少company_id');
}
if(empty($param['deposit'])){
return $this->fail('缺少押金金额');
}
if(empty($param['voucher'])){
return $this->fail('缺少凭证文件');
}
try {
$adminId = 1;
Db::startTrans();
$data = [
'company_id' => $param['company_id'],
'deposit' => $param['deposit'],
'voucher' => $param['voucher'],
'create_admin_id' => $adminId,
'create_time' => time(),
'update_time' => time()
];
$result = (new CompanyDepositVoucher())->save($data);
$companyModel = Company::where(['id' => $param['company_id']])->find();
$left_amount = bcadd($companyModel['deposit'], $param['deposit'], 2);
// 添加流水记录
$datas = [
'sn' => generate_sn(CompanyAccountLog::class, 'sn', 20),
'user_id' => 0,
'company_id' => $param['company_id'],
'change_type' => CompanyAccountLog::COMPANY_DEPOSIT,
'change_object' => CompanyAccountLog::DEPOSIT,
'action' => 1,
'change_amount' => $param['deposit'],
'left_amount' =>$left_amount,
'remark' => '后台押金转账凭证充值',
'status' => 1,
];
CompanyAccountLog::create($datas);
// 更新公司押金金额
$companyModel->deposit = $left_amount;
$companyModel->save();
Db::commit();
return $this->success('成功');
} catch (Exception $exception) {
Db::rollback();
return $this->fail($exception->getMessage());
}
}
public function getPartyA(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$companyId = $this->request->param('company_id');
if(empty($companyId)){
return $this->fail('缺少公司id');
}
$re = CompanyLogic::getPartyA($companyId);
if ($re === false) {
return $this->fail(CompanyLogic::getError());
}
return $this->success('成功', $re);
}
//获取公司成员
public function companyUserLists(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params = $this->request->post(['company_id','page_no','page_size']);
if(empty($params['company_id'])){
return $this->fail('参数错误');
}
$pageNo = !empty($params['page_no']) ? $params['page_no'] : 1;
$pageSize = !empty($params['page_size']) ? $params['page_size'] : 20;
$field = "id,id contract,sn,nickname,sex,avatar,account,mobile,channel,create_time,admin_id,company_id,street,street as street_name,is_contract";
$lists = User::with(['company'])
->where('company_id',$params['company_id'])
->page($pageNo, $pageSize)
->field($field)
->order('id desc')
->select()
->toArray();
foreach ($lists as &$item) {
$item['channel'] = UserTerminalEnum::getTermInalDesc($item['channel']);
}
$count = User::where('company_id',$params['company_id'])->count();
$result = [
'lists' => $lists,
'count' => $count,
'page_no' => $pageNo,
'page_size' => $pageSize
];
return $this->success('请求成功',$result);
}
//获取公司成员详情
public function companyUserDetail(): Json
{
if(!$this->request->isPost()){
return $this->fail('请求方式错误');
}
$params = $this->request->post(['user_id']);
if(empty($params['user_id'])){
return $this->fail('参数错误');
}
$detail = UserLogic::detail($params['user_id']);
return $this->success('', $detail);
}
}