AdministerServiceImpl.java 22.4 KB
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
package com.subsidy.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.subsidy.common.configure.SubsidyConfig;
import com.subsidy.common.exception.HttpException;

import com.subsidy.dto.administer.*;
import com.subsidy.mapper.*;
import com.subsidy.model.*;
import com.subsidy.service.AdministerService;
import com.subsidy.service.CompanyMemberMappingService;
import com.subsidy.service.ServiceAdministerMappingService;
import com.subsidy.util.*;
import com.subsidy.util.excel.ExcelUtil;
import com.subsidy.vo.administer.AuthVO;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

/**
 * <p>
 * 管理平台用户 服务实现类
 * </p>
 *
 * @author Tuyp
 * @since 2024-01-11
 */
@Service
public class AdministerServiceImpl extends ServiceImpl<AdministerMapper, AdministerDO> implements AdministerService {


    @Autowired
    private SubsidyConfig subsidyConfig;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private DepartmentDictMapper departmentDictMapper;

    @Autowired
    private CompanyMemberMappingMapper companyMemberMappingMapper;

    @Autowired
    private MemberDepartmentMappingMapper memberDepartmentMappingMapper;

    @Autowired
    private AdministerCompanyMappingMapper administerCompanyMappingMapper;

    @Autowired
    private ServiceAdministerMappingMapper serviceAdministerMappingMapper;

    public LoginVO login(AdministerDO administerDO) {

        LoginVO loginVO = new LoginVO();

        List<Integer> integers = new ArrayList<>();
        integers.add(3);
        integers.add(1);
        integers.add(0);

        AdministerDO administerDO1 = this.baseMapper.selectOne(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, administerDO.getAccountName())
                .in(AdministerDO::getRole, integers));

        if (null == administerDO1) {
            throw new HttpException(10012);
        }

        if ("0".equals(administerDO1.getStatus())) {
            throw new HttpException(10013);
        }

