123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349 |
- package com.fdkankan.manage_jp.service.impl;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
- import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
- import com.fdkankan.common.util.DateUtil;
- import com.fdkankan.common.util.SecurityUtil;
- import com.fdkankan.manage_jp.common.PageInfo;
- import com.fdkankan.manage_jp.common.ResultCode;
- import com.fdkankan.manage_jp.entity.*;
- import com.fdkankan.manage_jp.exception.BusinessException;
- import com.fdkankan.manage_jp.mapper.ICompanyMapper;
- import com.fdkankan.manage_jp.service.*;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.fdkankan.manage_jp.vo.request.RequestCompany;
- import com.fdkankan.manage_jp.vo.response.ResponseCompany;
- import com.github.yulichang.wrapper.MPJLambdaWrapper;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.BeanUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import org.springframework.util.CollectionUtils;
- import org.springframework.util.ObjectUtils;
- import org.springframework.util.StringUtils;
- import java.util.Date;
- import java.util.List;
- import java.util.stream.Collectors;
- /**
- * <p>
- * 客户企业logo信息 服务实现类
- * </p>
- *
- * @author
- * @since 2022-12-23
- */
- @Service
- @Slf4j
- public class CompanyServiceImpl extends ServiceImpl<ICompanyMapper, Company> implements ICompanyService {
- @Autowired
- IUserService userService;
- @Autowired
- IUserRoleService userRoleService;
- @Autowired
- ICameraService cameraService;
- @Autowired
- ICameraDetailService cameraDetailService;
- @Autowired
- ISceneProService sceneProService;
- @Autowired
- IMailTemplateService mailTemplateService;
- @Override
- public void insertOrUpdate(RequestCompany param) {
- Company entity = new Company();
- if(param.getId() != null){
- entity = this.getById(param.getId());
- BeanUtils.copyProperties(param, entity);
- entity.setUpdateTime(null);
- this.updateById(entity);
- return;
- }
- // 判断邮箱是否注册过
- User userEntity = userService.getByUserName(param.getManagerPhone());
- if(!ObjectUtils.isEmpty(userEntity)){
- throw new BusinessException(ResultCode.USER_EXIST);
- }
- BeanUtils.copyProperties(param, entity);
- entity.setState(1);
- entity.setAuditTime(new Date());
- this.save(entity);
- userEntity = new User();
- userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
- userEntity.setUserName(param.getManagerPhone());
- userEntity.setCompanyId(entity.getId());
- userEntity.setNickName(param.getManagerName());
- userEntity.setCreateTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
- userEntity.setUpdateTime(new Date());
- userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
- userEntity.setRecStatus("A");
- userService.save(userEntity);
- UserRole userRoleEntity = new UserRole();
- userRoleEntity.setUserId(userEntity.getId());
- userRoleEntity.setRoleId(6L);
- userRoleService.save(userRoleEntity);
- entity.setManagerId(userEntity.getId());
- entity.setUpdateTime(null);
- this.updateById(entity);
- }
- @Override
- public void auditCompany(Company bo) {
- if(bo == null || bo.getId() == null){
- throw new BusinessException(ResultCode.PARAM_ERROR);
- }
- Company companyEntity = this.getById(bo.getId());
- if(ObjectUtils.isEmpty(companyEntity)){
- throw new BusinessException(ResultCode.PARAM_ERROR);
- }
- bo.setAuditTime(new Date());
- String expirationDate = DateUtil.date2String(new Date(), DateUtil.YYYY_MM_DD_DATE_FORMAT);
- bo.setExpirationTime(DateUtil.string2Date(expirationDate + " 23:59:59", null));
- bo.setUpdateTime(null);
- bo.setCreateTime(null);
- this.updateById(bo);
- // 等于审核通过
- if(bo.getState() == 1){
- // 发送企业认证通过邮件
- User userEntity = userService.getById(companyEntity.getManagerId());
- if(!ObjectUtils.isEmpty(userEntity)){
- log.info("发送审核通过邮件:{}",userEntity.getUserName());
- MailTemplate mailTemplate = mailTemplateService.getById(1);
- Boolean mail = mailTemplateService.sendMail(userEntity.getUserName(), mailTemplate,null);
- if(!mail){
- throw new BusinessException(ResultCode.MAIL_SEND_ERROR);
- }
- }
- }
- }
- @Override
- public void addPoint(Long id, Integer point) {
- Company company = getById(id);
- if(company == null){
- throw new BusinessException(ResultCode.NOT_RECORD);
- }
- point = company.getPoint() + point;
- if (point < 0) {
- throw new BusinessException(ResultCode.POINT_GT_ZERO);
- }
- LambdaUpdateWrapper<Company> wrapper = new LambdaUpdateWrapper<>();
- wrapper.eq(Company::getId,id);
- wrapper.set(Company::getPoint,point);
- this.update(wrapper);
- }
- @Override
- public void checkDevice(String childName) {
- Camera instance = cameraService.findByChildName(childName);
- if(ObjectUtils.isEmpty(instance)){
- throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
- }
- }
- @Override
- public PageInfo<ResponseCompany> companyList(RequestCompany param) {
- MPJLambdaWrapper<Company> wrapper = new MPJLambdaWrapper<Company>()
- .selectAll(Company.class)
- .selectAll(User.class)
- .leftJoin(User.class,User::getId,Company::getManagerId);
- if(!StringUtils.isEmpty(param.getManagerName())){
- wrapper.like(User::getUserName,param.getManagerName());
- }
- wrapper.eq(Company :: getState ,1);
- wrapper.orderByDesc(Company::getCreateTime);
- Page<Company> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
- List<ResponseCompany> list = page.getRecords().stream().map(entity->{
- ResponseCompany company = new ResponseCompany();
- BeanUtils.copyProperties(entity,company);
- if(entity.getManagerId() != null){
- User userEntity = userService.getById(entity.getManagerId());
- if(userEntity != null){
- company.setManagerName(userEntity.getUserName());
- company.setManagerPhone(userEntity.getUserName());
- }
- }
- company.setSceneNum(0);
- List<User> users = userService.findByCompanyId(company.getId());
- company.setTotalSubNum(company.getSubNum());
- // 去除企业账号
- company.setSubNum(users.size());
- if (users.size() > 0) {
- company.setSubNum(users.size() - 1);
- }
- List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
- if(!CollectionUtils.isEmpty(userIds)){
- Long sceneNum = sceneProService.getCountByUserIds(userIds);
- company.setSceneNum(Math.toIntExact(sceneNum));
- }
- int cameraNum = cameraDetailService.findCountByCompanyId(company.getId());
- company.setCameraNum(cameraNum);
- return company;
- }).collect(Collectors.toList());
- Page<ResponseCompany> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
- pageVo.setRecords(list);
- pageVo.setTotal(page.getTotal());
- return PageInfo.PageInfo(pageVo);
- }
- @Override
- public PageInfo selectCompanyByType(RequestCompany param) {
- LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
- if(!StringUtils.isEmpty(param.getCompanyName())){
- wrapper.eq(Company::getCompanyName,param.getCompanyName());
- }
- if(!StringUtils.isEmpty(param.getState())){
- wrapper.eq(Company::getState,param.getState());
- }
- if(!ObjectUtils.isEmpty(param.getStartTime())){
- String startTime = param.getStartTime().split(" ")[0]+" 00:00:00";
- wrapper.ge(Company::getCreateTime,startTime);
- }
- if(!ObjectUtils.isEmpty(param.getUserName())){
- List<User> nameList = userService.findByUserNameList(param.getUserName());
- if(CollectionUtils.isEmpty(nameList)){
- return PageInfo.PageInfoEmpty(param.getPageNum(),param.getPageSize());
- }
- List<Long> companyIds = nameList.stream().map(User::getCompanyId).collect(Collectors.toList());
- wrapper.in(Company::getId,companyIds);
- }
- if(!ObjectUtils.isEmpty(param.getEndTime())){
- String endTime = param.getEndTime().split(" ")[0]+" 23:59:59";
- wrapper.le(Company::getCreateTime,endTime);
- }
- wrapper.orderByDesc(Company::getId);
- Page<Company> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
- List<ResponseCompany> list = page.getRecords().stream().map(entity->{
- ResponseCompany company = new ResponseCompany();
- BeanUtils.copyProperties(entity,company);
- if(entity.getManagerId() != null){
- User userEntity = userService.getById(entity.getManagerId());
- if(userEntity != null){
- company.setManagerName(userEntity.getUserName());
- company.setManagerPhone(userEntity.getUserName());
- }
- }
- company.setSceneNum(0);
- return company;
- }).collect(Collectors.toList());
- Page<ResponseCompany> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
- pageVo.setRecords(list);
- pageVo.setTotal(page.getTotal());
- return PageInfo.PageInfo(pageVo);
- }
- @Override
- public void saveSubUsers(RequestCompany bo) {
- Company tbCompany = getById(bo.getId());
- if(ObjectUtils.isEmpty(tbCompany)){
- throw new BusinessException("企业数据查询失败");
- }
- // 更新子账号数量
- tbCompany.setSubNum(bo.getSubNum());
- tbCompany.setUpdateTime(null);
- this.updateById(tbCompany);
- Date currentDate = new Date();
- List<User> oldUsers = userService.findByCompanyId(tbCompany.getId());
- oldUsers = oldUsers.stream().filter(param -> param.getId() - tbCompany.getManagerId() != 0).collect(Collectors.toList());
- // 获取编辑的子账号
- oldUsers.stream().forEach(user -> {
- bo.getSubUsers().stream().filter(param -> user.getId().equals(param.getId()))
- .filter(param -> !org.apache.commons.lang3.StringUtils.equals(param.getNickName(), user.getNickName()))
- .forEach(upUser -> {
- user.setNickName(upUser.getNickName());
- user.setUpdateTime(currentDate);
- userService.updateById(user);
- }
- );
- });
- // 获取解绑的子账号
- oldUsers.stream().forEach(user -> {
- if (bo.getSubUsers().stream().noneMatch(param -> user.getId().equals(param.getId()))) {
- // 将场景重新绑定到企业账号下面
- sceneProService.rebindUser(user.getId(),tbCompany.getManagerId());
- cameraDetailService.unbindUser(user.getId(),null);
- userService.removeById(user.getId());
- }
- });
- // 获取新增的子账号
- bo.getSubUsers().stream().filter(user -> ObjectUtils.isEmpty(user.getId())).forEach(user -> {
- // 获取用户是否存在
- User userEntity = userService.findByUserName(user.getUserName());
- if (!ObjectUtils.isEmpty(userEntity)) {
- if (!ObjectUtils.isEmpty(userEntity.getCompanyId())) {
- throw new BusinessException(user.getUserName() + "已经被绑定,请确认!");
- }
- }else{
- userEntity = new User();
- userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
- }
- userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
- userEntity.setUserName(user.getUserName());
- userEntity.setCompanyId(tbCompany.getId());
- userEntity.setNickName(user.getNickName());
- userEntity.setCreateTime(DateUtil.date2String(currentDate,"yyyy-MM-dd HH:mm:ss"));
- userEntity.setUpdateTime(currentDate);
- userEntity.setRecStatus("A");
- if (ObjectUtils.isEmpty(userEntity.getId())) {
- userService.save(userEntity);
- } else {
- userService.updateById(userEntity);
- }
- // 保存用户员工角色
- UserRole userRole = new UserRole();
- userRole.setUserId(userEntity.getId());
- userRole.setRoleId(8L);
- userRoleService.save(userRole);
- });
- }
- @Override
- public Company getByUserName(String userName) {
- User user = userService.getByUserName(userName);
- if(user != null && user.getCompanyId() != null){
- return this.getById(user.getCompanyId());
- }
- return null;
- }
- @Override
- public Company getByCameraId(Long cameraId) {
- CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
- if(cameraDetail == null || cameraDetail.getCameraId() == null){
- return null;
- }
- return this.getById(cameraDetail.getCompanyId());
- }
- @Override
- public Company getByChildName(String childName) {
- CameraDetail cameraDetail = cameraDetailService.getByChildName(childName);
- if(cameraDetail == null || cameraDetail.getCameraId() == null){
- return null;
- }
- return this.getById(cameraDetail.getCompanyId());
- }
- @Override
- public List<Company> getLikeCompanyName(String companyName) {
- LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
- wrapper.like(Company::getCompanyName,companyName);
- return this.list(wrapper);
- }
- }
|