123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- package service
-
- import (
- "XT_New/models"
- "XT_New/utils"
- "fmt"
- "github.com/shopspring/decimal"
- "math/rand"
- "strconv"
- "time"
- )
-
- //生成编号?+yymmdd+随机数(4位)
- //新增参数为CP
- //退款参数为AF
- func CreateCPCode(str string) string {
- s := fmt.Sprintf("%04v", rand.New(rand.NewSource(time.Now().UnixNano())).Int63n(10000))
- t := time.Now().Format("20060102")
- code := str + t + s
- return code
- }
-
- //获取his中的有效患者
- func GetHisUser(orgid int64) (hisname []models.GetHisName, err error) {
- err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ?", orgid).Find(&hisname).Error
- return
- }
-
- //获取his中的有效患者
- func GetHisUserName(orgid, id int64) (hisname models.GetHisName, err error) {
- err = XTReadDB().Model(&models.GetHisName{}).Where("status = 1 and user_org_id = ? and id = ?", orgid, id).Find(&hisname).Error
- return
- }
-
- //添加押金(开始)调用接口前记得判断审核状态,确认通过审核后在调用
- func UpDeposit(code, remarks string, his_patient_id, orgid, trial_status, createid int64, deposit decimal.Decimal) (err error) {
- //查押金表是否存在记录
- tmp, tmpdeposit := IsHisPatientId(his_patient_id, orgid)
- if tmp == 0 {
- //需要生成一条数据
- de := models.Deposit{
- UserOrgId: orgid,
- HisPatientId: his_patient_id,
- Ctime: time.Now().Unix(),
- Status: 1,
- }
- i, d, err := AddDeposit(de)
- if err != nil || i == 0 {
- utils.ErrorLog("添加用户押金记录失败: %v", err.Error())
- return err
- }
- tmp = i
- tmpdeposit = d
- }
- if trial_status == 1 {
- de := models.Deposit{
- ID: tmp,
- Mtime: time.Now().Unix(),
- Deposit: deposit.Add(tmpdeposit),
- }
- err = UpdateDeposit(de)
- if err != nil {
- utils.ErrorLog("添加用户押金失败: %v", err.Error())
- return
- }
- }
- dehistory := models.DepositHistory{
- UserOrgId: orgid,
- HisPatientId: his_patient_id,
- DepositCode: code,
- Deposit: deposit, //本次操作的押金
- SurplusDeposit: tmpdeposit.Add(deposit), //剩余金额
- DepositStatus: 1,
- Status: 1,
- CreateId: createid,
- Ctime: time.Now().Unix(),
- Mtime: time.Now().Unix(),
- TrialStatus: trial_status,
- Remarks: remarks,
- }
- err = AddDepositHistory(dehistory)
- if err != nil {
- utils.ErrorLog("添加用户押金历史记录失败: %v", err.Error())
- }
- return
- }
-
- //添加押金记录
- func AddDepositHistory(dh models.DepositHistory) error {
- err := XTWriteDB().Create(&dh).Error
- return err
- }
-
- //押金中添加一条记录,并返回该数据的id和押金余额(当没有该用户记录时调用)
- func AddDeposit(de models.Deposit) (int64, decimal.Decimal, error) {
- var tmp models.Deposit
- err := XTWriteDB().Create(&de).Find(&tmp).Error
- if err != nil {
- return 0, decimal.NewFromFloat(0.00), err
- }
- return tmp.ID, tmp.Deposit, err
- }
-
- //更改押金记录
- func UpdateDeposit(de models.Deposit) (err error) {
- err = XTWriteDB().Model(&models.Deposit{}).Where("id = ? and status = 1", de.ID).Updates(map[string]interface{}{
- "mtime": de.Mtime,
- "deposit": de.Deposit,
- }).Error
- return
- }
-
- //判断押金表是否存在当前的患者的一条有效数据,如果有返回id和押金余额,没有返回0
- func IsHisPatientId(his_patient_id, orgid int64) (int64, decimal.Decimal) {
- var tmp []models.Deposit
- XTReadDB().Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1", orgid, his_patient_id).Find(&tmp)
- if len(tmp) != 1 {
- return 0, decimal.NewFromFloat(0.00)
- }
- return tmp[0].ID, tmp[0].Deposit
- }
-
- //查询押金编号是否重复
- func FindDecimalCode(orgid int64, code string) bool {
- var total int
- XTReadDB().Model(&models.DepositHistory{}).Where("user_org_id = ? and deposit_code = ? and status = 1", orgid, code).Count(&total)
- if total > 0 {
- return true
- } else {
- return false
- }
- }
-
- //充值明细列表
- func DetailsList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistoryname, err error) {
- db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and user_org_id = ? and deposit_status = 1 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
- if len(keyword) > 0 {
- var tmp string = "deposit_code like ?"
- if len(slicekey) > 0 {
- for i := 0; i < len(slicekey); i++ {
- tmp = tmp + " or his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
- }
- }
- keyword = "%" + keyword + "%"
- db = db.Where(tmp, keyword)
- }
- err = db.Order("ctime desc").Find(&deposithistory).Error
- return
- }
-
- //充值汇总列表
- func SummaryList(orgid, stime, etime int64, keyword string, slicekey []int64) (deposithistory []models.DepositHistory, err error) {
- db := XTReadDB().Model(&models.DepositHistory{}).Where("status = 1 and trial_status = 1 and user_org_id = ? and deposit_status = 1 ", orgid).Where("ctime >= ? and ctime <= ?", stime, etime)
- if len(slicekey) > 0 {
- tmp := ""
- for i := 0; i < len(slicekey); i++ {
- tmp = tmp + " his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
- if i < len(slicekey)-1 {
- tmp = tmp + " or "
- }
- }
- db = db.Where(tmp)
- } else {
- if len(keyword) > 0 {
- return
- }
- }
- err = db.Order("ctime desc").Find(&deposithistory).Error
- return
- }
-
- //获取本周周一和周日的起止时间戳
- func GetMondayOfWeek() (int64, int64) {
- t := time.Now()
- dayObj := GetZeroTime(t)
- var dayStr string
- loc, _ := time.LoadLocation("Local")
- if t.Weekday() == time.Monday {
- dayStr = dayObj.Format("2006-01-02")
- } else {
- offset := int(time.Monday - t.Weekday())
- if offset > 0 {
- offset = -6
- }
- dayStr = dayObj.AddDate(0, 0, offset).Format("2006-01-02")
- }
- dayStr = dayStr + " 00:00:00"
- stime, _ := time.ParseInLocation("2006-01-02 15:04:05", dayStr, loc)
- return stime.Unix(), stime.Unix() + 604799
- }
-
- //获取本月的起止时间戳
- func GetMonth() (int64, int64) {
- timeNow := time.Now()
- timeToday := time.Date(timeNow.Year(), timeNow.Month(), timeNow.Day(), 0, 0, 0, 0, timeNow.Location()) // 获取当天0点时间 time类型
- timeMonthStartUnix1 := timeToday.AddDate(0, 0, -timeToday.Day()+1).Unix() // 获取本月第一天0点 时间戳类型
- timeMonthEndUnix1 := timeToday.AddDate(0, 1, -timeToday.Day()+1).Unix() - 1 // 获取下个月第一天/ 本月最后一天24点 时间戳类型
- return timeMonthStartUnix1, timeMonthEndUnix1
- }
- func GetCreateidName(id int64) string {
- var tmp models.CreateUser
- XTReadDB().Select("name").Where("id = ?", id).Find(&tmp)
- return tmp.Name
- }
-
- //审核通过
- func UpDecimalHistory(id int64) (err error) {
- //开事务
- tx := XTWriteDB().Begin()
- defer func() {
- if err != nil {
- utils.ErrorLog("事务失败,原因为", err)
- tx.Rollback()
- } else {
- tx.Commit()
- }
- }()
- //改状态
- err = tx.Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
- "trial_status": 1,
- "mtime": time.Now().Unix(),
- }).Error
- if err != nil {
- return
- }
- //查记录x2
- var history models.DepositHistory
- var detmp models.Deposit
- err = tx.Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
- if err != nil {
- return
- }
- err = tx.Model(&models.Deposit{}).Where("user_org_id = ? and his_patient_id = ? and status = 1 ", history.UserOrgId, history.HisPatientId).Find(&detmp).Error
- if err != nil {
- return
- }
- //相加
- err = tx.Model(&models.Deposit{}).Where("id = ? and status = 1", detmp.ID).Updates(map[string]interface{}{
- "mtime": time.Now().Unix(),
- "deposit": detmp.Deposit.Add(history.Deposit),
- }).Error
-
- return
- }
-
- //删除本次记录
- func DelDecimalHistory(id int64) (err error) {
- err = XTWriteDB().Model(models.DepositHistory{}).Where("id = ?", id).Updates(map[string]interface{}{
- "status": 0,
- "mtime": time.Now().Unix(),
- }).Error
- return
- }
-
- //根据id获取一条押金历史记录
- func GetDecimalHistoryOne(id int64) (history models.DepositHistory, err error) {
- err = XTReadDB().Model(&models.DepositHistory{}).Where("id = ?", id).Find(&history).Error
- return
- }
-
- //根据患者id获取该患者当前剩余的押金
- func GetUserMoney(id, orgid int64) decimal.Decimal {
- tmp := models.Deposit{}
- XTReadDB().Model(&models.Deposit{}).Where("his_patient_id = ? and user_org_id = ? and status = 1", id, orgid).Find(&tmp)
- return tmp.Deposit
- }
-
- //查询时间段内患者的余额
- func GetMoneyforTime(id, orgid, etime int64) decimal.Decimal {
- tmp := models.DepositHistory{}
- XTReadDB().Model(&models.DepositHistory{}).Where("his_patient_id = ? and user_org_id = ? and status = 1 and mtime <= ?", id, orgid, etime).Order("mtime").Find(&tmp)
- return tmp.SurplusDeposit
- }
-
- //押金流水
- func GetFlowList(id, orgid, stime, etime, deposit_status int64) (deposit []models.DepositHistory, err error) {
- s := "status = 1 and trial_status = 1 and user_org_id = ? and his_patient_id = ? and mtime >= ? and mtime <= ?"
- if deposit_status != 0 {
- s = s + " and deposit_status = " + string(deposit_status)
- }
- err = XTReadDB().Model(&models.DepositHistory{}).Where(s, orgid, id, stime, etime).Order("mtime desc").Find(&deposit).Error
- return
- }
-
- func GetUserList(page, limit, orgid int64, keyword string, slicekey []int64) (m []models.Deposit1, total int64, err error) {
- db := XTReadDB().Model(&models.Deposit{}).Where("status = 1 and user_org_id = ? ", orgid)
- offset := (page - 1) * limit
- if len(slicekey) > 0 {
- tmp := ""
- for i := 0; i < len(slicekey); i++ {
- tmp = tmp + " his_patient_id = " + strconv.FormatInt(slicekey[i], 10)
- if i < len(slicekey)-1 {
- tmp = tmp + " or "
- }
- }
- db = db.Where(tmp)
- } else {
- if len(keyword) > 0 {
- return
- }
- }
- err = db.Count(&total).Offset(offset).Order("mtime desc").Find(&m).Error
- return
- }
|