AreaRegionServiceImpl.java 11.6 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
package pwc.taxtech.atms.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import pwc.taxtech.atms.common.AuthUserHelper;
import pwc.taxtech.atms.common.CommonConstants;
import pwc.taxtech.atms.common.CommonUtils;
import pwc.taxtech.atms.common.OperateLogType;
import pwc.taxtech.atms.common.OperationAction;
import pwc.taxtech.atms.common.OperationModule;
import pwc.taxtech.atms.common.message.AreaMessage;
import pwc.taxtech.atms.common.message.AreaRegionMessage;
import pwc.taxtech.atms.common.message.LogMessage;
import pwc.taxtech.atms.dao.AreaMapper;
import pwc.taxtech.atms.dao.AreaRegionMapper;
import pwc.taxtech.atms.dao.RegionMapper;
import pwc.taxtech.atms.dto.OperationLogDto;
import pwc.taxtech.atms.dto.OperationResultDto;
import pwc.taxtech.atms.dto.UpdateLogParams;
import pwc.taxtech.atms.dto.arearegion.AreaRegionDto;
import pwc.taxtech.atms.dto.arearegion.AreaRegionInfo;
import pwc.taxtech.atms.dto.arearegion.CityInfo;
25 26 27 28 29 30
import pwc.taxtech.atms.entity.Area;
import pwc.taxtech.atms.entity.AreaExample;
import pwc.taxtech.atms.entity.AreaRegion;
import pwc.taxtech.atms.entity.AreaRegionExample;
import pwc.taxtech.atms.entity.Region;
import pwc.taxtech.atms.entity.RegionExample;
eddie.woo's avatar
eddie.woo committed
31

32 33 34 35 36
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

eddie.woo's avatar
eddie.woo committed
37
@Service
38
public class AreaRegionServiceImpl {
eddie.woo's avatar
eddie.woo committed
39 40 41 42 43 44 45 46

    @Autowired
    private AreaRegionMapper areaRegionMapper;

    @Autowired
    private AreaMapper areaMapper;

    @Autowired
47
    private OperationLogServiceImpl operationLogService;
eddie.woo's avatar
eddie.woo committed
48 49 50 51 52 53 54 55 56 57 58 59
    @Autowired
    private AuthUserHelper authUserHelper;
    @Autowired
    private RegionMapper regionMapper;

    public List<AreaRegion> findAreaRegionsByArea(String areaId) {

        return areaRegionMapper.selectAreaRegionByAreaId(areaId);
    }

    /*
     * (non-Javadoc)
60
     *
eddie.woo's avatar
eddie.woo committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
     * @see
     * pwc.taxtech.atms.service.AreaRegionService#add(pwc.taxtech.atms.service.dto.
     * AreaRegionInfo)
     */
    @Transactional
    public OperationResultDto add(AreaRegionInfo areaRegionInfo) {

        // check area name is duplicate or not
        AreaExample areaExample = new AreaExample();
        areaExample.createCriteria().andNameEqualTo(areaRegionInfo.getName());
        if (areaMapper.countByExample(areaExample) > 0) {
            return new OperationResultDto<>(false, AreaRegionMessage.AreaRegionRepeat);
        }

        // save area
        Area area = new Area();
77
        area.setId(CommonUtils.getUUID());
eddie.woo's avatar
eddie.woo committed
78 79
        area.setName(areaRegionInfo.getName());
        area.setIsActive(CommonConstants.ACTIVE_STATUS);
80
        area.setParentId(areaRegionInfo.getParentId());
eddie.woo's avatar
eddie.woo committed
81 82 83 84 85 86 87 88 89 90 91
        areaMapper.insert(area);

        // save areaRegion
        List<CityInfo> cityList = areaRegionInfo.getCityList();
        if (cityList != null && !cityList.isEmpty()) {

            // province
            List<String> provinceIdList = cityList.stream().map(city -> city.getParentId()).distinct()
                    .collect(Collectors.toList());
            for (String provinceId : provinceIdList) {
                AreaRegion provinceRegion = new AreaRegion();
92 93 94
                provinceRegion.setId(CommonUtils.getUUID());
                provinceRegion.setAreaId(area.getId());
                provinceRegion.setRegionId(provinceId);
eddie.woo's avatar
eddie.woo committed
95 96 97 98 99
                areaRegionMapper.insert(provinceRegion);
            }
            // city
            for (CityInfo city : cityList) {
                AreaRegion cityRegion = new AreaRegion();
100 101 102
                cityRegion.setId(CommonUtils.getUUID());
                cityRegion.setAreaId(area.getId());
                cityRegion.setRegionId(city.getId());
eddie.woo's avatar
eddie.woo committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
                areaRegionMapper.insert(cityRegion);
            }
        }

        OperationLogDto operationLogDto = new OperationLogDto();
        operationLogDto.setOperationContent("");
        operationLogDto.setAction(OperationAction.New.value());
        operationLogDto.setOperationObject(area.getName());
        operationLogDto.setOriginalState("");
        operationLogDto.setUpdateState("");
        operationLogDto.setModule(OperationModule.BasicDataArea.value());
        operationLogDto.setComment(LogMessage.Add);
        operationLogDto.setLogType(OperateLogType.OperationLogBasicData.value());
        operationLogService.addOperationLog(operationLogDto);

        return new OperationResultDto<>(true);
    }

