【功能开发】程序文件管理——后端API接口(二)
controller
@RestController
@RequestMapping("/ProgramFile")
public class BuProgramFileController extends BaseController{
@Autowired
private IBuProgramFileService programFileService;
@Autowired
private IBbCompanyService companyService;
/**
* 查询程序文件列表
*/
@GetMapping("/list")
public TableDataInfo list(BuProgramFile buProgramFile){
startPage();
List<BuProgramFile> list = programFileService.selectProgramFileList(buProgramFile);
return getDataTable(list);
}
/**
* 获取程序文件详细信息
*/
@GetMapping(value = { "/", "/{programId}" })
public AjaxResult getInfo(@PathVariable(value = "programId",required = false) Long programId){
AjaxResult ajax = AjaxResult.success();
ajax.put("companyOptions", companyService.selectCompanyAll());
if (StringUtils.isNotNull(programId)){
BuProgramFile programFile = programFileService.selectBuProgramFileByProgramId(programId);
ajax.put("data",programFile);
}
return ajax;
}
/**
* 新增程序文件
*/
@PostMapping
public AjaxResult add(@RequestBody BuProgramFile programFile){
return toAjax(programFileService.insertBuProgramFile(programFile));
}
/**
* 修改程序文件
*/
@PutMapping
public AjaxResult edit(@RequestBody BuProgramFile buProgramFile){
return toAjax(programFileService.updateBuProgramFile(buProgramFile));
}
/**
* 删除程序文件
*/
@DeleteMapping("/{programIds}")
public AjaxResult remove(@PathVariable Long[] programIds){
return toAjax(programFileService.deleteBuProgramFileByProgramIds(programIds));
}
/**
* 删除程序文件上传中的附件
* 在点击删除附件后,不点击确认就退出,会出发BUG:
* (1)文件服务器的附件被删除
* (2) 但是该程序清单中的物料编号,切削时间,理论加工时间,文件url,程序名依然存在。
* (3) 此时再次点击编辑,由于文件名是从程序名取出,而程序名依然存在,但是文件不存在,再删除就会报错
* 解决方案:文件删除后,需要将该记录的file_url和file_name置为空,同时前台文件名关联file_name字段
*/
@PutMapping("/deleteFile/{programId}")
public AjaxResult deleteFile(@PathVariable("programId") Long programId){
return toAjax(programFileService.deleteFileByProgramId(programId));
}
companyService.selectCompanyAll()的SQL语句
<resultMap id="companyResult" type="java.util.HashMap">
<result property="companyId" column="CompanyID" />
<result property="companyName" column="CompanyName" />
<result property="companyShortName" column="CompanyShortName" />
</resultMap>
<select id="selectCompanyAll" resultMap="companyResult">
select CompanyID, CompanyName, CompanyShortName from BB_Company where IsDel = 0
</select>
其实这里没有必要新增一个 resultMap id="companyResult"
。
直接把select语句的 resultMap="companyResult"
改为 resultType="Map"
这样查询的column自动映射成map中的key,值则为value。之所以单独写个 resultMap ,是因为实体类和数据库的列名不一致。
我这里并没有把 resultMap 映射成实体类,而是直接返回Map,可以改成 resultType="Map"
。
Service
BuProgramFileServiceImpl
@Service
public class BuProgramFileServiceImpl implements IBuProgramFileService{
@Autowired
private BuProgramFileMapper programFileMapper;
@Autowired
private BuProgramPartMapper programPartMapper;
/**
* 查询程序文件
* @param programId 程序文件主键
*/
@Override
public BuProgramFile selectBuProgramFileByProgramId(Long programId){
return programFileMapper.selectBuProgramFileByProgramId(programId);
}
/**
* 查询程序文件列表
* @param buProgramFile 程序文件
*/
@Override
public List<BuProgramFile> selectProgramFileList(BuProgramFile buProgramFile){
return programFileMapper.selectProgramFileList(buProgramFile);
}
/**
* 新增程序文件
* @param programFile 程序文件
*/
@Override
@Transactional
public int insertBuProgramFile(BuProgramFile programFile){
// 新纪录版本默认为1
programFile.setVersion(1);
BuProgramFile result = programFileMapper.selectBuProgramFileByProgramName(programFile.getProgramName());
if(StringUtils.isNotNull(result)){
programFile.setVersion(result.getVersion()+1);
}
programFile.setCreateTime(System.currentTimeMillis());
Integer personId = Convert.toInt(ServletUtils.getHeaderByName("PersonID"),0);
programFile.setCreateBy(personId);
programFileMapper.insertBuProgramFile(programFile);
// 获取物料编号
String partName = programFile.getPartName();
Long programId = programFile.getProgramId();
BuProgramPart programPart = new BuProgramPart();
programPart.setProgramId(programId);
programPart.setPartName(partName);
return programPartMapper.insertBuProgramPart(programPart);
}
@Override
public int deleteFileByProgramId(Long programId) {
return programFileMapper.deleteFileByProgramId(programId);
}
/**
* 修改程序文件
* @param programFile 程序文件
*/
@Override
public int updateBuProgramFile(BuProgramFile programFile){
programFile.setUpdateTime(System.currentTimeMillis());
Integer personId = Convert.toInt(ServletUtils.getHeaderByName("PersonID"),0);
programFile.setUpdateBy(personId);
programFileMapper.updateBuProgramFile(programFile);
// 获取物料编号
String partName = programFile.getPartName();
Long programId = programFile.getProgramId();
BuProgramPart programPart = new BuProgramPart();
programPart.setProgramId(programId);
programPart.setPartName(partName);
return programPartMapper.updateBuProgramPart(programPart);
}
/**
* 批量删除程序文件
* @param programIds 需要删除的程序文件主键
*/
@Override
public int deleteBuProgramFileByProgramIds(Long[] programIds){
programFileMapper.deleteBuProgramFileByProgramIds(programIds);
return programPartMapper.deleteBuProgramPartByProgramIds(programIds);
}
/**
* 删除程序文件信息
* @param programId 程序文件主键
*/
@Override
public int deleteBuProgramFileByProgramId(Long programId){
programFileMapper.deleteBuProgramFileByProgramId(programId);
return programPartMapper.deleteBuProgramPartByProgramId(programId);
}
mapper
BuProgramFileMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.ruoyi.digital.mapper.BuProgramFileMapper">
<resultMap type="BuProgramFile" id="BuProgramFileResult">
<result property="programId" column="program_id" />
<result property="programCode" column="program_code" />
<result property="programName" column="program_name" />
<result property="version" column="version" />
<result property="companyId" column="company_id" />
<result property="companyName" column="CompanyName" />
<result property="fileUrl" column="file_url" />
<result property="fileName" column="file_name" />
<result property="processCode" column="process_code" />
<result property="status" column="status" />
<result property="processName" column="process_name" />
<result property="createBy" column="create_by" />
<result property="matchTime" column="match_time" />
<result property="createTime" column="create_time" />
<result property="cutTime" column="cut_time" />
<result property="updateBy" column="update_by" />
<result property="updateTime" column="update_time" />
</resultMap>
<sql id="selectBuProgramFileVo">
SELECT a.program_id, a.program_name, a.program_code, a.version, a.company_id, c.CompanyName,
b.partName,a.process_code, a.process_name, a.match_time, a.cut_time, a.file_url,a.file_name,
a.status,a.create_by,a.create_time, a.update_by, a.update_time FROM BU_Program_File a
LEFT JOIN ( SELECT program_id, GROUP_CONCAT(part_name) AS partName
FROM BU_Program_Part GROUP BY program_id) b ON a.program_id = b.program_id
LEFT JOIN BB_Company c ON a.company_id = c.CompanyID
</sql>
<select id="selectProgramFileList" parameterType="BuProgramFile" resultMap="BuProgramFileResult">
<include refid="selectBuProgramFileVo"/>
<where>
a.status = '0'
<if test="programName != null and programName != ''"> and a.program_name like concat('%', #{programName}, '%')</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and a.create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
</where>
</select>
<select id="selectBuProgramFileByProgramId" parameterType="Long" resultMap="BuProgramFileResult">
<include refid="selectBuProgramFileVo"/>
where a.program_id = #{programId}
</select>
<select id="selectBuProgramFileByProgramName" parameterType="String" resultMap="BuProgramFileResult">
<include refid="selectBuProgramFileVo"/>
where a.program_name = #{programName} order by version desc limit 1
</select>
<insert id="insertBuProgramFile" parameterType="BuProgramFile" useGeneratedKeys="true" keyProperty="programId">
insert into BU_Program_File
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="programCode != null and programCode != ''">program_code,</if>
<if test="programName != null and programName != ''">program_name,</if>
<if test="version != null">version,</if>
<if test="companyId != null">company_id,</if>
<if test="fileUrl != null">file_url,</if>
<if test="fileName != null">file_name,</if>
<if test="processCode != null">process_code,</if>
<if test="status != null and status != ''">status,</if>
<if test="processName != null">process_name,</if>
<if test="createBy != null">create_by,</if>
<if test="matchTime != null">match_time,</if>
<if test="createTime != null">create_time,</if>
<if test="cutTime != null">cut_time,</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="programCode != null and programCode != ''">#{programCode},</if>
<if test="programName != null and programName != ''">#{programName},</if>
<if test="version != null">#{version},</if>
<if test="companyId != null">#{companyId},</if>
<if test="fileUrl != null">#{fileUrl},</if>
<if test="fileName != null">#{fileName},</if>
<if test="processCode != null">#{processCode},</if>
<if test="status != null and status != ''">#{status},</if>
<if test="processName != null">#{processName},</if>
<if test="createBy != null">#{createBy},</if>
<if test="matchTime != null">#{matchTime},</if>
<if test="createTime != null">#{createTime},</if>
<if test="cutTime != null">#{cutTime},</if>
</trim>
</insert>
<update id="updateBuProgramFile" parameterType="BuProgramFile">
update BU_Program_File
<trim prefix="SET" suffixOverrides=",">
<if test="programCode != null and programCode != ''">program_code = #{programCode},</if>
<if test="programName != null and programName != ''">program_name = #{programName},</if>
<if test="version != null">version = #{version},</if>
<if test="companyId != null">company_id = #{companyId},</if>
<if test="fileUrl != null">file_url = #{fileUrl},</if>
<if test="fileName != null">file_name = #{fileName},</if>
<if test="processCode != null">process_code = #{processCode},</if>
<if test="status != null and status != ''">status = #{status},</if>
<if test="processName != null">process_name = #{processName},</if>
<if test="matchTime != null">match_time = #{matchTime},</if>
<if test="cutTime != null">cut_time = #{cutTime},</if>
<if test="updateBy != null">update_by = #{updateBy},</if>
<if test="updateTime != null">update_time = #{updateTime},</if>
</trim>
where program_id = #{programId}
</update>
<update id="deleteFileByProgramId" parameterType="Long">
update BU_Program_File set file_url = '' , file_name = '' where program_id = #{programId}
</update>
<delete id="deleteBuProgramFileByProgramId" parameterType="Long">
update BU_Program_File set status = '1' where program_id = #{programId}
</delete>
<delete id="deleteBuProgramFileByProgramIds" parameterType="Long">
update BU_Program_File set status = '1' where program_id in
<foreach item="programId" collection="array" open="(" separator="," close=")">
#{programId}
</foreach>
</delete>
</mapper>