CollegeUserPointsService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. package com.ygj.yuemum.service.college;
  2. import com.ygj.yuemum.component.Constant;
  3. import com.ygj.yuemum.controller.global.WeChatInfoController;
  4. import com.ygj.yuemum.dao.college.CollegeUserPointsDao;
  5. import com.ygj.yuemum.domain.college.*;
  6. import com.ygj.yuemum.domain.customer.CustomerBooking;
  7. import com.ygj.yuemum.domain.wxmini.WXUser;
  8. import com.ygj.yuemum.service.coupon.CouponCreateService;
  9. import com.ygj.yuemum.service.customer.CustomerBookingService;
  10. import com.ygj.yuemum.service.wxmini.WXUserService;
  11. import com.ygj.yuemum.utils.DateUtil;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Service;
  14. import com.ygj.yuemum.utils.DistanceUtil;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Date;
  17. import java.util.LinkedHashMap;
  18. import java.util.List;
  19. @Service
  20. public class CollegeUserPointsService {
  21. @Autowired
  22. private CollegeUserPointsDao collegeUserPointsDao;
  23. @Autowired
  24. private WXUserService wxUserService;
  25. @Autowired
  26. private CollegePresentRuleService collegePresentRuleService;
  27. @Autowired
  28. private CollegeCurriculumCoreService collegeCurriculumCoreService;
  29. @Autowired
  30. private CollegeCurriculumPracticeService collegeCurriculumPracticeService;
  31. @Autowired
  32. private CollegeCurriculumExperienceService collegeCurriculumExperienceService;
  33. @Autowired
  34. private CollegePointsDetailService collegePointsDetailService;
  35. @Autowired
  36. private CustomerBookingService customerBookingService;
  37. @Autowired
  38. private WeChatInfoController weChatInfoController;
  39. @Autowired
  40. private CouponCreateService couponCreateService;
  41. @Autowired
  42. private CollegeBookingService collegeBookingService;
  43. @Autowired
  44. private CollegeLearningDetailService collegeLearningDetailService;
  45. public Integer LMP = Constant.LMP;
  46. public Integer AGE = Constant.AGE;
  47. public List<CollegeUserPoints> getCollegeUserPoints() {
  48. List<CollegeUserPoints> collegeUserPoints = collegeUserPointsDao.getAll();
  49. return collegeUserPoints;
  50. }
  51. public CollegeUserPoints getCollegeUserPoint(Integer id) {
  52. CollegeUserPoints collegeUserPoints = collegeUserPointsDao.selectByPrimaryKey(id);
  53. return collegeUserPoints;
  54. }
  55. public int addCollegeUserPoints(CollegeUserPoints collegeUserPoints) {
  56. return collegeUserPointsDao.insertSelective(collegeUserPoints);
  57. }
  58. public int deleteCollegeUserPoints(Integer id) {
  59. return collegeUserPointsDao.deleteByPrimaryKey(id);
  60. }
  61. public int updateCollegeUserPoints(CollegeUserPoints collegeUserPoints) {
  62. return collegeUserPointsDao.updateByPrimaryKeySelective(collegeUserPoints);
  63. }
  64. public int updatePointsByTest(CollegePointsDetail collegePointsDetail) {
  65. return collegeUserPointsDao.updatePointsByTest(collegePointsDetail);
  66. }
  67. public CollegeUserPoints queryOneUserPoints(String openid) {
  68. return collegeUserPointsDao.queryOneUserPoints(openid);
  69. }
  70. public LinkedHashMap<String, Object> QueryUserPoints(String openid, String city) {
  71. LinkedHashMap<String, Object> tableData = new LinkedHashMap<>();
  72. WXUser wxUser = wxUserService.getWXUser(openid);
  73. tableData.put("stage", null);
  74. tableData.put("stage_date", null);
  75. if (wxUser != null && wxUser.getXu_phone() != null) {
  76. if (wxUser.getXu_content_type() != null && wxUser.getXu_content_date() != null) {
  77. try {
  78. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  79. SimpleDateFormat ldf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  80. Date dateNow = new Date();
  81. Date getDate = sdf.parse(wxUser.getXu_content_date());
  82. CollegeCurriculumCore collegeCurriculumCoreQuery = new CollegeCurriculumCore();
  83. CollegeCurriculumPractice collegeCurriculumPracticeQuery = new CollegeCurriculumPractice();
  84. CollegeCurriculumExperience collegeCurriculumExperienceQuery = new CollegeCurriculumExperience();
  85. collegeCurriculumCoreQuery.setOpenid(openid);
  86. collegeCurriculumPracticeQuery.setOpenid(openid);
  87. collegeCurriculumExperienceQuery.setOpenid(openid);
  88. CollegePointsDetail collegePointsDetail = new CollegePointsDetail();
  89. if (wxUser.getXu_content_type().equals("B01")) { //育儿
  90. if (dateNow.getTime() < getDate.getTime()) {
  91. //还没出生
  92. return tableData;
  93. } else {
  94. if (DateUtil.getMonthDiff(dateNow, getDate) + 1 > AGE) {
  95. return tableData;
  96. } else {
  97. tableData.put("stage_step", DateUtil.getMonthDiff(dateNow, getDate) + 1);
  98. collegeCurriculumCoreQuery.setC_type("B01");
  99. collegeCurriculumCoreQuery.setC_step(DateUtil.getMonthDiff(dateNow, getDate) + 1);
  100. collegeCurriculumPracticeQuery.setC_type("B01");
  101. collegeCurriculumPracticeQuery.setC_step(DateUtil.getMonthDiff(dateNow, getDate) + 1);
  102. collegeCurriculumExperienceQuery.setC_type("B01");
  103. collegeCurriculumExperienceQuery.setC_step(DateUtil.getMonthDiff(dateNow, getDate) + 1);
  104. }
  105. }
  106. } else if (wxUser.getXu_content_type().equals("P01")) { //孕周
  107. if (dateNow.getTime() < getDate.getTime()) {
  108. if (LMP - DateUtil.getWeekDiff(dateNow, getDate) < 0) {
  109. return tableData; //还没怀孕
  110. } else {
  111. tableData.put("stage_step", LMP - DateUtil.getWeekDiff(dateNow, getDate));
  112. collegeCurriculumCoreQuery.setC_type("P01");
  113. collegeCurriculumCoreQuery.setC_step(LMP - DateUtil.getWeekDiff(dateNow, getDate));
  114. collegeCurriculumPracticeQuery.setC_type("P01");
  115. collegeCurriculumPracticeQuery.setC_step(LMP - DateUtil.getWeekDiff(dateNow, getDate));
  116. collegeCurriculumExperienceQuery.setC_type("P01");
  117. collegeCurriculumExperienceQuery.setC_step(LMP - DateUtil.getWeekDiff(dateNow, getDate));
  118. }
  119. } else {
  120. return tableData;
  121. }
  122. } else {
  123. return tableData;
  124. }
  125. tableData.put("stage", wxUser.getXu_content_type());
  126. tableData.put("stage_date", wxUser.getXu_content_date());
  127. tableData.put("avatar_url", wxUser.getXu_avatarUrl());
  128. //查询学分
  129. int point_available = 0;
  130. CollegeUserPoints collegeUserPoints = collegeUserPointsDao.queryOneUserPoints(openid);
  131. if (collegeUserPoints == null) {
  132. CollegeUserPoints collegeUserPointsNew = new CollegeUserPoints();
  133. collegeUserPointsNew.setOpenid(openid);
  134. collegeUserPointsNew.setCredit_total(0);
  135. collegeUserPointsNew.setCredit_available(0);
  136. collegeUserPointsDao.insertSelective(collegeUserPointsNew);
  137. tableData.put("credit", 0);
  138. } else {
  139. tableData.put("credit", collegeUserPoints.getCredit_total());
  140. point_available = collegeUserPoints.getCredit_total();
  141. }
  142. //礼品领取情况
  143. List<CollegePresentRule> collegePresentRules = collegePresentRuleService.queryUserReceived(openid);
  144. for (CollegePresentRule collegePresentRule : collegePresentRules) {
  145. if (collegePresentRule.getReceived() != 1 && point_available >= collegePresentRule.getValue()) {
  146. //返回礼品内容
  147. tableData.put("surprise", "true");
  148. tableData.put("surprise_type", collegePresentRule.getType());
  149. tableData.put("surprise_name", collegePresentRule.getPresent_name());
  150. //自动发放礼品
  151. if (collegePresentRule.getType() == 1) {
  152. // 实物生成一条预约信息并发送通知
  153. CustomerBooking customerBooking = new CustomerBooking();
  154. customerBooking.setBk_branches_id(Integer.valueOf(city));
  155. customerBooking.setBk_phone(wxUser.getXu_phone());
  156. customerBooking.setBk_date(ldf.format(new Date()));
  157. customerBooking.setBk_type(6);
  158. customerBooking.setBk_desc(collegePresentRule.getPresent_name());
  159. customerBookingService.insertCustomerBooking(customerBooking);
  160. weChatInfoController.WXSendBookingMessage(customerBooking);
  161. collegePointsDetail.setContext(collegePresentRule.getPresent_name());
  162. } else {
  163. //优惠券领取接口
  164. String cb_code = couponCreateService.couponIssue(collegePresentRule.getCoupon_cid(), wxUser.getXu_phone());
  165. collegePointsDetail.setContext(collegePresentRule.getPresent_name() + ":" + cb_code);
  166. }
  167. //保存领取日志
  168. collegePointsDetail.setOpenid(openid);
  169. collegePointsDetail.setDate(ldf.format(new Date()));
  170. collegePointsDetail.setEvent_code(4);
  171. collegePointsDetail.setCredit(collegePresentRule.getValue() * -1);
  172. collegePointsDetail.setRule_id(collegePresentRule.getId());
  173. collegePointsDetailService.addCollegePointsDetail(collegePointsDetail);
  174. //更新学分 学分不减
  175. // collegeUserPoints.setCredit_available(collegeUserPoints.getCredit_available() - collegePresentRule.getValue());
  176. // collegeUserPointsDao.updateByPrimaryKeySelective(collegeUserPoints);
  177. break;
  178. }
  179. }
  180. tableData.put("present_received", collegePresentRules);
  181. //常规课
  182. List<CollegeCurriculumCore> collegeCurriculumCores = collegeCurriculumCoreService.queryCoreByStep(collegeCurriculumCoreQuery);
  183. for (CollegeCurriculumCore collegeCurriculumCore : collegeCurriculumCores) {
  184. if (collegeCurriculumCore.getStatus() != null && collegeCurriculumCore.getStatus().equals("9")) { //1-已预约,2-学习中,3-已过期, 9-已完成
  185. collegeCurriculumCore.setStatus("已完成");
  186. } else if (collegeCurriculumCore.getStatus() != null && collegeCurriculumCore.getStatus().equals("2")) {
  187. collegeCurriculumCore.setStatus("待完成");
  188. } else {
  189. if (collegeCurriculumCore.getC_step() >= DateUtil.getWeekDiff(dateNow, getDate)) {
  190. collegeCurriculumCore.setStatus("待完成");
  191. } else {
  192. collegeCurriculumCore.setStatus("未完成");
  193. }
  194. }
  195. }
  196. tableData.put("college_core", collegeCurriculumCores);
  197. //实践课
  198. collegeCurriculumPracticeQuery.setDeadline(sdf.format(new Date()));
  199. collegeCurriculumPracticeQuery.setCity(city);
  200. collegeCurriculumPracticeQuery.setStartdate(sdf.format(new Date()));
  201. List<CollegeCurriculumPractice> collegeCurriculumPractices = collegeCurriculumPracticeService.queryPracticeByStep(collegeCurriculumPracticeQuery);
  202. //判断日期
  203. for (CollegeCurriculumPractice collegeCurriculumPractice : collegeCurriculumPractices) {
  204. Date deadLine = ldf.parse(collegeCurriculumPractice.getDeadline());
  205. Date now = new Date();
  206. if (collegeCurriculumPractice.getLearningStatus() != null && collegeCurriculumPractice.getLearningStatus().equals("9")) {
  207. //已完成
  208. collegeCurriculumPractice.setStatus("已完成");
  209. //已完成
  210. collegeCurriculumPractice.setLearningStatus("已完成");
  211. } else if (collegeCurriculumPractice.getLearningStatus() != null && collegeCurriculumPractice.getLearningStatus().equals("1")) {
  212. if (now.getTime() > deadLine.getTime()) {
  213. //未完成
  214. collegeCurriculumPractice.setStatus("未完成");
  215. //已过期
  216. collegeCurriculumPractice.setLearningStatus("已过期");
  217. } else {
  218. //待完成
  219. collegeCurriculumPractice.setStatus("已报名");
  220. //查看报名
  221. collegeCurriculumPractice.setLearningStatus("查看报名");
  222. }
  223. } else {
  224. if (now.getTime() > deadLine.getTime()) {
  225. //未完成
  226. collegeCurriculumPractice.setStatus("未完成");
  227. //已过期
  228. collegeCurriculumPractice.setLearningStatus("已过期");
  229. } else if (collegeCurriculumPractice.getPcount() >= 99) {
  230. //未完成
  231. collegeCurriculumPractice.setStatus("未完成");
  232. //已约满
  233. collegeCurriculumPractice.setLearningStatus("已约满");
  234. } else {
  235. //待完成
  236. collegeCurriculumPractice.setStatus("待完成");
  237. //立即报名
  238. collegeCurriculumPractice.setLearningStatus("立即报名");
  239. }
  240. }
  241. }
  242. tableData.put("college_practice", collegeCurriculumPractices);
  243. //体验课
  244. collegeCurriculumExperienceQuery.setCity(city);
  245. List<CollegeCurriculumExperience> collegeCurriculumExperiences = collegeCurriculumExperienceService.queryExperienceByStep(collegeCurriculumExperienceQuery);
  246. for (CollegeCurriculumExperience collegeCurriculumExperience : collegeCurriculumExperiences) {
  247. if (collegeCurriculumExperience.getStatus() != null && collegeCurriculumExperience.getStatus().equals("9")) { //1-已预约,2-学习中,3-已过期, 9-已完成
  248. collegeCurriculumExperience.setStatus("已完成");
  249. collegeCurriculumExperience.setLearningStatus("已完成");
  250. } else if (collegeCurriculumExperience.getStatus() != null && collegeCurriculumExperience.getStatus().equals("1")) {
  251. collegeCurriculumExperience.setStatus("已预约");
  252. collegeCurriculumExperience.setLearningStatus("查看预约");
  253. } else {
  254. collegeCurriculumExperience.setStatus("未完成");
  255. collegeCurriculumExperience.setLearningStatus("立即预约");
  256. }
  257. }
  258. tableData.put("college_experience", collegeCurriculumExperiences);
  259. return tableData;
  260. } catch (Exception ex) {
  261. ex.printStackTrace();
  262. return tableData;
  263. }
  264. } else {
  265. tableData.put("status", "5"); //没有设置日期
  266. return tableData;
  267. }
  268. } else {
  269. tableData.put("status", "6"); //没有授权手机号
  270. return tableData;
  271. }
  272. }
  273. public int getPointsByOffline(String openid, String c_code, String lat, String lon) {
  274. //51-参数错误 52-没有预约 53-不在范围 54-已完成
  275. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  276. if (c_code.indexOf("College") == -1) {
  277. return 51; //参数错误
  278. }
  279. CollegeBooking collegeBooking = new CollegeBooking();
  280. collegeBooking.setOpenid(openid);
  281. collegeBooking.setCollege_code(c_code.substring(8, c_code.length()));
  282. String lat_l, lon_l;
  283. int credit = 0;
  284. if (collegeBookingService.checkBooking(collegeBooking) != null) {
  285. if (c_code.substring(7, 1) == "P") {
  286. CollegeCurriculumPractice collegeCurriculumPractice = collegeCurriculumPracticeService.getPracticeByCode(c_code);
  287. lat_l = collegeCurriculumPractice.getMi_latitude();
  288. lon_l = collegeCurriculumPractice.getMi_longitude();
  289. credit = collegeCurriculumPractice.getCredit();
  290. } else if (c_code.substring(7, 1) == "E") {
  291. CollegeCurriculumExperience collegeCurriculumExperience = collegeCurriculumExperienceService.getExperienceByCode(c_code.substring(8, c_code.length()));
  292. lat_l = collegeCurriculumExperience.getLatitude();
  293. lon_l = collegeCurriculumExperience.getLongitude();
  294. credit = collegeCurriculumExperience.getCredit();
  295. } else {
  296. return 51; //参数错误
  297. }
  298. DistanceUtil distanceUtil = new DistanceUtil();
  299. double distance = distanceUtil.GetDistance(Double.parseDouble(lat_l), Double.parseDouble(lon_l), Double.parseDouble(lat), Double.parseDouble(lon));
  300. if (distance > 500) {
  301. return 53; //不在范围内
  302. } else {
  303. //判断是否已完成
  304. CollegeLearningDetail collegeLearningDetail = new CollegeLearningDetail();
  305. collegeLearningDetail.setOpen_id(openid);
  306. collegeLearningDetail.setC_code(c_code.substring(8, c_code.length()));
  307. CollegeLearningDetail collegeLearningDetailC = collegeLearningDetailService.checkCorePositive(collegeLearningDetail);
  308. if (collegeLearningDetailC.getStatus() != 9) {
  309. collegeLearningDetailC.setDate(sdf.format(new Date()));
  310. collegeLearningDetailC.setStatus(9);
  311. collegeLearningDetailC.setCredit(credit);
  312. collegeLearningDetailService.addCollegeLearningDetail(collegeLearningDetailC);
  313. } else {
  314. return 54; //已完成
  315. }
  316. //判断是否已经学习 增加学分
  317. CollegeUserPoints collegeUserPoints = collegeUserPointsDao.queryOneUserPoints(openid);
  318. if (collegeUserPoints == null) {
  319. CollegeUserPoints collegeUserPointsNew = new CollegeUserPoints();
  320. collegeUserPointsNew.setOpenid(openid);
  321. collegeUserPointsNew.setCredit_total(credit);
  322. collegeUserPointsNew.setCredit_available(credit);
  323. } else {
  324. CollegeUserPoints collegeUserPointsNew = new CollegeUserPoints();
  325. collegeUserPointsNew.setOpenid(openid);
  326. collegeUserPointsNew.setCredit_total(collegeUserPoints.getCredit_total() + credit);
  327. collegeUserPointsNew.setCredit_available(collegeUserPoints.getCredit_available() + credit);
  328. }
  329. return 1;
  330. }
  331. } else {
  332. return 52; //没有预约
  333. }
  334. }
  335. }