CompanyServiceImpl.java 14 KB

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