bodycheck.go 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package bodycheck
  2. import (
  3. "errors"
  4. "spaceofcheng/services/models/bodycheck"
  5. "spaceofcheng/services/models/customer"
  6. "spaceofcheng/services/models/model"
  7. "spaceofcheng/services/utils"
  8. "strconv"
  9. "time"
  10. "github.com/astaxie/beego"
  11. "github.com/astaxie/beego/config"
  12. "github.com/zjxpcyc/wechat/wx"
  13. )
  14. // BodyCheckServ 系统处理
  15. type BodyCheckServ struct {
  16. ctx *utils.Context
  17. dao *bodycheck.DAO
  18. customerdao *customer.CustomerDAO
  19. }
  20. // NewBodyCheckServ 初始化
  21. func NewBodyCheckServ(ctx *utils.Context) *BodyCheckServ {
  22. return &BodyCheckServ{
  23. ctx: ctx,
  24. dao: bodycheck.NewDAO(ctx),
  25. customerdao: customer.NewCustomerDAO(ctx),
  26. }
  27. }
  28. // GetCheckByUser 根据用户获取体检报告
  29. func (s *BodyCheckServ) GetCheckByUser() (map[string]interface{}, error) {
  30. customer := s.ctx.Get("customer").(model.TaCustomer)
  31. bodyCheck, err := s.dao.GetBodyCheckByUser(customer.CustomerId)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if len(bodyCheck) == 0 {
  36. return nil, errors.New("log-error-没有查询到数据")
  37. }
  38. presentations, err := s.dao.GetPresentationByCheckID(bodyCheck[0].Id)
  39. if err != nil {
  40. return nil, err
  41. }
  42. return map[string]interface{}{
  43. "Message": presentations,
  44. "Info": bodyCheck,
  45. "UserInfo": customer,
  46. }, nil
  47. }
  48. // PostCheckResult 测量结果返回
  49. func (s *BodyCheckServ) PostCheckResult(formVal map[string]interface{}) error {
  50. type CheckResult struct {
  51. WechatAccount string
  52. EquipmentId string
  53. CheckType int
  54. CheckDate time.Time
  55. CheckResult string
  56. ReportUrl string
  57. HMSReportUrl string
  58. }
  59. var result = CheckResult{}
  60. result.EquipmentId = formVal["EquipmentId"].(string)
  61. result.WechatAccount = formVal["WechatAccount"].(string)
  62. // result.CheckResult = formVal["CheckResult"].(string)
  63. result.CheckResult = ""
  64. result.ReportUrl = formVal["ReportUrl"].(string)
  65. result.HMSReportUrl = formVal["HMSReportUrl"].(string)
  66. result.CheckType = int(formVal["CheckType"].(float64))
  67. var err error
  68. loc, _ := time.LoadLocation("Local")
  69. result.CheckDate, err = time.ParseInLocation("2006-01-02 15:04:05", formVal["CheckDate"].(string), loc)
  70. if err != nil {
  71. utils.LogError("参数错误:", err)
  72. return errors.New("参数错误")
  73. }
  74. openid := result.WechatAccount
  75. customer, err := s.customerdao.GetCustWithWXByOpenID(openid)
  76. if err != nil {
  77. utils.LogError("获取用户信息失败:", err)
  78. return errors.New("获取用户信息失败")
  79. }
  80. if customer == nil || customer.CustomerId == "" {
  81. return errors.New("没有当前用户信息!")
  82. }
  83. userid := customer.CustomerId
  84. checkinfo, err := s.dao.GetCheckByUserAndEquipmentID(userid, result.EquipmentId)
  85. if err != nil {
  86. utils.LogError("获取体检信息失败:", err)
  87. return errors.New("获取体检信息失败")
  88. }
  89. if checkinfo == nil || checkinfo.Id == "" || time.Now().Local().Format("2006-01-02") != checkinfo.CreateDate.Format("2006-01-02") {
  90. caseEquipment, err := s.dao.GetCaseEquipment(result.EquipmentId)
  91. if err != nil {
  92. utils.LogError("获取设备信息失败:", err)
  93. return errors.New("获取设备信息失败")
  94. }
  95. if caseEquipment == nil || caseEquipment.EquipmentId == "" {
  96. return errors.New("设备未维护!")
  97. }
  98. var checkNew = model.TaBodyCheck{}
  99. checkNew.CaseId = caseEquipment.CaseId
  100. checkNew.EquipmentId = result.EquipmentId
  101. checkNew.UserId = userid
  102. checkNew.ReportUrl = result.HMSReportUrl
  103. checkNew.CreateDate = result.CheckDate
  104. checkinfo, err = s.dao.SaveBodyCheckInfo(checkNew)
  105. wxconf, _ := config.NewConfig("ini", "conf/wechat.conf")
  106. messageTplID := wxconf.String("messageTplID")
  107. org := s.ctx.Get("org").(model.SysOrg)
  108. utils.WxClientFor(org.OrgId).SendTplMessage(result.WechatAccount, messageTplID, beego.AppConfig.String("resultURL"), map[string]wx.TplMessageData{
  109. "first": wx.TplMessageData{
  110. Value: "您的体检报告已生成",
  111. },
  112. "keyword1": wx.TplMessageData{
  113. Value: checkinfo.Id,
  114. },
  115. "keyword2": wx.TplMessageData{
  116. Value: result.CheckDate.Format("2006-01-02 15:04"),
  117. },
  118. "remark": wx.TplMessageData{
  119. Value: "",
  120. },
  121. })
  122. }
  123. presentation, err := s.dao.GetPresentation(checkinfo.Id, result.CheckType)
  124. if err != nil {
  125. utils.LogError("获取报告明细失败:", err)
  126. return errors.New("获取报告明细失败")
  127. }
  128. if presentation == nil || presentation.Id == "" {
  129. // 新增
  130. var preNew = model.TaPresentation{}
  131. preNew.CheckId = checkinfo.Id
  132. preNew.CheckDate = result.CheckDate
  133. preNew.CheckResult = result.CheckResult
  134. preNew.CheckType = result.CheckType
  135. preNew.ReportUrl = result.ReportUrl
  136. presentation, err = s.dao.SavePresentation(preNew)
  137. if err != nil {
  138. utils.LogError("保存报告明细失败:", err)
  139. return errors.New("保存报告明细失败")
  140. }
  141. } else {
  142. // 修改
  143. presentation.CheckDate = result.CheckDate
  144. presentation.CheckResult = result.CheckResult
  145. presentation.ReportUrl = result.ReportUrl
  146. err = s.dao.UpdatePresentation(presentation)
  147. if err != nil {
  148. utils.LogError("修改报告明细失败:", err)
  149. return errors.New("修改报告明细失败")
  150. }
  151. // 删除之前的记录
  152. err = s.dao.DeletePresentionDetail(presentation.Id)
  153. if err != nil {
  154. utils.LogError("删除报告明细失败:", err)
  155. return errors.New("删除报告明细失败")
  156. }
  157. }
  158. specs, err := s.dao.GetCheckSpecs()
  159. if err != nil {
  160. utils.LogError("获取spec信息失败:", err)
  161. return errors.New("获取spec信息失败")
  162. }
  163. if len(formVal) > 0 {
  164. var details []model.TaPresentationDetail
  165. for k, v := range formVal {
  166. if k != "WechatAccount" && k != "EquipmentId" && k != "CheckType" && k != "CheckDate" && k != "CheckResult" && k != "ReportUrl" && k != "HMSReportUrl" && k != "WXID" {
  167. var detail model.TaPresentationDetail
  168. detail.CheckName = k
  169. switch vx := v.(type) {
  170. case string:
  171. detail.CheckVal = vx
  172. case float64:
  173. detail.CheckVal = strconv.FormatFloat(vx, 'f', 2, 64)
  174. }
  175. detail.PresentationId = presentation.Id
  176. var specName = ""
  177. for _, spec := range specs {
  178. if spec.SortName == k {
  179. specName = spec.Name
  180. break
  181. }
  182. }
  183. detail.SpecName = specName
  184. detail.Id = utils.GetGUID()
  185. details = append(details, detail)
  186. }
  187. }
  188. if len(details) > 0 {
  189. err := s.dao.SavePresentationDetail(details)
  190. if err != nil {
  191. utils.LogError("保存体检明细失败:", err)
  192. return errors.New("保存体检明细失败")
  193. }
  194. }
  195. }
  196. return nil
  197. }