CompanyServiceImpl.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. package com.fdkankan.manage_jp.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.fdkankan.common.util.DateUtil;
  7. import com.fdkankan.common.util.SecurityUtil;
  8. import com.fdkankan.manage_jp.common.PageInfo;
  9. import com.fdkankan.manage_jp.common.ResultCode;
  10. import com.fdkankan.manage_jp.entity.*;
  11. import com.fdkankan.manage_jp.exception.BusinessException;
  12. import com.fdkankan.manage_jp.mapper.ICompanyMapper;
  13. import com.fdkankan.manage_jp.service.*;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.fdkankan.manage_jp.vo.request.RequestCompany;
  16. import com.fdkankan.manage_jp.vo.response.ResponseCompany;
  17. import com.github.yulichang.wrapper.MPJLambdaWrapper;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.beans.BeanUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import org.springframework.util.CollectionUtils;
  23. import org.springframework.util.ObjectUtils;
  24. import org.springframework.util.StringUtils;
  25. import java.util.Date;
  26. import java.util.List;
  27. import java.util.stream.Collectors;
  28. /**
  29. * <p>
  30. * 客户企业logo信息 服务实现类
  31. * </p>
  32. *
  33. * @author
  34. * @since 2022-12-23
  35. */
  36. @Service
  37. @Slf4j
  38. public class CompanyServiceImpl extends ServiceImpl<ICompanyMapper, Company> implements ICompanyService {
  39. @Autowired
  40. IUserService userService;
  41. @Autowired
  42. IUserRoleService userRoleService;
  43. @Autowired
  44. ICameraService cameraService;
  45. @Autowired
  46. ICameraDetailService cameraDetailService;
  47. @Autowired
  48. ISceneProService sceneProService;
  49. @Autowired
  50. IMailTemplateService mailTemplateService;
  51. @Override
  52. public void insertOrUpdate(RequestCompany param) {
  53. Company entity = new Company();
  54. if(param.getId() != null){
  55. entity = this.getById(param.getId());
  56. BeanUtils.copyProperties(param, entity);
  57. entity.setUpdateTime(null);
  58. this.updateById(entity);
  59. return;
  60. }
  61. // 判断邮箱是否注册过
  62. User userEntity = userService.getByUserName(param.getManagerPhone());
  63. if(!ObjectUtils.isEmpty(userEntity)){
  64. throw new BusinessException(ResultCode.USER_EXIST);
  65. }
  66. BeanUtils.copyProperties(param, entity);
  67. entity.setState(1);
  68. entity.setAuditTime(new Date());
  69. this.save(entity);
  70. userEntity = new User();
  71. userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
  72. userEntity.setUserName(param.getManagerPhone());
  73. userEntity.setCompanyId(entity.getId());
  74. userEntity.setNickName(param.getManagerName());
  75. userEntity.setCreateTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
  76. userEntity.setUpdateTime(new Date());
  77. userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
  78. userEntity.setRecStatus("A");
  79. userService.save(userEntity);
  80. UserRole userRoleEntity = new UserRole();
  81. userRoleEntity.setUserId(userEntity.getId());
  82. userRoleEntity.setRoleId(6L);
  83. userRoleService.save(userRoleEntity);
  84. entity.setManagerId(userEntity.getId());
  85. entity.setUpdateTime(null);
  86. this.updateById(entity);
  87. }
  88. @Override
  89. public void auditCompany(Company bo) {
  90. if(bo == null || bo.getId() == null){
  91. throw new BusinessException(ResultCode.PARAM_ERROR);
  92. }
  93. Company companyEntity = this.getById(bo.getId());
  94. if(ObjectUtils.isEmpty(companyEntity)){
  95. throw new BusinessException(ResultCode.PARAM_ERROR);
  96. }
  97. bo.setAuditTime(new Date());
  98. String expirationDate = DateUtil.date2String(new Date(), DateUtil.YYYY_MM_DD_DATE_FORMAT);
  99. bo.setExpirationTime(DateUtil.string2Date(expirationDate + " 23:59:59", null));
  100. bo.setUpdateTime(null);
  101. bo.setCreateTime(null);
  102. this.updateById(bo);
  103. // 等于审核通过
  104. if(bo.getState() == 1){
  105. // 发送企业认证通过邮件
  106. User userEntity = userService.getById(companyEntity.getManagerId());
  107. if(!ObjectUtils.isEmpty(userEntity)){
  108. log.info("发送审核通过邮件:{}",userEntity.getUserName());
  109. MailTemplate mailTemplate = mailTemplateService.getById(1);
  110. Boolean mail = mailTemplateService.sendMail(userEntity.getUserName(), mailTemplate,null);
  111. if(!mail){
  112. throw new BusinessException(ResultCode.MAIL_SEND_ERROR);
  113. }
  114. }
  115. }
  116. }
  117. @Override
  118. public void addPoint(Long id, Integer point) {
  119. Company company = getById(id);
  120. if(company == null){
  121. throw new BusinessException(ResultCode.NOT_RECORD);
  122. }
  123. point = company.getPoint() + point;
  124. if (point < 0) {
  125. throw new BusinessException(ResultCode.POINT_GT_ZERO);
  126. }
  127. LambdaUpdateWrapper<Company> wrapper = new LambdaUpdateWrapper<>();
  128. wrapper.eq(Company::getId,id);
  129. wrapper.set(Company::getPoint,point);
  130. this.update(wrapper);
  131. }
  132. @Override
  133. public void checkDevice(String childName) {
  134. Camera instance = cameraService.findByChildName(childName);
  135. if(ObjectUtils.isEmpty(instance)){
  136. throw new BusinessException(ResultCode.CAMERA_NOT_EXIST);
  137. }
  138. }
  139. @Override
  140. public PageInfo<ResponseCompany> companyList(RequestCompany param) {
  141. MPJLambdaWrapper<Company> wrapper = new MPJLambdaWrapper<Company>()
  142. .selectAll(Company.class)
  143. .selectAll(User.class)
  144. .leftJoin(User.class,User::getId,Company::getManagerId);
  145. if(!StringUtils.isEmpty(param.getManagerName())){
  146. wrapper.like(User::getUserName,param.getManagerName());
  147. }
  148. wrapper.eq(Company :: getState ,1);
  149. wrapper.orderByDesc(Company::getCreateTime);
  150. Page<Company> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  151. List<ResponseCompany> list = page.getRecords().stream().map(entity->{
  152. ResponseCompany company = new ResponseCompany();
  153. BeanUtils.copyProperties(entity,company);
  154. if(entity.getManagerId() != null){
  155. User userEntity = userService.getById(entity.getManagerId());
  156. if(userEntity != null){
  157. company.setManagerName(userEntity.getUserName());
  158. company.setManagerPhone(userEntity.getUserName());
  159. }
  160. }
  161. company.setSceneNum(0);
  162. List<User> users = userService.findByCompanyId(company.getId());
  163. company.setTotalSubNum(company.getSubNum());
  164. // 去除企业账号
  165. company.setSubNum(users.size());
  166. if (users.size() > 0) {
  167. company.setSubNum(users.size() - 1);
  168. }
  169. List<Long> userIds = users.stream().map(User::getId).collect(Collectors.toList());
  170. if(!CollectionUtils.isEmpty(userIds)){
  171. Long sceneNum = sceneProService.getCountByUserIds(userIds);
  172. company.setSceneNum(Math.toIntExact(sceneNum));
  173. }
  174. int cameraNum = cameraDetailService.findCountByCompanyId(company.getId());
  175. company.setCameraNum(cameraNum);
  176. return company;
  177. }).collect(Collectors.toList());
  178. Page<ResponseCompany> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
  179. pageVo.setRecords(list);
  180. pageVo.setTotal(page.getTotal());
  181. return PageInfo.PageInfo(pageVo);
  182. }
  183. @Override
  184. public PageInfo selectCompanyByType(RequestCompany param) {
  185. LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
  186. if(!StringUtils.isEmpty(param.getCompanyName())){
  187. wrapper.eq(Company::getCompanyName,param.getCompanyName());
  188. }
  189. if(!StringUtils.isEmpty(param.getState())){
  190. wrapper.eq(Company::getState,param.getState());
  191. }
  192. if(!ObjectUtils.isEmpty(param.getStartTime())){
  193. String startTime = param.getStartTime().split(" ")[0]+" 00:00:00";
  194. wrapper.ge(Company::getCreateTime,startTime);
  195. }
  196. if(!ObjectUtils.isEmpty(param.getUserName())){
  197. List<User> nameList = userService.findByUserNameList(param.getUserName());
  198. if(CollectionUtils.isEmpty(nameList)){
  199. return PageInfo.PageInfoEmpty(param.getPageNum(),param.getPageSize());
  200. }
  201. List<Long> companyIds = nameList.stream().map(User::getCompanyId).collect(Collectors.toList());
  202. wrapper.in(Company::getId,companyIds);
  203. }
  204. if(!ObjectUtils.isEmpty(param.getEndTime())){
  205. String endTime = param.getEndTime().split(" ")[0]+" 23:59:59";
  206. wrapper.le(Company::getCreateTime,endTime);
  207. }
  208. wrapper.orderByDesc(Company::getId);
  209. Page<Company> page = this.page(new Page<>(param.getPageNum(), param.getPageSize()), wrapper);
  210. List<ResponseCompany> list = page.getRecords().stream().map(entity->{
  211. ResponseCompany company = new ResponseCompany();
  212. BeanUtils.copyProperties(entity,company);
  213. if(entity.getManagerId() != null){
  214. User userEntity = userService.getById(entity.getManagerId());
  215. if(userEntity != null){
  216. company.setManagerName(userEntity.getUserName());
  217. company.setManagerPhone(userEntity.getUserName());
  218. }
  219. }
  220. company.setSceneNum(0);
  221. return company;
  222. }).collect(Collectors.toList());
  223. Page<ResponseCompany> pageVo = new Page<>(param.getPageNum(),param.getPageSize());
  224. pageVo.setRecords(list);
  225. pageVo.setTotal(page.getTotal());
  226. return PageInfo.PageInfo(pageVo);
  227. }
  228. @Override
  229. public void saveSubUsers(RequestCompany bo) {
  230. Company tbCompany = getById(bo.getId());
  231. if(ObjectUtils.isEmpty(tbCompany)){
  232. throw new BusinessException("企业数据查询失败");
  233. }
  234. // 更新子账号数量
  235. tbCompany.setSubNum(bo.getSubNum());
  236. tbCompany.setUpdateTime(null);
  237. this.updateById(tbCompany);
  238. Date currentDate = new Date();
  239. List<User> oldUsers = userService.findByCompanyId(tbCompany.getId());
  240. oldUsers = oldUsers.stream().filter(param -> param.getId() - tbCompany.getManagerId() != 0).collect(Collectors.toList());
  241. // 获取编辑的子账号
  242. oldUsers.stream().forEach(user -> {
  243. bo.getSubUsers().stream().filter(param -> user.getId().equals(param.getId()))
  244. .filter(param -> !org.apache.commons.lang3.StringUtils.equals(param.getNickName(), user.getNickName()))
  245. .forEach(upUser -> {
  246. user.setNickName(upUser.getNickName());
  247. user.setUpdateTime(currentDate);
  248. userService.updateById(user);
  249. }
  250. );
  251. });
  252. // 获取解绑的子账号
  253. oldUsers.stream().forEach(user -> {
  254. if (bo.getSubUsers().stream().noneMatch(param -> user.getId().equals(param.getId()))) {
  255. // 将场景重新绑定到企业账号下面
  256. sceneProService.rebindUser(user.getId(),tbCompany.getManagerId());
  257. cameraDetailService.unbindUser(user.getId(),null);
  258. userService.removeById(user.getId());
  259. }
  260. });
  261. // 获取新增的子账号
  262. bo.getSubUsers().stream().filter(user -> ObjectUtils.isEmpty(user.getId())).forEach(user -> {
  263. // 获取用户是否存在
  264. User userEntity = userService.findByUserName(user.getUserName());
  265. if (!ObjectUtils.isEmpty(userEntity)) {
  266. if (!ObjectUtils.isEmpty(userEntity.getCompanyId())) {
  267. throw new BusinessException(user.getUserName() + "已经被绑定,请确认!");
  268. }
  269. }else{
  270. userEntity = new User();
  271. userEntity.setRegisterTime(DateUtil.date2String(new Date(),"yyyy-MM-dd HH:mm:ss"));
  272. }
  273. userEntity.setPassword(SecurityUtil.MD5("Geosign123"));
  274. userEntity.setUserName(user.getUserName());
  275. userEntity.setCompanyId(tbCompany.getId());
  276. userEntity.setNickName(user.getNickName());
  277. userEntity.setCreateTime(DateUtil.date2String(currentDate,"yyyy-MM-dd HH:mm:ss"));
  278. userEntity.setUpdateTime(currentDate);
  279. userEntity.setRecStatus("A");
  280. if (ObjectUtils.isEmpty(userEntity.getId())) {
  281. userService.save(userEntity);
  282. } else {
  283. userService.updateById(userEntity);
  284. }
  285. // 保存用户员工角色
  286. UserRole userRole = new UserRole();
  287. userRole.setUserId(userEntity.getId());
  288. userRole.setRoleId(8L);
  289. userRoleService.save(userRole);
  290. });
  291. }
  292. @Override
  293. public Company getByUserName(String userName) {
  294. User user = userService.getByUserName(userName);
  295. if(user != null && user.getCompanyId() != null){
  296. return this.getById(user.getCompanyId());
  297. }
  298. return null;
  299. }
  300. @Override
  301. public Company getByCameraId(Long cameraId) {
  302. CameraDetail cameraDetail = cameraDetailService.getByCameraId(cameraId);
  303. if(cameraDetail == null || cameraDetail.getCameraId() == null){
  304. return null;
  305. }
  306. return this.getById(cameraDetail.getCompanyId());
  307. }
  308. @Override
  309. public Company getByChildName(String childName) {
  310. CameraDetail cameraDetail = cameraDetailService.getByChildName(childName);
  311. if(cameraDetail == null || cameraDetail.getCameraId() == null){
  312. return null;
  313. }
  314. return this.getById(cameraDetail.getCompanyId());
  315. }
  316. @Override
  317. public List<Company> getLikeCompanyName(String companyName) {
  318. LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();
  319. wrapper.like(Company::getCompanyName,companyName);
  320. return this.list(wrapper);
  321. }
  322. }