package com.changhu.service.impl; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.changhu.common.enums.ResultCode; import com.changhu.common.exception.MessageException; import com.changhu.common.pojo.model.JsonResult; import com.changhu.module.management.mapper.PoliceUnitMapper; import com.changhu.module.management.mapper.SecurityUnitMapper; import com.changhu.module.management.pojo.entity.PoliceUnit; import com.changhu.module.management.pojo.entity.SecurityUnit; import com.changhu.pojo.params.PoliceUnitRegisterParams; import com.changhu.pojo.params.SecurityUnitRegisterParams; import com.changhu.service.CommonService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Optional; /** * @author 20252 * @createTime 2024/8/30 下午3:18 * @desc CommonServiceImpl... */ @Service public class CommonServiceImpl implements CommonService { @Autowired private SecurityUnitMapper securityUnitMapper; @Autowired private PoliceUnitMapper policeUnitMapper; @Override public JsonResult securityUnitRegister(SecurityUnitRegisterParams params) { //先查看该社会编码是否存在 boolean exists = securityUnitMapper.exists(Wrappers.lambdaQuery().eq(SecurityUnit::getSocialCode, params.getSocialCode())); if (exists) { throw new MessageException("该单位已存在或已提交申请!"); } SecurityUnit securityUnit = BeanUtil.copyProperties(params, SecurityUnit.class); Optional.ofNullable(params.getAdministrativeDivisionCodes()) .ifPresent(codes -> { if (!codes.isEmpty()) { securityUnit.setProvince(codes.get(0)); } if (codes.size() >= 2) { securityUnit.setCity(codes.get(1)); } if (codes.size() >= 3) { securityUnit.setDistricts(codes.get(2)); } if (codes.size() >= 4) { securityUnit.setStreet(codes.get(3)); } }); int i = securityUnitMapper.insert(securityUnit); if (i <= 0) { throw new MessageException(ResultCode.ERROR); } return JsonResult.successMsg("申请已提交,请等待审核"); } @Override public JsonResult policeUnitRegister(PoliceUnitRegisterParams params) { //先查看该公安单位是否存在 boolean exists = policeUnitMapper.exists(Wrappers.lambdaQuery().eq(PoliceUnit::getCode, params.getCode())); if (exists) { throw new MessageException("该单位已存在或已提交申请!"); } PoliceUnit policeUnit = BeanUtil.copyProperties(params, PoliceUnit.class); Optional.ofNullable(params.getAdministrativeDivisionCodes()) .ifPresent(codes -> { if (!codes.isEmpty()) { policeUnit.setProvince(codes.get(0)); } if (codes.size() >= 2) { policeUnit.setCity(codes.get(1)); } if (codes.size() >= 3) { policeUnit.setDistricts(codes.get(2)); } if (codes.size() >= 4) { policeUnit.setStreet(codes.get(3)); } }); int i = policeUnitMapper.insert(policeUnit); if (i <= 0) { throw new MessageException(ResultCode.ERROR); } return JsonResult.successMsg("申请已提交,请等待审核"); } }