|
@@ -0,0 +1,235 @@
|
|
|
+package com.care.bms.service;
|
|
|
+
|
|
|
+
|
|
|
+import cn.hutool.core.bean.BeanUtil;
|
|
|
+import cn.hutool.core.collection.CollUtil;
|
|
|
+import cn.hutool.core.util.StrUtil;
|
|
|
+import cn.hutool.json.JSONArray;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.metadata.IPage;
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import com.care.common.entity.*;
|
|
|
+
|
|
|
+import com.care.common.enums.UserRoleEnum;
|
|
|
+import com.care.common.exception.BDException;
|
|
|
+import com.care.common.service.CareDeviceService;
|
|
|
+import com.care.common.service.CareOrganizationService;
|
|
|
+import com.care.common.service.CareStationService;
|
|
|
+import com.care.common.service.CareSysUserService;
|
|
|
+import com.care.common.util.DateUtils;
|
|
|
+
|
|
|
+import com.care.common.util.HttpUtil;
|
|
|
+import com.care.common.util.ImgUtil;
|
|
|
+import com.care.common.util.Result;
|
|
|
+import com.care.common.vo.org.OrganizationVO;
|
|
|
+import com.care.common.vo.PageReqVO;
|
|
|
+import com.care.common.vo.UserLogindConvertVO;
|
|
|
+
|
|
|
+import com.care.common.vo.station.CareStationVO;
|
|
|
+import com.care.common.vo.sysuser.CareSysUserVO;
|
|
|
+import org.apache.commons.codec.digest.DigestUtils;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.annotation.Resource;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @Author: lilt
|
|
|
+ * @Date: 2021/7/14
|
|
|
+ * @Desc:
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class OrganizationService {
|
|
|
+ @Resource
|
|
|
+ private CareOrganizationService careOrganizationService;
|
|
|
+ @Resource
|
|
|
+ private CareDeviceService careDeviceService;
|
|
|
+ @Resource
|
|
|
+ private CareStationService careStationService;
|
|
|
+ @Resource
|
|
|
+ private CareSysUserService careSysUserService;
|
|
|
+ @Resource
|
|
|
+ private StationService stationService;
|
|
|
+
|
|
|
+ @Value("${file.savePath:#{null}}")
|
|
|
+ private String savePath;
|
|
|
+ @Value("${file.url:#{null}}")
|
|
|
+ private String fileUrl;
|
|
|
+
|
|
|
+ @Value("${syn.org.url:#{null}}")
|
|
|
+ private String synOrgUrl;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询服务站列表
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public IPage<OrganizationVO> listOrganization(String name, String systemName, UserLogindConvertVO loginUser, PageReqVO pageReqVo) {
|
|
|
+ IPage<CareOrganization> page = new Page<>(pageReqVo.getCurrent(), pageReqVo.getPageSize());
|
|
|
+ QueryWrapper<CareOrganization> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().like(StrUtil.isNotEmpty(name), CareOrganization::getName, name)
|
|
|
+ .like(StrUtil.isNotEmpty(systemName), CareOrganization::getSystemName, systemName)
|
|
|
+ .orderByAsc(CareOrganization::getName);
|
|
|
+
|
|
|
+ IPage<CareOrganization> pageRes = this.careOrganizationService.page(page, queryWrapper);
|
|
|
+ IPage<OrganizationVO> results = new Page<>(pageRes.getCurrent(), pageRes.getSize(), pageRes.getTotal());
|
|
|
+ if (CollUtil.isNotEmpty(pageRes.getRecords())) {
|
|
|
+ List<OrganizationVO> list = new ArrayList<>();
|
|
|
+ pageRes.getRecords().forEach(item -> {
|
|
|
+ OrganizationVO resVO = new OrganizationVO();
|
|
|
+ BeanUtils.copyProperties(item, resVO);
|
|
|
+ //服务站数量
|
|
|
+ QueryWrapper<CareStation> queryWrapper0 = new QueryWrapper<>();
|
|
|
+ queryWrapper0.lambda().eq(CareStation::getOrgId, resVO.getId());
|
|
|
+ resVO.setStationCount(this.careStationService.count(queryWrapper0));
|
|
|
+
|
|
|
+ //服务设备数量
|
|
|
+ QueryWrapper<CareDevice> queryWrapper1 = new QueryWrapper<>();
|
|
|
+ queryWrapper1.lambda().eq(CareDevice::getOrgId, resVO.getId());
|
|
|
+ resVO.setDevCount(this.careDeviceService.count(queryWrapper1));
|
|
|
+
|
|
|
+ //服务有效期
|
|
|
+ QueryWrapper<CareSysUser> queryWrapper2 = new QueryWrapper<>();
|
|
|
+ queryWrapper2.lambda().eq(CareSysUser::getOrgId, resVO.getId()).eq(CareSysUser::getRole,UserRoleEnum.ORG);
|
|
|
+ CareSysUser careSysUser = this.careSysUserService.getOne(queryWrapper2);
|
|
|
+ if (careSysUser != null && careSysUser.getServStartDate() != null && careSysUser.getServEndDate() != null) {
|
|
|
+ resVO.setServValidPeriod(DateUtils.formatToDateStr(careSysUser.getServStartDate(), "yyyy-MM-dd") + "~" + DateUtils.formatToDateStr(careSysUser.getServEndDate(), "yyyy-MM-dd"));
|
|
|
+ }
|
|
|
+
|
|
|
+ list.add(resVO);
|
|
|
+ });
|
|
|
+ results.setRecords(list);
|
|
|
+ }
|
|
|
+ return results;
|
|
|
+ }
|
|
|
+
|
|
|
+ public OrganizationVO getOrganizationInfo(Long organizationId) {
|
|
|
+ OrganizationVO vo = new OrganizationVO();
|
|
|
+ CareOrganization organizationInfo = this.careOrganizationService.getById(organizationId);
|
|
|
+ BeanUtil.copyProperties(organizationInfo,vo);
|
|
|
+ //服务有效期
|
|
|
+ QueryWrapper<CareSysUser> queryWrapper2 = new QueryWrapper<>();
|
|
|
+ queryWrapper2.lambda().eq(CareSysUser::getOrgId, organizationId).eq(CareSysUser::getRole,UserRoleEnum.ORG);
|
|
|
+ CareSysUser careSysUser = this.careSysUserService.getOne(queryWrapper2);
|
|
|
+ CareSysUserVO careSysUserVO = new CareSysUserVO();
|
|
|
+ BeanUtil.copyProperties(careSysUser,careSysUserVO);
|
|
|
+ vo.setCareSysUserVO(careSysUserVO);
|
|
|
+
|
|
|
+ return vo;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 同步机构
|
|
|
+ */
|
|
|
+ public boolean synOrganization() {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 通过synOrgUrl获取data
|
|
|
+ JSONObject jsonObject = HttpUtil.httpGet(synOrgUrl);
|
|
|
+ int code = jsonObject.getInt("code");
|
|
|
+ if (code == 1) {
|
|
|
+ JSONArray data = jsonObject.getJSONArray("data");
|
|
|
+ Iterator it = data.iterator();
|
|
|
+
|
|
|
+ while (it.hasNext()) {
|
|
|
+ JSONObject obj = (JSONObject) it.next();
|
|
|
+ CareOrganization careOrganization = new CareOrganization();
|
|
|
+
|
|
|
+ careOrganization.setName(obj.getStr("companyName"));
|
|
|
+ careOrganization.setShortName(obj.getStr("shortName"));
|
|
|
+ careOrganization.setCompanyId(obj.getStr("companyId"));
|
|
|
+ careOrganization.setOrgCode(obj.getStr("companyId"));
|
|
|
+ careOrganization.setCompanyKey(obj.getStr("companyKey"));
|
|
|
+ careOrganizationService.synOrganization(careOrganization);
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 修改机构
|
|
|
+ * @param vo
|
|
|
+ */
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void updateOrganization(OrganizationVO vo) throws Exception {
|
|
|
+ Result<Object> ret = Result.success("修改成功!");
|
|
|
+ MultipartFile logoImg = vo.getLogoImg();
|
|
|
+ if (logoImg != null) {
|
|
|
+ //获取文件名
|
|
|
+ String fileName = logoImg.getOriginalFilename();
|
|
|
+ if (StringUtils.isEmpty(fileName) || logoImg.getSize() == 0) {
|
|
|
+ throw new BDException("logo文件不能为空!");
|
|
|
+ }
|
|
|
+ //验证文件名是否合格
|
|
|
+ if (!ImgUtil.isImg(fileName)) {
|
|
|
+ throw new BDException("logo文件必须是图片格式!");
|
|
|
+ }
|
|
|
+ String saveFileName = UUID.randomUUID().toString() + "_" + fileName;
|
|
|
+ String picFullFileName = savePath + "/logo/" + saveFileName;
|
|
|
+ FileOutputStream fos = new FileOutputStream(picFullFileName);
|
|
|
+ fos.write(logoImg.getBytes());
|
|
|
+ vo.setLogo(fileUrl + "logo/" + saveFileName);
|
|
|
+ }
|
|
|
+
|
|
|
+ CareOrganization careOrganization = new CareOrganization();
|
|
|
+ BeanUtil.copyProperties(vo, careOrganization);
|
|
|
+ if(!this.careOrganizationService.updateById(careOrganization)){
|
|
|
+ throw new BDException("机构修改失败!");
|
|
|
+ }
|
|
|
+
|
|
|
+ //机构账号
|
|
|
+ CareSysUserVO careSysUserVO = vo.getCareSysUserVO();
|
|
|
+ QueryWrapper<CareSysUser> queryWrapper = new QueryWrapper<>();
|
|
|
+ queryWrapper.lambda().eq(CareSysUser::getPhone,careSysUserVO.getPhone());
|
|
|
+ CareSysUser careSysUserDb = careSysUserService.getOne(queryWrapper);
|
|
|
+ if(careSysUserDb != null){
|
|
|
+ careSysUserVO.setId(careSysUserDb.getId());
|
|
|
+ CareSysUser careSysUser = new CareSysUser();
|
|
|
+ BeanUtil.copyProperties(careSysUserVO,careSysUser);
|
|
|
+ if(!this.careSysUserService.updateById(careSysUser)){
|
|
|
+ throw new BDException("机构账号修改失败!");
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ CareSysUser careSysUser = new CareSysUser();
|
|
|
+ BeanUtil.copyProperties(careSysUserVO,careSysUser);
|
|
|
+ careSysUser.setName(vo.getShortName() + "管理账号");
|
|
|
+ careSysUser.setCreateTime(new Date());
|
|
|
+ careSysUser.setRole(UserRoleEnum.ORG.getValue());
|
|
|
+ careSysUser.setOrgId(vo.getId());
|
|
|
+ String md5Password = DigestUtils.md5Hex("123456");
|
|
|
+ careSysUser.setPassword(md5Password);
|
|
|
+ this.careSysUserService.save(careSysUser);
|
|
|
+
|
|
|
+ //创建一个默认服务站
|
|
|
+
|
|
|
+ CareOrganization careOrganizationDb = this.careOrganizationService.getById(vo.getId());
|
|
|
+
|
|
|
+ CareStationVO careStationVO = new CareStationVO();
|
|
|
+ careStationVO.setOrgId(vo.getId());
|
|
|
+ careStationVO.setName(careOrganizationDb.getName());
|
|
|
+ careStationVO.setShortName(careOrganizationDb.getShortName());
|
|
|
+ careStationVO.setPhone(careSysUser.getPhone());
|
|
|
+ stationService.addStation(careStationVO);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|