WokerTask/app/adminapi/controller/CompanyController.php

564 lines
24 KiB
PHP
Raw Normal View History

2023-12-27 14:06:33 +08:00
<?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\adminapi\controller;
use app\adminapi\lists\CompanyLists;
use app\adminapi\logic\auth\AdminLogic;
use app\common\enum\PayEnum;
use app\common\logic\CompanyLogic;
use app\adminapi\validate\CompanyValidate;
use app\api\controller\JunziqianController;
use app\common\logic\RedisLogic;
use app\common\model\Approve;
use app\common\model\auth\Admin;
use app\common\model\Company;
use app\common\model\company\CompanyAccountLog;
use app\common\model\CompanyDepositVoucher;
use app\common\model\recharge\RechargeOrder;
use app\common\model\user\UserAccountLog;
use think\Exception;
use think\facade\Db;
use app\common\logic\contract\ContractLogic;
use app\common\model\contract\Contract;
use app\common\model\task_scheduling\TaskScheduling;
use app\common\model\user\User;
use think\facade\Log;
/**
* Company控制器
* Class CompanyController
* @package app\adminapi\controller
*/
class CompanyController extends BaseAdminController
{
public array $notNeedLogin = ['createShopMerchant'];
/**
* @notes 获取列表
* @return \think\response\Json
* @author likeadmin
* @date 2023/07/15 14:43
*/
public function lists()
{
return $this->dataLists(new CompanyLists());
}
public function list_two()
{
return $this->success('ok', (new CompanyLists())->list_two());
}
/**
* @notes 添加
* @return \think\response\Json
* @author likeadmin
* @date 2023/07/15 14:43
*/
public function add()
{
$params = (new CompanyValidate())->post()->goCheck('add');
$params['other_contacts'] = json_encode($params['other_contacts']);
$params['qualification'] = json_encode($params['qualification']);
$params['area_manager'] = $this->adminId;
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());
}
/**
* @notes 编辑
* @return \think\response\Json
* @author likeadmin
* @date 2023/07/15 14:43
*/
public function edit()
{
$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());
}
/**
* @notes 删除
* @return \think\response\Json
* @author likeadmin
* @date 2023/07/15 14:43
*/
public function delete()
{
$params = (new CompanyValidate())->post()->goCheck('delete');
$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);
}
/**
* @notes 获取详情
* @return \think\response\Json
* @author likeadmin
* @date 2023/07/15 14:43
*/
public function detail()
{
$params = (new CompanyValidate())->goCheck('detail');
$result = CompanyLogic::detail($params);
return $this->data($result);
}
//**发起合同 */
public function initiate_contract()
{
$params = $this->request->param();
if (isset($params['party_a']) && $params['party_a'] > 0) {
$params['party_a'] = $params['party_a'];
} else {
$params['party_a'] = $this->adminInfo['company_id'];
}
//判断是否是租赁合同
//如果是租赁合同则必须传递租赁数量参数
// if($params['contract_type'] == 29) {
// if(empty($params['num'])){
// 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 Draftingcontracts()
{
$params = $this->request->param();
$result = ContractLogic::Draftingcontracts($params);
if ($result == true) {
return $this->success('发送合同成功', [], 1, 1);
}
return $this->fail(ContractLogic::getError());
}
//**发送短信 */
public function postsms()
{
$params = (new CompanyValidate())->goCheck('detail');
$id = Contract::where('party_b', $params['id'])->value('id');
$res = ContractLogic::postsms(['id' => $id]);
if ($res == true) {
return $this->success('发送成功', [], 1, 1);
} else {
return $this->fail(ContractLogic::getError());
}
}
//企业认证
public function authentication()
{
$params = (new CompanyValidate())->goCheck('detail');
$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 == true) {
// 企业人脸上传
// $company['master_email'] = $email; // 法人邮箱
// $faceCreateRe = CompanyLogic::originationFaceCreate($company);
// if ($faceCreateRe !== true) {
// return $this->fail($faceCreateRe);
// }
//
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('认证成功',[],1, 1);
return $this->success('系统认证中,请稍后刷新页面查看', ['email' => $res->data], 1, 1);
} else {
return $this->fail($res->msg);
}
} else {
return $this->fail("公司不存在");
}
}
//重新认证
public function organizationReapply()
{
$params = (new CompanyValidate())->goCheck('detail');
$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'],
];
$res = app(JunziqianController::class)->organizationReapply($data);
if ($res->success == true) {
Db::name('company')->where('id', $params['id'])->update(['master_email' => $res->data, 'face_create_status'=>1]); //todo 上人脸采集功能时
return $this->success('系统认证中,请稍后刷新页面查看', ['email' => $res->data], 1, 1);
} else {
return $this->fail($res->msg);
}
}
}
// 企业人脸校验上传
public function organizationFaceCreate()
{
$params = (new CompanyValidate())->goCheck('detail');
$company = Db::name('company')->where('id', $params['id'])->find();
$re = CompanyLogic::originationFaceCreate($company);
if ($re === true) {
return $this->success('人脸采集中,请稍后刷新页面查看',[],1, 1);
} else {
return $this->fail($re);
}
}
private function companyCheck($company) {
if (empty($company)) {
return $this->fail("公司不存在");
}
if(empty($company['master_id_card'])) {
return $this->fail("主联系人证件号为空,无法上传企业人脸");
}
if(empty($company['master_email'])) {
return $this->fail("主联系人邮箱为空,无法上传企业人脸");
}
}
public function userList()
{
$existUsers = Company::where('status', '<>', -1)->column('admin_id');
$existUsers = array_unique($existUsers);
$users = Admin::whereNotIn('id', $existUsers)->field('id,name,avatar')->select()->toArray();
return $this->success('success', $users);
}
/**
* 所有成员公司
* @param $id
* @return \think\response\Json
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function subordinate($company_id)
{
$ids = Contract::where('party_a', $company_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')->select();
} else {
$result = [];
}
$data['lists'] = $result;
return $this->success('success', $data);
}
public function responsible_area()
{
$parmas = $this->request->param();
if (!isset($parmas['key']) && isset($parmas['key']) == '' || !isset($parmas['value']) && isset($parmas['value']) == '') {
return $this->fail('参数错误');
}
if ($parmas['key'] == 'city') {
$where[] = ['area', '=', 0];
}
if ($parmas['value'] == '') {
return $this->fail('参数不能为空');
}
$where[] = [$parmas['key'], '=', $parmas['value']];
$where[] = ['company_type', '=', $parmas['company_type']];
switch ($parmas['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', '=', $parmas['value'])->column('street_code');
$where[] = ['street', 'in', $street_code];
$where[] = ['village', '=', 0];
break;
case 'street':
$street_code = Db::name('geo_village')->where('street_code', '=', $parmas['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', '=', $parmas['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 createShopMerchant()
{
try {
$params = $this->request->param();
Log::info(['商户入驻申请-请求参数', $params]);
// if (empty($params['company_name'])) {
// throw new Exception('商户名称不能为空');
// }
// if (empty($params['organization_code'])) {
// throw new Exception('社会统一信用代码不能为空');
// }
// if (empty($params['master_name'])) {
// throw new Exception('商户法人名称不能为空');
// }
// $master_email=Db::name('company_form')->where('organization_code',$params['organization_code'])->value('master_email');
//
// $isQueryStatus = false; // 需要进一步查询是否已做过企业认证
// // $master_email为空表示没有做过认证
// if($master_email){
// $isQueryStatus = true;asdasd,fsdfdf
// }else{
// $master_email=substr(md5(uniqid()),rand(0, 22),10)."@lihai.com";
// }
// 市级供应链直接和海之农签约
if (isset($params['type_id']) && $params['type_id'] == 12) {
$company = Company::where(['company_name' => '泸州市海之农科技有限公司'])->find();
} else {
// 根据street码查询所属镇农科公司
$company_select=Db::query("select * from la_company where company_type=:company_type and FIND_IN_SET(:street,responsible_area)", ['company_type' => 41,'street'=>$params['street']], true);
if(empty($company_select)) {
throw new Exception('当前区域无镇农科公司');
}
$company=$company_select[0];
}
$data = [
'mer_intention_id' => $params['mer_intention_id']??'', // 商城商户入驻申请id签约完成后回调使用
'company_name' => $params['company_name']??'',
'mer_name' => $params['mer_name']??'', // 店铺名称
'organization_code' => $params['organization_code']??'',
'city' => $params['city']??'',
'area' => $params['area']??'',
'street' => $params['street']??'',
'address' => $params['address']??'',
'master_name' => $params['master_name']??'',
'master_phone' => $params['master_phone']??'',
'master_email' => $master_email??'',
'type_name' => $params['type_name'] ?? '', //店铺类型
'category_name' => $params['category_name'] ?? '', //商户分类
'qualification' => !empty($params['images'])?$params['images']:json_encode([])
];
$approveModel = new Approve();
$shopApproveType = $params['type']; // 商户申请类型 1入驻 2开通交易
// 插入审批流
$approveModel->type = $shopApproveType == 1? 2: 3; // 审批类型,后台配置 1 其他任务 2商户入驻 3开通交易
$approveModel->flow_id = 2;
$approveModel->name = $shopApproveType == 1? '商户入驻审批': '开通交易审批';
$approveModel->admin_id = 0; // 后台发起人id 暂时为0
$approveModel->department_id = '0';
$approveModel->check_admin_ids = $company['admin_id']; // 当前审批人ID 镇农科公司负责人
$approveModel->check_status = 1; // 状态 0待审核,1审核中,2审核通过,3审核不通过,4撤销审核
$approveModel->other_type = 6;
$approveModel->extend = json_encode(array_merge($params, $data));
$approveModel->create_time = time();
$approveModel->update_time = time();
$approveModel->save();
// $shopMerchantModel = ShopMerchant::create($data);
// 如果是通滩镇的商户,查询一下企业认证状态,通过的情况,则直接发起生成合同
// if ($params['street'] == '510502106' && $isQueryStatus) {
// $statusRe = app(JunziqianController::class)->shopMerchantStatusQuery($master_email);
// Log::info(['商户入驻请求-通滩镇商户-认证状态查询结果', $statusRe]);
// if($statusRe->success == true) {
// $statusData = json_decode($statusRe->data, true);
// if ($statusData['status'] == 1) {
// // 生成合同
// $createContractData = [
// 'id' => $shopMerchantModel->id,
// 'party_a' => 1,
// 'party_a_name' => '泸州市海之农科技有限公司',
// 'party_b' => $shopMerchantModel->id,
// 'party_b_name' => $params['company_name'],
// 'contract_type' => 22,
// ];
// $shopContractModel = new ShopContract();
// $shopContractModel->contract_no = time();
// $shopContractModel->create_time = time();
// $shopContractModel->check_status = 1;
// $shopContractModel->update_time = time();
// $shopContractModel->setAttrs($createContractData);
// $shopContractModel->save($createContractData);
// return $this->success('已做过企业认证,直接生成合同成功', [], 1, 1);
// }
// }
//
// }
// if (!$shopMerchantModel->isEmpty()) {
// // 自动发起企业认证
// $shopMerchantCertificationData = [
// 'name' => $shopMerchantModel->company_name,
// 'organization_code' => $shopMerchantModel->organization_code,
// 'business_license' => 'https://lihai001.oss-cn-chengdu.aliyuncs.com/def/561f8202305171526091317.png',
// 'master_name' => $shopMerchantModel->master_name,
// 'master_email' => $shopMerchantModel->master_email,
// 'master_phone' => $shopMerchantModel->master_phone,
// 'id' => $shopMerchantModel->id,
// ];
// app(JunziqianController::class)->ShopMerchantCertification($shopMerchantCertificationData);
// } else {
// throw new Exception('商户创建失败');
// }
return $this->success('审批创建成功', [], 1, 1);
} catch (Exception $exception) {
Log::error(['商户入驻审批失败', $exception->getMessage()]);
return $this->fail($exception->getMessage());
}
}
// 押金凭证录入,并将押金写入公司押金账户,新增一条押金充值流水
public function depositRechargeTransferVoucher()
{
try {
$param = $this->request->param();
Db::startTrans();
$data = [
'company_id' => $param['company_id'],
'deposit' => $param['deposit'],
'voucher' => $param['voucher'],
'create_admin_id' => $this->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()
{
$companyId = $this->request->param('company_id');
$re = CompanyLogic::getPartyA($companyId);
if ($re === false) {
return $this->fail(CompanyLogic::getError());
}
return $this->success('成功', $re);
}
public function getDepositRechargeTransferVoucherList()
{
$companyId = $this->request->param('company_id');
$re = (new CompanyLists())->list_three($companyId);
return $this->success('成功', $re);
}
public function depositRechargeTransferVoucherDetail()
{
$param = $this->request->param();
$companyDepositVoucher = CompanyDepositVoucher::where(['id' => $param['id']])->findOrEmpty()->toArray();
if (empty($companyDepositVoucher)) {
return $this->fail('数据不存在');
}
$contract = Contract::where(['party_b'=>$companyDepositVoucher['company_id']])->with(['partyA', 'partyB'])->findOrEmpty()->toArray();
return $this->success('成功', array_merge($companyDepositVoucher, $contract));
}
}