package bodycheck import ( "errors" "spaceofcheng/services/models/bodycheck" "spaceofcheng/services/models/customer" "spaceofcheng/services/models/model" "spaceofcheng/services/utils" "strconv" "time" "github.com/astaxie/beego" "github.com/astaxie/beego/config" "github.com/zjxpcyc/wechat/wx" ) // BodyCheckServ 系统处理 type BodyCheckServ struct { ctx *utils.Context dao *bodycheck.DAO customerdao *customer.CustomerDAO } // NewBodyCheckServ 初始化 func NewBodyCheckServ(ctx *utils.Context) *BodyCheckServ { return &BodyCheckServ{ ctx: ctx, dao: bodycheck.NewDAO(ctx), customerdao: customer.NewCustomerDAO(ctx), } } // GetCheckByUser 根据用户获取体检报告 func (s *BodyCheckServ) GetCheckByUser() (map[string]interface{}, error) { customer := s.ctx.Get("customer").(model.TaCustomer) bodyCheck, err := s.dao.GetBodyCheckByUser(customer.CustomerId) if err != nil { return nil, err } if len(bodyCheck) == 0 { return nil, errors.New("log-error-没有查询到数据") } presentations, err := s.dao.GetPresentationByCheckID(bodyCheck[0].Id) if err != nil { return nil, err } return map[string]interface{}{ "Message": presentations, "Info": bodyCheck, "UserInfo": customer, }, nil } // PostCheckResult 测量结果返回 func (s *BodyCheckServ) PostCheckResult(formVal map[string]interface{}) error { type CheckResult struct { WechatAccount string EquipmentId string CheckType int CheckDate time.Time CheckResult string ReportUrl string HMSReportUrl string } var result = CheckResult{} result.EquipmentId = formVal["EquipmentId"].(string) result.WechatAccount = formVal["WechatAccount"].(string) // result.CheckResult = formVal["CheckResult"].(string) result.CheckResult = "" result.ReportUrl = formVal["ReportUrl"].(string) result.HMSReportUrl = formVal["HMSReportUrl"].(string) result.CheckType = int(formVal["CheckType"].(float64)) var err error loc, _ := time.LoadLocation("Local") result.CheckDate, err = time.ParseInLocation("2006-01-02 15:04:05", formVal["CheckDate"].(string), loc) if err != nil { utils.LogError("参数错误:", err) return errors.New("参数错误") } openid := result.WechatAccount customer, err := s.customerdao.GetCustWithWXByOpenID(openid) if err != nil { utils.LogError("获取用户信息失败:", err) return errors.New("获取用户信息失败") } if customer == nil || customer.CustomerId == "" { return errors.New("没有当前用户信息!") } userid := customer.CustomerId checkinfo, err := s.dao.GetCheckByUserAndEquipmentID(userid, result.EquipmentId) if err != nil { utils.LogError("获取体检信息失败:", err) return errors.New("获取体检信息失败") } if checkinfo == nil || checkinfo.Id == "" || time.Now().Local().Format("2006-01-02") != checkinfo.CreateDate.Format("2006-01-02") { caseEquipment, err := s.dao.GetCaseEquipment(result.EquipmentId) if err != nil { utils.LogError("获取设备信息失败:", err) return errors.New("获取设备信息失败") } if caseEquipment == nil || caseEquipment.EquipmentId == "" { return errors.New("设备未维护!") } var checkNew = model.TaBodyCheck{} checkNew.CaseId = caseEquipment.CaseId checkNew.EquipmentId = result.EquipmentId checkNew.UserId = userid checkNew.ReportUrl = result.HMSReportUrl checkNew.CreateDate = result.CheckDate checkinfo, err = s.dao.SaveBodyCheckInfo(checkNew) wxconf, _ := config.NewConfig("ini", "conf/wechat.conf") messageTplID := wxconf.String("messageTplID") org := s.ctx.Get("org").(model.SysOrg) utils.WxClientFor(org.OrgId).SendTplMessage(result.WechatAccount, messageTplID, beego.AppConfig.String("resultURL"), map[string]wx.TplMessageData{ "first": wx.TplMessageData{ Value: "您的体检报告已生成", }, "keyword1": wx.TplMessageData{ Value: checkinfo.Id, }, "keyword2": wx.TplMessageData{ Value: result.CheckDate.Format("2006-01-02 15:04"), }, "remark": wx.TplMessageData{ Value: "", }, }) } presentation, err := s.dao.GetPresentation(checkinfo.Id, result.CheckType) if err != nil { utils.LogError("获取报告明细失败:", err) return errors.New("获取报告明细失败") } if presentation == nil || presentation.Id == "" { // 新增 var preNew = model.TaPresentation{} preNew.CheckId = checkinfo.Id preNew.CheckDate = result.CheckDate preNew.CheckResult = result.CheckResult preNew.CheckType = result.CheckType preNew.ReportUrl = result.ReportUrl presentation, err = s.dao.SavePresentation(preNew) if err != nil { utils.LogError("保存报告明细失败:", err) return errors.New("保存报告明细失败") } } else { // 修改 presentation.CheckDate = result.CheckDate presentation.CheckResult = result.CheckResult presentation.ReportUrl = result.ReportUrl err = s.dao.UpdatePresentation(presentation) if err != nil { utils.LogError("修改报告明细失败:", err) return errors.New("修改报告明细失败") } // 删除之前的记录 err = s.dao.DeletePresentionDetail(presentation.Id) if err != nil { utils.LogError("删除报告明细失败:", err) return errors.New("删除报告明细失败") } } specs, err := s.dao.GetCheckSpecs() if err != nil { utils.LogError("获取spec信息失败:", err) return errors.New("获取spec信息失败") } if len(formVal) > 0 { var details []model.TaPresentationDetail for k, v := range formVal { if k != "WechatAccount" && k != "EquipmentId" && k != "CheckType" && k != "CheckDate" && k != "CheckResult" && k != "ReportUrl" && k != "HMSReportUrl" && k != "WXID" { var detail model.TaPresentationDetail detail.CheckName = k switch vx := v.(type) { case string: detail.CheckVal = vx case float64: detail.CheckVal = strconv.FormatFloat(vx, 'f', 2, 64) } detail.PresentationId = presentation.Id var specName = "" for _, spec := range specs { if spec.SortName == k { specName = spec.Name break } } detail.SpecName = specName detail.Id = utils.GetGUID() details = append(details, detail) } } if len(details) > 0 { err := s.dao.SavePresentationDetail(details) if err != nil { utils.LogError("保存体检明细失败:", err) return errors.New("保存体检明细失败") } } } return nil }