package cn.com.goldenwater.dcproj.service.impl.tac; import cn.com.goldenwater.core.service.AbstractCrudService; import cn.com.goldenwater.dcproj.dao.BisInspAllRlationPersDao; import cn.com.goldenwater.dcproj.dao.TacAttPawpBaseChangeLogDao; import cn.com.goldenwater.dcproj.dao.TacAttPawpBaseDao; import cn.com.goldenwater.dcproj.dao.TacInspYearBatchObjDao; import cn.com.goldenwater.dcproj.model.DingChuanTacProject; import cn.com.goldenwater.dcproj.model.TacAttPawpBase; import cn.com.goldenwater.dcproj.model.TacAttPawpBaseChangeLog; import cn.com.goldenwater.dcproj.model.TacInspYearBatchObj; import cn.com.goldenwater.dcproj.model.WorkInxMonth; import cn.com.goldenwater.dcproj.param.TacAttPawpBaseParam; import cn.com.goldenwater.dcproj.param.TacInspYearBatchObjParam; import cn.com.goldenwater.dcproj.service.TacAttPawpBaseService; import cn.com.goldenwater.dcproj.utils.GeoUtil; import cn.com.goldenwater.dcproj.utils.expExcel.ExcelExport; import cn.com.goldenwater.dcproj.utils.expExcel.ExportAbstract; import cn.com.goldenwater.dcproj.utils.expExcel.ExportUtil; import cn.com.goldenwater.id.util.UuidUtil; import cn.com.goldenwater.target.CheckException; import io.swagger.annotations.ApiModelProperty; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletResponse; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; /** * @author lune * @date 2019-6-19 */ @Service @Transactional public class TacAttPawpBaseServiceImpl extends AbstractCrudService implements TacAttPawpBaseService { private static Logger logger = LoggerFactory.getLogger(TacAttPawpBaseServiceImpl.class); @Autowired private TacAttPawpBaseDao tacAttPawpBaseDao; @Autowired private TacAttPawpBaseChangeLogDao tacAttPawpBaseChangeLogDao; @Autowired private BisInspAllRlationPersDao rlationPersDao; @Autowired private TacInspYearBatchObjDao tacInspYearBatchObjDao; public TacAttPawpBaseServiceImpl(TacAttPawpBaseDao tacAttPawpBaseDao) { super(tacAttPawpBaseDao); this.tacAttPawpBaseDao = tacAttPawpBaseDao; } @Override public int insert(TacAttPawpBase tacAttPawpBase) { int ret = 0; if (tacAttPawpBase.getCenterX() != null && tacAttPawpBase.getGdX() == null) { Map map = GeoUtil.wgs84togcj02(tacAttPawpBase.getCenterX(), tacAttPawpBase.getCenterY()); tacAttPawpBase.setGdX(map.get("lon")); tacAttPawpBase.setGdY(map.get("lat")); } else if (tacAttPawpBase.getCenterX() == null && tacAttPawpBase.getGdX() != null) { Map map = GeoUtil.gcj02towgs84(tacAttPawpBase.getGdX(), tacAttPawpBase.getGdY()); tacAttPawpBase.setCenterX(map.get("lon")); tacAttPawpBase.setCenterY(map.get("lat")); } ret = tacAttPawpBaseDao.insert(tacAttPawpBase); return ret; } @Override public int update(TacAttPawpBase tacAttPawpBase) { int ret = 0; if (tacAttPawpBase.getCenterX() != null && tacAttPawpBase.getGdX() == null) { Map map = GeoUtil.wgs84togcj02(tacAttPawpBase.getCenterX(), tacAttPawpBase.getCenterY()); tacAttPawpBase.setGdX(map.get("lon")); tacAttPawpBase.setGdY(map.get("lat")); } else if (tacAttPawpBase.getCenterX() == null && tacAttPawpBase.getGdX() != null) { Map map = GeoUtil.gcj02towgs84(tacAttPawpBase.getGdX(), tacAttPawpBase.getGdY()); tacAttPawpBase.setCenterX(map.get("lon")); tacAttPawpBase.setCenterY(map.get("lat")); } TacAttPawpBase tacAttPawpBaseOld = tacAttPawpBaseDao.get(tacAttPawpBase.getId()); //调整项记录 StringBuffer contentbuffer = showContent(tacAttPawpBaseOld, tacAttPawpBase); //调整年度计划工程日志 TacAttPawpBaseChangeLog tacAttPawpBaseChangeLog = new TacAttPawpBaseChangeLog(); tacAttPawpBaseChangeLog.setId(UuidUtil.uuid()); tacAttPawpBaseChangeLog.setYear(tacAttPawpBase.getYear()); tacAttPawpBaseChangeLog.setOjbId(tacAttPawpBase.getId()); tacAttPawpBaseChangeLog.setOjbName(tacAttPawpBase.getName()); tacAttPawpBaseChangeLog.setChangeReson(tacAttPawpBase.getChangeReson()); tacAttPawpBaseChangeLog.setIntm(new Date()); tacAttPawpBaseChangeLog.setPersId(tacAttPawpBase.getPersId()); tacAttPawpBaseChangeLog.setPersName(rlationPersDao.get(tacAttPawpBase.getPersId()).getPersName()); tacAttPawpBaseChangeLog.setContent(contentbuffer.toString()); tacAttPawpBaseChangeLog.setProvince(tacAttPawpBase.getProvince()); tacAttPawpBaseChangeLogDao.insert(tacAttPawpBaseChangeLog); //修改年度计划工程项目 tacAttPawpBase.setUptm(new Date()); ret = tacAttPawpBaseDao.update(tacAttPawpBase); return ret; } @Override public List getBaseType(TacAttPawpBaseParam attPawpBaseParam) { List list = tacAttPawpBaseDao.getBaseType(attPawpBaseParam); return list; } @Override public int deleteNew(TacAttPawpBaseParam tacAttPawpBaseParam) { // 判断项目是否分配给稽查组 TacInspYearBatchObjParam tacInspYearBatchObjParam = new TacInspYearBatchObjParam(); tacInspYearBatchObjParam.setOjbId(tacAttPawpBaseParam.getId()); tacInspYearBatchObjParam.setProvince(tacAttPawpBaseParam.getProvince()); List batchObjList = this.tacInspYearBatchObjDao.findList(tacInspYearBatchObjParam); if(null != batchObjList && batchObjList.size()>0){ throw new CheckException("此项目已加入稽查组,请先把项目从组里删除!!!"); } TacAttPawpBase tacAttPawpBase = tacAttPawpBaseDao.get(tacAttPawpBaseParam.getId()); //调整年度计划工程日志 TacAttPawpBaseChangeLog tacAttPawpBaseChangeLog = new TacAttPawpBaseChangeLog(); tacAttPawpBaseChangeLog.setId(UuidUtil.uuid()); tacAttPawpBaseChangeLog.setYear(tacAttPawpBase.getYear()); tacAttPawpBaseChangeLog.setOjbId(tacAttPawpBase.getId()); tacAttPawpBaseChangeLog.setOjbName(tacAttPawpBase.getName()); tacAttPawpBaseChangeLog.setIntm(new Date()); if (StringUtils.isNotBlank(tacAttPawpBaseParam.getPersId())) { tacAttPawpBaseChangeLog.setPersId(tacAttPawpBaseParam.getPersId()); tacAttPawpBaseChangeLog.setPersName(rlationPersDao.get(tacAttPawpBaseParam.getPersId()).getPersName()); } tacAttPawpBaseChangeLog.setContent("删除" + tacAttPawpBase.getName() + "工程"); tacAttPawpBaseChangeLog.setProvince(tacAttPawpBaseParam.getProvince()); tacAttPawpBaseChangeLogDao.insert(tacAttPawpBaseChangeLog); return this.tacAttPawpBaseDao.delete(tacAttPawpBaseParam.getId()); } @Override public List getMonthPro(String adCode, Integer year, Integer month, String province, String tm) { //获取年度任务数和完成数 List workInxMonths = tacAttPawpBaseDao.getMonthPro(adCode, year, province, month); for (WorkInxMonth workInxMonth : workInxMonths) { workInxMonth.setTimeYear(year); workInxMonth.setTimeMonth(month); workInxMonth.setTimeRecord(tm); //workInxMonth.setInxNumValue(workInxMonth.getInxStatValue()); workInxMonth.setInxStatus("1"); } return workInxMonths; } @Override public List getAllPrject(String year) { //根据年份获取所有最后阶段项目 List dingChuanTacProjects = tacAttPawpBaseDao.getAllPrject(year); return dingChuanTacProjects; } @Override public List> getJcDescData(String rgstrId) { List> jcDescData = tacAttPawpBaseDao.getJcDescData(rgstrId); return jcDescData; } /** * 获取两个对象同名属性内容不相同的列表 * * @param class1 对象1 * @param class2 对象2 * @return * @throws ClassNotFoundException * @throws IllegalAccessException */ public static List> compareTwoClass(Object class1, Object class2) throws ClassNotFoundException, IllegalAccessException { List> list = new ArrayList>(); //获取对象的class SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd"); Class clazz1 = class1.getClass(); Class clazz2 = class2.getClass(); //获取对象的属性列表 Field[] field1 = clazz1.getDeclaredFields(); Field[] field2 = clazz2.getDeclaredFields(); //遍历属性列表field1 for (int i = 0; i < field1.length; i++) { //遍历属性列表field2 for (int j = 0; j < field2.length; j++) { //如果field1[i]属性名与field2[j]属性名内容相同 if (field1[i].getName().equals(field2[j].getName())) { field1[i].setAccessible(true); field2[j].setAccessible(true); //如果field1[i]属性值与field2[j]属性值内容不相同 if (!compareTwo(field1[i].get(class1), field2[j].get(class2))) { Map map2 = new HashMap(); if (field1[i].getAnnotation(ApiModelProperty.class) != null) { map2.put("name", field1[i].getAnnotation(ApiModelProperty.class).value()); if (field1[i].get(class1) != null && field1[i].get(class1) instanceof Date && field2[j].get(class2) != null && field2[j].get(class2) instanceof Date) { map2.put("old", fmt.format(field1[i].get(class1))); map2.put("new", fmt.format(field2[j].get(class2))); } else if (field1[i].get(class1) != null && field1[i].get(class1) instanceof Date) { map2.put("old", fmt.format(field1[i].get(class1))); } else if (field2[j].get(class2) != null && field2[j].get(class2) instanceof Date) { map2.put("new", fmt.format(field2[j].get(class2))); } else { map2.put("old", field1[i].get(class1)); map2.put("new", field1[j].get(class2)); } } list.add(map2); } break; } } } return list; } //对比两个数据是否内容相同 public static boolean compareTwo(Object object1, Object object2) { if (object1 == null && object2 == null) { return true; } //以下注掉代码,看具体需求。因有时会出现源数据是没有进行赋值,因此是null;而通过EditText获取值的时候,虽然没有值,但是会变成"",但是本质是没有赋值的。 if (object1 == "" && object2 == null) { return true; } if (object1 == null && object2 == "") { return true; } if (object1 == null && object2 != null) { return false; } if (object1 != null && object2 == null) { return true; } if (object1 != null && object2 == "") { return true; } if (object1.equals(object2)) { return true; } return false; } //对比两个数据是否内容相同 public static StringBuffer showContent(Object tacAttPawpBaseOld, Object tacAttPawpBase) { //对比两个对象的属性值是否相同,不相同记录调整值 StringBuffer contentbuffer = new StringBuffer(); try { List> maps = compareTwoClass(tacAttPawpBaseOld, tacAttPawpBase); for (Map map : maps) { if (map.get("name") != null && map.get("name") != "") { contentbuffer.append(map.get("name")).append(":"); if (map.get("old") != null && map.get("new") != null) { contentbuffer.append(map.get("old")).append("修改值为").append(map.get("new")).append(","); } else if (map.get("old") == null) { contentbuffer.append("添加值为").append(map.get("new")); } } } } catch (ClassNotFoundException | IllegalAccessException e) { logger.error(e.getMessage(), e); } return contentbuffer; } public static void main(String[] args) { TacAttPawpBase tacAttPawpBaseOld = new TacAttPawpBase(); tacAttPawpBaseOld.setId("12345"); tacAttPawpBaseOld.setAdCode("23232"); tacAttPawpBaseOld.setLocation("23232"); TacAttPawpBase tacAttPawpBaseNew = new TacAttPawpBase(); tacAttPawpBaseNew.setId("12345"); tacAttPawpBaseNew.setAdCode("23232"); try { List> maps = compareTwoClass(tacAttPawpBaseOld, tacAttPawpBaseNew); StringBuffer contentbuffer = new StringBuffer(); for (Map map : maps) { System.out.println(contentbuffer.append(map.get("name")).append(map.get("old")).append(map.get("new"))); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } /** * 导出列表 * @param attPawpBaseParam * @param response */ @Override public void export(TacAttPawpBaseParam attPawpBaseParam, HttpServletResponse response){ // 查询导出的数据 List attPawpBaseList = this.tacAttPawpBaseDao.findList(attPawpBaseParam); List columns = new ArrayList() {{ add("name"); add("type"); add("location"); add("commencementDate"); add("totalInvestment"); }}; exportExcel(attPawpBaseList, response, "稽察年度计划项目列表", columns, TacAttPawpBase.class); } public void exportExcel(List result, HttpServletResponse response, String fileName, List list, Class cla) { if (list == null || list.size() == 0) { ExportUtil.exportExcel(result, response, fileName); return; } List> mapList = new ArrayList<>(); result.forEach(worker -> mapList.add(transBean2Map(worker))); ExportAbstract export = new ExcelExport(); if (StringUtils.isBlank(fileName)) { fileName = "结果列表"; } export.setFileName(fileName); export.setExport_ps_export(true); export.setExport_ps_type(ExportAbstract.XLS); export.setExport_bzip(false); export.setTitle(fileName); ArrayList cols = new ArrayList(); Field[] fields = cla.getDeclaredFields(); Class clazz = cla.getSuperclass(); if (clazz != null && clazz != Objects.class) { fields = ArrayUtils.addAll(fields, clazz.getDeclaredFields()); } for (String name : list) { for (Field field : fields) { if (name.equals(field.getName()) && field.getAnnotation(ApiModelProperty.class) != null) { field.setAccessible(true); cols.add(field.getName() + "[" + ExportUtil.fileFormat(field.getAnnotation(ApiModelProperty.class).value()) + "]"); break; } } } if (cols.size() < 1) { cols.add("name[名称]"); cols.add("code[编码]"); } export.setCols(cols); export.setGroupable(false); // 设置视图指标 export.setLevel(1); export.setLocksize(0); try { export.Export(response); // 导出表头 export.ExportHeadForCustom(response); export.ContinueExport(mapList); export.EndExport(); } catch (Exception e) { e.printStackTrace(); } } private Map transBean2Map(Object obj) { if (obj == null) { return null; } Map map = new HashMap(); try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); String key=""; for (PropertyDescriptor property : propertyDescriptors) { key = property.getName(); // 过滤class属性 if (!"class".equals(key)) { // 得到property对应的getter方法 Method getter = property.getReadMethod(); Object value = getter.invoke(obj); map.put(key, value); if("totalInvestment".equals(key)){ if(null != value && StringUtils.isNotBlank(value.toString())){ map.put(key, value.toString()); }else { map.put(key, ""); } }else if("commencementDate".equals(key)){ if(null != value && StringUtils.isNotBlank(value.toString())){ LocalDateTime localDateTime = convertDateToLocalDateTime((Date)value); if(null != localDateTime){ String dateStr = String.format("%d年%d月%d日",localDateTime.getYear(),localDateTime.getMonthValue(),localDateTime.getDayOfMonth()); map.put(key, dateStr); }else { map.put(key, ""); } } } } } } catch (Exception e) { System.out.println("transBean2Map Error " + e); } return map; } private LocalDateTime convertDateToLocalDateTime(Date date){ Calendar calendar = Calendar.getInstance(); calendar.setTime(date); LocalDateTime localDateTime = LocalDateTime.of(calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH),0,0,0); return localDateTime; } }