吉安感知网项目-后端
linwei
2026-06-09 04056b0490135ddfc348928b2eb46b51bf71c600
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
57
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
120
121
122
123
124
125
126
127
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
/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.sxkj.fw.area.service.impl;
 
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.StringUtil;
import org.sxkj.common.utils.HeaderUtils;
import org.sxkj.fw.area.entity.FwPoliceStationEntity;
import org.sxkj.fw.area.param.FwPoliceStationQueryParam;
import org.sxkj.fw.area.vo.FwPoliceStationVO;
import org.sxkj.fw.area.excel.FwPoliceStationExcel;
import org.sxkj.fw.area.mapper.FwPoliceStationMapper;
import org.sxkj.fw.area.service.IFwPoliceStationService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.core.mp.base.BaseServiceImpl;
import java.util.List;
import java.util.Date;
 
/**
 * 派出所信息表 服务实现类
 *
 * @author lw
 * @since 2026-01-08
 */
@Service
public class FwPoliceStationServiceImpl extends BaseServiceImpl<FwPoliceStationMapper, FwPoliceStationEntity>
    implements IFwPoliceStationService {
 
    @Override
    public IPage<FwPoliceStationVO> selectFwPoliceStationPage(IPage<FwPoliceStationVO> page,
                                                              FwPoliceStationQueryParam param) {
        return page.setRecords(baseMapper.selectFwPoliceStationPage(page, param));
    }
 
    @Override
    public List<FwPoliceStationVO> selectFwPoliceStationList(String stationName) {
        return baseMapper.selectFwPoliceStationList(stationName);
    }
 
    @Override
    public List<FwPoliceStationExcel> exportFwPoliceStation(Wrapper<FwPoliceStationEntity> queryWrapper) {
        List<FwPoliceStationExcel> fwPoliceStationList = baseMapper.exportFwPoliceStation(queryWrapper);
        // fwPoliceStationList.forEach(fwPoliceStation -> {
        // fwPoliceStation.setTypeName(DictCache.getValue(DictEnum.YES_NO,
        // FwPoliceStation.getType()));
        // });
        return fwPoliceStationList;
    }
 
    @Override
    public List<FwPoliceStationVO> selectByPolygons(List<String> polygons) {
        // 仅做空间过滤,具体坐标规则由 SQL 处理
        return baseMapper.selectByPolygons(polygons);
    }
 
    @Override
    public boolean save(FwPoliceStationEntity fwPoliceStation) {
        // 校验派出所名称唯一性
        checkStationNameUnique(fwPoliceStation.getStationName(), null);
        fillCreateFields(fwPoliceStation);
        return super.save(fwPoliceStation);
    }
 
    @Override
    public boolean updateById(FwPoliceStationEntity fwPoliceStation) {
        // 校验派出所名称唯一性(排除当前记录)
        checkStationNameUnique(fwPoliceStation.getStationName(), fwPoliceStation.getId());
        fillUpdateFields(fwPoliceStation);
        return super.updateById(fwPoliceStation);
    }
 
    @Override
    public boolean saveOrUpdate(FwPoliceStationEntity fwPoliceStation) {
        // 校验派出所名称唯一性
        checkStationNameUnique(fwPoliceStation.getStationName(), fwPoliceStation.getId());
        if (fwPoliceStation.getId() == null) {
            fillCreateFields(fwPoliceStation);
        } else {
            fillUpdateFields(fwPoliceStation);
        }
        return super.saveOrUpdate(fwPoliceStation);
    }
 
    /**
     * 校验派出所名称唯一性
     *
     * @param stationName 派出所名称
     * @param excludeId   排除的ID(更新时使用,排除当前记录)
     * @throws RuntimeException 当名称已存在时抛出异常
     */
    private void checkStationNameUnique(String stationName, Long excludeId) {
        if (StringUtil.isBlank(stationName)) {
            return;
        }
        LambdaQueryWrapper<FwPoliceStationEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FwPoliceStationEntity::getStationName, stationName);
        queryWrapper.eq(FwPoliceStationEntity::getIsDeleted, BladeConstant.DB_NOT_DELETED);
        if (excludeId != null) {
            queryWrapper.ne(FwPoliceStationEntity::getId, excludeId);
        }
        long count = count(queryWrapper);
        if (count > 0) {
            throw new RuntimeException("派出所名称 '" + stationName + "' 已存在");
        }
    }
 
    private void fillCreateFields(FwPoliceStationEntity fwPoliceStation) {
        // 创建/更新基础审计字段统一设置
        Long userId = AuthUtil.getUserId();
        String deptIdStr = AuthUtil.getDeptId();
        Long deptId = null;
        if (StringUtil.isNotBlank(deptIdStr)) {
            try {
                deptId = Long.valueOf(deptIdStr);
            } catch (NumberFormatException ignored) {
                deptId = null;
            }
        }
        // String areaCode = HeaderUtils.getAreaCode();
        // if (StringUtil.isBlank(areaCode)) {
        // areaCode = fwPoliceStation.getAreaCode();
        // }
        Date now = new Date();
        fwPoliceStation.setCreateUser(userId);
        fwPoliceStation.setUpdateUser(userId);
        if (deptId != null) {
            fwPoliceStation.setCreateDept(deptId);
        }
        // if (StringUtil.isNotBlank(areaCode)) {
        // fwPoliceStation.setAreaCode(areaCode);
        // }
        fwPoliceStation.setCreateTime(now);
        fwPoliceStation.setUpdateTime(now);
    }
 
    private void fillUpdateFields(FwPoliceStationEntity fwPoliceStation) {
        Long userId = AuthUtil.getUserId();
        if (userId != null) {
            fwPoliceStation.setUpdateUser(userId);
        }
        fwPoliceStation.setUpdateTime(new Date());
    }
 
}