MaTechnicianServiceImpl.java 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  1. package com.ylx.massage.service.impl;
  2. import java.math.BigDecimal;
  3. import java.text.SimpleDateFormat;
  4. import java.time.Duration;
  5. import java.time.LocalDate;
  6. import java.time.LocalDateTime;
  7. import java.time.ZoneId;
  8. import java.time.format.DateTimeFormatter;
  9. import java.util.*;
  10. import java.util.function.Function;
  11. import java.util.stream.Collectors;
  12. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  13. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  14. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  15. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  16. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  17. import com.ylx.attendanceconfig.domain.AttendanceRule;
  18. import com.ylx.attendanceconfig.mapper.AttendanceRuleMapper;
  19. import com.ylx.common.core.domain.AjaxResult;
  20. import com.ylx.common.core.domain.model.LoginUser;
  21. import com.ylx.common.exception.ServiceException;
  22. import com.ylx.common.utils.DateUtils;
  23. import com.ylx.common.utils.StringUtils;
  24. import com.ylx.massage.controller.CityOperationApplicationController;
  25. import com.ylx.massage.domain.*;
  26. import com.ylx.massage.domain.dto.*;
  27. import com.ylx.massage.domain.vo.*;
  28. import com.ylx.massage.enums.*;
  29. import com.ylx.massage.mapper.*;
  30. import com.ylx.massage.domain.ContractRecord;
  31. import com.ylx.massage.domain.MaProject;
  32. import com.ylx.massage.domain.MaTeProject;
  33. import com.ylx.massage.domain.dto.MaProjectSaveDto;
  34. import com.ylx.massage.domain.dto.MaTechnicianAuditQueryDTO;
  35. import com.ylx.massage.domain.dto.MaTechnicianAuditSubmitDTO;
  36. import com.ylx.massage.domain.dto.MaTechnicianMerchantAddDTO;
  37. import com.ylx.massage.domain.dto.MaTechnicianMerchantQueryDTO;
  38. import com.ylx.massage.domain.dto.MassageMerchantRecommendDto;
  39. import com.ylx.massage.domain.vo.MaTechnicianAppAddVo;
  40. import com.ylx.massage.domain.vo.MaTechnicianAuditListVO;
  41. import com.ylx.massage.domain.vo.MaTechnicianCertificateVO;
  42. import com.ylx.massage.domain.vo.MaTechnicianMerchantDetailVO;
  43. import com.ylx.massage.domain.vo.MaTechnicianMerchantListVO;
  44. import com.ylx.massage.domain.vo.MerchantVo;
  45. import com.ylx.massage.mapper.ContractRecordMapper;
  46. import com.ylx.massage.mapper.MaProjectMapper;
  47. import com.ylx.massage.mapper.MaTeProjectMapper;
  48. import com.ylx.massage.service.TbFileService;
  49. import com.ylx.order.domain.TOrder;
  50. import com.ylx.order.mapper.TOrderMapper;
  51. import com.ylx.project.domain.Project;
  52. import com.ylx.project.mapper.ProjectMapper;
  53. import lombok.Data;
  54. import org.springframework.beans.BeanUtils;
  55. import org.springframework.beans.factory.annotation.Autowired;
  56. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  57. import org.springframework.stereotype.Service;
  58. import com.ylx.massage.service.IMaTechnicianService;
  59. import org.springframework.transaction.annotation.Transactional;
  60. import org.springframework.util.CollectionUtils;
  61. import org.springframework.web.multipart.MultipartFile;
  62. import javax.annotation.Resource;
  63. import static com.ylx.massage.enums.FileTypeEnum.*;
  64. /**
  65. * 技师Service业务层处理
  66. *
  67. * @author ylx
  68. * @date 2024-03-22
  69. */
  70. @Service
  71. public class MaTechnicianServiceImpl extends ServiceImpl<MaTechnicianMapper, MaTechnician> implements IMaTechnicianService {
  72. private static final int SERVICE_STATE_AVAILABLE = 1;
  73. private static final int POST_STATE_OFFLINE = 0;
  74. private static final int ENABLED = 1;
  75. private static final int DEFAULT_AGE = 18;
  76. private static final int AUDIT_APPROVED = 2;
  77. private static final int AUDIT_WAIT_ENTER = 0;
  78. private static final int AUDIT_WAIT_REVIEW = 1;
  79. private static final int AUDIT_REJECTED = 3;
  80. private static final int AUDIT_REMARK_MAX_LENGTH = 500;
  81. private static final int NS_STATUS_NOT_ON_DUTY = -1;
  82. private static final int DEFAULT_STAT_VALUE = 0;
  83. private static final Integer NOT_DELETED = 0;
  84. private static final String MERCHANT_STATUS_NORMAL = "0";
  85. private static final String PASSWORD = "123456";
  86. @Resource
  87. private MaTechnicianMapper maTechnicianMapper;
  88. @Resource
  89. private MaTeProjectMapper maTeProjectMapper;
  90. @Resource
  91. private MaProjectMapper maProjectMapper;
  92. @Autowired
  93. private ProjectMapper projectMapper;
  94. @Autowired
  95. private TbFileService fileService;
  96. @Resource
  97. private TFareFreeRuleMapper tFareFreeRuleMapper;
  98. @Resource
  99. private ContractRecordMapper contractRecordMapper;
  100. @Resource
  101. private MerchantDailyAttendanceMapper merchantDailyAttendanceMapper;
  102. @Resource
  103. private AttendanceRuleMapper attendanceRuleMapper;
  104. @Resource
  105. private TAddressMapper addressMapper;
  106. @Resource
  107. private MerchantApplyFileMapper merchantApplyFileMapper;
  108. @Resource
  109. private TOrderMapper orderMapper;
  110. @Autowired
  111. private IMaTechnicianService maTechnicianService;
  112. @Resource
  113. private CityOperationApplicationMapper cityOperationApplicationMapper;
  114. /**
  115. * 商户入驻申请注册
  116. *
  117. * @param req 申请参数
  118. */
  119. @Override
  120. @Transactional(rollbackFor = Exception.class)
  121. public void apply(MaTechnicianAppAddVo req) {
  122. // 初始化加密工具
  123. BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
  124. String phone = req.getTePhone();
  125. //商户入住前置条件校验
  126. getMaTechnician(req, phone);
  127. MaTechnician maTechnician = new MaTechnician();
  128. BeanUtils.copyProperties(req, maTechnician);
  129. //技师类型默认为真实商户
  130. maTechnician.setTechType(0);
  131. maTechnician.setCreateBy("admin");
  132. maTechnician.setAuditStatus(AUDIT_WAIT_ENTER);
  133. maTechnician.setTePassword(encoder.encode(PASSWORD));
  134. LambdaQueryWrapper<MaTechnician> queryWrapper = new LambdaQueryWrapper<>();
  135. queryWrapper.eq(MaTechnician::getCOpenid, req.getCOpenid());
  136. MaTechnician maTechnician1 = maTechnicianMapper.selectOne(queryWrapper);
  137. if (maTechnician1 == null) {
  138. throw new RuntimeException("商户不存在");
  139. }
  140. LambdaUpdateWrapper<MaTechnician> updateWrapper = new LambdaUpdateWrapper<>();
  141. updateWrapper.eq(MaTechnician::getId, maTechnician1.getId());
  142. maTechnicianMapper.update(maTechnician, updateWrapper);
  143. CityOperationApplication cityOperationApplication = new CityOperationApplication();
  144. cityOperationApplication.setMerchantId(maTechnician1.getId());
  145. cityOperationApplication.setOperationCenterId(req.getOperationCenterId());
  146. cityOperationApplication.setProvinceCode(req.getProvinceCode());
  147. cityOperationApplication.setProvinceName(req.getProvinceName());
  148. cityOperationApplication.setCityCode(req.getCityCode());
  149. cityOperationApplication.setCityName(req.getCityName());
  150. cityOperationApplication.setDistrictCode(req.getDistrictCode());
  151. cityOperationApplication.setDistrictName(req.getDistrictName());
  152. cityOperationApplication.setOperationCenterName(req.getOperationCenterName());
  153. cityOperationApplication.setCreateBy(maTechnician1.getId().toString());
  154. cityOperationApplication.setUpdateBy(maTechnician1.getId().toString());
  155. cityOperationApplicationMapper.insert(cityOperationApplication);
  156. }
  157. /**
  158. * 商户入驻申请文件上传
  159. *
  160. * @param req
  161. */
  162. @Override
  163. public void applyFile(MerchantApplyFileRequestDto req) {
  164. if (req == null || req.getReq().isEmpty()) {
  165. }
  166. for (MerchantApplyFileDto re : req.getReq()) {
  167. LambdaQueryWrapper<MerchantApplyFile> queryWrapper = new LambdaQueryWrapper<>();
  168. queryWrapper.eq(MerchantApplyFile::getMerchantId, re.getMerchantId());
  169. queryWrapper.eq(MerchantApplyFile::getFileType, re.getFileType());
  170. MerchantApplyFile merchantApplyFile = merchantApplyFileMapper.selectOne(queryWrapper);
  171. if (merchantApplyFile != null) {
  172. // 删除原有文件
  173. merchantApplyFileMapper.deleteById(merchantApplyFile);
  174. } else {
  175. //插入文件信息
  176. MerchantApplyFile maTechnician = new MerchantApplyFile();
  177. BeanUtils.copyProperties(re, maTechnician);
  178. maTechnician.setCreateBy(re.getMerchantId().toString());
  179. maTechnician.setUpdateBy(re.getMerchantId().toString());
  180. merchantApplyFileMapper.insert(maTechnician);
  181. }
  182. }
  183. LambdaUpdateWrapper<MaTechnician> updateWrapper = new LambdaUpdateWrapper<>();
  184. updateWrapper.eq(MaTechnician::getId, req.getTechnician().getId());
  185. updateWrapper.set(MaTechnician::getTeNickName, req.getTechnician().getTeNickName());
  186. updateWrapper.set(MaTechnician::getTeBrief, req.getTechnician().getTeBrief());
  187. maTechnicianService.update(updateWrapper);
  188. }
  189. /**
  190. * 商户入住前置条件校验
  191. *
  192. * @param req
  193. * @param phone
  194. */
  195. private void getMaTechnician(MaTechnicianAppAddVo req, String phone) {
  196. // 1. 判断当前用户是否已入驻
  197. MaTechnician userProfile = getMaTechnician(req);
  198. if (userProfile != null) {
  199. throw new RuntimeException("当前用户已入驻,请勿重复提交");
  200. }
  201. // 2. 判断手机号是否已存在
  202. LambdaQueryWrapper<MaTechnician> queryPhoneWrapper = new LambdaQueryWrapper<>();
  203. queryPhoneWrapper.eq(MaTechnician::getTePhone, phone);
  204. queryPhoneWrapper.eq(MaTechnician::getIsDelete, 0);
  205. MaTechnician maTechnicianPhone = maTechnicianMapper.selectOne(queryPhoneWrapper);
  206. if (maTechnicianPhone != null) {
  207. throw new RuntimeException("手机号已存在,请更换手机号");
  208. }
  209. //3、判断手机号是否已绑定其他用户
  210. LambdaQueryWrapper<MaTechnician> queryTePhoneWrapper = new LambdaQueryWrapper<>();
  211. queryTePhoneWrapper.eq(MaTechnician::getTePhone, phone);
  212. queryTePhoneWrapper.eq(MaTechnician::getIsDelete, 0);
  213. queryTePhoneWrapper.eq(MaTechnician::getAuditStatus, 2);
  214. MaTechnician maTechnicianTePhone = maTechnicianMapper.selectOne(queryTePhoneWrapper);
  215. if (maTechnicianTePhone != null) {
  216. throw new RuntimeException("手机号已被其他用户绑定,请更换手机号");
  217. }
  218. }
  219. /**
  220. * 判断当前用户是否已入驻
  221. *
  222. * @return
  223. */
  224. private MaTechnician getMaTechnician(MaTechnicianAppAddVo req) {
  225. LambdaQueryWrapper<MaTechnician> queryWrapper = new LambdaQueryWrapper<>();
  226. queryWrapper.eq(MaTechnician::getTePhone, req.getTePhone());
  227. queryWrapper.eq(MaTechnician::getIsDelete, 0);
  228. queryWrapper.eq(MaTechnician::getAuditStatus, 2);
  229. //queryWrapper.eq(MaTechnician::getOpenService, req.getOpenService());
  230. queryWrapper.eq(MaTechnician::getServiceTag, req.getServiceTag());
  231. MaTechnician userProfile = maTechnicianMapper.selectOne(queryWrapper);
  232. return userProfile;
  233. }
  234. /**
  235. * 查询商户服务项目列表
  236. *
  237. * @param userId 商户id
  238. * @param auditStatus 审核状态
  239. * @return 技师列表
  240. */
  241. @Override
  242. public List<MaProject> selectMaTechnicianListBy(String userId, String auditStatus) {
  243. LambdaQueryWrapper<MaProject> queryWrapper = new LambdaQueryWrapper<>();
  244. queryWrapper.eq(MaProject::getMerchantId, userId);
  245. queryWrapper.eq(MaProject::getAuditStatus, auditStatus);
  246. return maProjectMapper.selectList(queryWrapper);
  247. }
  248. /**
  249. * 查询服务分类项目列表
  250. *
  251. * @param typeId 技师类型
  252. * @return 技师列表
  253. */
  254. @Override
  255. public List<Project> selectTechnicianListBy(String typeId) {
  256. LambdaQueryWrapper<Project> queryWrapper = new LambdaQueryWrapper<>();
  257. queryWrapper.eq(Project::getType, typeId);
  258. return projectMapper.selectList(queryWrapper);
  259. }
  260. /**
  261. * 查询技师
  262. *
  263. * @param id 技师主键
  264. * @return 技师
  265. */
  266. @Override
  267. public MaTechnician selectMaTechnicianById(Long id) {
  268. return maTechnicianMapper.selectMaTechnicianById(id);
  269. }
  270. /**
  271. * 查询技师列表
  272. *
  273. * @param maTechnician 技师
  274. * @return 技师
  275. */
  276. @Override
  277. public List<MaTechnician> selectMaTechnicianList(MaTechnician maTechnician) {
  278. return maTechnicianMapper.selectMaTechnicianList(maTechnician);
  279. }
  280. /**
  281. * 新增技师
  282. *
  283. * @param maTechnicianAppAddVo 技师
  284. * @return 结果
  285. */
  286. @Override
  287. @Transactional(rollbackFor = Exception.class)
  288. public int insertMaTechnician(MaTechnicianAppAddVo maTechnicianAppAddVo) {
  289. MaTechnician maTechnician = new MaTechnician();
  290. BeanUtils.copyProperties(maTechnicianAppAddVo, maTechnician);
  291. int rows = maTechnicianMapper.insertMaTechnician(maTechnician);
  292. if (maTechnicianAppAddVo.getProjectIds() != null && !maTechnicianAppAddVo.getProjectIds().isEmpty()) {
  293. insertProjectRelations(maTechnician.getId(), new LinkedHashSet<>(maTechnicianAppAddVo.getProjectIds()));
  294. }
  295. return rows;
  296. }
  297. /**
  298. * 后台新增商户
  299. *
  300. * @param dto 新增商户参数
  301. * @param loginUser 当前登录用户
  302. * @return 结果
  303. */
  304. @Override
  305. @Transactional(rollbackFor = Exception.class)
  306. public int insertMerchant(MaTechnicianMerchantAddDTO dto, LoginUser loginUser) {
  307. MerchantProjectSelection selection = checkMerchantAddParam(dto);
  308. String userName = loginUser.getUser().getUserName();
  309. MaTechnician maTechnician = new MaTechnician();
  310. maTechnician.setTeName(dto.getTeName().trim());
  311. maTechnician.setTeNickName(dto.getTeNickName().trim());
  312. maTechnician.setTeSex(dto.getTeSex());
  313. maTechnician.setTePhone(dto.getTePhone().trim());
  314. maTechnician.setOpenService(joinIds(selection.getCategoryIds()));
  315. maTechnician.setTeProject(joinProjectTitles(selection.getProjectIds(), selection.getProjectMap()));
  316. maTechnician.setTechType(dto.getTechType());
  317. maTechnician.setIsRecommend(normalizeSwitchValue(dto.getIsRecommend(), "是否推荐"));
  318. maTechnician.setServiceState(SERVICE_STATE_AVAILABLE);
  319. maTechnician.setPostState(POST_STATE_OFFLINE);
  320. maTechnician.setTeIsEnable(ENABLED);
  321. //上岗状态:默认-1 未上岗
  322. maTechnician.setNStatus2(NS_STATUS_NOT_ON_DUTY);
  323. maTechnician.setMerchantStatus(MERCHANT_STATUS_NORMAL);
  324. //审核状态
  325. maTechnician.setAuditStatus(AUDIT_APPROVED);
  326. maTechnician.setTeAddress("");
  327. maTechnician.setNStar(DEFAULT_STAT_VALUE);
  328. maTechnician.setNNum(DEFAULT_STAT_VALUE);
  329. maTechnician.setCreateBy(userName);
  330. maTechnician.setUpdateBy(userName);
  331. maTechnician.setCreateTime(DateUtils.getNowDate());
  332. maTechnician.setUpdateTime(DateUtils.getNowDate());
  333. int rows = maTechnicianMapper.insert(maTechnician);
  334. if (rows <= 0) {
  335. throw new ServiceException("新增商户失败");
  336. }
  337. insertProjectRelations(maTechnician.getId(), selection.getProjectIds());
  338. return rows;
  339. }
  340. /**
  341. * 后台编辑商户
  342. *
  343. * @param id 商户ID
  344. * @param dto 编辑商户参数
  345. * @param loginUser 当前登录用户
  346. * @return 结果
  347. */
  348. @Override
  349. @Transactional(rollbackFor = Exception.class)
  350. public int updateMerchant(Integer id, MaTechnicianMerchantAddDTO dto, LoginUser loginUser) {
  351. if (id == null) {
  352. throw new ServiceException("商户ID不能为空");
  353. }
  354. MaTechnician existsMerchant = maTechnicianMapper.selectMerchantById(id.intValue());
  355. if (existsMerchant == null || !NOT_DELETED.equals(existsMerchant.getIsDelete())) {
  356. throw new ServiceException("商户不存在或已删除");
  357. }
  358. MerchantProjectSelection selection = checkMerchantAddParam(dto);
  359. String userName = loginUser.getUser().getUserName();
  360. MaTechnician maTechnician = new MaTechnician();
  361. maTechnician.setId(id);
  362. maTechnician.setTeName(dto.getTeName().trim());
  363. maTechnician.setTeNickName(dto.getTeNickName().trim());
  364. maTechnician.setTeSex(dto.getTeSex());
  365. maTechnician.setTePhone(dto.getTePhone().trim());
  366. maTechnician.setOpenService(joinIds(selection.getCategoryIds()));
  367. maTechnician.setTeProject(joinProjectTitles(selection.getProjectIds(), selection.getProjectMap()));
  368. maTechnician.setTechType(dto.getTechType());
  369. maTechnician.setIsRecommend(normalizeSwitchValue(dto.getIsRecommend(), "是否推荐"));
  370. maTechnician.setUpdateBy(userName);
  371. maTechnician.setUpdateTime(DateUtils.getNowDate());
  372. int rows = maTechnicianMapper.updateMerchantById(maTechnician);
  373. if (rows <= 0) {
  374. throw new ServiceException("编辑商户失败");
  375. }
  376. replaceProjectRelations(id, selection.getProjectIds());
  377. return rows;
  378. }
  379. /**
  380. * 后台上传商户合同文件
  381. *
  382. * @param id 商户ID
  383. * @param
  384. * @param loginUser 当前登录用户
  385. * @return 上传结果
  386. */
  387. @Override
  388. @Transactional(rollbackFor = Exception.class)
  389. public Integer uploadMerchantContract(Integer id, Map<String, Object> map, LoginUser loginUser) {
  390. if (id == null) {
  391. throw new ServiceException("商户ID不能为空");
  392. }
  393. MaTechnician existsMerchant = maTechnicianMapper.selectMerchantById(id);
  394. if (existsMerchant == null || !NOT_DELETED.equals(existsMerchant.getIsDelete())) {
  395. throw new ServiceException("商户不存在或已删除");
  396. }
  397. // 合同的名称
  398. String contractName = String.valueOf(map.get("contractName"));
  399. // 合同文件的URL
  400. String url = String.valueOf(map.get("url"));
  401. if (StringUtils.isBlank(url)) {
  402. throw new ServiceException("合同文件上传失败,未返回文件地址");
  403. }
  404. ContractRecord contractRecord = new ContractRecord();
  405. contractRecord.setMerchantId(id);
  406. contractRecord.setContractName(contractName);
  407. contractRecord.setFileUrl(url);
  408. contractRecord.setSignTime(DateUtils.getNowDate());
  409. contractRecord.setSignerName(existsMerchant.getTeName());
  410. contractRecord.setCreateTime(DateUtils.getNowDate());
  411. int rows = contractRecordMapper.insert(contractRecord);
  412. if (rows <= 0) {
  413. throw new ServiceException("保存合同记录失败");
  414. }
  415. return rows;
  416. }
  417. /**
  418. * 商户入驻审核。
  419. *
  420. * @param id 商户ID
  421. * @param dto 审核提交参数
  422. * @param loginUser 当前登录用户
  423. * @return 结果
  424. */
  425. @Override
  426. @Transactional(rollbackFor = Exception.class)
  427. public int submitMerchantAudit(Integer id, MaTechnicianAuditSubmitDTO dto, LoginUser loginUser) {
  428. if (id == null) {
  429. throw new ServiceException("商户ID不能为空");
  430. }
  431. if (dto == null) {
  432. throw new ServiceException("审核参数不能为空");
  433. }
  434. checkEnumValue(dto.getAuditStatus(), "审核意见", AUDIT_APPROVED, AUDIT_REJECTED);
  435. String auditRemark = dto.getAuditRemark() == null ? "" : dto.getAuditRemark().trim();
  436. if (dto.getAuditStatus() == AUDIT_REJECTED && StringUtils.isBlank(auditRemark)) {
  437. throw new ServiceException("审核驳回时审核备注不能为空");
  438. }
  439. if (auditRemark.length() > AUDIT_REMARK_MAX_LENGTH) {
  440. throw new ServiceException("审核备注长度不能超过" + AUDIT_REMARK_MAX_LENGTH + "个字符");
  441. }
  442. MaTechnician existsMerchant = maTechnicianMapper.selectMerchantById(id);
  443. if (existsMerchant == null || !NOT_DELETED.equals(existsMerchant.getIsDelete())) {
  444. throw new ServiceException("商户不存在或已删除");
  445. }
  446. Integer currentAuditStatus = existsMerchant.getAuditStatus();
  447. if (currentAuditStatus == null || (currentAuditStatus != AUDIT_WAIT_ENTER)) {
  448. throw new ServiceException("当前商户待入驻已审核,不用重复审核");
  449. }
  450. MaTechnician maTechnician = new MaTechnician();
  451. maTechnician.setId(id);
  452. // 修改为1:待审核
  453. maTechnician.setAuditStatus(1);
  454. maTechnician.setAuditRemark(auditRemark);
  455. maTechnician.setApproveTime(DateUtils.getNowDate());
  456. if (loginUser != null && loginUser.getUser() != null) {
  457. maTechnician.setUpdateBy(loginUser.getUser().getUserName());
  458. }
  459. maTechnician.setUpdateTime(DateUtils.getNowDate());
  460. int rows = maTechnicianMapper.submitMerchantAuditById(maTechnician);
  461. if (rows <= 0) {
  462. throw new ServiceException("提交商户审核失败");
  463. }
  464. return rows;
  465. }
  466. /**
  467. * 后台查询商户证照
  468. *
  469. * @param id 商户ID
  470. * @return 商户证照
  471. */
  472. @Override
  473. public MaTechnicianCertificateVO selectMerchantCertificate(Integer id) {
  474. if (id == null) {
  475. throw new ServiceException("商户ID不能为空");
  476. }
  477. LambdaQueryWrapper<MerchantApplyFile> queryWrapper = Wrappers.lambdaQuery();
  478. queryWrapper.eq(MerchantApplyFile::getMerchantId, id);
  479. List<MerchantApplyFile> merchantApplyFiles = merchantApplyFileMapper.selectList(queryWrapper);
  480. if (merchantApplyFiles == null) {
  481. throw new ServiceException("商户不存在或已删除");
  482. }
  483. MaTechnicianCertificateVO certificate = new MaTechnicianCertificateVO();
  484. certificate.setMerchantId(merchantApplyFiles.get(0).getMerchantId());
  485. merchantApplyFiles.forEach(merchant -> {
  486. certificate.setAvatar(typeFIleUrl(merchant, PORTRAIT.getCode()));
  487. certificate.setLifePhotos(typeFIleUrl(merchant, LIFE_PHOTO.getCode()));
  488. certificate.setIdCardFrout(typeFIleUrl(merchant, ID_CARD_FRONT.getCode()));
  489. certificate.setIdCardBack(typeFIleUrl(merchant, ID_CARD_BACK.getCode()));
  490. certificate.setIdCardHandheld(typeFIleUrl(merchant, ID_CARD_HANDHELD.getCode()));
  491. certificate.setHealthCertificate(typeFIleUrl(merchant, HEALTH_CERT.getCode()));
  492. certificate.setQualificationCertificate(typeFIleUrl(merchant, QUALIFICATION_CERT.getCode()));
  493. certificate.setNoCrimeRecord(typeFIleUrl(merchant, NO_CRIME_RECORD.getCode()));
  494. certificate.setCommitmentPdf(typeFIleUrl(merchant, COMMITMENT_LETTER.getCode()));
  495. certificate.setCommitmentVideo(typeFIleUrl(merchant, COMMITMENT_VIDEO.getCode()));
  496. certificate.setCommitmentAudio(typeFIleUrl(merchant, COMMITMENT_AUDIO.getCode()));
  497. });
  498. return certificate;
  499. }
  500. private String typeFIleUrl(MerchantApplyFile merchant, String type) {
  501. LambdaQueryWrapper<MerchantApplyFile> queryWrapper1 = Wrappers.lambdaQuery();
  502. queryWrapper1.eq(MerchantApplyFile::getMerchantId, merchant.getMerchantId());
  503. queryWrapper1.eq(MerchantApplyFile::getFileType, type);
  504. MerchantApplyFile merchantApplyFiles = merchantApplyFileMapper.selectOne(queryWrapper1);
  505. if (merchantApplyFiles == null) {
  506. return null;
  507. }
  508. return merchantApplyFiles.getFileUrl();
  509. }
  510. /**
  511. * 全量替换商户与服务项目关联关系。
  512. *
  513. * @param technicianId 商户ID
  514. * @param projectIds 服务项目ID集合
  515. */
  516. private void replaceProjectRelations(Integer technicianId, Set<Integer> projectIds) {
  517. if (technicianId == null) {
  518. throw new ServiceException("商户ID不能为空");
  519. }
  520. maTeProjectMapper.deleteByTechnicianId(technicianId);
  521. for (Integer projectId : projectIds) {
  522. MaTeProject relation = new MaTeProject();
  523. relation.setTeId(technicianId);
  524. relation.setProjectId(projectId);
  525. int rows = maTeProjectMapper.insert(relation);
  526. if (rows <= 0) {
  527. throw new ServiceException("编辑商户服务项目失败");
  528. }
  529. }
  530. }
  531. /**
  532. * 后台查询商户入驻审核列表
  533. *
  534. * @param page 分页参数
  535. * @param dto 查询条件
  536. * @return 商户入驻审核分页列表
  537. */
  538. @Override
  539. public Page<MaTechnicianAuditListVO> selectMerchantAuditList(Page<MaTechnicianAuditListVO> page, MaTechnicianAuditQueryDTO dto) {
  540. if (dto != null && dto.getAuditStatus() != null) {
  541. checkEnumValue(dto.getAuditStatus(), "审核状态", 0, 1, 2, 3);
  542. }
  543. Page<MaTechnicianAuditListVO> pageParam = page == null ? new Page<>(1, 10) : page;
  544. return maTechnicianMapper.selectMerchantAuditList(pageParam, dto);
  545. }
  546. /**
  547. * 后台查询商户列表
  548. *
  549. * @param page 分页参数
  550. * @param dto 查询条件
  551. * @return 商户分页列表
  552. */
  553. @Override
  554. public Page<MaTechnicianMerchantListVO> selectMerchantList(Page<MaTechnicianMerchantListVO> page,
  555. MaTechnicianMerchantQueryDTO dto) {
  556. Page<MaTechnicianMerchantListVO> pageParam = page == null ? new Page<>(1, 10) : page;
  557. return maTechnicianMapper.selectMerchantList(pageParam, dto);
  558. }
  559. /**
  560. * 后台查询商户详情
  561. *
  562. * @param id 商户ID
  563. * @return 商户详情
  564. */
  565. @Override
  566. public MaTechnicianMerchantDetailVO selectMerchantDetail(Long id) {
  567. if (id == null) {
  568. throw new ServiceException("商户ID不能为空");
  569. }
  570. MaTechnicianMerchantDetailVO detail = maTechnicianMapper.selectMerchantDetailById(id);
  571. if (detail == null) {
  572. throw new ServiceException("商户不存在或已删除");
  573. }
  574. return detail;
  575. }
  576. /**
  577. * 修改技师
  578. *
  579. * @param maTechnicianAppAddVo
  580. * @return 结果
  581. */
  582. @Override
  583. public int updateMaTechnician(MaTechnicianAppAddVo maTechnicianAppAddVo) {
  584. MaTechnician maTechnician = new MaTechnician();
  585. BeanUtils.copyProperties(maTechnicianAppAddVo, maTechnician);
  586. return maTechnicianMapper.updateMaTechnician(maTechnician);
  587. }
  588. /**
  589. * 批量删除技师
  590. *
  591. * @param ids 需要删除的技师主键
  592. * @return 结果
  593. */
  594. @Override
  595. public int deleteMaTechnicianByIds(Long[] ids) {
  596. return maTechnicianMapper.deleteMaTechnicianByIds(ids);
  597. }
  598. /**
  599. * 删除技师信息
  600. *
  601. * @param id 技师主键
  602. * @return 结果
  603. */
  604. @Override
  605. public int deleteMaTechnicianById(Long id) {
  606. return maTechnicianMapper.deleteMaTechnicianById(id);
  607. }
  608. /**
  609. * 首页选中的城市是否有开通服务
  610. *
  611. * @param areaCode
  612. * @return
  613. */
  614. @Override
  615. public Boolean isHasMerchantCity(String areaCode) {
  616. return maTechnicianMapper.isHasMerchantCity(areaCode);
  617. }
  618. /**
  619. * 首页按摩推荐
  620. *
  621. * @param dto
  622. * @return
  623. */
  624. @Override
  625. public List<MerchantVo> getMerchantRecommend(MassageMerchantRecommendDto dto) {
  626. return maTechnicianMapper.getMerchantRecommend(dto);
  627. }
  628. private MerchantProjectSelection checkMerchantAddParam(MaTechnicianMerchantAddDTO dto) {
  629. if (dto == null) {
  630. throw new ServiceException("商户参数不能为空");
  631. }
  632. checkRequiredText(dto.getTeName(), "姓名", 10);
  633. checkRequiredText(dto.getTeNickName(), "昵称", 10);
  634. checkRequiredText(dto.getTePhone(), "电话", 11);
  635. checkEnumValue(dto.getTeSex(), "性别", 0, 1);
  636. Set<Integer> categoryIds = checkOpenServiceIds(dto.getOpenService());
  637. checkEnumValue(dto.getTechType(), "商户类型", 0, 1);
  638. if (dto.getIsRecommend() != null) {
  639. checkEnumValue(dto.getIsRecommend(), "是否推荐", 0, 1);
  640. }
  641. return checkProjectIds(dto.getProjectIds(), categoryIds);
  642. }
  643. private void checkRequiredText(String value, String fieldName, int maxLength) {
  644. if (StringUtils.isBlank(value)) {
  645. throw new ServiceException(fieldName + "不能为空");
  646. }
  647. if (value.trim().length() > maxLength) {
  648. throw new ServiceException(fieldName + "长度不能超过" + maxLength + "个字符");
  649. }
  650. }
  651. private void checkEnumValue(Integer value, String fieldName, int... allowedValues) {
  652. if (value == null) {
  653. throw new ServiceException(fieldName + "不能为空");
  654. }
  655. for (int allowedValue : allowedValues) {
  656. if (value == allowedValue) {
  657. return;
  658. }
  659. }
  660. throw new ServiceException(fieldName + "值不正确");
  661. }
  662. private Integer normalizeSwitchValue(Integer value, String fieldName) {
  663. if (value == null) {
  664. return 0;
  665. }
  666. checkEnumValue(value, fieldName, 0, 1);
  667. return value;
  668. }
  669. /**
  670. * 校验服务项目ID集合
  671. *
  672. * @param projectIds 服务项目ID集合
  673. * @param categoryIds 服务类目ID集合
  674. * @return 有效服务项目ID集合
  675. */
  676. private MerchantProjectSelection checkProjectIds(List<Integer> projectIds, Set<Integer> categoryIds) {
  677. if (projectIds == null || projectIds.isEmpty()) {
  678. throw new ServiceException("服务项目不能为空");
  679. }
  680. Set<Integer> distinctProjectIds = new LinkedHashSet<>();
  681. for (Integer projectId : projectIds) {
  682. if (projectId == null) {
  683. throw new ServiceException("服务项目ID不能为空");
  684. }
  685. distinctProjectIds.add(projectId);
  686. }
  687. List<Project> projects = projectMapper.selectList(new LambdaQueryWrapper<Project>()
  688. .in(Project::getId, distinctProjectIds)
  689. .eq(Project::getIsDelete, 0));
  690. if (projects.size() != distinctProjectIds.size()) {
  691. throw new ServiceException("服务项目不存在或已删除");
  692. }
  693. Map<Integer, Project> projectMap = projects.stream()
  694. .collect(Collectors.toMap(project -> project.getId(), Function.identity(), (left, right) -> left));
  695. Set<Integer> projectCategoryIds = new LinkedHashSet<>();
  696. for (Integer projectId : distinctProjectIds) {
  697. Project project = projectMap.get(projectId);
  698. if (project == null || project.getCategoryId() == null) {
  699. throw new ServiceException("服务项目类目不能为空");
  700. }
  701. if (!categoryIds.contains(project.getCategoryId())) {
  702. throw new ServiceException("服务项目不属于所选服务类目");
  703. }
  704. projectCategoryIds.add(project.getCategoryId());
  705. }
  706. if (!projectCategoryIds.containsAll(categoryIds)) {
  707. throw new ServiceException("每个服务类目至少选择一个服务项目");
  708. }
  709. return new MerchantProjectSelection(categoryIds, distinctProjectIds, projectMap);
  710. }
  711. /**
  712. * 校验服务类目ID集合
  713. *
  714. * @param openService 服务类目ID集合
  715. * @return 去重后的服务类目ID集合
  716. */
  717. private Set<Integer> checkOpenServiceIds(List<Integer> openService) {
  718. if (openService == null || openService.isEmpty()) {
  719. throw new ServiceException("服务类目不能为空");
  720. }
  721. Set<Integer> categoryIds = new LinkedHashSet<>();
  722. for (Integer categoryId : openService) {
  723. if (categoryId == null) {
  724. throw new ServiceException("服务类目ID不能为空");
  725. }
  726. categoryIds.add(categoryId);
  727. }
  728. return categoryIds;
  729. }
  730. private String joinIds(Set<Integer> ids) {
  731. return ids.stream()
  732. .map(String::valueOf)
  733. .collect(Collectors.joining(","));
  734. }
  735. private String joinProjectTitles(Set<Integer> projectIds, Map<Integer, Project> projectMap) {
  736. return projectIds.stream()
  737. .map(projectMap::get)
  738. .map(Project::getTitle)
  739. .filter(StringUtils::isNotBlank)
  740. .collect(Collectors.joining(","));
  741. }
  742. /**
  743. * 新增商户与服务项目关联关系
  744. *
  745. * @param technicianId
  746. * @param projectIds
  747. */
  748. private void insertProjectRelations(Integer technicianId, Set<Integer> projectIds) {
  749. if (technicianId == null) {
  750. throw new ServiceException("商户ID不能为空");
  751. }
  752. List<MaTeProject> relations = new ArrayList<>();
  753. for (Integer projectId : projectIds) {
  754. MaTeProject relation = new MaTeProject();
  755. relation.setTeId(technicianId);
  756. relation.setProjectId(projectId);
  757. relations.add(relation);
  758. }
  759. int rows = maTeProjectMapper.insertBatch(relations);
  760. if (rows != relations.size()) {
  761. throw new ServiceException("新增商户服务项目失败");
  762. }
  763. }
  764. private static class MerchantProjectSelection {
  765. private final Set<Integer> categoryIds;
  766. private final Set<Integer> projectIds;
  767. private final Map<Integer, Project> projectMap;
  768. private MerchantProjectSelection(Set<Integer> categoryIds, Set<Integer> projectIds, Map<Integer, Project> projectMap) {
  769. this.categoryIds = categoryIds;
  770. this.projectIds = projectIds;
  771. this.projectMap = projectMap;
  772. }
  773. private Set<Integer> getCategoryIds() {
  774. return categoryIds;
  775. }
  776. private Set<Integer> getProjectIds() {
  777. return projectIds;
  778. }
  779. private Map<Integer, Project> getProjectMap() {
  780. return projectMap;
  781. }
  782. }
  783. /**
  784. * 获取未申请技能列表
  785. *
  786. * @param userId
  787. * @param typeId
  788. * @return
  789. */
  790. @Override
  791. public List<Project> getNotApplyList(String userId, String typeId) {
  792. LambdaQueryWrapper<MaProject> query = new LambdaQueryWrapper<>();
  793. query.eq(MaProject::getMerchantId, userId);
  794. query.eq(MaProject::getServiceTag, typeId);
  795. List<MaProject> maProjectList = maProjectMapper.selectList(query);
  796. // 获取已申请技能ID集合
  797. List<Long> projectIdList = maProjectList.stream().map(MaProject::getProjectId).collect(Collectors.toList());
  798. if (projectIdList.size() == 0) {
  799. LambdaQueryWrapper<Project> query1 = new LambdaQueryWrapper<>();
  800. query1.eq(Project::getType, typeId);
  801. return projectMapper.selectList(query1);
  802. }
  803. LambdaQueryWrapper<Project> query2 = new LambdaQueryWrapper<>();
  804. query2.eq(Project::getType, typeId);
  805. query2.notIn(Project::getId, projectIdList);
  806. return projectMapper.selectList(query2);
  807. }
  808. /**
  809. * 申请开通新服务
  810. *
  811. * @param dto
  812. * @return
  813. */
  814. @Transactional(rollbackFor = Exception.class)
  815. public int applyForService(MaProjectSaveDto dto) {
  816. if (Objects.isNull(dto)) {
  817. return 0;
  818. }
  819. if (dto.getProjectIdList().size() > 0) {
  820. // 插入商户技能
  821. extracted(dto);
  822. } else {
  823. return 0;
  824. }
  825. return 1;
  826. }
  827. /**
  828. * 商户入住信息
  829. *
  830. * @param userId
  831. * @return
  832. */
  833. @Override
  834. public MerchantAuditFile getTechnicianList(Long userId) {
  835. LambdaQueryWrapper<MaTechnician> query = new LambdaQueryWrapper<>();
  836. query.eq(MaTechnician::getId, userId);
  837. MaTechnician merchant = maTechnicianMapper.selectOne(query);
  838. LambdaQueryWrapper<MerchantApplyFile> query1 = new LambdaQueryWrapper<>();
  839. query1.eq(MerchantApplyFile::getMerchantId, userId);
  840. List<MerchantApplyFile> merchantApplyFile = merchantApplyFileMapper.selectList(query1);
  841. MerchantAuditFile merchantAuditFile = new MerchantAuditFile();
  842. merchantAuditFile.setMerchant(merchant);
  843. merchantAuditFile.setMerchantAuditFile(merchantApplyFile);
  844. return merchantAuditFile;
  845. }
  846. /**
  847. * 查询商户合同记录信息
  848. *
  849. * @param userId
  850. * @return
  851. */
  852. @Override
  853. public List<ContractRecord> getContractRecords(Long userId){
  854. LambdaQueryWrapper<ContractRecord> query = new LambdaQueryWrapper<>();
  855. query.eq(ContractRecord::getMerchantId, userId);
  856. List<ContractRecord> contractRecordList = contractRecordMapper.selectList(query);
  857. if(contractRecordList.size() == 0) {
  858. return new ArrayList<>();
  859. }else {
  860. Set<String> seen = new HashSet<>();
  861. contractRecordList = contractRecordList.stream()
  862. .filter(record -> record.getContractName() != null && seen.add(record.getContractName()))
  863. .collect(Collectors.toList());
  864. }
  865. return contractRecordList;
  866. }
  867. private void extracted(MaProjectSaveDto dto) {
  868. LambdaQueryWrapper<Project> query = new LambdaQueryWrapper<>();
  869. query.in(Project::getId, dto.getProjectIdList());
  870. List<Project> projectList = projectMapper.selectList(query);
  871. for (Project project : projectList) {
  872. MaProject maProject = new MaProject();
  873. maProject.setProjectId(project.getId().longValue());
  874. maProject.setProjectName(project.getTitle());
  875. maProject.setProjectDescribe(project.getDetail());
  876. maProject.setProjectDuration(project.getStandardDuration());
  877. maProject.setProjectOriginalPrice(project.getPrice());
  878. maProject.setProjectMaxPrice(project.getPriceMax());
  879. maProject.setProjectLowestPrice(project.getPriceMin());
  880. maProject.setCreateBy(dto.getUserId());
  881. maProject.setUpdateBy(dto.getUserId());
  882. maProject.setMerchantId(dto.getUserId());
  883. maProject.setApplyTime(DateUtils.getNowDate());
  884. maProject.setMerchantPhone(dto.getMerchantPhone());
  885. maProjectMapper.insert(maProject);
  886. }
  887. }
  888. /**
  889. * 状态切换
  890. *
  891. * @param userId
  892. * @param forceConfirm
  893. * @return
  894. */
  895. @Override
  896. public Result switchToOffline(Long userId, Boolean forceConfirm) {
  897. MaTechnician technician = getTechnician(userId);
  898. // 1. 基础权限校验 (对应流程图左下角)
  899. if (!hasActiveSkills(userId)) {
  900. throw new RuntimeException("请先申请开通技能");
  901. }
  902. if (!hasHomeAddress(userId)) {
  903. // 这里通常会触发前端弹窗要求添加地址,或者直接阻断
  904. throw new RuntimeException("请完善家庭地址");
  905. }
  906. if (ProjectCategoryEnum.MASSAGE.getCode().equals(technician.getServiceTag())) {
  907. // 2. 状态判断逻辑 (对应流程图中间的菱形判断)
  908. // 只有处于“在线接单”状态才需要检查疲劳度/时长限制
  909. if (TechnicianStatusEnum.ONLINE.getCode().equals(technician.getPostState())) {
  910. if (technician.getServiceState().equals(JsStatusEnum.JS_SERVICE.getCode())) {
  911. throw new ServiceException("您有服务中的订单,不能下岗");
  912. }
  913. // 获取今日的商户考勤记录
  914. MerchantDailyAttendance attendance = getTodayAttendance(userId);
  915. long minutesOnline = 0;
  916. if (attendance != null && attendance.getAttendanceStartTime() != null) {
  917. LocalDateTime localDateTime = attendance.getAttendanceStartTime().toInstant()
  918. .atZone(ZoneId.systemDefault())
  919. .toLocalDateTime();
  920. //计算截止现在的时长,单位为分钟
  921. minutesOnline = Duration.between(localDateTime, LocalDateTime.now()).toMinutes();
  922. // 计算今日的累加在线时长
  923. minutesOnline = minutesOnline + getWorkDuration(userId);
  924. }
  925. AttendanceRule rule = getAttendanceRule();
  926. if (rule != null) {
  927. // 将小时转换为分钟进行比较
  928. BigDecimal minutes = rule.getBasicWorkHours().multiply(new BigDecimal(60));
  929. // 2. 精确转换成 long(无小数、无溢出才成功)
  930. long requiredMinutes = minutes.longValueExact();
  931. // 判断是否超过了平台规定的在线时间 (X小时)
  932. if (minutesOnline < requiredMinutes) {
  933. // 情况 A: 超时了,且用户还没有点击“确认下线”(forceConfirm=false)
  934. if (!forceConfirm) {
  935. // 返回特定错误码或数据结构,告诉前端弹出“我在想想/确认下线”的模态框
  936. return Result.ok("平台对您的在线时间做了约定,每日在线需满足"
  937. + (requiredMinutes / 60) + "小时,距离您下线时间还剩余" + ((requiredMinutes / 60) - minutesOnline) + "小时"
  938. + "不满足在线时间将收到平台处罚,是否确认下线?");
  939. }
  940. // 情况 B: 超时了,但用户已经点击了“确认下线”,允许通过
  941. }
  942. }
  943. }
  944. // 3. 执行状态更新 (更新为休息中状态)
  945. updateStatus(userId, TechnicianStatusEnum.RESTING);
  946. if (!forceConfirm) {
  947. // 格式化成 yyyy-MM-dd 纯日期字符串
  948. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  949. String today = sdf.format(new Date());
  950. // 查询商户今日的最近考勤记录
  951. LambdaQueryWrapper<MerchantDailyAttendance> query = new LambdaQueryWrapper<>();
  952. query.eq(MerchantDailyAttendance::getMerchantId, userId)
  953. .eq(MerchantDailyAttendance::getAttendanceDate, today)
  954. .orderByDesc(MerchantDailyAttendance::getCreateTime);
  955. MerchantDailyAttendance update = merchantDailyAttendanceMapper.selectOne(query);
  956. if (update != null) {
  957. LocalDateTime localDateTime = update.getAttendanceStartTime().toInstant()
  958. .atZone(ZoneId.systemDefault())
  959. .toLocalDateTime();
  960. LambdaUpdateWrapper<MerchantDailyAttendance> updateWrapper = new LambdaUpdateWrapper<>();
  961. updateWrapper.eq(MerchantDailyAttendance::getId, update.getId())
  962. .set(MerchantDailyAttendance::getAttendanceEndTime, DateUtils.getNowDate())
  963. .set(MerchantDailyAttendance::getTotalWorkMinutes, Duration.between(localDateTime, LocalDateTime.now()).toMinutes())
  964. .set(MerchantDailyAttendance::getUpdateTime, DateUtils.getNowDate());
  965. merchantDailyAttendanceMapper.update(update, updateWrapper);
  966. }
  967. }
  968. } else {
  969. //更新状态为在线接单
  970. updateStatus(userId, TechnicianStatusEnum.ONLINE);
  971. // 插入今日考勤记录
  972. MerchantDailyAttendance merchantDailyAttendance = new MerchantDailyAttendance()
  973. .setMerchantId(userId.intValue())
  974. .setAttendanceDate(DateUtils.getNowDate())
  975. .setMerchantName(technician.getTeName())
  976. .setAttendanceStartTime(DateUtils.getNowDate())
  977. .setCreateBy(technician.getTeName())
  978. .setCreateTime(LocalDateTime.now());
  979. merchantDailyAttendanceMapper.insert(merchantDailyAttendance);
  980. }
  981. return Result.ok("状态已切换成功");
  982. }
  983. /**
  984. * 获取今天商户的考勤记录
  985. *
  986. * @param userId 技师ID
  987. * @return 考勤记录
  988. */
  989. private MerchantDailyAttendance getTodayAttendance(Long userId) {
  990. LambdaQueryWrapper<MerchantDailyAttendance> wrapper = new LambdaQueryWrapper<>();
  991. wrapper.eq(MerchantDailyAttendance::getMerchantId, userId)
  992. .eq(MerchantDailyAttendance::getAttendanceDate, DateUtils.getNowDate())
  993. .orderByDesc(MerchantDailyAttendance::getCreateTime)
  994. .last("LIMIT 1");
  995. return merchantDailyAttendanceMapper.selectOne(wrapper);
  996. }
  997. /**
  998. * 获取商户的累计工作时长
  999. *
  1000. * @param userId 技师ID
  1001. * @return 工作时长(分钟)
  1002. */
  1003. private long getWorkDuration(Long userId) {
  1004. LambdaQueryWrapper<MerchantDailyAttendance> wrapper = new LambdaQueryWrapper<>();
  1005. wrapper.eq(MerchantDailyAttendance::getMerchantId, userId)
  1006. .eq(MerchantDailyAttendance::getAttendanceDate, DateUtils.getNowDate());
  1007. List<MerchantDailyAttendance> attendances = merchantDailyAttendanceMapper.selectList(wrapper);
  1008. if (attendances == null || attendances.isEmpty()) return 0;
  1009. // 计算指定日期的总分钟数
  1010. long totalMinutes = attendances.stream()
  1011. .filter(attendance -> DateUtils.getNowDate().toString().equals(attendance.getAttendanceDate().toString()))
  1012. .mapToLong(MerchantDailyAttendance::getTotalWorkMinutes)
  1013. .sum();
  1014. return totalMinutes;
  1015. }
  1016. /**
  1017. * 获取商户的考勤规则
  1018. *
  1019. * @return 考勤规则
  1020. */
  1021. private AttendanceRule getAttendanceRule() {
  1022. LambdaQueryWrapper<AttendanceRule> wrapper = new LambdaQueryWrapper<>();
  1023. wrapper.eq(AttendanceRule::getIsDelete, 0);
  1024. wrapper.eq(AttendanceRule::getWorkDurationRuleEnabled, 1);
  1025. wrapper.last("LIMIT 1");
  1026. return attendanceRuleMapper.selectOne(wrapper);
  1027. }
  1028. /**
  1029. * 判断用户是否有生效中的技能
  1030. *
  1031. * @param userId 技师ID
  1032. * @return true: 有可用技能, false: 无
  1033. */
  1034. public boolean hasActiveSkills(Long userId) {
  1035. if (userId == null) return false;
  1036. // 构建查询条件:用户ID匹配 AND 状态为已发布/生效中
  1037. LambdaQueryWrapper<MaProject> wrapper = new LambdaQueryWrapper<>();
  1038. wrapper.eq(MaProject::getMerchantId, userId)
  1039. .eq(MaProject::getAuditStatus, 1); // 假设 1 代表 "生效/已审核"
  1040. // 只要查到一条记录即返回 true
  1041. return maProjectMapper.selectCount(wrapper) > 0;
  1042. }
  1043. /**
  1044. * 判断用户是否有家庭地址(通常指设置为默认的地址)
  1045. *
  1046. * @param userId 技师ID
  1047. * @return true: 有地址, false: 无
  1048. */
  1049. public boolean hasHomeAddress(Long userId) {
  1050. if (userId == null) return false;
  1051. // 构建查询条件:用户ID匹配 AND 是默认地址(可选) AND 未删除
  1052. LambdaQueryWrapper<TAddress> wrapper = new LambdaQueryWrapper<>();
  1053. wrapper.eq(TAddress::getMerchantId, userId)
  1054. .eq(TAddress::getUserType, 2) // 商户类型
  1055. .eq(TAddress::getIsDelete, 0); // 逻辑未删除
  1056. // 统计数量是否大于0
  1057. long count = addressMapper.selectCount(wrapper);
  1058. return count > 0;
  1059. }
  1060. // 辅助方法:模拟获取用户
  1061. private MaTechnician getTechnician(Long userId) {
  1062. // ... DB查询逻辑
  1063. LambdaQueryWrapper<MaTechnician> query = new LambdaQueryWrapper<>();
  1064. query.eq(MaTechnician::getId, userId);
  1065. return maTechnicianMapper.selectOne(query);
  1066. }
  1067. // 辅助方法:更新状态
  1068. private void updateStatus(Long userId, TechnicianStatusEnum status) {
  1069. LambdaUpdateWrapper<MaTechnician> update = new LambdaUpdateWrapper<>();
  1070. update.eq(MaTechnician::getId, userId);
  1071. update.set(MaTechnician::getPostState, status.getCode());
  1072. // ... DB Update逻辑,同时记录上线/下线时间
  1073. maTechnicianMapper.update(null, update);
  1074. }
  1075. /**
  1076. * 后台待审核页面审核通过商户。
  1077. *
  1078. * @param id 商户ID
  1079. * @param dto 待审核通过参数
  1080. * @param loginUser 当前登录用户
  1081. * @return 结果
  1082. */
  1083. @Override
  1084. @Transactional(rollbackFor = Exception.class)
  1085. public int approvePendingMerchantAudit(Integer id, MaTechnicianPendingAuditSubmitDTO dto, LoginUser loginUser) {
  1086. if (id == null) {
  1087. throw new ServiceException("商户ID不能为空");
  1088. }
  1089. if (dto == null) {
  1090. throw new ServiceException("审核参数不能为空");
  1091. }
  1092. String idCardFront = checkRequiredFileUrl(dto.getIdCardFront(), "身份证正面加密图片");
  1093. String idCardBack = checkRequiredFileUrl(dto.getIdCardBack(), "身份证反面加密图片");
  1094. String healthCertificate = checkRequiredFileUrl(dto.getHealthCertificate(), "健康证加密图片");
  1095. String qualificationCertificate = checkRequiredFileUrl(dto.getQualificationCertificate(), "资格证加密图片");
  1096. checkRequiredExpirationDate(dto.getIdCardExpirationDate(), "身份证到期时间");
  1097. checkRequiredExpirationDate(dto.getHealthCertificateExpirationDate(), "健康证到期时间");
  1098. checkRequiredExpirationDate(dto.getQualificationCertificateExpirationDate(), "资格证到期时间");
  1099. String auditRemark = dto.getAuditRemark() == null ? "" : dto.getAuditRemark().trim();
  1100. if (auditRemark.length() > AUDIT_REMARK_MAX_LENGTH) {
  1101. throw new ServiceException("审核备注长度不能超过" + AUDIT_REMARK_MAX_LENGTH + "个字符");
  1102. }
  1103. MaTechnician existsMerchant = maTechnicianMapper.selectMerchantById(id);
  1104. if (existsMerchant == null || !NOT_DELETED.equals(existsMerchant.getIsDelete())) {
  1105. throw new ServiceException("商户不存在或已删除");
  1106. }
  1107. if (!Integer.valueOf(AUDIT_WAIT_REVIEW).equals(existsMerchant.getAuditStatus())) {
  1108. throw new ServiceException("当前商户不是待审核状态,不能审核通过");
  1109. }
  1110. MaTechnician maTechnician = new MaTechnician();
  1111. maTechnician.setId(id);
  1112. /*maTechnician.setIdCard(String.join(",", idCardFront, idCardBack));
  1113. maTechnician.setHealthCertificate(healthCertificate);
  1114. maTechnician.setQualificationCertificate(qualificationCertificate);*/
  1115. maTechnician.setIdCardExpirationDate(dto.getIdCardExpirationDate());
  1116. maTechnician.setHealthCertificateExpirationDate(dto.getHealthCertificateExpirationDate());
  1117. maTechnician.setQualificationCertificateExpirationDate(dto.getQualificationCertificateExpirationDate());
  1118. maTechnician.setAuditStatus(AUDIT_APPROVED);
  1119. maTechnician.setAuditRemark(auditRemark);
  1120. maTechnician.setApproveTime(DateUtils.getNowDate());
  1121. if (loginUser != null && loginUser.getUser() != null) {
  1122. maTechnician.setUpdateBy(loginUser.getUser().getUserName());
  1123. }
  1124. maTechnician.setUpdateTime(DateUtils.getNowDate());
  1125. int rows = maTechnicianMapper.approvePendingMerchantAuditById(maTechnician);
  1126. if (rows <= 0) {
  1127. throw new ServiceException("待审核商户审核通过失败");
  1128. }
  1129. return rows;
  1130. }
  1131. private String checkRequiredFileUrl(String value, String fieldName) {
  1132. if (StringUtils.isBlank(value)) {
  1133. throw new ServiceException(fieldName + "不能为空");
  1134. }
  1135. return value.trim();
  1136. }
  1137. private void checkRequiredExpirationDate(LocalDate value, String fieldName) {
  1138. if (value == null) {
  1139. throw new ServiceException(fieldName + "不能为空");
  1140. }
  1141. if (value.isBefore(LocalDate.now())) {
  1142. throw new ServiceException(fieldName + "不能早于当前日期");
  1143. }
  1144. }
  1145. /**
  1146. * 技师待处理订单列表
  1147. *
  1148. * @param query
  1149. * @return
  1150. */
  1151. @Override
  1152. public List<WaitOrderDTO> listWaitOrder(WaitOrderQueryDTO query) {
  1153. LambdaQueryWrapper<TOrder> queryWrapper = new LambdaQueryWrapper<>();
  1154. queryWrapper.eq(TOrder::getStatus, OrderStatusEnum.WAIT_JD.getCode());
  1155. // 1.查询所有待派未接单订单(status=待接单)
  1156. List<TOrder> allWaitOrder = orderMapper.selectList(queryWrapper);
  1157. if (CollectionUtils.isEmpty(allWaitOrder)) {
  1158. return Collections.emptyList();
  1159. }
  1160. BigDecimal techLat = query.getTechLat();
  1161. BigDecimal techLng = query.getTechLng();
  1162. // 2.逐个计算两点距离(Haversine公式)
  1163. List<WaitOrderDTO> dtoList = allWaitOrder.stream().map(order -> {
  1164. WaitOrderDTO dto = new WaitOrderDTO();
  1165. dto.setOrderId(order.getId());
  1166. dto.setProjectName(getShortProjectName(order.getProjectName()));
  1167. dto.setCustomerType("新客户");//无历史绑定默认新客,接单后再统计老客
  1168. dto.setOrderCreateTime(order.getCreateTime());
  1169. dto.setAppointTime(order.getAppointmentStartTime());
  1170. dto.setTargetAddress(order.getContactAddressInfo());
  1171. dto.setOrderLat(order.getUserLatitude());
  1172. dto.setOrderLng(order.getUserLongitude());
  1173. // 计算两点距离 单位:米
  1174. BigDecimal disMeter = calcDistance(techLat, techLng, order.getUserLatitude(), order.getUserLongitude());
  1175. dto.setDistanceMeter(disMeter);
  1176. dto.setDistanceDesc(formatDistance(disMeter));
  1177. return dto;
  1178. }).collect(Collectors.toList());
  1179. // 3.距离升序:近的排在最前面
  1180. return dtoList.stream()
  1181. .sorted(Comparator.comparing(WaitOrderDTO::getDistanceMeter))
  1182. .collect(Collectors.toList());
  1183. }
  1184. /**
  1185. * 接单
  1186. *
  1187. * @param req
  1188. * @return
  1189. */
  1190. @Override
  1191. public String acceptOrder(AcceptOrderReqDTO req) {
  1192. Long techId = req.getTechId();
  1193. Long orderId = req.getOrderId();
  1194. //【校验1:订单是否已被其他技师接单】
  1195. TOrder order = orderMapper.selectById(orderId);
  1196. if (OrderStatusEnum.RECEIVED_ORDER.getCode().equals(order.getStatus())) {
  1197. return OrderTipEnum.REPEAT_ORDER.getTip();
  1198. }
  1199. //【校验2:时间冲突校验(该技师已有已接单订单)】
  1200. boolean isTimeConflict = checkOrderTimeConflict(techId, order);
  1201. if (isTimeConflict) {
  1202. String tip = String.format(OrderTipEnum.TIME_CONFLICT.getTip(),
  1203. order.getStartTime().format(DateTimeFormatter.ofPattern("MM月dd日HH:mm")),
  1204. order.getCompletedTime().format(DateTimeFormatter.ofPattern("MM月dd日HH:mm")));
  1205. return tip;
  1206. }
  1207. //【校验3:技师休息状态】
  1208. MaTechnician tech = maTechnicianMapper.selectById(techId);
  1209. if (TechnicianStatusEnum.RESTING.getCode().equals(tech.getPostState())) {
  1210. return OrderTipEnum.REST_CONFIRM.getTip();
  1211. }
  1212. // 正常接单,绑定技师ID到订单
  1213. doAcceptOrder(techId, orderId);
  1214. return OrderTipEnum.ALREADY_ACCEPT.getTip();
  1215. }
  1216. /**
  1217. * 技师接单确认接单
  1218. *
  1219. * @param techId
  1220. * @param orderId
  1221. * @return
  1222. */
  1223. @Override
  1224. public String confirmRestAccept(Long techId, Long orderId){
  1225. LambdaUpdateWrapper<MaTechnician> update = new LambdaUpdateWrapper<>();
  1226. update.eq(MaTechnician::getId, techId);
  1227. update.set(MaTechnician::getPostState, TechnicianStatusEnum.ONLINE.getCode());
  1228. maTechnicianMapper.update(null, update);
  1229. doAcceptOrder(techId, orderId);
  1230. return OrderTipEnum.ALREADY_ACCEPT.getTip();
  1231. }
  1232. /**
  1233. * 技师拒绝接单
  1234. *
  1235. * @param req
  1236. * @return
  1237. */
  1238. @Override
  1239. public void refuseOrder(RefuseOrderReqDTO req){
  1240. LambdaUpdateWrapper<TOrder> update = new LambdaUpdateWrapper<>();
  1241. update.eq(TOrder::getId, req.getOrderId());
  1242. update.set(TOrder::getStatus, OrderStatusEnum.REFUSE.getCode());
  1243. update.set(TOrder::getRejectedReason, req.getRefuseReason());
  1244. orderMapper.update(null, update);
  1245. //拒单后订单重回待接单池,其他技师可刷到
  1246. LambdaUpdateWrapper<TOrder> update2 = new LambdaUpdateWrapper<>();
  1247. update2.eq(TOrder::getId, req.getOrderId());
  1248. update2.set(TOrder::getStatus, OrderStatusEnum.WAIT_JD.getCode());
  1249. update2.set(TOrder::getMerchantId, "");
  1250. update2.set(TOrder::getRejectedReason, "");
  1251. orderMapper.update(null, update2);
  1252. }
  1253. // =================工具方法=================
  1254. /**
  1255. * Haversine 计算经纬度距离 返回米
  1256. */
  1257. private BigDecimal calcDistance(BigDecimal lat1, BigDecimal lng1, BigDecimal lat2, BigDecimal lng2) {
  1258. // 球面距离计算公式,地球半径6371000米
  1259. // 可使用BigDecimal三角函数或数据库函数优化
  1260. double latRad1 = Math.toRadians(lat1.doubleValue());
  1261. double latRad2 = Math.toRadians(lat2.doubleValue());
  1262. double lngRad1 = Math.toRadians(lng1.doubleValue());
  1263. double lngRad2 = Math.toRadians(lng2.doubleValue());
  1264. double dLat = latRad2 - latRad1;
  1265. double dLng = lngRad2 - lngRad1;
  1266. double a = Math.pow(Math.sin(dLat / 2), 2)
  1267. + Math.cos(latRad1) * Math.cos(latRad2)
  1268. * Math.pow(Math.sin(dLng / 2), 2);
  1269. double dis = 2 * 6371000 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
  1270. return BigDecimal.valueOf(dis).setScale(2, BigDecimal.ROUND_HALF_UP);
  1271. }
  1272. private String getShortProjectName(String name) {
  1273. if (name != null && name.length() > 10) {
  1274. return name.substring(0, 8) + "...";
  1275. }
  1276. return name;
  1277. }
  1278. private String formatDistance(BigDecimal distanceMeter) {
  1279. BigDecimal km = distanceMeter.divide(new BigDecimal(1000), 2, BigDecimal.ROUND_HALF_UP);
  1280. if (km.compareTo(BigDecimal.ONE) > 0) {
  1281. return km + "km";
  1282. } else {
  1283. return distanceMeter.intValue() + "m";
  1284. }
  1285. }
  1286. /**
  1287. * 校验技师已有订单时间冲突(只查该技师已接单数据)
  1288. */
  1289. private boolean checkOrderTimeConflict(Long techId, TOrder newOrder) {
  1290. LambdaQueryWrapper<TOrder> query = new LambdaQueryWrapper<>();
  1291. query.eq(TOrder::getMerchantId, techId);
  1292. query.eq(TOrder::getStatus, OrderStatusEnum.RECEIVED_ORDER.getCode());
  1293. query.eq(TOrder::getStartTime, newOrder.getStartTime());
  1294. List<TOrder> acceptedOrders = orderMapper.selectList(query);
  1295. LocalDate newOrderDate = newOrder.getStartTime().toLocalDate();
  1296. LocalDateTime newStart = newOrder.getStartTime();
  1297. for (TOrder old : acceptedOrders) {
  1298. if (!newOrderDate.isEqual(old.getCompletedTime().toLocalDate())) {
  1299. continue;
  1300. }
  1301. LocalDateTime oldEnd = old.getCompletedTime();
  1302. if (newStart.isBefore(oldEnd) || newStart.isEqual(oldEnd)) {
  1303. return true;
  1304. }
  1305. }
  1306. return false;
  1307. }
  1308. /**
  1309. * 接单:给订单赋值技师ID
  1310. */
  1311. private void doAcceptOrder(Long techId, Long orderId) {
  1312. TOrder update = new TOrder();
  1313. update.setId(orderId);
  1314. update.setMerchantId(techId);
  1315. update.setStatus(OrderStatusEnum.RECEIVED_ORDER.getCode());
  1316. orderMapper.updateById(update);
  1317. }
  1318. }