    /*
     * (non-Javadoc)
123
     *
eddie.woo's avatar
eddie.woo committed
124 125 126 127 128 129 130 131
     * @see
     * pwc.taxtech.atms.service.AreaRegionService#update(pwc.taxtech.atms.service.
     * dto.AreaRegionInfo)
     */
    @Transactional
    public OperationResultDto update(AreaRegionInfo areaRegionInfo) {

        // Find oringinal area entity
132
        Area originalArea = areaMapper.selectByPrimaryKey(areaRegionInfo.getId());
eddie.woo's avatar
eddie.woo committed
133
        Area updateToArea = new Area();
134
        updateToArea.setId(areaRegionInfo.getId());
eddie.woo's avatar
eddie.woo committed
135 136 137 138 139 140 141 142 143 144
        updateToArea.setName(areaRegionInfo.getName());

        if (originalArea == null) {
            return new OperationResultDto<>(false, AreaMessage.NoAreaRootData);
        }

        // area name change
        if (!originalArea.getName().equals(updateToArea.getName())) {
            // check area name is duplicate or not
            AreaExample areaExample = new AreaExample();
145
            areaExample.createCriteria().andNameEqualTo(updateToArea.getName()).andIdNotEqualTo(updateToArea.getId());
eddie.woo's avatar
eddie.woo committed
146 147 148 149 150 151 152

            if (areaMapper.countByExample(areaExample) > 0) {
                return new OperationResultDto<>(false, AreaRegionMessage.AreaRegionRepeat);
            }

            // update area
            areaMapper.updateByPrimaryKeySelective(updateToArea);
153
            updateToArea = areaMapper.selectByPrimaryKey(originalArea.getId());
eddie.woo's avatar
eddie.woo committed
154 155 156 157 158 159 160 161 162

            // add operation log
            UpdateLogParams updateLogParams = new UpdateLogParams();
            updateLogParams.setOperateLogType(OperateLogType.OperationLogBasicData.value());
            updateLogParams.setComment("");
            updateLogParams.setOperationModule(OperationModule.BasicDataArea.value());
            updateLogParams.setOperationContent("");
            updateLogParams.setOriginalState(originalArea);
            updateLogParams.setUpdateState(updateToArea);
163
            updateLogParams.setOperationUser(authUserHelper.getCurrentAuditor().get());
eddie.woo's avatar
eddie.woo committed
164 165 166 167 168
            updateLogParams.setOperationObject(updateToArea.getName());
            updateLogParams.setOperationAction(OperationAction.Update.value());
            operationLogService.updateDataAddLog(updateLogParams);
        }

169
        List<AreaRegion> orginalRegionList = findAreaRegionsByArea(originalArea.getId());
eddie.woo's avatar
eddie.woo committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183
        String orginalCityNames = "";
        if (null != orginalRegionList && !orginalRegionList.isEmpty()) {
            orginalCityNames = StringUtils
                    .collectionToDelimitedString(
                            orginalRegionList.stream()
                                    .filter(areaRegion -> areaRegion.getRegion()
                                            .getLevelType() == CommonConstants.REGION_LEVELTYPE_CITY)
                                    .map(areaRegion -> areaRegion.getRegion().getName()).collect(Collectors.toList()),
                            ",");
        }
        List<CityInfo> updateToCityList = areaRegionInfo.getCityList();
        String updateToCityNames = "";
        if (null != updateToCityList && !updateToCityList.isEmpty()) {
            // updateToCityList.stream().sorted(Comparator.comparing(cityInfo ->
184
            // cityInfo.getId())).collect(Collectors.toList());
eddie.woo's avatar
eddie.woo committed
185
            updateToCityNames = StringUtils.collectionToDelimitedString(
186
                    updateToCityList.stream().sorted(Comparator.comparing(cityInfo -> cityInfo.getId()))
eddie.woo's avatar
eddie.woo committed
187 188 189 190 191 192 193 194
                            .map(cityInfo -> cityInfo.getName()).collect(Collectors.toList()),
                    ",");
        }

        // region change
        if (!orginalCityNames.equals(updateToCityNames)) {
            // delete first
            AreaRegionExample areaRegionExample = new AreaRegionExample();
195
            areaRegionExample.createCriteria().andAreaIdEqualTo(originalArea.getId());
eddie.woo's avatar
eddie.woo committed
196 197 198 199 200 201 202 203 204
            areaRegionMapper.deleteByExample(areaRegionExample);

            if (null != updateToCityList && !updateToCityList.isEmpty()) {

                // add province
                List<String> provinceIdList = updateToCityList.stream().map(city -> city.getParentId()).distinct()
                        .collect(Collectors.toList());
                for (String provinceId : provinceIdList) {
                    AreaRegion provinceRegion = new AreaRegion();
205 206 207
                    provinceRegion.setId(CommonUtils.getUUID());
                    provinceRegion.setAreaId(originalArea.getId());
                    provinceRegion.setRegionId(provinceId);
eddie.woo's avatar
eddie.woo committed
208 209 210 211 212
                    areaRegionMapper.insert(provinceRegion);
                }
                // city
                for (CityInfo city : updateToCityList) {
                    AreaRegion cityRegion = new AreaRegion();
213 214 215
                    cityRegion.setId(CommonUtils.getUUID());
                    cityRegion.setAreaId(originalArea.getId());
                    cityRegion.setRegionId(city.getId());
eddie.woo's avatar
eddie.woo committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
                    areaRegionMapper.insert(cityRegion);
                }
            }

            // add operation log
            OperationLogDto operationLogDto = new OperationLogDto();
            operationLogDto.setAction(OperationAction.Update.value());
            operationLogDto.setComment("");
            operationLogDto.setLogType(OperateLogType.OperationLogBasicData.value());
            operationLogDto.setModule(OperationModule.BasicDataArea.value());
            operationLogDto.setOperationContent(AreaMessage.AreaRegion);
            operationLogDto.setOperationObject(updateToArea.getName());
            operationLogDto.setOriginalState(orginalCityNames);
            operationLogDto.setUpdateState(updateToCityNames);
            operationLogService.addOperationLog(operationLogDto);
        }

        return new OperationResultDto(true);
    }
235 236

