CommonUtils.java 7.34 KB
Newer Older
frank.xa.zhang's avatar
frank.xa.zhang committed
1 2 3 4 5 6 7 8 9 10 11
package pwc.taxtech.atms.common;

import org.apache.commons.io.IOUtils;
import org.nutz.lang.Lang;
import org.springframework.core.io.ClassPathResource;
import pwc.taxtech.atms.common.message.ErrorMessage;
import pwc.taxtech.atms.dpo.EnterpriseAccountSetOrgDto;
import pwc.taxtech.atms.exception.ServiceException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
12 13 14 15
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;
frank.xa.zhang's avatar
frank.xa.zhang committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

public class CommonUtils {
    public static final int BATCH_NUM = 500;
    public static final int BATCH_NUM_1000 = 1000;
    public static final int BATCH_NUM_2000 = 2000;

    public static String getUUID() {
        return UUID.randomUUID().toString().toUpperCase();
    }
    
    public static <T> T copyProperties(Object sourceObject, T targetObject) {
        Lang.copyProperties(sourceObject, targetObject);
        return targetObject;
    }
    
    /**
     * Determines whether [is date time overlapp] [the specified start].
     * @param organiztionAccountSetList
     * @return
     */
    public static boolean isOrganizationDateTimeOverlap(List<EnterpriseAccountSetOrgDto> organiztionAccountSetList) {
        
        if (organiztionAccountSetList == null || organiztionAccountSetList.isEmpty()) {
            return false;
        }
        
        for (int i = organiztionAccountSetList.size() - 1; i >= 0; i--)
        {
            organiztionAccountSetList.get(i).setOverlapList(new ArrayList<EnterpriseAccountSetOrgDto>());
            for (int j = i - 1; j >= 0; j--)
            {
                if (isDateTimeOverlap(organiztionAccountSetList.get(i).getEffectiveDate(),
                        organiztionAccountSetList.get(i).getExpiredDate(),
                        organiztionAccountSetList.get(j).getEffectiveDate(),
                        organiztionAccountSetList.get(j).getExpiredDate()
                    ))
                {
                    organiztionAccountSetList.get(i).getOverlapList().add(organiztionAccountSetList.get(j));
                }
            }
        }
57
        return organiztionAccountSetList.stream().anyMatch(sa -> !sa.getOverlapList().isEmpty());
frank.xa.zhang's avatar
frank.xa.zhang committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    }
    
    public static boolean validateOnlyOncePerYear(List<EnterpriseAccountSetOrgDto> organiztionAccountSetList) {
        
        List<String> accountSetIdList = organiztionAccountSetList.stream()
                .map(sa -> sa.getEnterpriseAccountSetId()).distinct().collect(Collectors.toList());
        
        for(String accountSetId: accountSetIdList) {
            List<EnterpriseAccountSetOrgDto> sameAccountSetList = organiztionAccountSetList.stream()
                    .filter(sa -> sa.getEnterpriseAccountSetId().equals(accountSetId)).collect(Collectors.toList());
            for(int i = 0; i < sameAccountSetList.size() -1; i++) {
                for(int j = i+1; j<sameAccountSetList.size(); j++) {
                    EnterpriseAccountSetOrgDto first = sameAccountSetList.get(i);
                    EnterpriseAccountSetOrgDto second = sameAccountSetList.get(j);
                    
                    //DateTime firstE = new DateTime
                    if(first.getExpiredDate()!=null && second.getEffectiveDate()!=null) {
                        Calendar firstExpired = Calendar.getInstance();
                        firstExpired.setTime(first.getExpiredDate());
                        Calendar secondEffective = Calendar.getInstance();
                        secondEffective.setTime(second.getEffectiveDate());
                        if(firstExpired.get(Calendar.YEAR) == secondEffective.get(Calendar.YEAR)) {
                            return false;
                        }
                    }
                    if(first.getEffectiveDate()!=null && second.getExpiredDate()!=null) {
                        Calendar firstEffective = Calendar.getInstance();
                        firstEffective.setTime(first.getEffectiveDate());
                        Calendar secondExpired = Calendar.getInstance();
                        secondExpired.setTime(second.getExpiredDate());
                        if(firstEffective.get(Calendar.YEAR) == secondExpired.get(Calendar.YEAR)) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }
    
    /**
     * Determines whether [is date time overlapp] [the specified start].
     * @param start - The start
     * @param end - The end
     * @param compareStart - The compare start
     * @param compareEnd - The compare end
     * @return true - there is overlap; false - there is no overlap
     */
    public static boolean isDateTimeOverlap(Date start, Date end, Date compareStart, Date compareEnd) {
        
        //1/1/0001 12:00:00 AM  (Equals Date.MinValue)
        //9999/12/31 23:59:59 PM (Equals Date.MaxValue)
        
        Calendar calMin = Calendar.getInstance();
        calMin.set(0001, 1, 1, 0, 0, 0);
        Calendar calMax = Calendar.getInstance();
        calMax.set(9999, 12, 32, 23, 59, 59);

        start = start == null ? calMin.getTime() : start;
        end = end == null ? calMin.getTime() : end;
        compareStart = compareStart == null ? calMin.getTime() : compareStart;
        compareEnd = compareEnd == null ? calMin.getTime() : compareEnd;
120 121 122

        return start.compareTo(compareEnd) <= 0 && end.compareTo(compareStart) >= 0;

frank.xa.zhang's avatar
frank.xa.zhang committed
123 124 125 126
    }
    
    
    public static String readClasspathFileToString(String path) {
127
        String text = null;
frank.xa.zhang's avatar
frank.xa.zhang committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
        try {
            ClassPathResource classPathResource = new ClassPathResource(path);
            text = IOUtils.toString(classPathResource.getInputStream(), "UTF-8");
        } catch (IOException e) {
          throw Lang.wrapThrow(e);
        }
        return text;
    }

    public static <T> List<List<T>> subListWithLen(List<T> source, int len) {
        if (source == null || source.size() == 0 || len < 1) {
            return Collections.emptyList();
        }
        List<List<T>> result = new ArrayList<>();
        int count = (source.size() + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> value;
            if ((i + 1) * len < source.size()) {
                value = source.subList(i * len, (i + 1) * len);
            } else {
                value = source.subList(i * len, source.size());
            }
            result.add(value);
        }
        return result;
    }


    /**
     * 输出文件流 下载
     */
    public static void FileOut(HttpServletResponse response, InputStream inputStream, String fileName){
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xlsx");
        ServletOutputStream out = null;
        try {
            out = response.getOutputStream();
            int b = 0;
            byte[] buffer = new byte[512];
            while ((b = inputStream.read(buffer)) > 0) {
                out.write(buffer, 0, b);
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
            e.printStackTrace();
            throw new ServiceException(ErrorMessage.SystemError);
        } finally {
            try {
                out.close();
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


}