diff --git a/api/controller/base.go b/api/controller/base.go index 3dbea4e..1c35f58 100644 --- a/api/controller/base.go +++ b/api/controller/base.go @@ -41,6 +41,7 @@ type userDoctorManage struct { type basic struct { Department // 科室管理 Hospital // 医院管理 + Pharmacy // 药房管理 DiseaseClassExpertise // 专长管理 Bank // 银行管理 } diff --git a/api/controller/pharmacy.go b/api/controller/pharmacy.go new file mode 100644 index 0000000..302e64d --- /dev/null +++ b/api/controller/pharmacy.go @@ -0,0 +1,234 @@ +package controller + +import ( + "github.com/gin-gonic/gin" + "hospital-admin-api/api/dao" + "hospital-admin-api/api/dto" + "hospital-admin-api/api/requests" + "hospital-admin-api/api/responses" + "hospital-admin-api/api/service" + "hospital-admin-api/global" + "hospital-admin-api/utils" + "strconv" +) + +type Pharmacy struct{} + +// GetPharmacyPage 获取药房列表-分页 +func (r *Pharmacy) GetPharmacyPage(c *gin.Context) { + pharmacyRequest := requests.PharmacyRequest{} + req := pharmacyRequest.GetPharmacyPage + if err := c.ShouldBindJSON(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + if req.Page <= 0 { + req.Page = 1 + } + + if req.PageSize <= 0 { + req.PageSize = 20 + } + + pharmacyDao := dao.PharmacyDao{} + pharmacies, total, err := pharmacyDao.GetPharmacyPageSearch(req, req.Page, req.PageSize) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + resList := dto.GetPharmacyListDto(pharmacies) + + result := make(map[string]interface{}) + result["page"] = req.Page + result["page_size"] = req.PageSize + result["total"] = total + result["data"] = resList + responses.OkWithData(result, c) +} + +// GetPharmacyList 获取药房列表(下拉/限制条件) +func (r *Pharmacy) GetPharmacyList(c *gin.Context) { + pharmacyRequest := requests.PharmacyRequest{} + req := pharmacyRequest.GetPharmacyList + if err := c.ShouldBind(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + maps := make(map[string]interface{}) + if req.PharmacyName != "" { + maps["pharmacy_name"] = req.PharmacyName + } + if req.PharmacyCode != "" { + maps["pharmacy_code"] = req.PharmacyCode + } + if req.Status != nil { + maps["status"] = *req.Status + } else { + maps["status"] = 1 // 默认只查正常启用的 + } + if req.IsPickup != nil { + maps["is_pickup"] = *req.IsPickup + } + + pharmacyDao := dao.PharmacyDao{} + pharmacies, err := pharmacyDao.GetPharmacyList(maps) + if err != nil { + responses.Ok(c) + return + } + + resList := dto.GetPharmacyListDto(pharmacies) + responses.OkWithData(resList, c) +} + +// GetPharmacy 获取药房详情 +func (r *Pharmacy) GetPharmacy(c *gin.Context) { + id := c.Param("pharmacy_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + pharmacyId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyService := service.PharmacyService{} + pharmacyDto, err := pharmacyService.GetPharmacy(pharmacyId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.OkWithData(pharmacyDto, c) +} + +// AddPharmacy 新增药房 +func (r *Pharmacy) AddPharmacy(c *gin.Context) { + pharmacyRequest := requests.PharmacyRequest{} + req := pharmacyRequest.AddPharmacy + if err := c.ShouldBindJSON(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + pharmacyService := service.PharmacyService{} + _, err := pharmacyService.AddPharmacy(req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// PutPharmacy 修改药房 +func (r *Pharmacy) PutPharmacy(c *gin.Context) { + pharmacyRequest := requests.PharmacyRequest{} + req := pharmacyRequest.PutPharmacy + if err := c.ShouldBindJSON(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + id := c.Param("pharmacy_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + pharmacyId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyService := service.PharmacyService{} + _, err = pharmacyService.PutPharmacy(pharmacyId, req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// PutPharmacyStatus 修改药房状态 +func (r *Pharmacy) PutPharmacyStatus(c *gin.Context) { + pharmacyRequest := requests.PharmacyRequest{} + req := pharmacyRequest.PutPharmacyStatus + if err := c.ShouldBindJSON(&req); err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + if err := global.Validate.Struct(req); err != nil { + responses.FailWithMessage(utils.Translate(err), c) + return + } + + id := c.Param("pharmacy_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + pharmacyId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyService := service.PharmacyService{} + _, err = pharmacyService.PutPharmacyStatus(pharmacyId, req) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} + +// DeletePharmacy 删除药房 +func (r *Pharmacy) DeletePharmacy(c *gin.Context) { + id := c.Param("pharmacy_id") + if id == "" { + responses.FailWithMessage("缺少参数", c) + return + } + + pharmacyId, err := strconv.ParseInt(id, 10, 64) + if err != nil { + responses.Fail(c) + return + } + + pharmacyService := service.PharmacyService{} + _, err = pharmacyService.DeletePharmacy(pharmacyId) + if err != nil { + responses.FailWithMessage(err.Error(), c) + return + } + + responses.Ok(c) +} diff --git a/api/dao/pharmacy.go b/api/dao/pharmacy.go new file mode 100644 index 0000000..2d0d2ac --- /dev/null +++ b/api/dao/pharmacy.go @@ -0,0 +1,111 @@ +package dao + +import ( + "gorm.io/gorm" + "hospital-admin-api/api/model" + "hospital-admin-api/api/requests" + "hospital-admin-api/global" +) + +type PharmacyDao struct{} + +// GetPharmacyById 获取药房数据-药房id +func (r *PharmacyDao) GetPharmacyById(pharmacyId int64) (m *model.Pharmacy, err error) { + err = global.Db.Where("pharmacy_id = ? AND status != ?", pharmacyId, 2).First(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetPharmacyByCode 获取药房数据-药房代码 +func (r *PharmacyDao) GetPharmacyByCode(code string) (m *model.Pharmacy, err error) { + err = global.Db.Where("pharmacy_code = ? AND status != ?", code, 2).First(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// AddPharmacy 新增药房 +func (r *PharmacyDao) AddPharmacy(tx *gorm.DB, m *model.Pharmacy) (*model.Pharmacy, error) { + if err := tx.Create(m).Error; err != nil { + return nil, err + } + return m, nil +} + +// EditPharmacyById 修改药房-药房id +func (r *PharmacyDao) EditPharmacyById(tx *gorm.DB, pharmacyId int64, data interface{}) error { + err := tx.Model(&model.Pharmacy{}).Where("pharmacy_id = ?", pharmacyId).Updates(data).Error + if err != nil { + return err + } + return nil +} + +// DeletePharmacyById 软删除药房-药房id +func (r *PharmacyDao) DeletePharmacyById(tx *gorm.DB, pharmacyId int64) error { + err := tx.Model(&model.Pharmacy{}).Where("pharmacy_id = ?", pharmacyId).Update("status", 2).Error + if err != nil { + return err + } + return nil +} + +// GetPharmacyList 获取药房列表 +func (r *PharmacyDao) GetPharmacyList(maps interface{}) (m []*model.Pharmacy, err error) { + err = global.Db.Where(maps).Where("status != ?", 2).Order("created_at desc").Find(&m).Error + if err != nil { + return nil, err + } + return m, nil +} + +// GetPharmacyPageSearch 获取药房列表-分页 +func (r *PharmacyDao) GetPharmacyPageSearch(req requests.GetPharmacyPage, page, pageSize int) (m []*model.Pharmacy, total int64, err error) { + var totalRecords int64 + + query := global.Db.Model(&model.Pharmacy{}).Where("status != ?", 2) + + if req.PharmacyName != "" { + query = query.Where("pharmacy_name LIKE ?", "%"+req.PharmacyName+"%") + } + + if req.PharmacyCode != "" { + query = query.Where("pharmacy_code LIKE ?", "%"+req.PharmacyCode+"%") + } + + if req.Status != nil { + query = query.Where("status = ?", *req.Status) + } + + if req.IsPickup != nil { + query = query.Where("is_pickup = ?", *req.IsPickup) + } + + if req.ProvinceId != 0 { + query = query.Where("province_id = ?", req.ProvinceId) + } + + if req.CityId != nil { + query = query.Where("city_id = ?", req.CityId) + } + + if req.CountyId != nil { + query = query.Where("county_id = ?", req.CountyId) + } + + query = query.Order("created_at desc") + + if err := query.Count(&totalRecords).Error; err != nil { + return nil, 0, err + } + + err = query.Scopes(model.Paginate(page, pageSize)).Find(&m).Error + if err != nil { + return nil, 0, err + } + + return m, totalRecords, nil +} diff --git a/api/dto/Pharmacy.go b/api/dto/Pharmacy.go new file mode 100644 index 0000000..175e6a4 --- /dev/null +++ b/api/dto/Pharmacy.go @@ -0,0 +1,62 @@ +package dto + +import ( + "fmt" + "hospital-admin-api/api/model" +) + +type PharmacyDto struct { + PharmacyId string `json:"pharmacy_id"` // 主键id + PharmacyName string `json:"pharmacy_name"` // 药房名称 + PharmacyCode string `json:"pharmacy_code"` // 药房代码 + Postage float64 `json:"postage"` // 基础邮费 + FreeShippingThreshold float64 `json:"free_shipping_threshold"` // 满XX包邮门槛金额 + IsPickup int `json:"is_pickup"` // 是否支持自提(0:否 1:是) + Telephone string `json:"telephone"` // 电话 + ProvinceId int `json:"province_id"` // 省份id + Province string `json:"province"` // 省份 + CityId int `json:"city_id"` // 城市id + City string `json:"city"` // 城市 + CountyId int `json:"county_id"` // 区县id + County string `json:"county"` // 区县 + Address string `json:"address"` // 详细地址 + FullAddress string `json:"full_address"` // 完整地址(省市区+详细地址) + Status int `json:"status"` // 状态(0:禁用 1:正常 2:删除) + CreatedAt model.LocalTime `json:"created_at"` // 创建时间 + UpdatedAt model.LocalTime `json:"updated_at"` // 修改时间 +} + +func GetPharmacyDto(m *model.Pharmacy) *PharmacyDto { + if m == nil { + return nil + } + fullAddress := fmt.Sprintf("%s%s%s%s", m.Province, m.City, m.County, m.Address) + return &PharmacyDto{ + PharmacyId: fmt.Sprintf("%d", m.PharmacyId), + PharmacyName: m.PharmacyName, + PharmacyCode: m.PharmacyCode, + Postage: m.Postage, + FreeShippingThreshold: m.FreeShippingThreshold, + IsPickup: m.IsPickup, + Telephone: m.Telephone, + ProvinceId: m.ProvinceId, + Province: m.Province, + CityId: m.CityId, + City: m.City, + CountyId: m.CountyId, + County: m.County, + Address: m.Address, + FullAddress: fullAddress, + Status: m.Status, + CreatedAt: m.CreatedAt, + UpdatedAt: m.UpdatedAt, + } +} + +func GetPharmacyListDto(m []*model.Pharmacy) []PharmacyDto { + responses := make([]PharmacyDto, len(m)) + for i, v := range m { + responses[i] = *GetPharmacyDto(v) + } + return responses +} diff --git a/api/model/pharmacy.go b/api/model/pharmacy.go new file mode 100644 index 0000000..6f00cde --- /dev/null +++ b/api/model/pharmacy.go @@ -0,0 +1,45 @@ +package model + +import ( + "gorm.io/gorm" + "hospital-admin-api/global" + "time" +) + +// Pharmacy 药房信息表 +type Pharmacy struct { + PharmacyId int64 `gorm:"column:pharmacy_id;type:bigint(20);primary_key;comment:主键id" json:"pharmacy_id"` + PharmacyName string `gorm:"column:pharmacy_name;type:varchar(100);comment:药房名称;NOT NULL" json:"pharmacy_name"` + PharmacyCode string `gorm:"column:pharmacy_code;type:varchar(64);comment:药房代码;NOT NULL" json:"pharmacy_code"` + Postage float64 `gorm:"column:postage;type:decimal(10,2);default:0.00;comment:基础邮费" json:"postage"` + FreeShippingThreshold float64 `gorm:"column:free_shipping_threshold;type:decimal(10,2);default:0.00;comment:满XX包邮门槛金额" json:"free_shipping_threshold"` + IsPickup int `gorm:"column:is_pickup;type:tinyint(1);default:0;comment:是否支持自提(0:否 1:是)" json:"is_pickup"` + Telephone string `gorm:"column:telephone;type:varchar(30);comment:电话" json:"telephone"` + ProvinceId int `gorm:"column:province_id;type:int(11);default:0;comment:省份id" json:"province_id"` + Province string `gorm:"column:province;type:varchar(50);comment:省份" json:"province"` + CityId int `gorm:"column:city_id;type:int(11);default:0;comment:城市id" json:"city_id"` + City string `gorm:"column:city;type:varchar(50);comment:城市" json:"city"` + CountyId int `gorm:"column:county_id;type:int(11);default:0;comment:区县id" json:"county_id"` + County string `gorm:"column:county;type:varchar(50);comment:区县" json:"county"` + Address string `gorm:"column:address;type:varchar(255);comment:地址" json:"address"` + Status int `gorm:"column:status;type:tinyint(1);default:1;comment:状态(0:禁用 1:正常 2:删除)" json:"status"` + Model +} + +func (m *Pharmacy) TableName() string { + return "gdxz_pharmacy" +} + +func (m *Pharmacy) BeforeCreate(tx *gorm.DB) error { + if m.PharmacyId == 0 { + m.PharmacyId = global.Snowflake.Generate().Int64() + } + + m.CreatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("CreatedAt", m.CreatedAt) + + m.UpdatedAt = LocalTime(time.Now()) + tx.Statement.SetColumn("UpdatedAt", m.UpdatedAt) + + return nil +} diff --git a/api/requests/pharmacy.go b/api/requests/pharmacy.go new file mode 100644 index 0000000..65e6372 --- /dev/null +++ b/api/requests/pharmacy.go @@ -0,0 +1,65 @@ +package requests + +type PharmacyRequest struct { + GetPharmacyList // 获取药房列表 + GetPharmacyPage // 获取药房列表-分页 + AddPharmacy // 新增药房 + PutPharmacy // 修改药房 + PutPharmacyStatus // 修改药房状态 +} + +// GetPharmacyList 获取药房列表 +type GetPharmacyList struct { + PharmacyName string `json:"pharmacy_name" form:"pharmacy_name" label:"药房名称"` + PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"药房代码"` + Status *int `json:"status" form:"status" label:"状态"` + IsPickup *int `json:"is_pickup" form:"is_pickup" label:"是否支持自提"` +} + +// GetPharmacyPage 获取药房列表-分页 +type GetPharmacyPage struct { + Page int `json:"page" form:"page" label:"页码"` + PageSize int `json:"page_size" form:"page_size" label:"每页个数"` + PharmacyName string `json:"pharmacy_name" form:"pharmacy_name" label:"药房名称"` + PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"药房代码"` + Status *int `json:"status" form:"status" label:"状态"` + IsPickup *int `json:"is_pickup" form:"is_pickup" label:"是否支持自提"` + ProvinceId int `json:"province_id" form:"province_id" label:"省份id"` + CityId *int `json:"city_id" form:"city_id" label:"城市id"` + CountyId *int `json:"county_id" form:"county_id" label:"区县id"` +} + +// AddPharmacy 新增药房 +type AddPharmacy struct { + PharmacyName string `json:"pharmacy_name" form:"pharmacy_name" label:"药房名称" validate:"required"` + PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"药房代码" validate:"required"` + Postage *float64 `json:"postage" form:"postage" label:"邮费" validate:"required,gte=0"` + FreeShippingThreshold *float64 `json:"free_shipping_threshold" form:"free_shipping_threshold" label:"满XX包邮" validate:"required,gte=0"` + IsPickup *int `json:"is_pickup" form:"is_pickup" label:"是否支持自提" validate:"required,oneof=0 1"` + Telephone string `json:"telephone" form:"telephone" label:"电话"` + ProvinceId int `json:"province_id" form:"province_id" label:"省份id"` + CityId int `json:"city_id" form:"city_id" label:"城市id"` + CountyId int `json:"county_id" form:"county_id" label:"区县id"` + Address string `json:"address" form:"address" label:"地址" validate:"required"` + Status *int `json:"status" form:"status" label:"状态" validate:"omitempty,oneof=0 1"` +} + +// PutPharmacy 修改药房 +type PutPharmacy struct { + PharmacyName string `json:"pharmacy_name" form:"pharmacy_name" label:"药房名称" validate:"required"` + PharmacyCode string `json:"pharmacy_code" form:"pharmacy_code" label:"药房代码" validate:"required"` + Postage *float64 `json:"postage" form:"postage" label:"邮费" validate:"required,gte=0"` + FreeShippingThreshold *float64 `json:"free_shipping_threshold" form:"free_shipping_threshold" label:"满XX包邮" validate:"required,gte=0"` + IsPickup *int `json:"is_pickup" form:"is_pickup" label:"是否支持自提" validate:"required,oneof=0 1"` + Telephone string `json:"telephone" form:"telephone" label:"电话"` + ProvinceId int `json:"province_id" form:"province_id" label:"省份id"` + CityId int `json:"city_id" form:"city_id" label:"城市id"` + CountyId int `json:"county_id" form:"county_id" label:"区县id"` + Address string `json:"address" form:"address" label:"地址" validate:"required"` + Status *int `json:"status" form:"status" label:"状态" validate:"omitempty,oneof=0 1"` +} + +// PutPharmacyStatus 修改药房状态 +type PutPharmacyStatus struct { + Status *int `json:"status" form:"status" label:"状态" validate:"required,oneof=0 1"` +} diff --git a/api/router/router.go b/api/router/router.go index 58f7e3d..160fccf 100644 --- a/api/router/router.go +++ b/api/router/router.go @@ -161,6 +161,13 @@ func basicRouter(r *gin.Engine, api controller.Api) { hospitalGroup.GET("/list", api.Hospital.GetHospitalList) } + // 药房管理-基础数据 + pharmacyGroup := basicGroup.Group("/pharmacy") + { + // 获取药房列表 + pharmacyGroup.GET("/list", api.Pharmacy.GetPharmacyList) + } + // 医生专长管理-基础数据 expertiseGroup := basicGroup.Group("/expertise") { @@ -885,6 +892,31 @@ func privateRouter(r *gin.Engine, api controller.Api) { hospitalGroup.PUT("/:hospital_id", api.Hospital.PutHospital) } + // 药房管理 + pharmacyGroup := basicGroup.Group("/pharmacy") + { + // 获取药房列表-分页 + pharmacyGroup.POST("/page", api.Pharmacy.GetPharmacyPage) + + // 获取药房列表 + pharmacyGroup.GET("/list", api.Pharmacy.GetPharmacyList) + + // 获取药房详情 + pharmacyGroup.GET("/:pharmacy_id", api.Pharmacy.GetPharmacy) + + // 新增药房 + pharmacyGroup.POST("", api.Pharmacy.AddPharmacy) + + // 修改药房 + pharmacyGroup.PUT("/:pharmacy_id", api.Pharmacy.PutPharmacy) + + // 修改药房状态 + pharmacyGroup.PUT("/status/:pharmacy_id", api.Pharmacy.PutPharmacyStatus) + + // 删除药房 + pharmacyGroup.DELETE("/:pharmacy_id", api.Pharmacy.DeletePharmacy) + } + // 科普分类管理 articleClassGroup := basicGroup.Group("/article/class") { diff --git a/api/service/pharmacy.go b/api/service/pharmacy.go new file mode 100644 index 0000000..800b688 --- /dev/null +++ b/api/service/pharmacy.go @@ -0,0 +1,261 @@ +package service + +import ( + "errors" + "fmt" + "gorm.io/gorm" + "hospital-admin-api/api/dao" + "hospital-admin-api/api/dto" + "hospital-admin-api/api/model" + "hospital-admin-api/api/requests" + "hospital-admin-api/global" +) + +type PharmacyService struct{} + +// AddPharmacy 新增药房 +func (r *PharmacyService) AddPharmacy(req requests.AddPharmacy) (bool, error) { + pharmacyDao := dao.PharmacyDao{} + + // 检测药房代码是否已存在 + existPharmacy, err := pharmacyDao.GetPharmacyByCode(req.PharmacyCode) + if err == nil && existPharmacy != nil { + return false, errors.New("药房代码已存在,请勿重复添加") + } + + var province string + var city string + var county string + + areaDao := dao.AreaDao{} + if req.ProvinceId != 0 { + area, err := areaDao.GetAreaById(req.ProvinceId) + if err != nil || area == nil { + return false, errors.New("省份数据错误") + } + province = area.AreaName + } + + if req.CityId != 0 { + area, err := areaDao.GetAreaById(req.CityId) + if err != nil || area == nil { + return false, errors.New("城市数据错误") + } + city = area.AreaName + } + + if req.CountyId != 0 { + area, err := areaDao.GetAreaById(req.CountyId) + if err != nil || area == nil { + return false, errors.New("区县数据错误") + } + county = area.AreaName + } + + status := 1 + if req.Status != nil { + status = *req.Status + } + + // 开启事务 + tx := global.Db.Begin() + defer func() { + if rec := recover(); rec != nil { + tx.Rollback() + } + }() + + pharmacy := &model.Pharmacy{ + PharmacyName: req.PharmacyName, + PharmacyCode: req.PharmacyCode, + Postage: *req.Postage, + FreeShippingThreshold: *req.FreeShippingThreshold, + IsPickup: *req.IsPickup, + Telephone: req.Telephone, + ProvinceId: req.ProvinceId, + Province: province, + CityId: req.CityId, + City: city, + CountyId: req.CountyId, + County: county, + Address: req.Address, + Status: status, + } + + pharmacy, err = pharmacyDao.AddPharmacy(tx, pharmacy) + if err != nil || pharmacy == nil { + tx.Rollback() + return false, errors.New(err.Error()) + } + + tx.Commit() + return true, nil +} + +// PutPharmacy 修改药房 +func (r *PharmacyService) PutPharmacy(pharmacyId int64, req requests.PutPharmacy) (bool, error) { + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + return false, errors.New("药房不存在或已删除") + } + + // 如果修改了代码,需检查新代码是否被其他药房占用 + if req.PharmacyCode != pharmacy.PharmacyCode { + exist, err := pharmacyDao.GetPharmacyByCode(req.PharmacyCode) + if err == nil && exist != nil && exist.PharmacyId != pharmacyId { + return false, errors.New("药房代码已被占用") + } + } + + tx := global.Db.Begin() + defer func() { + if rec := recover(); rec != nil { + tx.Rollback() + fmt.Println(rec) + } + }() + + pharmacyData := make(map[string]interface{}) + + if req.PharmacyName != pharmacy.PharmacyName { + pharmacyData["pharmacy_name"] = req.PharmacyName + } + + if req.PharmacyCode != pharmacy.PharmacyCode { + pharmacyData["pharmacy_code"] = req.PharmacyCode + } + + if *req.Postage != pharmacy.Postage { + pharmacyData["postage"] = *req.Postage + } + + if *req.FreeShippingThreshold != pharmacy.FreeShippingThreshold { + pharmacyData["free_shipping_threshold"] = *req.FreeShippingThreshold + } + + if *req.IsPickup != pharmacy.IsPickup { + pharmacyData["is_pickup"] = *req.IsPickup + } + + if req.Telephone != pharmacy.Telephone { + pharmacyData["telephone"] = req.Telephone + } + + if req.ProvinceId != 0 && req.ProvinceId != pharmacy.ProvinceId { + areaDao := dao.AreaDao{} + area, err := areaDao.GetAreaById(req.ProvinceId) + if err != nil || area == nil { + return false, errors.New("省份数据错误") + } + pharmacyData["province_id"] = req.ProvinceId + pharmacyData["province"] = area.AreaName + } + + if req.CityId != 0 && req.CityId != pharmacy.CityId { + areaDao := dao.AreaDao{} + area, err := areaDao.GetAreaById(req.CityId) + if err != nil || area == nil { + return false, errors.New("城市数据错误") + } + pharmacyData["city_id"] = req.CityId + pharmacyData["city"] = area.AreaName + } + + if req.CountyId != 0 && req.CountyId != pharmacy.CountyId { + areaDao := dao.AreaDao{} + area, err := areaDao.GetAreaById(req.CountyId) + if err != nil || area == nil { + return false, errors.New("区县数据错误") + } + pharmacyData["county_id"] = req.CountyId + pharmacyData["county"] = area.AreaName + } + + if req.Address != pharmacy.Address { + pharmacyData["address"] = req.Address + } + + if req.Status != nil && *req.Status != pharmacy.Status { + pharmacyData["status"] = *req.Status + } + + if len(pharmacyData) > 0 { + err = pharmacyDao.EditPharmacyById(tx, pharmacyId, pharmacyData) + if err != nil { + tx.Rollback() + return false, errors.New(err.Error()) + } + } + + tx.Commit() + return true, nil +} + +// PutPharmacyStatus 修改药房状态 +func (r *PharmacyService) PutPharmacyStatus(pharmacyId int64, req requests.PutPharmacyStatus) (bool, error) { + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + return false, errors.New("药房不存在或已删除") + } + + data := map[string]interface{}{ + "status": *req.Status, + } + + tx := global.Db.Begin() + defer func() { + if rec := recover(); rec != nil { + tx.Rollback() + } + }() + + err = pharmacyDao.EditPharmacyById(tx, pharmacyId, data) + if err != nil { + tx.Rollback() + return false, errors.New(err.Error()) + } + + tx.Commit() + return true, nil +} + +// DeletePharmacy 软删除药房 +func (r *PharmacyService) DeletePharmacy(pharmacyId int64) (bool, error) { + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + return false, errors.New("药房不存在或已删除") + } + + tx := global.Db.Begin() + defer func() { + if rec := recover(); rec != nil { + tx.Rollback() + } + }() + + err = pharmacyDao.DeletePharmacyById(tx, pharmacyId) + if err != nil { + tx.Rollback() + return false, errors.New(err.Error()) + } + + tx.Commit() + return true, nil +} + +// GetPharmacy 药房详情 +func (r *PharmacyService) GetPharmacy(pharmacyId int64) (*dto.PharmacyDto, error) { + pharmacyDao := dao.PharmacyDao{} + pharmacy, err := pharmacyDao.GetPharmacyById(pharmacyId) + if err != nil || pharmacy == nil { + if errors.Is(err, gorm.ErrRecordNotFound) || pharmacy == nil { + return nil, errors.New("未找到相关药房信息") + } + return nil, err + } + + return dto.GetPharmacyDto(pharmacy), nil +}