MaTechnicianServiceImpl.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. package com.ylx.massage.service.impl;
  2. import java.util.ArrayList;
  3. import java.util.LinkedHashSet;
  4. import java.util.List;
  5. import java.util.Set;
  6. import cn.hutool.core.util.StrUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.ylx.common.core.domain.model.LoginUser;
  10. import com.ylx.common.exception.ServiceException;
  11. import com.ylx.common.utils.DateUtils;
  12. import com.ylx.common.utils.StringUtils;
  13. import com.ylx.massage.domain.MaProject;
  14. import com.ylx.massage.domain.MaTeProject;
  15. import com.ylx.massage.domain.dto.MaTechnicianMerchantAddDTO;
  16. import com.ylx.massage.domain.dto.MaTechnicianMerchantQueryDTO;
  17. import com.ylx.massage.domain.dto.MassageMerchantRecommendDto;
  18. import com.ylx.massage.domain.vo.MaTechnicianAppAddVo;
  19. import com.ylx.massage.domain.vo.MaTechnicianMerchantListVO;
  20. import com.ylx.massage.domain.vo.MerchantVo;
  21. import com.ylx.massage.mapper.MaProjectMapper;
  22. import com.ylx.massage.mapper.MaTeProjectMapper;
  23. import com.ylx.project.domain.Project;
  24. import com.ylx.project.mapper.ProjectMapper;
  25. import org.springframework.beans.BeanUtils;
  26. import org.springframework.beans.factory.annotation.Autowired;
  27. import org.springframework.stereotype.Service;
  28. import com.ylx.massage.mapper.MaTechnicianMapper;
  29. import com.ylx.massage.domain.MaTechnician;
  30. import com.ylx.massage.service.IMaTechnicianService;
  31. import org.springframework.transaction.annotation.Transactional;
  32. /**
  33. * 技师Service业务层处理
  34. *
  35. * @author ylx
  36. * @date 2024-03-22
  37. */
  38. @Service
  39. public class MaTechnicianServiceImpl implements IMaTechnicianService
  40. {
  41. private static final int SERVICE_STATE_AVAILABLE = 1;
  42. private static final int POST_STATE_OFFLINE = 0;
  43. private static final int ENABLED = 1;
  44. private static final int DEFAULT_AGE = 18;
  45. private static final int AUDIT_APPROVED = 2;
  46. private static final int NS_STATUS_NOT_ON_DUTY = -1;
  47. private static final int DEFAULT_STAT_VALUE = 0;
  48. private static final long NOT_DELETED = 0L;
  49. private static final String MERCHANT_STATUS_NORMAL = "0";
  50. @Autowired
  51. private MaTechnicianMapper maTechnicianMapper;
  52. @Autowired
  53. private MaTeProjectMapper maTeProjectMapper;
  54. @Autowired
  55. private ProjectMapper projectMapper;
  56. /**
  57. * 查询技师
  58. *
  59. * @param id 技师主键
  60. * @return 技师
  61. */
  62. @Override
  63. public MaTechnician selectMaTechnicianById(Long id)
  64. {
  65. return maTechnicianMapper.selectMaTechnicianById(id);
  66. }
  67. /**
  68. * 查询技师列表
  69. *
  70. * @param maTechnician 技师
  71. * @return 技师
  72. */
  73. @Override
  74. public List<MaTechnician> selectMaTechnicianList(MaTechnician maTechnician)
  75. {
  76. return maTechnicianMapper.selectMaTechnicianList(maTechnician);
  77. }
  78. /**
  79. * 新增技师
  80. *
  81. * @param maTechnicianAppAddVo 技师
  82. * @return 结果
  83. */
  84. @Override
  85. @Transactional(rollbackFor = Exception.class)
  86. public int insertMaTechnician(MaTechnicianAppAddVo maTechnicianAppAddVo)
  87. {
  88. MaTechnician maTechnician = new MaTechnician();
  89. BeanUtils.copyProperties(maTechnicianAppAddVo, maTechnician);
  90. int rows = maTechnicianMapper.insertMaTechnician(maTechnician);
  91. if (maTechnicianAppAddVo.getProjectIds() != null && !maTechnicianAppAddVo.getProjectIds().isEmpty()) {
  92. insertProjectRelations(maTechnician.getId(), new LinkedHashSet<>(maTechnicianAppAddVo.getProjectIds()));
  93. }
  94. return rows;
  95. }
  96. /**
  97. * 后台新增商户
  98. *
  99. * @param dto 新增商户参数
  100. * @param loginUser 当前登录用户
  101. * @return 结果
  102. */
  103. @Override
  104. @Transactional(rollbackFor = Exception.class)
  105. public int insertMerchant(MaTechnicianMerchantAddDTO dto, LoginUser loginUser) {
  106. Set<Long> projectIds = checkMerchantAddParam(dto);
  107. String userName = loginUser.getUser().getUserName();
  108. MaTechnician maTechnician = new MaTechnician();
  109. maTechnician.setTeName(dto.getTeName().trim());
  110. maTechnician.setTeNickName(dto.getTeNickName().trim());
  111. maTechnician.setTeSex(dto.getTeSex());
  112. maTechnician.setTePhone(dto.getTePhone().trim());
  113. maTechnician.setOpenService(dto.getOpenService());
  114. dto.getProjectIds().forEach(projectId -> {
  115. Project project = projectMapper.selectById(projectId);
  116. if (project != null) {
  117. maTechnician.setTeProject(project.getTitle() + ",");
  118. }
  119. });
  120. //移除末尾的逗号
  121. maTechnician.setTeProject(StrUtil.removeSuffix(maTechnician.getTeProject(), ","));
  122. maTechnician.setTechType(dto.getTechType());
  123. maTechnician.setIsRecommend(normalizeSwitchValue(dto.getIsRecommend(), "是否推荐"));
  124. maTechnician.setServiceState(SERVICE_STATE_AVAILABLE);
  125. maTechnician.setPostState(POST_STATE_OFFLINE);
  126. maTechnician.setTeIsEnable(ENABLED);
  127. //上岗状态:默认-1 未上岗
  128. maTechnician.setNStatus2(NS_STATUS_NOT_ON_DUTY);
  129. maTechnician.setMerchantStatus(MERCHANT_STATUS_NORMAL);
  130. //审核状态
  131. maTechnician.setAuditStatus(AUDIT_APPROVED);
  132. maTechnician.setTeAddress("");
  133. maTechnician.setNStar(DEFAULT_STAT_VALUE);
  134. maTechnician.setNNum(DEFAULT_STAT_VALUE);
  135. maTechnician.setCreateBy(userName);
  136. maTechnician.setUpdateBy(userName);
  137. maTechnician.setCreateTime(DateUtils.getNowDate());
  138. maTechnician.setUpdateTime(DateUtils.getNowDate());
  139. int rows = maTechnicianMapper.insert(maTechnician);
  140. if (rows <= 0) {
  141. throw new ServiceException("新增商户失败");
  142. }
  143. insertProjectRelations(maTechnician.getId(), projectIds);
  144. return rows;
  145. }
  146. /**
  147. * 后台查询商户列表
  148. *
  149. * @param page 分页参数
  150. * @param dto 查询条件
  151. * @return 商户分页列表
  152. */
  153. @Override
  154. public Page<MaTechnicianMerchantListVO> selectMerchantList(Page<MaTechnicianMerchantListVO> page,
  155. MaTechnicianMerchantQueryDTO dto) {
  156. Page<MaTechnicianMerchantListVO> pageParam = page == null ? new Page<>(1, 10) : page;
  157. return maTechnicianMapper.selectMerchantList(pageParam, dto);
  158. }
  159. /**
  160. * 修改技师
  161. *
  162. * @param maTechnicianAppAddVo
  163. * @return 结果
  164. */
  165. @Override
  166. public int updateMaTechnician(MaTechnicianAppAddVo maTechnicianAppAddVo)
  167. {
  168. MaTechnician maTechnician = new MaTechnician();
  169. BeanUtils.copyProperties(maTechnicianAppAddVo, maTechnician);
  170. return maTechnicianMapper.updateMaTechnician(maTechnician);
  171. }
  172. /**
  173. * 批量删除技师
  174. *
  175. * @param ids 需要删除的技师主键
  176. * @return 结果
  177. */
  178. @Override
  179. public int deleteMaTechnicianByIds(Long[] ids)
  180. {
  181. return maTechnicianMapper.deleteMaTechnicianByIds(ids);
  182. }
  183. /**
  184. * 删除技师信息
  185. *
  186. * @param id 技师主键
  187. * @return 结果
  188. */
  189. @Override
  190. public int deleteMaTechnicianById(Long id)
  191. {
  192. return maTechnicianMapper.deleteMaTechnicianById(id);
  193. }
  194. /**
  195. * 首页选中的城市是否有开通服务
  196. * @param areaCode
  197. * @return
  198. */
  199. @Override
  200. public Boolean isHasMerchantCity(String areaCode) {
  201. LambdaQueryWrapper<MaTechnician> queryWrapper = new LambdaQueryWrapper<>();
  202. queryWrapper.eq(MaTechnician::getTeAreaCode, areaCode);
  203. return maTechnicianMapper.selectCount(queryWrapper) > 0;
  204. }
  205. /**
  206. * 首页按摩推荐
  207. * @param dto
  208. * @return
  209. */
  210. @Override
  211. public List<MerchantVo> getMerchantRecommend(MassageMerchantRecommendDto dto) {
  212. return maTechnicianMapper.getMerchantRecommend(dto);
  213. }
  214. private Set<Long> checkMerchantAddParam(MaTechnicianMerchantAddDTO dto) {
  215. if (dto == null) {
  216. throw new ServiceException("商户参数不能为空");
  217. }
  218. checkRequiredText(dto.getTeName(), "姓名", 10);
  219. checkRequiredText(dto.getTeNickName(), "昵称", 10);
  220. checkRequiredText(dto.getTePhone(), "电话", 11);
  221. checkEnumValue(dto.getTeSex(), "性别", 0, 1);
  222. //checkEnumValue(dto.getOpenService(), "服务类目", 1, 2);
  223. //校验服务类名不能为空
  224. if (dto.getOpenService() == null) {
  225. throw new ServiceException("服务类目不能为空");
  226. }
  227. checkEnumValue(dto.getTechType(), "商户类型", 0, 1);
  228. if (dto.getIsRecommend() != null) {
  229. checkEnumValue(dto.getIsRecommend(), "是否推荐", 0, 1);
  230. }
  231. return checkProjectIds(dto.getProjectIds());
  232. }
  233. private void checkRequiredText(String value, String fieldName, int maxLength) {
  234. if (StringUtils.isBlank(value)) {
  235. throw new ServiceException(fieldName + "不能为空");
  236. }
  237. if (value.trim().length() > maxLength) {
  238. throw new ServiceException(fieldName + "长度不能超过" + maxLength + "个字符");
  239. }
  240. }
  241. private void checkEnumValue(Integer value, String fieldName, int... allowedValues) {
  242. if (value == null) {
  243. throw new ServiceException(fieldName + "不能为空");
  244. }
  245. for (int allowedValue : allowedValues) {
  246. if (value == allowedValue) {
  247. return;
  248. }
  249. }
  250. throw new ServiceException(fieldName + "值不正确");
  251. }
  252. private Integer normalizeSwitchValue(Integer value, String fieldName) {
  253. if (value == null) {
  254. return 0;
  255. }
  256. checkEnumValue(value, fieldName, 0, 1);
  257. return value;
  258. }
  259. /**
  260. * 校验服务项目ID集合
  261. * @param projectIds
  262. * @return 有效服务项目ID集合
  263. */
  264. private Set<Long> checkProjectIds(List<Long> projectIds) {
  265. if (projectIds == null || projectIds.isEmpty()) {
  266. throw new ServiceException("服务项目不能为空");
  267. }
  268. Set<Long> distinctProjectIds = new LinkedHashSet<>();
  269. for (Long projectId : projectIds) {
  270. if (projectId == null) {
  271. throw new ServiceException("服务项目ID不能为空");
  272. }
  273. distinctProjectIds.add(projectId);
  274. }
  275. /*int validCount = 0;
  276. for (Long projectId : distinctProjectIds) {
  277. MaProject project = maProjectMapper.selectMaProjectById(projectId);
  278. if (project != null && (project.getIsDelete() == null || project.getIsDelete() != 1)) {
  279. validCount++;
  280. }
  281. }
  282. if (validCount != distinctProjectIds.size()) {
  283. throw new ServiceException("服务项目不存在或已删除");
  284. }*/
  285. return distinctProjectIds;
  286. }
  287. /**
  288. * 新增商户与服务项目关联关系
  289. *
  290. * @param technicianId
  291. * @param projectIds
  292. */
  293. private void insertProjectRelations(Long technicianId, Set<Long> projectIds) {
  294. if (technicianId == null) {
  295. throw new ServiceException("商户ID不能为空");
  296. }
  297. List<MaTeProject> relations = new ArrayList<>();
  298. for (Long projectId : projectIds) {
  299. MaTeProject relation = new MaTeProject();
  300. relation.setTeId(technicianId);
  301. relation.setProjectId(projectId);
  302. relations.add(relation);
  303. }
  304. int rows = maTeProjectMapper.insertBatch(relations);
  305. if (rows != relations.size()) {
  306. throw new ServiceException("新增商户服务项目失败");
  307. }
  308. }
  309. }