| | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.DeviceSpareMapper; |
| | | import com.whyc.pojo.web_site.DeviceSpare; |
| | | import com.whyc.util.CommonUtil; |
| | | import com.whyc.util.ThreadLocalUtil; |
| | | import org.apache.commons.lang.StringUtils; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | |
| | | return new Response<List<DeviceSpare>>().set(1, mapper.selectList(query)); |
| | | } |
| | | |
| | | public Response add(DeviceSpare spare) { |
| | | public Response add(DeviceSpare spare, List<MultipartFile> file) throws IOException { |
| | | //对file进行处理,保存到文件夹中 |
| | | //对存储路径进行定义 |
| | | Date now = new Date(); |
| | | String timeFormat = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM_DD_HH_MM_SS_UNION, now); |
| | | String dirMonth = ThreadLocalUtil.format(ThreadLocalUtil.TIME_YYYY_MM, now); |
| | | String fileDirPath = CommonUtil.getRootFile() + "deviceSpare" + File.separator + dirMonth; |
| | | File fileDir = new File(fileDirPath); |
| | | //如果文件夹不存在则创建 |
| | | if (!fileDir.exists()) { |
| | | fileDir.mkdirs(); |
| | | } |
| | | StringBuilder pictureUrlSb = new StringBuilder(); |
| | | if (file != null && file.size() > 0) { |
| | | for (int i = 0; i < file.size(); i++) { |
| | | MultipartFile multipartFile = file.get(i); |
| | | String fileName = multipartFile.getOriginalFilename(); |
| | | //将fileName中可能存在的,去掉 |
| | | fileName = fileName.replace(",",""); |
| | | String filePath = fileDirPath + File.separator + timeFormat+"_"+fileName; |
| | | |
| | | multipartFile.transferTo(new File(filePath)); |
| | | String split = "pis_file"+File.separator+"deviceSpare"; |
| | | String picUrl = File.separator + filePath.substring(filePath.indexOf(split)); |
| | | if(i == file.size()-1) { |
| | | pictureUrlSb.append(picUrl); |
| | | }else { |
| | | pictureUrlSb.append(picUrl).append(","); |
| | | } |
| | | } |
| | | } |
| | | spare.setPictureUrl(pictureUrlSb.toString()); |
| | | mapper.insert(spare); |
| | | return new Response().setII(1,"增加完成"); |
| | | } |