| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- from typing import Annotated
- import uuid
- from fastapi import APIRouter, Depends, Path, Query
- from fastapi.responses import JSONResponse
- from app.api.v1.module_system.auth.schema import AuthSchema
- from app.common.response import ResponseSchema, SuccessResponse
- from app.core.dependencies import AuthPermission
- from app.core.logger import log
- from app.core.router_class import OperationLogRoute
- from app.plugin.module_payment.expense.institution.schema import InstitutionListOutSchema
- from .service import InstitutionService, InstitutionScopeService, IssueruleService
- from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionCreateModel import (
- AlipayEbppInvoiceInstitutionCreateModel,
- )
- from alipay.aop.api.response.AlipayEbppInvoiceInstitutionCreateResponse import (
- AlipayEbppInvoiceInstitutionCreateResponse,
- )
- from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionDeleteModel import (
- AlipayEbppInvoiceInstitutionDeleteModel,
- )
- from alipay.aop.api.response.AlipayEbppInvoiceInstitutionDeleteResponse import (
- AlipayEbppInvoiceInstitutionDeleteResponse,
- )
- from alipay.aop.api.domain.AlipayEbppInvoiceInstitutionModifyModel import (
- AlipayEbppInvoiceInstitutionModifyModel,
- )
- from alipay.aop.api.response.AlipayEbppInvoiceInstitutionModifyResponse import (
- AlipayEbppInvoiceInstitutionModifyResponse,
- )
- InstitutionRouter = APIRouter(
- route_class=OperationLogRoute,
- prefix="/institution",
- tags=["费控制度"],
- )
- @InstitutionRouter.post(
- "",
- summary="创建费控制度",
- description="创建费控制度。支持串联调用:创建制度→设置成员→创建发放规则",
- )
- async def create_institution_controller(
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:create"]))],
- ) -> JSONResponse:
- """创建费控制度(含完整串联流程)"""
- enterprise_id = data.get("enterprise_id", "")
- if not enterprise_id:
- from app.plugin.module_payment.enterprise.model import EnterpriseModel
- from sqlalchemy import select
- tenant_id = auth.user.tenant_id if auth.user and auth.user.tenant_id else auth.tenant_id
- log.info(f"推导 enterprise_id: tenant_id={tenant_id}, user_tenant_id={getattr(auth.user, 'tenant_id', None)}")
- stmt = select(EnterpriseModel).where(EnterpriseModel.tenant_id == tenant_id).limit(1)
- result = await auth.db.execute(stmt)
- enterprise = result.scalar_one_or_none()
- log.info(f"查询 enterprise 结果: {enterprise.enterprise_id if enterprise else 'None'}")
- enterprise_id = enterprise.enterprise_id if enterprise else ""
- if enterprise_id:
- data["enterprise_id"] = enterprise_id
- # 字段映射:前端 name → Alipay institution_name
- if data.get("name") and not data.get("institution_name"):
- data["institution_name"] = data["name"]
- # Alipay 必填:商户外部单据号(唯一标识)
- if not data.get("outer_source_id"):
- data["outer_source_id"] = str(uuid.uuid4()).replace("-", "")
- # expense_type 映射:前端值 → 支付宝标准值
- EXPENSE_TYPE_MAP = {"GENERAL": "DEFAULT", "DEFAULT": "DEFAULT"}
- if data.get("expense_type") in EXPENSE_TYPE_MAP:
- data["expense_type"] = EXPENSE_TYPE_MAP[data["expense_type"]]
- # 时间格式补全:YYYY-MM-DD → YYYY-MM-DD HH:mm:ss
- if data.get("effective_start_date") and len(data["effective_start_date"]) == 10:
- data["effective_start_date"] = data["effective_start_date"] + " 00:00:00"
- if data.get("effective_end_date") and len(data["effective_end_date"]) == 10:
- data["effective_end_date"] = data["effective_end_date"] + " 23:59:59"
- elif not data.get("effective_end_date") and data.get("effective_time_type") == "unlimited":
- # 长期有效:设为2099年底
- data["effective_end_date"] = "2099-12-31 23:59:59"
- # 默认使用规则(支付宝必填)
- if not data.get("standard_info_list"):
- single_limit = data.get("single_limit", 0)
- period_type = data.get("period_type", "")
- amount = data.get("amount", 0)
- standard_info = {
- "standard_name": data.get("institution_name", "默认规则"),
- "standard_desc": f"单笔限额{single_limit}元" if single_limit else "通用规则",
- "consume_mode": "DEFAULT",
- "payment_policy": "PERSONAL",
- "personal_qrcode_mode": 0,
- "outer_source_id": str(uuid.uuid4()).replace("-", ""),
- }
- condition_list = []
- if single_limit:
- condition_list.append({"rule_factor": "QUOTA_TOTAL", "rule_name": "单次消费金额", "rule_value": str(single_limit)})
- # 定额发放时,将周期限额写入使用规则条件
- PERIOD_FACTOR_MAP = {
- "daily": "QUOTA_DAY", "weekly": "QUOTA_WEEK",
- "monthly": "QUOTA_MONTH", "quarterly": "QUOTA_QUARTER",
- "yearly": "QUOTA_YEAR",
- }
- if data.get("grant_mode") == "period" and period_type in PERIOD_FACTOR_MAP and amount:
- condition_list.append({
- "rule_factor": PERIOD_FACTOR_MAP[period_type],
- "rule_name": f"{period_type}限额",
- "rule_value": str(amount),
- })
- # 至少保证有一条条件规则(支付宝必填)
- if not condition_list:
- condition_list.append({
- "rule_factor": "QUOTA_TOTAL",
- "rule_name": "单次消费金额",
- "rule_value": "0",
- })
- standard_info["standard_condition_info_list"] = condition_list
- data["standard_info_list"] = [standard_info]
- institution_create_model = AlipayEbppInvoiceInstitutionCreateModel.from_alipay_dict(data)
- # 解析适用成员数据
- scope_data = None
- adapter_type = data.get("applicable_scope")
- if adapter_type and adapter_type not in ("NONE", "none"):
- ADAPTER_TYPE_MAP = {"all": "EMPLOYEE_ALL", "employee": "EMPLOYEE_SELECT", "department": "EMPLOYEE_DEPARTMENT"}
- mapped_adapter = ADAPTER_TYPE_MAP.get(adapter_type, adapter_type)
- scope_data = {
- "adapter_type": mapped_adapter,
- "owner_type": data.get("scope_owner_type", "EMPLOYEE"),
- "add_owner_id_list": data.get("scope_owner_id_list"),
- }
- # 全体员工时把 scope 写入创建请求(避免默认无scope导致支付宝后台不可操作)
- if adapter_type == "all":
- data["institution_scope_info"] = {
- "adapter_type": "ALL",
- "owner_type": "EMPLOYEE",
- }
- # 解析发放规则数据
- issuerule_data = None
- if data.get("grant_mode") == "period":
- period_type_raw = data.get("period_type", "monthly")
- # 映射前端period_type到支付宝枚举
- ISSUE_TYPE_MAP = {
- "daily": "ISSUE_DAY",
- "weekly": "ISSUE_WEEK",
- "monthly": "ISSUE_MONTH",
- "quarterly": "ISSUE_QUARTER",
- "yearly": "ISSUE_YEAR",
- }
- issue_type = ISSUE_TYPE_MAP.get(period_type_raw, "ISSUE_MONTH")
- amount = data.get("amount", 0)
- # 有效时间配置
- effective_time_type = data.get("effective_time_type", "unlimited")
- if effective_time_type == "unlimited":
- effective_period = '{"all": true}'
- elif effective_time_type == "workday":
- workday_start = data.get("workday_start_time", "00:00")
- workday_end = data.get("workday_end_time", "23:59")
- effective_period = f'{{"regular":{{"workday":[["{workday_start}","{workday_end}"]]}}}}'
- else:
- effective_period = '{"all": true}'
- issuerule_data = {
- "quota_type": "CAP",
- "issue_type": issue_type,
- "issue_amount_value": str(amount),
- "issue_rule_name": data.get("name", "") + "-发放规则",
- "effective_period": effective_period,
- "invalid_mode": 1 if data.get("effective_time_type") == "unlimited" else 0,
- "share_mode": 0,
- "outer_source_id": data.get("outer_source_id") or str(uuid.uuid4()),
- }
- result = await InstitutionService.create_institution_full_flow(
- auth=auth,
- institution_model=institution_create_model,
- enterprise_id=enterprise_id,
- scope_data=scope_data,
- issuerule_data=issuerule_data,
- raw_data=data,
- )
- log.info(f"创建费控制度成功: institution_id={result.get('institution_id')}")
- return SuccessResponse(data=result, msg="创建费控制度成功")
- @InstitutionRouter.get(
- "",
- summary="查询费控制度列表",
- description="分页查询费控制度列表",
- response_model=ResponseSchema[InstitutionListOutSchema],
- )
- async def list_institution_controller(
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:list"]))],
- page_no: Annotated[int, Query(description="页码")] = 1,
- page_size: Annotated[int, Query(description="每页数量")] = 20,
- enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
- name: Annotated[str | None, Query(description="制度名称")] = None,
- expense_type: Annotated[str | None, Query(description="费用类型")] = None,
- status: Annotated[str | None, Query(description="状态")] = None,
- ) -> JSONResponse:
- """查询费控制度列表"""
- search = {}
- if enterprise_id:
- search["enterprise_id"] = enterprise_id
- if name:
- search["name"] = name
- if expense_type:
- search["expense_type"] = expense_type
- if status:
- search["status"] = status
- result = await InstitutionService.list_service(
- auth=auth, page_no=page_no, page_size=page_size, search=search
- )
- return SuccessResponse(data=result, msg="查询费控制度列表成功")
- @InstitutionRouter.get(
- "/{institution_id}",
- summary="查询费控制度详情",
- description="查询费控制度详情 (alipay.ebpp.invoice.institution.detailinfo.query),失败时降级到本地DB",
- )
- async def detail_institution_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:detail"]))],
- enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
- ) -> JSONResponse:
- """查询费控制度详情"""
- if not enterprise_id:
- from app.plugin.module_payment.enterprise.model import EnterpriseModel
- from sqlalchemy import select
- tenant_id = auth.user.tenant_id if auth.user and auth.user.tenant_id else auth.tenant_id
- stmt = select(EnterpriseModel).where(EnterpriseModel.tenant_id == tenant_id).limit(1)
- result = await auth.db.execute(stmt)
- ent = result.scalar_one_or_none()
- enterprise_id = ent.enterprise_id if ent else ""
- result = await InstitutionService.detailinfo_query_service(
- auth=auth,
- institution_id=institution_id,
- enterprise_id=enterprise_id,
- )
- if result is None:
- return SuccessResponse(data=None, msg="制度不存在")
- return SuccessResponse(data=result, msg="查询费控制度详情成功")
- @InstitutionRouter.delete(
- "",
- summary="删除费控制度",
- description="删除费控制度 (alipay.ebpp.invoice.institution.delete)",
- )
- async def delete_institution_controller(
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:delete"]))],
- ) -> JSONResponse:
- """删除费控制度"""
- institution_delete_model = AlipayEbppInvoiceInstitutionDeleteModel.from_alipay_dict(data)
- result = await InstitutionService.delete_institution_service(auth=auth, data=institution_delete_model)
- log.info(f"删除费控制度成功: institution_id={institution_delete_model.institution_id}, enterprise_id={institution_delete_model.enterprise_id}")
- return SuccessResponse(data=result, msg="删除费控制度成功")
- @InstitutionRouter.post(
- "/modify",
- summary="编辑费控制度",
- description="编辑费控制度 (alipay.ebpp.invoice.institution.modify)",
- )
- async def modify_institution_controller(
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:modify"]))],
- ) -> JSONResponse:
- """编辑费控制度"""
- # enterprise_id 推导
- if not data.get("enterprise_id"):
- from app.plugin.module_payment.enterprise.model import EnterpriseModel
- from sqlalchemy import select
- tenant_id = auth.user.tenant_id if auth.user and auth.user.tenant_id else auth.tenant_id
- stmt = select(EnterpriseModel).where(EnterpriseModel.tenant_id == tenant_id).limit(1)
- result = await auth.db.execute(stmt)
- enterprise = result.scalar_one_or_none()
- if enterprise:
- data["enterprise_id"] = enterprise.enterprise_id
- # name → institution_name
- if data.get("name") and not data.get("institution_name"):
- data["institution_name"] = data["name"]
- # 时间格式
- if data.get("effective_start_date") and len(data["effective_start_date"]) == 10:
- data["effective_start_date"] = data["effective_start_date"] + " 00:00:00"
- if data.get("effective_end_date") and len(data["effective_end_date"]) == 10:
- data["effective_end_date"] = data["effective_end_date"] + " 23:59:59"
- elif not data.get("effective_end_date") and data.get("effective_time_type") == "unlimited":
- data["effective_end_date"] = "2099-12-31 23:59:59"
- # expense_type 映射
- EXPENSE_TYPE_MAP = {"GENERAL": "DEFAULT", "DEFAULT": "DEFAULT"}
- if data.get("expense_type") in EXPENSE_TYPE_MAP:
- data["expense_type"] = EXPENSE_TYPE_MAP[data["expense_type"]]
- # 提取 scope 变更数据(需与基础修改分两次请求)
- applicable_scope = data.get("applicable_scope", "")
- scope_info = None
- enterprise_id = data.get("enterprise_id", "")
- if applicable_scope and applicable_scope not in ("NONE", "none"):
- ADAPTER_MAP = {"all": "EMPLOYEE_ALL", "employee": "EMPLOYEE_SELECT", "department": "EMPLOYEE_DEPARTMENT"}
- new_adapter = ADAPTER_MAP.get(applicable_scope, applicable_scope)
- # 查询当前scope:计算旧→新的差异
- old_ids = []
- try:
- scope_old = await InstitutionScopeService.scopepageinfo_query_service(
- auth=auth, institution_id=institution_id, enterprise_id=enterprise_id,
- page_num=1, page_size=500,
- )
- old_ids = [str(i) for i in (scope_old.get("owner_id_list") or []) if i]
- except Exception:
- log.warning(f"查询旧scope失败,将全量覆盖: institution_id={institution_id}")
- new_ids_raw = data.get("scope_owner_id_list") or []
- new_ids = [str(i) for i in new_ids_raw if i is not None and str(i).strip()]
- # 计算差异
- old_set, new_set = set(old_ids), set(new_ids)
- add_ids = list(new_set - old_set)
- delete_ids = list(old_set - new_set)
- scope_info = {
- "enterprise_id": enterprise_id,
- "adapter_type": new_adapter,
- "owner_type": "EMPLOYEE",
- }
- if add_ids:
- scope_info["add_owner_id_list"] = add_ids
- if delete_ids:
- scope_info["delete_owner_id_list"] = delete_ids
- log.info(
- f"scope 差异: add={add_ids}, delete={delete_ids}, "
- f"old_count={len(old_ids)}, new_count={len(new_ids)}"
- )
- if not add_ids and not delete_ids:
- scope_info = None
- log.info("scope 无变化,跳过")
- # 从请求中移除 scope 数据,避免与基础修改冲突
- data.pop("modify_scope_info", None)
- # 第1次请求:仅修改制度基础信息(不含 scope)
- base_data = {k: v for k, v in data.items() if k != "modify_scope_info"}
- institution_modify_model = AlipayEbppInvoiceInstitutionModifyModel.from_alipay_dict(base_data)
- try:
- result = await InstitutionService.modify_institution_service(
- auth=auth, data=institution_modify_model, raw_data=base_data, scope_info=scope_info
- )
- except Exception as e:
- err_msg = str(e)
- if "consult" in err_msg.lower() or "咨询" in err_msg or "发" in err_msg:
- raise CustomException(msg="制度下存在发放规则,咨询模式不允许修改为外部服务商,请先删除发放规则后再试")
- raise
- log.info(f"编辑费控制度成功: institution_id={institution_modify_model.institution_id}")
- return SuccessResponse(data=result, msg="编辑费控制度成功")
- # ========== 制度成员范围管理 ==========
- @InstitutionRouter.get(
- "/{institution_id}/scope",
- summary="查询制度成员范围",
- description="查询制度下成员范围 (alipay.ebpp.invoice.institution.scopepageinfo.query)",
- )
- async def list_scope_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:scope:list"]))],
- enterprise_id: Annotated[str | None, Query(description="企业ID")] = None,
- owner_type: Annotated[str | None, Query(description="适配ID类型")] = None,
- page_num: Annotated[int, Query(description="页码")] = 1,
- page_size: Annotated[int, Query(description="每页条数")] = 20,
- ) -> JSONResponse:
- """查询制度成员"""
- result = await InstitutionScopeService.scopepageinfo_query_service(
- auth=auth,
- institution_id=institution_id,
- enterprise_id=enterprise_id,
- page_num=page_num,
- page_size=page_size,
- owner_type=owner_type,
- )
- return SuccessResponse(data=result, msg="查询成功")
- @InstitutionRouter.post(
- "/{institution_id}/scope",
- summary="设置制度成员范围",
- description="设置/修改制度成员范围 (alipay.ebpp.invoice.institution.scope.modify)",
- )
- async def modify_scope_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:scope:modify"]))],
- ) -> JSONResponse:
- """设置制度成员"""
- from app.plugin.module_payment.employee.model import EmployeeModel
- from sqlalchemy import select
- enterprise_id = data.get("enterprise_id", "")
- new_adapter = data.get("adapter_type", "EMPLOYEE_ALL")
- # ====== 1. 查询旧适配类型和老员工ID列表 ======
- old_adapter = new_adapter
- old_employee_ids: set[str] = set()
- try:
- scope_old = await InstitutionScopeService.scopepageinfo_query_service(
- auth=auth, institution_id=institution_id, enterprise_id=enterprise_id,
- page_num=1, page_size=500,
- )
- old_adapter = scope_old.get("adapter_type", old_adapter)
- raw_old = [str(i) for i in (scope_old.get("owner_id_list") or []) if i]
- if old_adapter == "EMPLOYEE_ALL":
- # 全体员工模式 → 所有已签约员工
- stmt = select(EmployeeModel.employee_id).where(
- EmployeeModel.enterprise_id == enterprise_id,
- EmployeeModel.status == "ACTIVATED",
- )
- result = await auth.db.execute(stmt)
- old_employee_ids = {row[0] for row in result.fetchall() if row[0]}
- elif old_adapter == "EMPLOYEE_DEPARTMENT":
- # 按部门模式 → 部门下的所有已签约员工
- from app.plugin.module_payment.employee.model import EmployeeModel
- stmt = select(EmployeeModel.employee_id, EmployeeModel.department_ids).where(
- EmployeeModel.enterprise_id == enterprise_id,
- EmployeeModel.status == "ACTIVATED",
- )
- result = await auth.db.execute(stmt)
- dept_set = set(raw_old)
- for row in result.fetchall():
- if row[1] and dept_set.intersection(set(row[1])):
- if row[0]:
- old_employee_ids.add(row[0])
- else:
- old_employee_ids = set(raw_old)
- except Exception:
- log.warning(f"查询旧scope失败(将全量处理): institution_id={institution_id}")
- # ====== 2. 计算新员工ID列表 ======
- new_employee_ids: set[str] = set()
- if new_adapter == "EMPLOYEE_ALL":
- stmt = select(EmployeeModel.employee_id).where(
- EmployeeModel.enterprise_id == enterprise_id,
- EmployeeModel.status == "ACTIVATED",
- )
- result = await auth.db.execute(stmt)
- new_employee_ids = {row[0] for row in result.fetchall() if row[0]}
- elif new_adapter == "EMPLOYEE_DEPARTMENT":
- dept_ids = data.get("add_owner_id_list") or []
- dept_set = set(str(d) for d in dept_ids if d)
- stmt = select(EmployeeModel.employee_id, EmployeeModel.department_ids).where(
- EmployeeModel.enterprise_id == enterprise_id,
- EmployeeModel.status == "ACTIVATED",
- )
- result = await auth.db.execute(stmt)
- for row in result.fetchall():
- if row[1] and dept_set.intersection(set(row[1])):
- if row[0]:
- new_employee_ids.add(row[0])
- else:
- raw_new = data.get("add_owner_id_list") or []
- new_employee_ids = set(str(i) for i in raw_new if i is not None and str(i).strip())
- # ====== 3. 计算差异 ======
- add_ids = list(new_employee_ids - old_employee_ids)
- delete_ids = list(old_employee_ids - new_employee_ids)
- # ====== 4. 调用支付宝 scope.modify(只传差异) ======
- scope_data = {
- "enterprise_id": enterprise_id,
- "adapter_type": new_adapter,
- "owner_type": data.get("owner_type", "EMPLOYEE"),
- }
- if add_ids:
- scope_data["add_owner_id_list"] = add_ids
- if delete_ids:
- scope_data["delete_owner_id_list"] = delete_ids
- result = await InstitutionScopeService.scope_modify_service(
- auth=auth,
- institution_id=institution_id,
- data=scope_data,
- )
- # ====== 5. 更新本地库 + 额度联动 ======
- try:
- from .crud import InstitutionCRUD
- crud = InstitutionCRUD(auth)
- scope_map = {"EMPLOYEE_ALL": "all", "EMPLOYEE_SELECT": "employee", "EMPLOYEE_DEPARTMENT": "department"}
- applicable_scope = scope_map.get(new_adapter, "all")
- update_data = {"applicable_scope": applicable_scope}
- if new_adapter == "EMPLOYEE_DEPARTMENT":
- dept_ids = data.get("add_owner_id_list") or []
- if dept_ids:
- update_data["department_id"] = str(dept_ids[0])
- await crud.update_by_institution_id(institution_id, update_data)
- if enterprise_id:
- from .service import InstitutionService
- scope_info = {
- "adapter_type": new_adapter,
- "owner_type": "EMPLOYEE",
- "add_owner_id_list": add_ids,
- "delete_owner_id_list": delete_ids,
- }
- await InstitutionService._sync_modify_quotas_by_scope(
- auth=auth,
- institution_id=institution_id,
- enterprise_id=enterprise_id,
- scope_info=scope_info,
- raw_data={},
- )
- except Exception as e:
- log.warning(f"本地scope同步失败(不影响支付宝侧): {e}")
- log.info(f"设置成员成功: {old_adapter}→{new_adapter}, "
- f"加{len(add_ids)}人, 减{len(delete_ids)}人")
- return SuccessResponse(data=result, msg="设置成功")
- # ========== 自动额度发放规则管理 ==========
- @InstitutionRouter.post(
- "/{institution_id}/issuerule",
- summary="创建自动发放规则",
- description="创建自动额度发放规则 (alipay.ebpp.invoice.issuerule.create)",
- )
- async def create_issuerule_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:create"]))],
- ) -> JSONResponse:
- """创建自动发放规则"""
- result = await IssueruleService.create_issuerule_service(
- auth=auth,
- institution_id=institution_id,
- enterprise_id=data.get("enterprise_id", ""),
- quota_type=data.get("quota_type", "CAP"),
- issue_type=data.get("issue_type", "ISSUE_MONTH"),
- issue_amount_value=data.get("issue_amount_value", "0"),
- outer_source_id=data.get("outer_source_id"),
- issue_rule_name=data.get("issue_rule_name"),
- effective_period=data.get("effective_period"),
- invalid_mode=data.get("invalid_mode"),
- share_mode=data.get("share_mode"),
- )
- log.info(f"创建自动发放规则成功: institution_id={institution_id}")
- return SuccessResponse(data=result, msg="创建自动发放规则成功")
- @InstitutionRouter.put(
- "/{institution_id}/issuerule/{issue_rule_id}",
- summary="编辑自动发放规则",
- description="编辑自动额度发放规则 (alipay.ebpp.invoice.issuerule.modify)",
- )
- async def modify_issuerule_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- issue_rule_id: Annotated[str, Path(description="发放规则ID")],
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:modify"]))],
- ) -> JSONResponse:
- result = await IssueruleService.modify_issuerule_service(
- auth=auth,
- institution_id=institution_id,
- issue_rule_id=issue_rule_id,
- enterprise_id=data.get("enterprise_id", ""),
- quota_type=data.get("quota_type"),
- issue_type=data.get("issue_type"),
- issue_amount_value=data.get("issue_amount_value"),
- issue_rule_name=data.get("issue_rule_name"),
- effective=data.get("effective"),
- effective_period=data.get("effective_period"),
- invalid_mode=data.get("invalid_mode"),
- share_mode=data.get("share_mode"),
- )
- log.info(f"编辑自动发放规则成功: issue_rule_id={issue_rule_id}")
- return SuccessResponse(data=result, msg="编辑自动发放规则成功")
- @InstitutionRouter.delete(
- "/{institution_id}/issuerule",
- summary="删除自动发放规则",
- description="删除自动额度发放规则 (alipay.ebpp.invoice.issuerule.delete)",
- )
- async def delete_issuerule_controller(
- institution_id: Annotated[str, Path(description="制度ID")],
- data: dict,
- auth: Annotated[AuthSchema, Depends(AuthPermission(["module_payment:expense:institution:issuerule:delete"]))],
- ) -> JSONResponse:
- result = await IssueruleService.delete_issuerule_service(
- auth=auth,
- institution_id=institution_id,
- issue_rule_id_list=data.get("issue_rule_id_list", []),
- enterprise_id=data.get("enterprise_id", ""),
- )
- log.info(f"删除自动发放规则成功: institution_id={institution_id}")
- return SuccessResponse(data=result, msg="删除自动发放规则成功")
|