{
  "properties": {},
  "id": "b022e3fd-e74f-6b1d-9a08-f8e9d45a3ebb",
  "script": null,
  "groupId": "25a971ba-6a80-5bb8-88f2-a0d53c183824",
  "name": "保存",
  "createTime": 1778891835481,
  "updateTime": null,
  "lock": null,
  "createBy": null,
  "updateBy": null,
  "path": "/save",
  "method": "POST",
  "parameters": [],
  "options": [],
  "requestBody": null,
  "headers": [],
  "paths": [],
  "responseBody": null,
  "description": null,
  "requestBodyDefinition": null,
  "responseBodyDefinition": null
}
================================
import 'com.itam.util.UuidUtil';

var operatorName = body.operator_name != null ? body.operator_name : 'system';
var operatorId = body.operator_id;

if (body.id != null) {
    assert body.name == null || body.name != '' : 400, '软件名称不能为空';

    var old = db.selectOne("""
        select * from itam_licenses where id = #{body.id} and deleted = 0
    """);

    db.update("""
    update itam_licenses
    <set>
        <if test="body.name != null">name = #{body.name},</if>
        <if test="body.license_key != null">license_key = #{body.license_key},</if>
        <if test="body.total_seats != null">total_seats = #{body.total_seats},</if>
        <if test="body.manufacturer_id != null">manufacturer_id = #{body.manufacturer_id},</if>
        <if test="body.category_id != null">category_id = #{body.category_id},</if>
        <if test="body.assigned_to != null">assigned_to = #{body.assigned_to},</if>
        <if test="body.min_qty != null">min_qty = #{body.min_qty},</if>
        <if test="body.licensed_to_name != null">licensed_to_name = #{body.licensed_to_name},</if>
        <if test="body.licensed_to_email != null">licensed_to_email = #{body.licensed_to_email},</if>
        <if test="body.supplier_id != null">supplier_id = #{body.supplier_id},</if>
        <if test="body.order_number != null">order_number = #{body.order_number},</if>
        <if test="body.purchase_cost != null">purchase_cost = #{body.purchase_cost},</if>
        <if test="body.purchase_date != null">purchase_date = #{body.purchase_date},</if>
        <if test="body.expiration_date != null">expiration_date = #{body.expiration_date},</if>
        <if test="body.termination_date != null">termination_date = #{body.termination_date},</if>
        <if test="body.notes != null">notes = #{body.notes},</if>
        update_time = now()
    </set>
    where id = #{body.id} and deleted = 0
    """);

    if (old != null) {
        var trackFields = ['name', 'license_key', 'total_seats', 'manufacturer_id', 'category_id', 'assigned_to', 'min_qty', 'licensed_to_name', 'licensed_to_email', 'supplier_id', 'order_number', 'purchase_cost', 'purchase_date', 'expiration_date', 'termination_date', 'notes'];
        for (field in trackFields) {
            var oldVal = old[field] != null ? '' + old[field] : null;
            var newVal = body[field] != null ? '' + body[field] : null;
            if (oldVal != newVal) {
                var changeId = UuidUtil.v7();
                db.insert("""
                insert into itam_change_record (id, asset_type, asset_id, asset_name, change_type, field_name, old_value, new_value, operator_id, operator_name, create_time)
                values (#{changeId}, 'license', #{body.id}, #{old.name}, 'update', #{field}, #{oldVal}, #{newVal}, #{operatorId}, #{operatorName}, now())
                """);
            }
        }
    }

    return { id: body.id };
} else {
    assert body.name != null && body.name != '' : 400, '软件名称不能为空';
    assert body.license_key != null && body.license_key != '' : 400, '序列号/密钥不能为空';

    var newId = UuidUtil.v7();
    db.insert("""
    insert into itam_licenses (id, name, license_key, total_seats, manufacturer_id, category_id, assigned_to, min_qty, licensed_to_name, licensed_to_email, supplier_id, order_number, purchase_cost, purchase_date, expiration_date, termination_date, notes, create_time, update_time)
    values (#{newId}, #{body.name}, #{body.license_key}, #{body.total_seats}, #{body.manufacturer_id}, #{body.category_id}, #{body.assigned_to}, #{body.min_qty}, #{body.licensed_to_name}, #{body.licensed_to_email}, #{body.supplier_id}, #{body.order_number}, #{body.purchase_cost}, #{body.purchase_date}, #{body.expiration_date}, #{body.termination_date}, #{body.notes}, now(), now())
    """);

    var changeId = UuidUtil.v7();
    db.insert("""
    insert into itam_change_record (id, asset_type, asset_id, asset_name, change_type, field_name, old_value, new_value, operator_id, operator_name, create_time)
    values (#{changeId}, 'license', #{newId}, #{body.name}, 'create', null, null, null, #{operatorId}, #{operatorName}, now())
    """);

    return { id: newId };
}
