package com.qxgmat.service.inline; import com.github.pagehelper.Page; import com.nuliji.tools.AbstractService; import com.nuliji.tools.exception.ParameterException; import com.nuliji.tools.exception.SystemException; import com.nuliji.tools.mybatis.Example; import com.qxgmat.data.dao.AdMapper; import com.qxgmat.data.dao.TextbookLibraryMapper; import com.qxgmat.data.dao.entity.Ad; import com.qxgmat.data.dao.entity.TextbookLibrary; import com.qxgmat.data.dao.entity.TextbookLibraryHistory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; import java.util.List; @Service public class TextbookLibraryService extends AbstractService { private static final Logger logger = LoggerFactory.getLogger(TextbookLibraryService.class); @Resource private TextbookLibraryMapper textbookLibraryMapper; @Resource private TextbookLibraryHistoryService textbookLibraryHistoryService; /** * 后台查看机经换库表 * @param page * @param size * @param keyword * @return */ public Page listAdmin(int page, int size, String keyword){ Example example = new Example(TextbookLibrary.class); if(keyword != null) example.and( example.createCriteria() .orLike("startDate", "%"+keyword+"%") .orLike("endDate", "%"+keyword+"%") ); example.orderBy("id").desc(); return page(()->select(textbookLibraryMapper, example), page, size); } /** * 获取往期:最后第二期 * @return */ public TextbookLibrary getSecond(){ Example example = new Example(TextbookLibrary.class); example.orderBy("id").desc(); List list = select(textbookLibraryMapper, example, 2, 1); return list.get(0); } /** * 获取最后一期 * @return */ public TextbookLibrary getLatest(){ Example example = new Example(TextbookLibrary.class); example.orderBy("id").desc(); return one(textbookLibraryMapper, example); } /** * 按时间获取换库表 * @param startTime * @param endTime * @return */ public List listByTime(Date startTime, Date endTime){ Example example = new Example(TextbookLibrary.class); example.and( example.createCriteria() .andGreaterThanOrEqualTo("startDate", startTime) .andLessThan("startDate", endTime) ); example.orderBy("id").asc(); return select(textbookLibraryMapper, example); } /** * 添加一次发布 * @param history * @return */ @Transactional public TextbookLibrary addHistory(TextbookLibraryHistory history){ Example example = new Example(TextbookLibrary.class); example.and( example.createCriteria() .andEqualTo("id", history.getLibraryId()) ); example.orderBy("id").desc(); TextbookLibrary library = one(textbookLibraryMapper, example); if (library == null){ throw new ParameterException("机经不存在"); } Date now = new Date(); if (!history.getQuant().isEmpty()){ library.setQuant(history.getQuant()); library.setQuantVersion(library.getQuantVersion() + 1); library.setQuantTime(now); library.setQuantDescription(history.getQuantDescription()); history.setQuantVersion(library.getQuantVersion() + 1); } if (!history.getIr().isEmpty()){ library.setIr(history.getIr()); library.setIrVersion(library.getIrVersion() + 1); library.setIrTime(now); library.setIrDescription(history.getIrDescription()); history.setIrVersion(library.getIrVersion() + 1); } if (!history.getRc().isEmpty()){ library.setRc(history.getRc()); library.setRcVersion(library.getRcVersion() + 1); library.setRcTime(now); library.setRcDescription(history.getRcDescription()); history.setRcVersion(library.getRcVersion() + 1); } library.setHistoryNumber(library.getHistoryNumber() + 1); history = textbookLibraryHistoryService.add(history); return edit(library); } public TextbookLibrary add(TextbookLibrary ad){ int result = insert(textbookLibraryMapper, ad); ad = one(textbookLibraryMapper, ad.getId()); if(ad == null){ throw new SystemException("换库添加失败"); } return ad; } public TextbookLibrary edit(TextbookLibrary ad){ TextbookLibrary in = one(textbookLibraryMapper, ad.getId()); if(in == null){ throw new ParameterException("换库不存在"); } int result = update(textbookLibraryMapper, ad); return ad; } public boolean delete(Number id){ TextbookLibrary in = one(textbookLibraryMapper, id); if(in == null){ throw new ParameterException("换库不存在"); } int result = delete(textbookLibraryMapper, id); return result > 0; } public TextbookLibrary get(Number id){ TextbookLibrary in = one(textbookLibraryMapper, id); if(in == null){ throw new ParameterException("换库不存在"); } return in; } public Page select(int page, int pageSize){ return select(textbookLibraryMapper, page, pageSize); } public Page select(Integer[] ids){ return page(()-> select(textbookLibraryMapper, ids), 1, ids.length); } public List select(Collection ids){ return select(textbookLibraryMapper, ids); } }