package service import ( "XT_New/models" "github.com/jinzhu/gorm" ) func GetGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64) (*models.XtGoodStockCount, error) { goodStock := models.XtGoodStockCount{} var err error err = XTReadDB().Where("user_org_id = ? and storehouse_id = ? and good_id = ?", user_org_id, storehouse_id, good_id).Find(&goodStock).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &goodStock, nil } func GetNewGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, tx *gorm.DB) (*models.XtGoodStockCount, error) { goodStock := models.XtGoodStockCount{} var err error err = tx.Where("user_org_id = ? and storehouse_id = ? and good_id = ?", user_org_id, storehouse_id, good_id).Find(&goodStock).Error if err == gorm.ErrRecordNotFound { if err != nil { tx.Rollback() return nil, err } } if err != nil { return nil, err } return &goodStock, nil } func CreateGoodCount(good models.XtGoodStockCount) error { db := writeDb.Begin() err := db.Create(&good).Error if err != nil { db.Rollback() return err } db.Commit() return err } func CreateNewGoodCount(good models.XtGoodStockCount, tx *gorm.DB) error { err := tx.Create(&good).Error if err != nil { tx.Rollback() return err } return err } func UpdateGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateNewGoodStockCount(user_org_id int64, storehouse_id int64, good_id int64, count int64, flush_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error if err != nil { tx.Rollback() return err } err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).Update(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { tx.Rollback() return err } return err } func UpdateGoodInCount(count int64, user_org_id int64, good_id int64, storehouse_id int64, flush_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and good_id = ? and storehouse_id = ? and status = 1", user_org_id, good_id, storehouse_id).Update(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func CreateGoodErrcode(errcode models.XtGoodErrcode) error { err := writeDb.Create(&errcode).Error return err } func GetGoodStockList(orgId int64, storehouse_id int64, good_type int64, keyword string, page int64, limit int64, ids []int64) (good []*models.GoodInformation, total int64, err error) { offset := (page - 1) * limit likeKey := "%" + keyword + "%" db := XTReadDB().Table("xt_good_information as x").Where("x.status = 1 and x.org_id =? and find_in_set('停用',x.good_status) = 0", orgId) if good_type == 2 { db = db.Where(" x.total_count<=x.stock_warn_count") } if good_type == 3 { db = db.Where("x.sum_count = 0") } if good_type == 4 { db = db.Where("x.sum_count > 0") } if len(keyword) > 0 { db = db.Where("good_name like ? or manufacturer in(?)", likeKey, ids) } db = db.Preload("XtGoodStockCount", func(db *gorm.DB) *gorm.DB { if storehouse_id > 0 { db = db.Where("storehouse_id = ?", storehouse_id) } return db.Where("status = 1 and user_org_id = ?", orgId) }) db = db.Preload("GoodStockCount", func(db *gorm.DB) *gorm.DB { if storehouse_id > 0 { db = db.Where("storehouse_id = ?", storehouse_id) } return db.Where("status = 1 and user_org_id = ?", orgId).Group("good_id,storehouse_id") }) db = db.Preload("WarehousingInfo", func(db *gorm.DB) *gorm.DB { if storehouse_id > 0 { db = db.Where("storehouse_id = ?", storehouse_id) } return db.Where("status = 1 and org_id = ? and is_check = 1", orgId) }) if orgId == 10489 { err = db.Count(&total).Offset(offset).Limit(limit).Order("x.sum_count desc").Find(&good).Error } else { err = db.Count(&total).Offset(offset).Limit(limit).Order("x.ctime desc").Find(&good).Error } return good, total, err } func GetSumGoodList(orgid int64, storehouse_id int64, good_id int64) (info []*models.WarehousingInfo, err error) { err = XTReadDB().Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgid, storehouse_id, good_id).Find(&info).Error return info, err } func GetNewSumGoodList(orgid int64, storehouse_id int64, good_id int64, tx *gorm.DB) (info []*models.WarehousingInfo, err error) { err = tx.Where("org_id = ? and storehouse_id = ? and good_id = ? and status =1 and is_check = 1", orgid, storehouse_id, good_id).Find(&info).Error if err != gorm.ErrRecordNotFound { if err != nil { return info, err } } return info, err } func UpdateSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateNewSumGood(orgid int64, storehouse_id int64, good_id int64, flush_count int64, tx *gorm.DB) error { err := tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", orgid, storehouse_id, good_id).Updates(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { tx.Rollback() return err } return err } func ReduceSumOutCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error { ut := XTWriteDB().Begin() err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", total_count)).Error err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", total_count)).Error if err != nil { ut.Rollback() return err } ut.Commit() return err } func ReduceSumInCount(good_id int64, total_count int64, storehouse_id int64, user_org_id int64) error { ut := XTWriteDB().Begin() err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", total_count)).Error err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", total_count)).Error if err != nil { ut.Rollback() return err } ut.Commit() return err } func UpdateSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error { ut := XTWriteDB().Begin() err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count - ?", count)).Error if err != nil { ut.Rollback() return err } ut.Commit() return err } func UpdateActSumCount(user_org_id int64, storehouse_id int64, good_id int64, count int64) error { ut := XTWriteDB().Begin() err = ut.Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and status = 1 and user_org_id = ? and good_id =?", storehouse_id, user_org_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count - ?", count)).Error if err != nil { ut.Rollback() return err } ut.Commit() return err } func GetHisPrescriptionList(orgid int64) (project []*models.HisPrescriptionProject, err error) { err = XTReadDB().Where("user_org_id = ? and status= 1 and record_date =1690819200", orgid).Find(&project).Error return project, err } func UpdateGoodFlow(project_id int64, price float64) error { flow := models.VmStockFlow{} err := XTWriteDB().Model(&flow).Where("warehousing_id = ? and user_org_id =10265", project_id).Updates(map[string]interface{}{"price": price}).Error err = XTWriteDB().Model(models.WarehouseOutInfo{}).Where("warehouse_info_id = ? and org_id =10265", project_id).Updates(map[string]interface{}{"price": price}).Error return err } func GetSendGoodInformation(org_id int64) (info []*models.WarehousingInfo, err error) { err = XTReadDB().Where("org_id = ? and is_check = 1 and status = 1", org_id).Find(&info).Error return info, err } func GetStockFlush(storehouse_id int64, good_id int64, orgid int64) (*models.XtGoodStockCount, error) { stockCount := models.XtGoodStockCount{} var err error err = XTReadDB().Where("storehouse_id =? and good_id = ? and user_org_id = ?", storehouse_id, good_id, orgid).Find(&stockCount).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &stockCount, nil } func CreateGoodCountSix(good models.XtGoodStockCount) error { err := XTWriteDB().Create(&good).Error return err } func UpdateGoodCount(warehousingCount int64, stock_count int64, out_count int64, storehouse_id int64, good_id int64, orgid int64, cancel_count int64, act_count int64) error { err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count+?", warehousingCount)).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_out_count": out_count}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", stock_count)).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_cancel_count": cancel_count}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_act_out_count": act_count}).Error return err } func UpdateGoodCountSend(warehousingCount int64, stock_count int64, out_count int64, storehouse_id int64, good_id int64, orgid int64, cancel_count int64, act_count int64) error { err := XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Updates(map[string]interface{}{"stock_in_count": warehousingCount}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_out_count": out_count}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"flush_count": stock_count}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_cancel_count": cancel_count}).Error err = XTWriteDB().Model(&models.XtGoodStockCount{}).Where("storehouse_id = ? and good_id = ? and status =1 and user_org_id = ?", storehouse_id, good_id, orgid).Update(map[string]interface{}{"stock_act_out_count": act_count}).Error return err } func GetAllCancelCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) { err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 4 or consumable_type= 7) and status = 1", storehouse_id, good_id, org_id).Find(&flow).Error return flow, err } func GetAllStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) { err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 2 or consumable_type= 3)", storehouse_id, good_id, org_id).Find(&flow).Error return flow, err } func GetActStockOutCount(storehouse_id int64, good_id int64, org_id int64) (flow []*models.VmStockFlow, err error) { err = XTReadDB().Where("storehouse_id = ? and good_id = ? and user_org_id = ? and (consumable_type = 2 or consumable_type= 3) and status = 1", storehouse_id, good_id, org_id).Find(&flow).Error return flow, err } func GetDrugStockCount(storehouse_id int64, drug_id int64, org_id int64) (*models.XtDrugStockCount, error) { drugStockCount := models.XtDrugStockCount{} var err error err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error if err == gorm.ErrRecordNotFound { return nil, err } if err != nil { return nil, err } return &drugStockCount, nil } func GetNewDrugStockCount(storehouse_id int64, drug_id int64, org_id int64, tx *gorm.DB) (*models.XtDrugStockCount, error) { drugStockCount := models.XtDrugStockCount{} var err error err = tx.Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error if err == gorm.ErrRecordNotFound { if err != nil { tx.Rollback() return nil, err } return nil, err } if err != nil { return nil, err } return &drugStockCount, nil } func GetDrugStockCountSix(storehouse_id int64, drug_id int64, org_id int64) (models.XtDrugStockCount, error) { drugStockCount := models.XtDrugStockCount{} err = XTReadDB().Where("storehouse_id = ? and drug_id = ? and user_org_id = ? and status = 1", storehouse_id, drug_id, org_id).Find(&drugStockCount).Error return drugStockCount, err } func CreateDrugStockSum(drug models.XtDrugStockCount) error { ut := XTWriteDB().Begin() err := ut.Create(&drug).Error if err != nil { ut.Rollback() return err } ut.Commit() return err } func CreateNewDrugStockSum(drug models.XtDrugStockCount, tx *gorm.DB) error { err := tx.Create(&drug).Error if err != nil { tx.Rollback() return err } return err } func AddDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", waresing_count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateNewDrugStockSum(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error if err != nil { tx.Rollback() return err } err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { tx.Rollback() return err } return err } func ReduceDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func ReduceNewDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", waresing_count)).Error if err != nil { tx.Rollback() return err } err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { tx.Rollback() return err } return err } func AddCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func AddCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func AddSelfCancelSumCountOne(patient_id int64, drug_id int64, user_org_id int64, cancel_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func AddNewCancelSumCountOne(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count + ?", cancel_count)).Error if err != nil { tx.Rollback() return err } return err } func ReduceCancelSumCount(storehouse_id int64, drug_id int64, user_org_id int64, cancel_count int64, over_count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_cancel_count", gorm.Expr("sum_cancel_count - ?", cancel_count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateSumDrug(orgid int64, storehouse_id int64, drug_id int64, flush_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", orgid, storehouse_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateSumDrugTwo(orgid int64, patient_id int64, drug_id int64, flush_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", orgid, patient_id, drug_id).Update(map[string]interface{}{"flush_count": flush_count}).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateSumOutDrug(orgid int64, storehouse_id int64, drug_id int64, sum_out_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", orgid, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", sum_out_count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func UpdateSelfSumOutDrug(orgid int64, patient_id int64, drug_id int64, sum_out_count int64) error { db := writeDb.Begin() err := db.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", orgid, patient_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count - ?", sum_out_count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func GetAutoGoodLastCount(goodid int64, count int64, record_time int64, patient_id int64) (models.AutomaticReduceDetail, error) { detail := models.AutomaticReduceDetail{} err := XTReadDB().Where("good_id = ? and count = ? and record_time = ? and patient_id = ?", goodid, count, record_time, patient_id).Last(&detail).Error return detail, err } func UpdateLastAutoCount(id int64, count int64) error { detail := models.AutomaticReduceDetail{} err := XTWriteDB().Model(&detail).Where("id = ?", id).Update(map[string]interface{}{"count": count, "status": 1}).Error return err } func GetWareOutInfo(good_id int64, record_date int64, user_org_id int64, patient_id int64, order_id int64) (outinfo []*models.WarehouseOutInfo, err error) { err = XTReadDB().Where("good_id = ? and sys_record_time = ? and org_id = ? and status = 1 and patient_id = ? and order_id = ?", good_id, record_date, user_org_id, patient_id, order_id).Find(&outinfo).Error return outinfo, err } func AddGoodStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func AddDrugStockCount(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count + ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func ReduceStockCount(storehouse_id int64, good_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() //err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_in_count", gorm.Expr("stock_in_count - ?", count)).Error //if err != nil { // db.Rollback() // return err //} err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func ReduceStockCountTwo(storehouse_id int64, good_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_out_count", gorm.Expr("stock_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_act_out_count", gorm.Expr("stock_act_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func ReduceDrugStockCountSix(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() //err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_in_count", gorm.Expr("sum_in_count - ?", count)).Error //if err != nil { // db.Rollback() // return err //} err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func ReduceDrugStockCountSeven(storehouse_id int64, drug_id int64, user_org_id int64, count int64) error { db := writeDb.Begin() err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_out_count", gorm.Expr("sum_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("sum_act_out_count", gorm.Expr("sum_act_out_count + ?", count)).Error if err != nil { db.Rollback() return err } err = db.Model(&models.XtDrugStockCount{}).Where("user_org_id = ? and storehouse_id = ? and drug_id = ? and status = 1", user_org_id, storehouse_id, drug_id).UpdateColumn("flush_count", gorm.Expr("flush_count - ?", count)).Error if err != nil { db.Rollback() return err } db.Commit() return err } func GetWarehouseOutInfoByProjectIdList(project_id int64, org_id int64, storehouse_id int64, good_id int64, patient_id int64, sys_record_time int64) (info []*models.WarehouseOutInfoNight, err error) { err = XTReadDB().Where("project_id = ? and org_id = ? and storehouse_id = ? and good_id = ? and patient_id = ? and sys_record_time = ? and status = 1", project_id, org_id, storehouse_id, good_id, patient_id, sys_record_time).Find(&info).Error return info, err } func GetWarehouseStockFlow(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, system_time int64) (models.VmStockFlow, error) { stockFlow := models.VmStockFlow{} err := XTReadDB().Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?", warehouse_info_id, good_id, user_org_id, patient_id, system_time).Find(&stockFlow).Error return stockFlow, err } func AddCountFlowOne(warehouse_info_id int64, good_id int64, user_org_id int64, patient_id int64, count int64, system_time int64, over_count int64) error { stockFlow := models.VmStockFlow{} err := XTWriteDB().Model(&stockFlow).Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?", warehouse_info_id, good_id, user_org_id, patient_id, system_time).UpdateColumn("count", gorm.Expr("count + ?", count)).Error err = XTWriteDB().Model(&stockFlow).Where("warehousing_detail_id = ? and good_id = ? and user_org_id = ? and patient_id = ? AND consumable_type = 7 and system_time = ?").Update(map[string]interface{}{"over_count": over_count}).Error return err } func GetWarehouseOutInfo(user_org_id int64) (info []*models.WarehouseOut, err error) { err = XTReadDB().Where("org_id = ? and status =1", 10485).Find(&info).Error return info, err } func UpdateWarehosueOutInfo(id int64, sys_record_time int64) (models.WarehouseOutInfo, error) { info := models.WarehouseOutInfo{} err := XTWriteDB().Model(&info).Where("warehouse_out_id = ? and org_id = ? ", id, 10485).Updates(map[string]interface{}{"sys_record_time": sys_record_time, "ctime": sys_record_time}).Error return info, err } func AddGoodNewCancelSumCountOne(storehouse_id int64, good_id int64, user_org_id int64, cancel_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtGoodStockCount{}).Where("user_org_id = ? and storehouse_id = ? and good_id = ? and status = 1", user_org_id, storehouse_id, good_id).UpdateColumn("stock_cancel_count", gorm.Expr("stock_cancel_count + ?", cancel_count)).Error if err != nil { tx.Rollback() return err } return err } func GetDrugNewInventoryList(org_id int64, keyword string) (drug []*models.BaseDrugLibTwentyOne, err error) { likeKey := "%" + keyword + "%" db := XTReadDB().Where("status=1") if org_id > 0 { db = db.Where("org_id = ?", org_id) } if len(keyword) > 0 { db = db.Where("drug_name like ?", likeKey) } err = db.Order("id desc").Find(&drug).Error return drug, err } func GetSelfDrugStockCount(patient_id int64, drug_id int64, org_id int64, tx *gorm.DB) (*models.XtSelfDrugStockCount, error) { drugStockCount := models.XtSelfDrugStockCount{} var err error err = tx.Where("patient_id = ? and drug_id = ? and user_org_id = ? and status = 1", patient_id, drug_id, org_id).Find(&drugStockCount).Error if err != gorm.ErrRecordNotFound { if err != nil { tx.Rollback() return nil, err } } return &drugStockCount, nil } func UpdateSelfDrugStockSum(patient_id int64, drug_id int64, user_org_id int64, waresing_count int64, over_count int64, tx *gorm.DB) error { err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).Update(map[string]interface{}{"sum_in_count": waresing_count}).Error if err != nil { tx.Rollback() return err } err = tx.Model(&models.XtSelfDrugStockCount{}).Where("user_org_id = ? and patient_id = ? and drug_id = ? and status = 1", user_org_id, patient_id, drug_id).Update(map[string]interface{}{"flush_count": over_count}).Error if err != nil { tx.Rollback() return err } return err } func GetSelfDrugBatchNumber(drug_id int64, org_id int64) (info []*models.XtSelfDrugWarehouseInfo, err error) { err = XTReadDB().Model(&info).Where("drug_id = ? and org_id = ? and status=1 and batch_number <>''", drug_id, org_id).Find(&info).Error return info, err }