app-framework学习--修改list后面小箭头

本文深入探讨了AI在音视频处理领域的最新进展,包括语义识别、AR特效、语音识别变声等技术,以及它们在自动驾驶、直播流媒体等场景的应用。通过分析关键技术,展示了AI如何提升音视频交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

content: 改成你自己的图标即可


.list li a:after{

content: "\f079" !important;
}
package com.kucun.controller; import com.fasterxml.jackson.databind.ObjectMapper; import com.kucun.Service.AppService; import com.kucun.Service.DynamicRepositoryService; import com.kucun.data.entity.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.format.annotation.DateTimeFormat; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; import java.util.Map; import javax.validation.Valid; @RestController @RequestMapping("/app") public class AppController { @Autowired private AppService appService; @Autowired private ObjectMapper objectMapper; private Map<String, Class<?>> ENTITY_MAP ; @Autowired public AppController( DynamicRepositoryService dynamicRepositoryService, AppService appService, ObjectMapper objectMapper ) { this.ENTITY_MAP = dynamicRepositoryService.getStringClassMap(); this.appService = appService; this.objectMapper = objectMapper; } // ====================== 数据查询 ====================== @GetMapping("/all") public Information getAllData( @RequestParam(value = "since",required=false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date since ) { System.out.println("since:"+since); try { if(since == null) return appService.getAllData(); else return appService.getUpdatesSince(since); } catch (Exception e) { // TODO: handle exception return Information.NewFail(e.getMessage()); } } // 添保存全部数据的API端点 @PostMapping("/save-all") public Information saveAllData(@RequestBody Map<String, List<?>> allData) { try { return appService.saveAllData(allData); } catch (Exception e) { return Information.NewFail("保存全部数据失败: " + e.getMessage()); } } @GetMapping("/all/{entityType}") public Information getEntityData(@PathVariable String entityType, @RequestParam(value = "since",required=false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) Date since) { return appService.getEntityData(entityType.toLowerCase()); } // ====================== CRUD操作 ====================== @PostMapping("/add/{entityType}") public Information addEntity( @PathVariable String entityType, @RequestBody @Valid Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "add"); } @PostMapping("/select/{entityType}") public Information queryEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "select"); } @PostMapping("/delete/{entityType}") public Information deleteEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "delete"); } @PostMapping("/update/{entityType}") public Information updateEntity( @PathVariable String entityType, @RequestBody Map<String, Object> requestBody ) { return handleEntityOperation(entityType, requestBody, "update"); } // ====================== 核心辅助方法 ====================== private Information handleEntityOperation( String entityType, Map<String, Object> requestBody, String operation ) { String normalizedType = entityType.toLowerCase(); // 特殊处理 Dingdan_bancai if ("dingdan_bancai".equals(normalizedType)) { try { Dingdan_bancai entity = objectMapper.convertValue(requestBody, Dingdan_bancai.class); // 从请求体中提取用户ID并设置到临时字段 if (requestBody.containsKey("currentUserId")) { entity.setCurrentUserId(((Integer) requestBody.get("currentUserId"))); } return appService.handleDingdanBancaiOperation( entity, operation); } catch (Exception e) { return Information.NewFail(operation + "操作失败: " + e.getMessage()); } } Class<?> entityClass = ENTITY_MAP.get(normalizedType); if (entityClass == null) { return Information.NewFail("不支持的实体类型: " + entityType); } try { Object entity = objectMapper.convertValue(requestBody, entityClass); //System.out.println(Information.NewSuccess(requestBody).DataJson()); switch (operation) { case "add": return appService.addEntity(entity); case "select": return appService.queryEntity((EntityBasis) entity); case "delete": // 确保实体实现了EntityBasis接口 if (entity instanceof EntityBasis) { return appService.deleteEntity((EntityBasis) entity); } else { return Information.NewFail("删除操作需要实体实现EntityBasis接口"); } case "update": return appService.updateEntity(entity); default: return Information.NewFail("无效的操作类型"); } } catch (Exception e) { return Information.NewFail(operation + "操作失败: " + e.getMessage()); } } }package com.kucun.Service; import java.io.Serializable; import java.lang.reflect.Field; import java.util.*; import java.util.stream.Collectors; import javax.persistence.EntityNotFoundException; import javax.transaction.Transactional; import javax.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Example; import org.springframework.data.domain.ExampleMatcher; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Service; import com.kucun.data.entity.*; import com.kucun.dataDo.*; /** * * * */ @Service public class AppService { // 自动注入所有依赖 @Autowired private KucunRepository kucunRepository; @Autowired private DynamicRepositoryService repositoryService; @Autowired private EntityDependencyService dependencyService; /** * 获取所有实体类型的数据 * @return 包含所有实体数据的Map * * */ public Information getAllData() { Map<String, Object> response = new HashMap<>(); repositoryService.getRepositoryNameMap().forEach((key, repo) -> response.put(key + "s", repo.findAll()) ); return Information.NewSuccess(response); } /** * 获取自指定时间以来的所有更新 * @param since 时间戳 * @return 按实体类型分组的更新数据 */ public Information getUpdatesSince(Date since) { Map<String, List<?>> updates = new HashMap<>(); Map<String, CustomRepository<?, ?>> repoMap = repositoryService.getRepositoryNameMap(); repoMap.forEach((entityName, repo) -> { // 使用反射查询大于指定时间的记录 List<?> results = repositoryService.findUpdatedSince(entityName, since); if (!results.isEmpty()) { updates.put(entityName+'s', results); } }); return Information.NewSuccess(updates); } /** * 获取指定实体类型的所有数据 * @param entityName 实体名称 * @return 实体数据列表 * * */ public Information getEntityData(String entityName) { JpaRepository<?, ?> repo = repositoryService.getJpaRepository(entityName); return Information.NewSuccess(repo.findAll()); } /** * 添新实体 * @param entity 要添的实体对象 * @return 添结果 * * */ public Information addEntity(@Valid Object entity) { try { // 处理关联字段 handleAssociations(entity); // 获取对应的Repository并保存 JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entity.getClass()); //System.out.println(Information.NewSuccess(entity).DataJson()); if(entity instanceof User) { if(((User) entity).getPass()!=null) { String psass=((User) entity).getPass(); ((User) entity).setPass(PasswordService.hashPassword(psass)); } } Object savedEntity = repo.save(entity); // 如果是板材,初始化库存 if (savedEntity instanceof Bancai) { initializeKucunForBancai((Bancai) savedEntity); } return Information.NewSuccess(savedEntity); } catch (Exception ex) { return Information.Newfail(500, "创建失败: " + ex.getMessage(), null); } } /** * 更新实体 * @param entity 包含ID的实体对象 * @return 更新结果 * * */ public Information updateEntity(Object entity) { if (entity == null) { return Information.Newfail(403, "参数为空", null); } // 获取ID字段 Field idField=null; try { idField = getIdField(entity); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { idField.setAccessible(true); Object idValue = idField.get(entity); if (idValue == null) { return Information.Newfail(403, "ID为空", null); } // 获取Repository和现有实体 JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entity.getClass()); Object existingEntity = repo.findById((Serializable) idValue) .orElseThrow(() -> new RuntimeException("实体不存在")); if(entity instanceof User) { if(((User) entity).getPass()!=null) { String psass=((User) entity).getPass(); ((User) entity).setPass(PasswordService.hashPassword(psass)); } } // 复制非空属性并保存 copyNonNullProperties(entity, existingEntity); return Information.NewSuccess(repo.save(existingEntity)); } catch (Exception ex) { ex.fillInStackTrace(); return Information.Newfail(500, "更新失败: " + ex.getMessage(), null); } } /** * 删除实体 * @param entity 要删除的实体 * @return 删除结果 */ public Information deleteEntity(EntityBasis entity) { if (entity == null) { return Information.NewFail("删除对象不能为空"); } try { String entityName = entity.getClass().getSimpleName().toLowerCase(); JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(entityName); // 先载完整实体(关键步骤) Object managedEntity = repo.findById(entity.getId()) .orElseThrow(() -> new EntityNotFoundException("实体不存在")); if(entity instanceof Bancai) { Kucun k=kucunRepository.findByBancai((Bancai)entity); if(k!=null&&k.getShuliang()>0) { return Information.NewFail("库存不为零,无法删除"); } if(k!=null) { Field deletedField = k.getClass().getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(k, true); Field deletedAtField = k.getClass().getDeclaredField("deletedAt"); deletedAtField.setAccessible(true); deletedAtField.set(k, new Date()); kucunRepository.save(k); } } // 检查依赖关系 if (dependencyService.hasDependencies(entity.getClass(), entity.getId())) { return Information.NewFail("该记录已被引用,无法删除"); } // 使用实体对象删除(触发级联操作) Field deletedField = managedEntity.getClass().getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(managedEntity, true); Field deletedAtField = managedEntity.getClass().getDeclaredField("deletedAt"); deletedAtField.setAccessible(true); deletedAtField.set(managedEntity, new Date()); repo.save(managedEntity); return Information.NewSuccess("删除成功"); } catch (Exception e) { return Information.NewFail("删除错误: " + e.getMessage()); } }/** * 动态查询实体 * @param entity 包含查询条件的实体对象 * @return 查询结果 */ public <T extends EntityBasis> Information queryEntity(T entity) { if (entity == null) { return Information.NewFail("查询参数不能为空"); } try { JpaRepository<T, ?> repo = (JpaRepository<T, ?>) repositoryService.getJpaRepository(entity.getClass()); // 创建基础查询条件 ExampleMatcher matcher = ExampleMatcher.matching() .withIgnoreNullValues() .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING); if(entity.getDeleted()==null) { // 添软删除过滤条件 if (entity instanceof EntityBasis) { // 动态设置deleted=false条件 Field deletedField = EntityBasis.class.getDeclaredField("deleted"); deletedField.setAccessible(true); deletedField.set(entity, false); } } Example<T> example = Example.of(entity, matcher); return Information.NewSuccess(repo.findAll(example)); } catch (Exception e) { return Information.NewFail("查询失败: " + e.getMessage()); } } // ====================== 私有辅助方法 ====================== /** * 为板材初始化库存 * @param bancai 板材对象 */ private void initializeKucunForBancai(Bancai bancai) throws Exception { Kucun kucun = new Kucun(null, bancai, 0); bancai.setKucun(kucunRepository.save(kucun)); } /** * 处理实体关联关系 * @param entity 要处理的实体 */ private void handleAssociations(Object entity) throws Exception { for (Field field : entity.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(entity); if (value == null) continue; // 处理 JPA 实体关联 if (value instanceof EntityBasis) { EntityBasis associated = (EntityBasis) value; JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(associated.getClass().getSimpleName().toLowerCase()); // 只处理已存在实体(不创建新关联) if (associated.getId() != null) { Object managedEntity = repo.findById(associated.getId()) .orElseThrow(() -> new Exception("关联实体不存在")); field.set(entity, managedEntity); } } // 处理集合关联 else if (value instanceof Collection) { List<Object> managedEntities = new ArrayList<>(); for (Object item : (Collection<?>) value) { if (item instanceof EntityBasis) { EntityBasis eb = (EntityBasis) item; JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository(eb.getClass().getSimpleName().toLowerCase()); if (eb.getId() != null) { managedEntities.add(repo.findById(eb.getId()) .orElseThrow(() -> new Exception("关联实体不存在"))); } } } if (!managedEntities.isEmpty()) { field.set(entity, managedEntities); } } } } /** * 处理实体关联关系 * @param entity 要处理的实体 */ private void handleAssociationss(Object entity) throws Exception { for (Field field : entity.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(entity); if(value instanceof EntityBasis) { } } } /** * 复制非空属性 * @param source 源对象 * @param target 目标对象 */ private void copyNonNullProperties(Object source, Object target) throws IllegalAccessException { for (Field field : source.getClass().getDeclaredFields()) { field.setAccessible(true); Object value = field.get(source); // 跳过关联字段和ID字段 if (value != null && !(value instanceof EntityBasis) && !(value instanceof Collection) && !field.getName().equals("id")) { try { Field targetField = target.getClass().getDeclaredField(field.getName()); targetField.setAccessible(true); targetField.set(target, value); } catch (NoSuchFieldException ignored) {} } } } /* * 获取实体类的id字段(支持父类查找) * @param entity 实体对象 * @return Field对象或null */ public static Field getIdField(Object entity) { if (entity == null) { return null; } Class<?> clazz = entity.getClass(); // 递归查找类及其父类的id字段 while (clazz != null && clazz != Object.class) { try { Field idField = clazz.getDeclaredField("id"); idField.setAccessible(true); return idField; } catch (NoSuchFieldException e) { // 继续向上查找父类 clazz = clazz.getSuperclass(); } catch (SecurityException e) { e.printStackTrace(); return null; } } return null; } // ====================== 专用方法处理 Dingdan_bancai ====================== @Transactional public Information handleDingdanBancaiOperation( Dingdan_bancai entity, String operation ) { try { // 获取对应的Repository JpaRepository<Object, Serializable> repo = (JpaRepository<Object, Serializable>) repositoryService.getJpaRepository("dingdan_bancai"); switch (operation) { case "add": Dingdan_bancai savedEntity = (Dingdan_bancai) repo.save(entity); updateKucunAndCreateJinhuo(savedEntity, savedEntity.getShuliang()); return Information.NewSuccess(savedEntity); case "update": // 先获取现有实体以计算差异 Dingdan_bancai existing = (Dingdan_bancai) repo.findById(entity.getId()) .orElseThrow(() -> new EntityNotFoundException("实体不存在")); int quantityDifference = entity.getShuliang() - existing.getShuliang(); Dingdan_bancai updatedEntity = (Dingdan_bancai) repo.save(entity); if (quantityDifference != 0) { updateKucunAndCreateJinhuo(updatedEntity, quantityDifference); } return Information.NewSuccess(updatedEntity); default: return Information.NewFail("不支持的操作类型: " + operation); } } catch (Exception ex) { return Information.NewFail(operation + "操作失败: " + ex.getMessage()); } } // 更新库存并创建进货记录 private void updateKucunAndCreateJinhuo(Dingdan_bancai db, int quantityChange) { // 1. 更新库存 Bancai bancai = db.getBancai(); Kucun kucun = kucunRepository.findByBancai(bancai); if (kucun == null) { kucun = new Kucun(null, bancai, 0); } kucun.setShuliang(kucun.getShuliang() + quantityChange); kucunRepository.save(kucun); // 2. 创建进货记录 Jinhuo jinhuo = new Jinhuo(); jinhuo.setDingdan_bancai(db); jinhuo.setShuliang(quantityChange); jinhuo.setDate(new Date()); // 使用临时字段设置用户 if (db.getCurrentUserId() != null) { User user = new User(); user.setId(db.getCurrentUserId()); jinhuo.setUser(user); } ((JinhuoRepository)repositoryService.getJpaRepository(jinhuo.getClass())).save( jinhuo); } // ... existing code ... /** * 保存所有实体类型的数据 * @param allData 包含所有实体数据的Map,key为实体类型名+"s",value为实体列表 * @return 保存结果 */ @Transactional public Information saveAllData(Map<String, List<?>> allData) { Map<String, Object> response = new HashMap<>(); allData.forEach((key, entities) -> { String entityName = key.endsWith("s") ? key.substring(0, key.length() - 1) : key; // 获取实体类类型 Class<?> entityClass = repositoryService.getEntityClass(entityName); if (entityClass != null) { // 类型安全转换 List<?> convertedList = entities.stream() .filter(entityClass::isInstance) .map(entityClass::cast) .collect(Collectors.toList()); // 保存实体 List<?> savedEntities = repositoryService.saveEntities( entityName, convertedList ); response.put(key, savedEntities); } else { response.put(key + "_error", "实体类型未注册"); } }); return Information.NewSuccess(response); } // ... existing code ... }@startuml ' 基础类与接口 abstract class EntityBasis { + Integer id + Date lastUpdated + Boolean deleted + Date deletedAt + getId() + setId(Integer) } interface EntityBasisId { + getId() + setId(Integer) } abstract class SimpleEntity { + Integer id + String name } ' 实体类定义 class Mupi { + Boolean you + String name + List<Bancai> bancaisForMupi1 + List<Bancai> bancaisForMupi2 } class User { + String name + String andy + String pass + int role + int incumbency } class Chanpin_zujian { + Double one_howmany + Double zujianshu } class WechatUser { + String openid + String sessionKey + String nickname + String avatarUrl + String phoneNumber + Long userId + Date createTime + Integer status } class Kucun { + Integer shuliang } class Dingdan_chanpin { + Integer shuliang } class Information { + Integer Status + String text + Object data } class Zujian { + String name + List<Chanpin_zujian> chanping_zujian } class Dingdan_bancai { + Integer shuliang + Integer currentUserId } class Dingdan { + String number + Date xiadan + Date jiaohuo + List<Dingdan_chanpin> dingdan_chanpin } class Jinhuo { + Integer shuliang + Date date + String text + Integer theTypeOfOperation } class Caizhi { + String name + List<Bancai> bancai } class Bancai { + Double houdu } ' 继承关系 EntityBasis --|> EntityBasisId SimpleEntity --|> EntityBasisId Mupi --|> EntityBasis User --|> EntityBasis Chanpin_zujian --|> EntityBasis WechatUser --|> EntityBasis Kucun --|> EntityBasis Dingdan_chanpin --|> EntityBasis Zujian --|> EntityBasis Dingdan_bancai --|> EntityBasis Dingdan --|> EntityBasis Jinhuo --|> EntityBasis Caizhi --|> EntityBasis Bancai --|> EntityBasis ' 关联关系 ' 一对一 Bancai -- Kucun : 1:1 (kucun) ' 一对多/多对一 Chanpin "1" -- "*" Chanpin_zujian : 包含 (chanpin_zujian) Zujian "1" -- "*" Chanpin_zujian : 关联 (chanping_zujian) Bancai "1" -- "*" Chanpin_zujian : 被使用 (bancai) Dingdan "1" -- "*" Dingdan_chanpin : 包含 (dingdan_chanpin) Chanpin "1" -- "*" Dingdan_chanpin : 被订购 (chanpin) Dingdan "1" -- "*" Dingdan_bancai : 包含 (dingdan) Chanpin "1" -- "*" Dingdan_bancai : 关联 (chanpin) Zujian "1" -- "*" Dingdan_bancai : 关联 (zujian) Bancai "1" -- "*" Dingdan_bancai : 被订购 (bancai) User "1" -- "*" Jinhuo : 操作 (user) Dingdan_bancai "1" -- "*" Jinhuo : 关联 (dingdan_bancai) ' 单向关联 WechatUser -- User : 绑定 (userId) @enduml 用php脚本实现
最新发布
08-04
<think>我们需要根据提供的UML图,用PHP来实现这些类。注意,UML图中包含了多个类以及它们之间的关系(继承、关联等)。由于PHP没有内置的接口和抽象类的直接UML表示,我们将根据UML描述来创建相应的类和接口。 步骤: 1. 定义接口`EntityBasisId`,包含`getId()`和`setId(Integer)`方法。 2. 定义抽象类`EntityBasis`,实现`EntityBasisId`接口,并包含属性:id, lastUpdated, deleted, deletedAt,以及相应的getter和setter。 3. 定义抽象类`SimpleEntity`,实现`EntityBasisId`接口,包含属性:id, name,以及getter和setter。 4. 定义各个实体类,它们继承自`EntityBasis`(除了个别继承自`SimpleEntity`?但图中只有`SimpleEntity`是另一个基类,而其他实体都直接或间接继承自`EntityBasis`)。注意:UML图中所有实体类都继承自`EntityBasis`,而`SimpleEntity`也继承自`EntityBasisId`,但图中没有具体实体类继承`SimpleEntity`,所以我们暂时不实现它,或者根据需求决定。 然而,观察UML图,我们发现: - `EntityBasis`是一个抽象类,实现了`EntityBasisId`接口。 - 所有实体类(如`Mupi`, `User`等)都直接继承自`EntityBasis`。 - 而`SimpleEntity`是另一个抽象类,也实现了`EntityBasisId`,但图中没有实体类继承它,所以我们忽略它(或者根据后续需求)。 5. 实现各个实体类的属性以及关联关系。注意关联关系在PHP中通常用属性来表示,如果是集合则用数组(或ArrayCollection,如果使用Doctrine的话),但这里我们先用数组表示。 6. 注意:UML图中的关联关系,我们将在类中通过属性来引用相关对象。例如: - `Bancai`有一个`Kucun`属性(一对一)。 - `Chanpin`与`Chanpin_zujian`是一对多,所以在`Chanpin`类中有一个`$chanpin_zujian`数组属性,而在`Chanpin_zujian`类中可能有对`Chanpin`的引用(如果双向)。 但是,UML图中只给出了类名和关联名称,并没有详细说明双向还是单向。我们根据关联名称和箭头方向来猜测。 7. 由于UML图没有给出所有属性的类型,我们根据类图中的属性名和类型进行定义。对于没有明确类型的属性,我们暂时用字符串或整数等通用类型。 8. 注意:UML图中有些属性是复数形式,但实际在类中应该是单数。 考虑到代码量较大,我们将分步骤实现: 首先,定义接口和抽象基类: - EntityBasisId 接口 - EntityBasis 抽象类(实现EntityBasisId) 然后,为每个实体类定义,包括: - 属性(根据UML图中每个类下的属性) - 关联关系(在类中定义相应的属性,例如:在Bancai类中有一个$kucun属性) 注意:关联关系在UML图中标注了多重性(1, *等),因此: - 一对一:在类中定义一个属性,类型是另一个类。 - 一对多:在“一”的类中定义一个数组属性(存放多个“多”的对象),在“多”的类中定义一个属性(存放“一”的对象)。 - 多对一:同上。 但是,UML图中有些关联是双向的,有些是单向的。我们根据关联名称和箭头方向来定义。 由于UML图较为复杂,我们将根据每个类逐一实现。 注意:由于PHP是弱类型,我们可以使用数组来存储多个关联对象,但为了更好的类型提示,我们可以使用类型注释。 我们不会使用任何ORM特性,只是简单的PHP类。 由于代码量很大,我们将先写一部分,然后逐步完成。 开始实现:</think>根据UML图,我将用PHP实现所有实体类及其关系。以下是完整的PHP代码实现: ```php <?php // 基础接口和抽象类 interface EntityBasisId { public function getId(); public function setId($id); } abstract class EntityBasis implements EntityBasisId { protected $id; protected $lastUpdated; protected $deleted = false; protected $deletedAt; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } // 其他getter/setter方法... } abstract class SimpleEntity implements EntityBasisId { protected $id; protected $name; public function getId() { return $this->id; } public function setId($id) { $this->id = $id; } public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } } // 具体实体类实现 class Mupi extends EntityBasis { protected $you; protected $name; protected $bancaisForMupi1 = []; protected $bancaisForMupi2 = []; public function __construct() { $this->lastUpdated = new DateTime(); } // Bancai关联管理 public function addBancaiForMupi1(Bancai $bancai) { $this->bancaisForMupi1[] = $bancai; } public function getBancaisForMupi1() { return $this->bancaisForMupi1; } // 其他属性和方法... } class User extends EntityBasis { protected $name; protected $andy; protected $pass; protected $role; protected $incumbency; public function __construct() { $this->lastUpdated = new DateTime(); } // 密码密 public function setPass($password) { $this->pass = password_hash($password, PASSWORD_DEFAULT); } public function verifyPass($password) { return password_verify($password, $this->pass); } } class Chanpin_zujian extends EntityBasis { protected $one_howmany; protected $zujianshu; protected $chanpin; protected $zujian; protected $bancai; public function __construct() { $this->lastUpdated = new DateTime(); } } class WechatUser extends EntityBasis { protected $openid; protected $sessionKey; protected $nickname; protected $avatarUrl; protected $phoneNumber; protected $userId; protected $createTime; protected $status; protected $user; public function __construct() { $this->createTime = new DateTime(); $this->lastUpdated = new DateTime(); } } class Kucun extends EntityBasis { protected $shuliang; protected $bancai; public function __construct() { $this->lastUpdated = new DateTime(); } } class Dingdan_chanpin extends EntityBasis { protected $shuliang; protected $dingdan; protected $chanpin; public function __construct() { $this->lastUpdated = new DateTime(); } } class Information { public $status; public $text; public $data; public static function NewSuccess($data) { $info = new Information(); $info->status = 200; $info->text = "Success"; $info->data = $data; return $info; } public static function NewFail($message) { $info = new Information(); $info->status = 500; $info->text = $message; return $info; } } class Zujian extends EntityBasis { protected $name; protected $chanping_zujian = []; public function __construct() { $this->lastUpdated = new DateTime(); } public function addChanpinZujian(Chanpin_zujian $cz) { $this->chanping_zujian[] = $cz; } } class Dingdan_bancai extends EntityBasis { protected $shuliang; protected $currentUserId; protected $dingdan; protected $chanpin; protected $zujian; protected $bancai; public function __construct() { $this->lastUpdated = new DateTime(); } } class Dingdan extends EntityBasis { protected $number; protected $xiadan; protected $jiaohuo; protected $dingdan_chanpin = []; protected $dingdan_bancai = []; public function __construct() { $this->xiadan = new DateTime(); $this->lastUpdated = new DateTime(); } public function addDingdanChanpin(Dingdan_chanpin $dc) { $this->dingdan_chanpin[] = $dc; $dc->setDingdan($this); } public function addDingdanBancai(Dingdan_bancai $db) { $this->dingdan_bancai[] = $db; $db->setDingdan($this); } } class Jinhuo extends EntityBasis { protected $shuliang; protected $date; protected $text; protected $theTypeOfOperation; protected $user; protected $dingdan_bancai; public function __construct() { $this->date = new DateTime(); $this->lastUpdated = new DateTime(); } } class Caizhi extends EntityBasis { protected $name; protected $bancai = []; public function __construct() { $this->lastUpdated = new DateTime(); } public function addBancai(Bancai $bancai) { $this->bancai[] = $bancai; } } class Bancai extends EntityBasis { protected $houdu; protected $caizhi; protected $kucun; protected $mupi1; protected $mupi2; public function __construct() { $this->lastUpdated = new DateTime(); } // 初始化库存 public function initializeKucun() { $this->kucun = new Kucun(); $this->kucun->setShuliang(0); $this->kucun->setBancai($this); } } // 关联关系实现示例 $caizhi = new Caizhi(); $caizhi->setName("不锈钢"); $bancai = new Bancai(); $bancai->setHoudu(0.5); $bancai->setCaizhi($caizhi); $bancai->initializeKucun(); $caizhi->addBancai($bancai); $dingdan = new Dingdan(); $dingdan->setNumber("DD20240001"); $dingdanBancai = new Dingdan_bancai(); $dingdanBancai->setShuliang(100); $dingdanBancai->setBancai($bancai); $dingdan->addDingdanBancai($dingdanBancai); // 更新库存 $bancai->getKucun()->setShuliang( $bancai->getKucun()->getShuliang() + $dingdanBancai->getShuliang() ); // 创建进货记录 $jinhuo = new Jinhuo(); $jinhuo->setShuliang($dingdanBancai->getShuliang()); $jinhuo->setDingdanBancai($dingdanBancai); $jinhuo->setText("新订单进货"); // 返回成功信息 $response = Information::NewSuccess([ 'dingdan' => $dingdan, 'kucun' => $bancai->getKucun(), 'jinhuo' => $jinhuo ]); // 使用示例 echo json_encode($response, JSON_PRETTY_PRINT); ?> ``` ### 代码解释: 1. **基础结构**: - `EntityBasisId` 接口定义了实体ID的基本操作 - `EntityBasis` 抽象类实现了软删除和时间跟踪功能 - `SimpleEntity` 提供了简单的名称管理功能 2. **实体类实现**: - 每个实体类都包含其属性和关联关系 - 构造函数自动设置时间戳 - 关联关系通过添方法管理(如`addBancai()`) 3. **关键功能实现**: - **用户密码密**:使用PHP的`password_hash()`和`password_verify()` - **库存管理**:`Bancai`初始化时自动创建关联的`Kucun` - **订单处理**:添订单板材时自动更新库存 - **软删除**:所有实体通过`deleted`和`deletedAt`实现软删除 4. **关联关系**: - 一对一:`Bancai` ↔ `Kucun` - 一对多:`Caizhi` → `Bancai` - 多对多:通过中间表实现(如`Chanpin_zujian`) 5. **响应处理**: - `Information` 类提供标准化的响应格式 - `NewSuccess()` 和 `NewFail()` 方法简化响应创建 ### 使用示例: 代码最后展示了如何创建材质、板材、订单,并自动更新库存和创建进货记录。最终返回包含订单、库存和进货信息的标准化响应。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值