        if (administerDO1.getPassword().equals(administerDO.getPassword())) {
            String token = JwtUtil.generateToken(administerDO1.getId(), ConstantUtils.ADMINISTER_TERMINATE);
            loginVO.setToken(token);
            return loginVO;
        } else {
            throw new HttpException(10029);
        }
    }


    public AdministerPermissionVO getPermissions() {

        AdministerPermissionVO administerPermissionVO = new AdministerPermissionVO();
        AdministerDO administerDO = (AdministerDO) Localstorage.getUser();
        if (administerDO == null) {
            throw new HttpException(10012);
        }
        BeanUtils.copyProperties(administerDO, administerPermissionVO);

        List<PermissionsVO> list = this.baseMapper.getPermissions(administerDO.getId());
        List<PermissionsVO> treeList = new ArrayList();
        if (list != null) {
            list.forEach(permission -> {
                if (permission.getParentId() == null) {
                    treeList.add(permission);
                }

                list.forEach(p -> {
                    if (null != p.getParentId() && p.getParentId().equals(permission.getId())) {
                        if (permission.getChildren() == null) {
                            permission.setChildren(new ArrayList<>());
                        }
                        permission.getChildren().add(p);
                    }
                });
            });
        }
        administerPermissionVO.setPermissions(treeList);
        administerPermissionVO.setTelephone(administerDO.getTelephone());
        administerPermissionVO.setAccountName(administerDO.getAccountName());

        /**
         * 所在公司
         */
        List<GetAllCompanyVO> getAllCompanyVOS = this.baseMapper.administerCompanys(administerDO.getId());
        administerPermissionVO.setGetAllCompanyVOS(getAllCompanyVOS);

        return administerPermissionVO;
    }

    public AuthVO auth(AdministerDO administerDO) {

        CloseableHttpClient client = null;
        CloseableHttpResponse response = null;
        String resValue = "";

//        long timestamp = System.currentTimeMillis();
//        String nonce = SecretUtils.getNonce(5);
//        String appId = subsidyConfig.getAppId();
//
//        String appKey = subsidyConfig.getSecurityKey();
//        String accountName = administerDO.getAccountName();

        //鉴权逻辑 对不对
        String preAuthen = "appId=qxywz5nnWMI7ABCS"
                + "&id=" + administerDO.getId()
                + "&accountName=" + administerDO.getAccountName()
                + "&appSecret=626737T1-65K0-5xC2-0Y0V-2Aq9QUAJ";


        String signature = SecretUtils.getMD5String(preAuthen);
        try {
            AuthVO qxyVO = new AuthVO();
            qxyVO.setToken(signature);
            return qxyVO;
//            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new AuthVO();
    }


    //    @Transactional(rollbackFor = Exception.class)
    public ImportMemberResultVO importMember(Long companyId, MultipartFile multipartFile) throws Exception {

        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        //拼音小写
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        //不带声调
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

        ImportMemberResultVO importMemberResultVO = new ImportMemberResultVO();

        List<ImportMemberVO> failMembers = new ArrayList<>();

        int successCount = 0;
        int failCount = 0;
        List<ImportMemberDTO> importMemberDTOS = ExcelUtil.readExcelWithoutTitle(null, ImportMemberDTO.class, multipartFile);
        for (ImportMemberDTO s : importMemberDTOS) {
            try {

                MemberDO memberDel = memberMapper.selectOne(new QueryWrapper<MemberDO>()
                        .lambda()
                        .eq(MemberDO::getTelephone, s.getTelephone())
                        .eq(MemberDO::getMemberType, 2));

                MemberDO memberIdCard = memberMapper.selectOne(new QueryWrapper<MemberDO>()
                        .lambda()
                        .eq(MemberDO::getIdCard, s.getIdCard())
                        .eq(MemberDO::getMemberType, 2));

                boolean departmentFlag = false;
                String[] fullName = s.getDepartments().split(",");

                for (String dep : fullName) {
                    DepartmentDictDO departmentDictDO = departmentDictMapper.selectOne(new QueryWrapper<DepartmentDictDO>()
                            .lambda()
                            .eq(DepartmentDictDO::getFullName, dep));
                    if (null == departmentDictDO) {
                        departmentFlag = true;
                    }
                }

                //日期格式
                SimpleDateFormat d = null;
                Date date = null;

                if (!StringUtils.isEmpty(s.getInductionDate())) {
                    String DATE_PATTERN = "yyyy-MM-dd";
                    SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US);
                    date = sdf.parse(s.getInductionDate());
                    d = new SimpleDateFormat(DATE_PATTERN);
                }

                if (StringUtils.isEmpty(s.getUserName()) || StringUtils.isEmpty(s.getTelephone()) || StringUtils.isEmpty(s.getGender()) || StringUtils.isEmpty(s.getIdCard()) || StringUtils.isEmpty(s.getDepartments())) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("必填字段为空");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (departmentFlag) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("未查询到部门名称");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != s.getIdCard() && s.getIdCard().length() != 18) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("身份证位数不正确");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != s.getTelephone() && s.getTelephone().length() != 11) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("手机号位数不正确");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != memberDel) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("手机号重复");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else if (null != memberIdCard) {
                    ImportMemberVO importMemberVO = new ImportMemberVO();
                    BeanUtils.copyProperties(s, importMemberVO);
                    importMemberVO.setReason("身份证重复");
                    if (null != d && null != date) {
                        s.setInductionDate(d.format(date));
                    }
                    importMemberVO.setDepartments(s.getDepartments());
                    failMembers.add(importMemberVO);
                    failCount++;
                } else {
                    MemberDO memberDO1 = new MemberDO();
                    memberDO1.setUserName(s.getUserName());
                    memberDO1.setGender(s.getGender());
                    memberDO1.setEmail(s.getEmail());

                    String accountName = PinyinHelper.toHanYuPinyinString(s.getUserName(), format, "", true);
                    //查看人员目前所在公司是否有重名的情况
                    List<MemberDO> memberDOS = companyMemberMappingMapper.companyAccountMembers(companyId, accountName);

                    if (memberDOS.size() > 0) {
                        String usernames = userName(accountName, 0, companyId);
                        memberDO1.setAccountName(usernames.replace("u:", "v"));
                    } else {
                        memberDO1.setAccountName(accountName.replace("u:", "v"));
                    }

//                    memberDO1.setCompanyId(companyId);
                    memberDO1.setFirstLogin(0);
                    memberDO1.setTelephone(s.getTelephone());
                    memberDO1.setIdCard(s.getIdCard().toUpperCase());
                    memberDO1.setStatus("启用");
                    memberDO1.setPassword("Ykhl123456");
                    memberDO1.setAccountNameEn(s.getAccountNameEn());
                    memberDO1.setMemberType(2);
                    memberMapper.insert(memberDO1);

                    //人和公司的映射
                    CompanyMemberMappingDO companyMemberMappingDO = new CompanyMemberMappingDO();
                    companyMemberMappingDO.setMemberId(memberDO1.getId());
                    companyMemberMappingDO.setCompanyId(companyId);
                    companyMemberMappingDO.setMemberStatus(1);
                    companyMemberMappingDO.setWorkNo(s.getWorkNo());
                    companyMemberMappingDO.setInductionDate(DateFormatUtil.format(new Date(), DateFormatUtil.FMT_sdf_yMd));
                    companyMemberMappingMapper.insert(companyMemberMappingDO);

                    successCount++;

                    //找到对应的部门
                    if (fullName.length > 0) {
                        for (String dep : fullName) {
                            DepartmentDictDO departmentDictDO = departmentDictMapper.selectOne(new QueryWrapper<DepartmentDictDO>()
                                    .lambda()
                                    .eq(DepartmentDictDO::getFullName, dep));
                            MemberDepartmentMappingDO memberDepartmentMappingDO = new MemberDepartmentMappingDO();
                            memberDepartmentMappingDO.setDepartmentId(departmentDictDO.getId());
                            memberDepartmentMappingDO.setMemberId(memberDO1.getId());
                            memberDepartmentMappingDO.setMemberStatus(1);
                            memberDepartmentMappingMapper.insert(memberDepartmentMappingDO);
                        }
                    }
                }
            } catch (Exception e) {
                ImportMemberVO importMemberVO = new ImportMemberVO();
                BeanUtils.copyProperties(s, importMemberVO);
                String DATE_PATTERN = "yyyy-MM-dd";
                SimpleDateFormat sdf = new SimpleDateFormat("E MMM dd HH:mm:ss z yyyy", Locale.US);
                if (null != s.getInductionDate()) {
                    Date date = sdf.parse(s.getInductionDate());
                    SimpleDateFormat d = new SimpleDateFormat(DATE_PATTERN);
                    importMemberVO.setInductionDate(d.format(date));
                }
                importMemberVO.setReason("其他原因");
                importMemberVO.setDepartments(s.getDepartments());
                failCount++;
                failMembers.add(importMemberVO);
            }
        }
        importMemberResultVO.setImportMemberVOS(failMembers);
        importMemberResultVO.setSuccessCount(successCount);
        importMemberResultVO.setFailCount(failCount);
        return importMemberResultVO;
    }

    public void downloadFailMembers(DownloadFailMembersDTO downloadFailMembersDTO) {
        List<ImportMemberVO> importMemberVOS = downloadFailMembersDTO.getImportMemberVOS();
        ExcelUtil.writeExcel(importMemberVOS, ImportMemberVO.class);
    }


    public String addAdminister(AddAdministerDTO addAdministerDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAdministerDTO.getAccountName()));

        int count1 = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, addAdministerDTO.getTelephone()));

        if (count + count1 > 0) {
            throw new HttpException(20002);
        }

        AdministerDO administerDO = new AdministerDO();
        BeanUtils.copyProperties(addAdministerDTO, administerDO);
        this.baseMapper.insert(administerDO);

        if (null != addAdministerDTO.getCompanyIds()) {
            List<Long> companyIds = addAdministerDTO.getCompanyIds();
            for (Long companyId : companyIds) {
                AdministerCompanyMappingDO administerCompanyMappingDO = new AdministerCompanyMappingDO();
                administerCompanyMappingDO.setAdministerId(administerDO.getId());
                administerCompanyMappingDO.setCompanyId(companyId);
                administerCompanyMappingMapper.insert(administerCompanyMappingDO);
            }
        }

        if (null != addAdministerDTO.getServiceIds()) {
            List<Long> serviceIds = addAdministerDTO.getServiceIds();
            for (Long service : serviceIds) {
                ServiceAdministerMappingDO serviceAdministerMappingDO = new ServiceAdministerMappingDO();
                serviceAdministerMappingDO.setAdministerId(administerDO.getId());
                serviceAdministerMappingDO.setServiceId(service);
                serviceAdministerMappingMapper.insert(serviceAdministerMappingDO);
            }
        }

        return ConstantUtils.ADD_SUCCESS;
    }

    @Transactional(rollbackFor = Exception.class)
    public String deleteAdminister(AdministerDO administerDO) {
        this.baseMapper.deleteById(administerDO.getId());

        administerCompanyMappingMapper.delete(new QueryWrapper<AdministerCompanyMappingDO>()
                .lambda()
                .eq(AdministerCompanyMappingDO::getAdministerId, administerDO.getId()));

        serviceAdministerMappingMapper.delete(new QueryWrapper<ServiceAdministerMappingDO>()
                .lambda()
                .eq(ServiceAdministerMappingDO::getAdministerId, administerDO.getId()));

        return ConstantUtils.DELETE_SUCCESS;
    }

    public String updateAdminister(AddAdministerDTO addAdministerDTO) {

        int count = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getAccountName, addAdministerDTO.getAccountName())
                .ne(AdministerDO::getId, addAdministerDTO.getId()));

        int count1 = this.baseMapper.selectCount(new QueryWrapper<AdministerDO>()
                .lambda()
                .eq(AdministerDO::getTelephone, addAdministerDTO.getTelephone())
                .ne(AdministerDO::getId, addAdministerDTO.getId()));

        if (count + count1 > 0) {
            throw new HttpException(20002);
        }
        AdministerDO administerDO = this.baseMapper.selectById(addAdministerDTO.getId());
        BeanUtils.copyProperties(addAdministerDTO, administerDO);
        this.baseMapper.updateById(administerDO);

        if (null != addAdministerDTO.getCompanyIds()) {

            //
            List<Long> oldIds = this.baseMapper.administerCompanyIds(addAdministerDTO.getId());
            List<Long> newIds = addAdministerDTO.getCompanyIds();

            List<Long> newCompanyIds2 = new ArrayList<>();
            for (Long lg : newIds) {
                newCompanyIds2.add(lg);
            }

            newIds.removeAll(oldIds); //
            if (newIds.size() > 0) {
                for (Long lg : newIds) {
                    AdministerCompanyMappingDO administerCompanyMappingDO = new AdministerCompanyMappingDO();
                    administerCompanyMappingDO.setAdministerId(addAdministerDTO.getId());
                    administerCompanyMappingDO.setCompanyId(lg);
                    administerCompanyMappingMapper.insert(administerCompanyMappingDO);
                }
            }

            oldIds.removeAll(newCompanyIds2);
            if (oldIds.size() > 0) {
                for (Long lg : oldIds) {
                    administerCompanyMappingMapper.delete(new QueryWrapper<AdministerCompanyMappingDO>()
                            .lambda()
                            .eq(AdministerCompanyMappingDO::getAdministerId, addAdministerDTO.getId())
                            .eq(AdministerCompanyMappingDO::getCompanyId, lg));
                }
            }
        }

        if (null != addAdministerDTO.getServiceIds()) {

            //
            List<Long> oldIds = this.baseMapper.administerServiceIds(addAdministerDTO.getId());
            List<Long> newIds = addAdministerDTO.getServiceIds();

            List<Long> newServices2 = new ArrayList<>();
            for (Long lg : newIds) {
                newServices2.add(lg);
            }

            newIds.removeAll(oldIds); //
            if (newIds.size() > 0) {
                for (Long lg : newIds) {
                    ServiceAdministerMappingDO serviceAdministerMappingDO = new ServiceAdministerMappingDO();
                    serviceAdministerMappingDO.setAdministerId(addAdministerDTO.getId());
                    serviceAdministerMappingDO.setServiceId(lg);
                    serviceAdministerMappingMapper.insert(serviceAdministerMappingDO);
                }
            }

            oldIds.removeAll(newServices2);
            if (oldIds.size() > 0) {
                for (Long lg : oldIds) {
                    serviceAdministerMappingMapper.delete(new QueryWrapper<ServiceAdministerMappingDO>()
                            .lambda()
                            .eq(ServiceAdministerMappingDO::getAdministerId, addAdministerDTO.getId())
                            .eq(ServiceAdministerMappingDO::getServiceId, lg));
                }
            }
        }

        return ConstantUtils.SET_SUCCESS;
    }

    public String userName(String originName, int i, Long companyId) {
        i++;
        List<MemberDO> memberDOS = memberMapper.selectList(new QueryWrapper<MemberDO>()
                .lambda()
                .eq(MemberDO::getAccountName, originName + i));
        if (memberDOS.size() > 0) {
            return userName(originName, i, companyId);
        } else {
            return originName + i;
        }
    }

}