123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- package service
-
- import (
- "Xcx_New/models"
- "crypto/aes"
- "crypto/cipher"
- "encoding/base64"
- "encoding/json"
- "errors"
- "fmt"
- "github.com/jinzhu/gorm"
- "time"
- )
-
- func GetXcxMobileInformation(mobile string) (*models.XcxAdminUserRole, error) {
- role := models.XcxAdminUserRole{}
-
- err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
-
- if err != nil {
- return nil, err
- }
- return &role, nil
- }
-
- func CreateXcxAdminUser(role models.XcxAdminUserRole) error {
-
- err := UserReadDB().Create(&role).Error
- return err
- }
-
- func GetLoginInfor(mobile string) (models.XcxAdminUserRole, error) {
- role := models.XcxAdminUserRole{}
- err := UserReadDB().Model(&role).Where("mobile = ? and status = 1", mobile).Find(&role).Error
- return role, err
- }
-
- func GetMobilePatientInfo(mobile string) (models.XcxPatients, error) {
-
- patients := models.XcxPatients{}
- err := XTReadDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and status = 1", mobile, mobile).Find(&patients).Error
- return patients, err
- }
-
- func GetPatientListByPatientId(id int64) (models.XcxPatients, error) {
-
- patients := models.XcxPatients{}
- err := XTReadDB().Model(&patients).Where("id = ? and status = 1", id).Find(&patients).Error
- return patients, err
- }
-
- func DecryptData(app_id, session_key, iv, encrypted_data string) (map[string]interface{}, error) {
-
- aesKey, err := base64.StdEncoding.DecodeString(session_key)
- if err != nil {
- return nil, err
- }
-
- ivKey, err := base64.StdEncoding.DecodeString(iv)
- if err != nil {
- return nil, err
- }
-
- decodeData, err := base64.StdEncoding.DecodeString(encrypted_data)
- if err != nil {
- return nil, err
- }
-
- dataBytes, err := AesDecrypt(decodeData, aesKey, ivKey)
-
- if err != nil {
- return nil, err
- }
-
- var result map[string]interface{}
- err = json.Unmarshal(dataBytes, &result)
-
- watermark := result["watermark"].(map[string]interface{})
- if watermark["appid"] != app_id {
- return nil, errors.New("Invalid appid data!")
- }
-
- return result, err
-
- }
-
- func AesDecrypt(crypted, key, iv []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- fmt.Println("w2222", err)
- if err != nil {
- return nil, err
- }
-
- blockMode := cipher.NewCBCDecrypter(block, iv)
- origData := make([]byte, len(crypted))
- blockMode.CryptBlocks(origData, crypted)
-
- // 去除填充
- length := len(origData)
- unp := int(origData[length-1])
- return origData[:(length - unp)], nil
-
- }
-
- func GetFirstDateOfWeek() (weekMonday string) {
- now := time.Now()
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
- weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- weekMonday = weekStartDate.Format("2006-01-02")
- return
- }
-
- func GetWeekDayOfWeek() (weekMonday string) {
- now := time.Now()
- offset := int(time.Monday - now.Weekday())
- if offset > 0 {
- offset = -6
- }
- weekStartDate := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local).AddDate(0, 0, offset)
- weekMonday = weekStartDate.Format("2006-01-02")
- return
- }
-
- func GetMobilePatient(mobile string) (*models.XcxPatients, error) {
-
- patient := models.XcxPatients{}
-
- err := XTReadDB().Model(&patient).Where("id_card_no = ? and status = 1", mobile).Find(&patient).Error
- if err == gorm.ErrRecordNotFound {
- return nil, err
- }
-
- if err != nil {
- return nil, err
- }
- return &patient, nil
-
- }
-
- func Dncrypt(rawData, key, iv string) (string, error) {
- data, err := base64.StdEncoding.DecodeString(rawData)
- key_b, err_1 := base64.StdEncoding.DecodeString(key)
- iv_b, _ := base64.StdEncoding.DecodeString(iv)
- if err != nil {
- return "", err
- }
- if err_1 != nil {
- return "", err_1
- }
- dnData, err := AesCBCDncrypt(data, key_b, iv_b)
- if err != nil {
- return "", err
- }
- return string(dnData), nil
- }
-
- // 解密
- func AesCBCDncrypt(encryptData, key, iv []byte) ([]byte, error) {
- block, err := aes.NewCipher(key)
- if err != nil {
- panic(err)
- }
- blockSize := block.BlockSize()
- if len(encryptData) < blockSize {
- panic("ciphertext too short")
- }
- if len(encryptData)%blockSize != 0 {
- panic("ciphertext is not a multiple of the block size")
- }
- mode := cipher.NewCBCDecrypter(block, iv)
- mode.CryptBlocks(encryptData, encryptData)
- // 解填充
- encryptData = PKCS7UnPadding(encryptData)
- return encryptData, nil
- }
-
- //去除填充
- func PKCS7UnPadding(origData []byte) []byte {
- length := len(origData)
- unpadding := int(origData[length-1])
- if length-unpadding < 0 {
- return origData[:0]
- }
- fmt.Println("changdu", length)
- fmt.Println("data", unpadding)
- return origData[:(length - unpadding)]
- }
-
- func GetScheduleInfo(startime int64, endtime int64, patient_id int64) (schedule []*models.VmBloodSchedule, err error) {
-
- db := XTReadDB().Table("xt_schedule as x").Where("x.status = 1")
- table := XTReadDB().Table("xt_patients as t").Where("t.status = 1")
- fmt.Println(table)
-
- if startime > 0 {
- db = db.Where("x.schedule_date >=?", startime)
- }
- if endtime > 0 {
- db = db.Where("x.schedule_date <=?", endtime)
- }
- if patient_id > 0 {
- db = db.Where("x.patient_id = ?", patient_id)
- }
-
- err = db.Select("x.schedule_date,x.schedule_type,x.schedule_week,x.mode_id,t.name").Joins("left join xt_patients as t on t.id = x.patient_id").Scan(&schedule).Error
- return schedule, err
- }
|