Skip to content

Commit 1a8b049

Browse files
committed
添加新的代码规则模板
1 parent 3be0fc1 commit 1a8b049

File tree

3 files changed

+150
-10
lines changed

3 files changed

+150
-10
lines changed

src-tauri/src/storage/init.sql

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ CREATE TABLE IF NOT EXISTS sys_config (
2626

2727
-- 初始化规则示例
2828
INSERT INTO code_sample (id,name,content) VALUES
29-
('dfebebb3-d75e-4ef7-8522-4db1cd2ba63c','mybatis-plus DO对象','### 数据库DO对象
29+
('dfebebb3-d75e-4ef7-8522-4db1cd2ba63c','数据库DO对象','### 数据库DO对象
3030
1.对象名使用表名的驼峰格式,以DO结尾;
3131
2.采用mybatis-plus的entity对象规范;
3232
3.主键采用IdType.ASSIGN_ID的方式自动生成;
@@ -71,4 +71,146 @@ public class UserDTO {
7171
@Schema(description = "创建时间")
7272
private LocalDate createTime;
7373
}
74+
~~~'),
75+
('338ec139-df26-4d40-98f8-51a9a0a27ee8','Converter对象','### 接口DTO和数据库DO对象的互相转换的Converter对象
76+
1. 使用MapStruct框架,用于将数据库的DO对象转换为DTO对象,或者将DTO对象转换为数据库的DO对象;
77+
2. 对象的名称为XXXConverter(例如DeviceConverter )
78+
3. 字段不完全匹配时,仅处理匹配的字段,忽略错误
79+
举例:
80+
~~~java
81+
import org.mapstruct.factory.Mappers;
82+
83+
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE)
84+
public interface DeviceConverter {
85+
86+
DeviceConverter INSTANCE = Mappers.getMapper(DeviceConverter.class);
87+
88+
DeviceDO toDataObj(DeviceDTO dto);
89+
90+
DeviceDTO toDTO(DeviceDO deviceDO);
91+
92+
}
93+
~~~
94+
95+
'),
96+
('5c5ff59e-9d6f-4ac9-81a1-304f95698ed8','数据库mapper对象','### 数据库mapper对象
97+
1. mybatis-plus的mapper接口,用于封装对某个DO对象的数据库操作;
98+
99+
~~~java
100+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
101+
102+
public interface BaseDeviceCheckPointMapper extends BaseMapper<BaseDeviceCheckPointDO> {
103+
104+
}
105+
~~~'),
106+
('ec69f9a9-4607-4f97-bac8-ac0908f9fd94','Service对象','### Service
107+
108+
1. 对象名使用表名的驼峰格式,以Service结尾,用于封装对数据库的相关业务操作,底层使用Myabtis-Plus的Mapper对象操作数据库;
109+
2. 覆盖数据库的增,删,改,按ID查询详情,分页查询列表的相关操作;
110+
3. create,edit接收DTO对象,并利用Converter对象转换成对应的DO对象,并调用Mapper对象进行数据库操作;
111+
4. 如果当前上下文中存在PageQuery对象的相关信息,则生成分页查询方法。该方法接收一个分页查询的PageQuery对象作为参数,进行分页查询,并返回分页后的查询结果(使用mybatis-plus的分页插件);
112+
5. 所有涉及数据库的操作都需要添加事务注解;
113+
114+
115+
举例:
116+
~~~java
117+
118+
@Service
119+
@RequiredArgsConstructor
120+
public class DeviceService {
121+
122+
private final DeviceMapper deviceMapper;
123+
124+
@Transactional
125+
@Override
126+
public String create(DeviceDTO command) {
127+
DeviceDO deviceDO = DeviceConverter.INSTANCE.toDataObj(command);
128+
deviceMapper.insert(deviceDO);
129+
return deviceDO.getId();
130+
}
131+
132+
@Transactional
133+
@Override
134+
public void delete(String id) {
135+
deviceMapper.deleteById(id);
136+
}
137+
138+
@Transactional
139+
@Override
140+
public void edit(DeviceDTO command) {
141+
DeviceDO deviceDO = DeviceConverter.INSTANCE.toDataObj(command);
142+
deviceMapper.updateById(orderDO);
143+
}
144+
145+
@Override
146+
public DeviceDTO detail(String id) {
147+
DeviceDO deviceDO = deviceMapper.selectById(id);
148+
if(deviceDO==null){
149+
return null;
150+
}
151+
return DeviceConverter.INSTANCE.toDTO(deviceDO);
152+
}
153+
154+
@Override
155+
public IPage<DeviceDTO> page(DevicePageQuery query) {
156+
var queryWrapper = Wrappers.<DeviceDO>lambdaQuery()
157+
.like(StrUtil.isNotBlank(query.getOrderNo()), DeviceMaintenanceOrderDO::getOrderNo, query.getOrderNo())
158+
.like(StrUtil.isNotBlank(query.getCustomerName()), DeviceMaintenanceOrderDO::getCustomerName, query.getCustomerName())
159+
.like(StrUtil.isNotBlank(query.getDeviceName()), DeviceMaintenanceOrderDO::getDeviceName, query.getDeviceName());
160+
return deviceMapper.selectPage(new Page<>(query.getCurrent(), query.getSize()),
161+
queryWrapper).convert(DeviceMaintenanceOrderConverter.INSTANCE::toDTO);
162+
}
163+
164+
}
165+
~~~'),
166+
('cb51e146-f673-470c-bb2b-694023cbd50a','分页接口查询对象','### 用于API 分页接口的查询对象
167+
1. 封装了需要'),
168+
('43c93804-cd08-4fc1-b604-a9568491c32e','Controller对象','### 封装API接口的Controller对象
169+
1. 对象名使用表名的驼峰格式,以Controller结尾;
170+
2. 采用SpringBoot的相关技术框架,并满足RESTFUL规范的要求;
171+
3. 如果上下文中存在Service对象的定义,则根据Service对象的相关方法生成API接口, 否则不生成任何接口;
172+
4. 接口参数(DTO对象)应该采用Spring validation的规范进行校验
173+
5. 接口的API地址以“/api/${实体对象名}”开头,例如“/api/device”
174+
示例
175+
~~~java
176+
@RestController
177+
@RequestMapping("/api/device")
178+
@RequiredArgsConstructor
179+
public class DeviceController {
180+
181+
private final DeviceService deviceService;
182+
183+
/**
184+
* 创建
185+
*/
186+
@PostMapping
187+
public void create(@Validated @RequestBody DeviceDTO deviceDTO) {
188+
deviceService.create(deviceDTO);
189+
}
190+
191+
/**
192+
* 更新
193+
*/
194+
@PutMapping
195+
public void update(@Validated @RequestBody DeviceDTO deviceDTO) {
196+
deviceService.update(deviceDTO);
197+
}
198+
199+
/**
200+
* 删除
201+
*/
202+
@DeleteMapping("/{id}")
203+
public void delete(@PathVariable String id) {
204+
deviceService.delete(id);
205+
}
206+
207+
/**
208+
* 分页查询
209+
*/
210+
@PostMapping("/page")
211+
public IPage<DeviceDTO> page(@RequestBody DevicePageQuery pageQuery) {
212+
return deviceService.page(pageQuery)
213+
}
214+
215+
}
74216
~~~');

src-tauri/src/task/code_gen_task.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ impl CodeGenTask {
142142
.await?;
143143
let context = LLM_CONTEXT_BUILDER.build(&self.req).await?;
144144
self.send_log(sender, "上下文已构建完成").await?;
145-
println!("完整的上下文为:{}", context);
146145
Ok(context)
147146
}
148147

src/services/RuleService.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export const ruleService = {
1313
const codeStandards = await invoke<Rule[]>('get_all_samples');
1414
return codeStandards;
1515
} catch (e) {
16-
ElMessage.error('查询代码规范失败:' + e);
17-
throw new Error('查询代码规范失败');
16+
ElMessage.error('查询规则失败:' + JSON.stringify(e));
17+
throw e;
1818
}
1919
},
2020

@@ -28,9 +28,8 @@ export const ruleService = {
2828
});
2929
return { ...data, id: newId };
3030
} catch (e) {
31-
console.log(e);
32-
ElMessage.error('创建代码规范发生错误:' + e);
33-
throw new Error('创建代码规范失败');
31+
ElMessage.error('创建规则发生错误:' + JSON.stringify(e));
32+
throw e;
3433
}
3534
},
3635

@@ -39,7 +38,7 @@ export const ruleService = {
3938
const item = await invoke<Rule>('get_sample_by_id', { id });
4039
return item;
4140
} catch (e) {
42-
ElMessage.error('查询代码规范发生错误:' + e);
41+
ElMessage.error('查询规则发生错误:' + JSON.stringify(e));
4342
return null;
4443
}
4544
},
@@ -50,7 +49,7 @@ export const ruleService = {
5049
cs: data
5150
});
5251
} catch (e) {
53-
ElMessage.error('更新代码规范失败:' + e);
52+
ElMessage.error('更新规则失败:' + JSON.stringify(e));
5453
throw e;
5554
}
5655
},
@@ -59,7 +58,7 @@ export const ruleService = {
5958
try {
6059
return await invoke<boolean>('delete_sample', { id });
6160
} catch (e) {
62-
ElMessage.error('删除代码规范发生错误:' + e);
61+
ElMessage.error('删除规则发生错误:' + JSON.stringify(e));
6362
return false;
6463
}
6564
}

0 commit comments

Comments
 (0)