ObtainOwnerAge.java 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. package com.java110.user.cmd.owner;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.java110.core.annotation.Java110Cmd;
  4. import com.java110.core.context.ICmdDataFlowContext;
  5. import com.java110.core.event.cmd.Cmd;
  6. import com.java110.core.event.cmd.CmdEvent;
  7. import com.java110.dto.owner.OwnerDto;
  8. import com.java110.utils.exception.CmdException;
  9. import com.java110.utils.util.Assert;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12. import org.springframework.http.HttpStatus;
  13. import org.springframework.http.ResponseEntity;
  14. import java.text.ParseException;
  15. import java.text.SimpleDateFormat;
  16. import java.util.Calendar;
  17. import java.util.Date;
  18. @Java110Cmd(serviceCode = "owner.obtainAge")
  19. public class ObtainOwnerAge extends Cmd {
  20. private Logger logger = LoggerFactory.getLogger(getClass());
  21. @Override
  22. public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
  23. Assert.jsonObjectHaveKey(reqJson, "idCard", "请求报文中未包含身份证号码");
  24. Assert.jsonObjectHaveKey(reqJson, "communityId", "请求报文中未包含communityId");
  25. //属性校验
  26. Assert.judgeAttrValue(reqJson);
  27. }
  28. @Override
  29. public void doCmd(CmdEvent event, ICmdDataFlowContext cmdDataFlowContext, JSONObject reqJson) throws CmdException, ParseException {
  30. //获取身份证号码
  31. String idCard = reqJson.getString("idCard");
  32. String year = null;
  33. String month = null;
  34. String day = null;
  35. Date birthDay = null;
  36. String msg = null;
  37. OwnerDto ownerDto = new OwnerDto();
  38. //正则匹配身份证号是否是正确的,15位或者17位数字+数字/x/X
  39. if (idCard.matches("^\\d{15}|\\d{17}[\\dxX]$")) {
  40. SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
  41. year = idCard.substring(6, 10);
  42. month = idCard.substring(10, 12);
  43. day = idCard.substring(12, 14);
  44. birthDay = simpleDateFormat.parse(year + "-" + month + "-" + day);
  45. Calendar cal = Calendar.getInstance();
  46. if (cal.before(birthDay)) { //出生日期晚于当前时间,无法计算
  47. msg = "业主出生日期晚于当前时间";
  48. ownerDto.setMsg(msg);
  49. ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(ownerDto), HttpStatus.OK);
  50. cmdDataFlowContext.setResponseEntity(responseEntity);
  51. }
  52. int yearNow = cal.get(Calendar.YEAR); //当前年份
  53. int monthNow = cal.get(Calendar.MONTH) + 1; //当前月份
  54. int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH); //当前日期
  55. int age = yearNow - Integer.parseInt(year); //计算整岁
  56. if (monthNow <= Integer.parseInt(month)) {
  57. if (monthNow == Integer.parseInt(month)) {
  58. if (dayOfMonthNow < Integer.parseInt(day)) { //当前日期在生日之前,年龄减一岁
  59. age--;
  60. }
  61. } else { //当前月份在生日之前,年龄减一岁
  62. age--;
  63. }
  64. }
  65. ownerDto.setAge(String.valueOf(age));
  66. ResponseEntity<String> responseEntity = new ResponseEntity<String>(JSONObject.toJSONString(ownerDto), HttpStatus.OK);
  67. cmdDataFlowContext.setResponseEntity(responseEntity);
  68. }
  69. }
  70. }