    public List<AreaRegionDto> getProvinces() {
eddie.woo's avatar
eddie.woo committed
237 238 239 240
        List<AreaRegionDto> provinces = getRegions(CommonConstants.REGION_LEVELTYPE_PROVINCE);
        return provinces;
    }

241
    public List<AreaRegionDto> getCities(String parentId) {
eddie.woo's avatar
eddie.woo committed
242
        List<AreaRegionDto> cities = getRegions(CommonConstants.REGION_LEVELTYPE_CITY);
243
        List<AreaRegionDto> query = cities.stream().filter(s -> s.getRegionParentId().equals(parentId))
eddie.woo's avatar
eddie.woo committed
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
                .collect(Collectors.toList());
        return query;

    }

    private List<AreaRegionDto> getRegions(int levelType) {
        RegionExample regionExample = new RegionExample();
        regionExample.createCriteria().andIsActiveEqualTo(CommonConstants.ACTIVE_STATUS).andLevelTypeEqualTo(levelType);
        List<Region> list = regionMapper.selectByExample(regionExample);
        return rotateAreaRegionList(list);

    }

    private List<AreaRegionDto> rotateAreaRegionList(List<Region> regionList) {
        List<AreaRegionDto> areaRegionDtoList = new ArrayList<>();
        if (regionList != null && !regionList.isEmpty()) {
            for (Region region : regionList) {
                areaRegionDtoList.add(rotateRegion(region));
            }
        }
        return areaRegionDtoList;
    }

    private AreaRegionDto rotateRegion(Region region) {
        AreaRegionDto areaRegionDto = new AreaRegionDto();
269
        areaRegionDto.setRegionId(region.getId());
eddie.woo's avatar
eddie.woo committed
270 271
        areaRegionDto.setRegionName(region.getName());
        areaRegionDto.setLevelType(region.getLevelType());
272
        areaRegionDto.setRegionParentId(region.getParentId());
eddie.woo's avatar
eddie.woo committed
273 274 275
        return areaRegionDto;
    }
}