|
@@ -1,10 +1,36 @@
|
|
|
package com.fdkankan.manage_jp.service.impl;
|
|
|
|
|
|
-import com.fdkankan.manage_jp.entity.Company;
|
|
|
+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.Constant;
|
|
|
+import com.fdkankan.manage_jp.common.PageInfo;
|
|
|
+import com.fdkankan.manage_jp.common.Result;
|
|
|
+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.ICompanyService;
|
|
|
+import com.fdkankan.manage_jp.service.*;
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import com.fdkankan.manage_jp.util.SendMailUtils;
|
|
|
+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.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* <p>
|
|
@@ -12,9 +38,275 @@ import org.springframework.stereotype.Service;
|
|
|
* </p>
|
|
|
*
|
|
|
* @author
|
|
|
- * @since 2022-12-22
|
|
|
+ * @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);
|
|
|
+ }
|
|
|
+ entity.setState(1);
|
|
|
+ BeanUtils.copyProperties(param, entity);
|
|
|
+ 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(new Date());
|
|
|
+ userEntity.setUpdateTime(new Date());
|
|
|
+ userEntity.setRegisterTime(new Date());
|
|
|
+ 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) || !ObjectUtils.isEmpty(instance.getActivatedTime())){
|
|
|
+ throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo<ResponseCompany> companyList(RequestCompany param) {
|
|
|
+ MPJLambdaWrapper<Company> wrapper = new MPJLambdaWrapper<Company>()
|
|
|
+ .selectAll(Company.class)
|
|
|
+ .leftJoin(User.class,User::getId,Company::getManagerId);
|
|
|
+ if(!StringUtils.isEmpty(param.getManagerName())){
|
|
|
+ wrapper.like(User::getUserName,param.getUserName());
|
|
|
+ }
|
|
|
+ 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 JSONObject 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 new JSONObject();
|
|
|
+ }
|
|
|
+ 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());
|
|
|
+ JSONObject result = JSONObject.parseObject(JSONObject.toJSONString(param));
|
|
|
+ result.put("list",list);
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @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());
|
|
|
+ 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(new Date());
|
|
|
+ }
|
|
|
+ userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
|
|
|
+ userEntity.setUserName(user.getUserName());
|
|
|
+ userEntity.setCompanyId(tbCompany.getId());
|
|
|
+ userEntity.setNickName(user.getNickName());
|
|
|
+ userEntity.setCreateTime(currentDate);
|
|
|
+ userEntity.setUpdateTime(currentDate);
|
|
|
+ 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);
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|