settle_account_main.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package settle_account_main
  2. import (
  3. "dashoo.cn/micro_libary/request"
  4. "errors"
  5. "fmt"
  6. "github.com/gogf/gf/os/gtime"
  7. "github.com/gogf/gf/util/gconv"
  8. "lims_adapter/dao/account"
  9. "lims_adapter/model"
  10. accountModel "lims_adapter/model/account"
  11. "strconv"
  12. )
  13. // Service 会议室服务
  14. type Service struct {
  15. Dao *account.SettleAccountMainDao
  16. Tenant string
  17. }
  18. // NewSrv 服务初始化
  19. func NewService(tenant string) Service {
  20. return Service{Dao: account.NewSettleAccountMainDao(tenant), Tenant: tenant}
  21. }
  22. // List 会议室列表
  23. func (s Service) List(req model.ListReq) ([]accountModel.SettleAccountMain, int, error) {
  24. entityModel := s.Dao.M
  25. where := "1=1"
  26. if req.Entity != nil {
  27. entity := new(accountModel.SettleAccountMainReq)
  28. err := gconv.Struct(req.Entity, entity)
  29. if err != nil {
  30. return nil, 0, err
  31. }
  32. if entity.MainUserId != 0 {
  33. where += fmt.Sprintf(" AND MainUserId='%v'", entity.MainUserId)
  34. }
  35. if entity.MainUser != "" {
  36. where += fmt.Sprintf(" AND MainUser LIKE '%%%v%%'", entity.MainUser)
  37. }
  38. if entity.AttachUserId != 0 {
  39. where += fmt.Sprintf(" AND AttachUserId='%v'", entity.AttachUserId)
  40. }
  41. if entity.AttachUser != "" {
  42. where += fmt.Sprintf(" AND AttachUser LIKE '%%%v%%'", entity.AttachUser)
  43. }
  44. if entity.InstrumentId != 0 {
  45. where += fmt.Sprintf(" AND InstrumentId='%v'", entity.InstrumentId)
  46. }
  47. if entity.AppointUserId != 0 {
  48. where += fmt.Sprintf(" AND AppointUserId='%v'", entity.AppointUserId)
  49. }
  50. if entity.AppointUser != "" {
  51. where += fmt.Sprintf(" AND AppointUser LIKE '%%%v%%'", entity.AppointUser)
  52. }
  53. if entity.Status != "" {
  54. where += fmt.Sprintf(" AND Status='%v'", entity.Status)
  55. }
  56. if entity.SettleStatus != "" {
  57. where += fmt.Sprintf(" AND SettleStatus='%v'", entity.SettleStatus)
  58. }
  59. if entity.FeeType != "" {
  60. where += fmt.Sprintf(" AND FeeType='%v'", entity.FeeType)
  61. }
  62. if entity.AppointStartDate != "" && entity.AppointEndDate != "" {
  63. where += fmt.Sprintf(" AND AppointStartDate>'%v' AND AppointEndDate<'%v'", entity.AppointStartDate, entity.AppointEndDate)
  64. }
  65. }
  66. entityModel = entityModel.Where(where)
  67. total, err := entityModel.Count()
  68. if err != nil {
  69. return nil, 0, err
  70. }
  71. if total == 0 {
  72. return nil, 0, nil
  73. }
  74. res, err := entityModel.Page(req.Current, req.Size).Order("settle_account_main.CreateOn DESC").Fields("settle_account_main.*").FindAll()
  75. if err != nil {
  76. return nil, 0, err
  77. }
  78. if res.IsEmpty() {
  79. return nil, 0, nil
  80. }
  81. list := make([]accountModel.SettleAccountMain, 0)
  82. err = res.Structs(&list)
  83. if err != nil {
  84. return nil, 0, err
  85. }
  86. return list, total, nil
  87. }
  88. // 新增
  89. func (s Service) Add(req accountModel.AccountMainAddReq, user request.UserInfo) error {
  90. now := gtime.Now() // 获取当前时间
  91. // 更新必要信息
  92. req.Main.CreateUserId = int(user.Id)
  93. req.Main.CreateBy = user.RealName
  94. req.Main.CreateOn = now
  95. req.Main.UpdateUserId = int(user.Id)
  96. req.Main.UpdateBy = user.RealName
  97. req.Main.UpdateOn = now
  98. tx, err := s.Dao.DB.Begin()
  99. if err != nil {
  100. return err
  101. }
  102. result, err := tx.Insert("settle_account_main", req.Main)
  103. if err != nil {
  104. tx.Rollback()
  105. return err
  106. }
  107. id , _ := result.LastInsertId()
  108. for index := range req.Details { // 更新必要信息
  109. req.Details[index].CreateUserId = int(user.Id)
  110. req.Details[index].CreateBy = user.RealName
  111. req.Details[index].CreateOn = now
  112. req.Details[index].UpdateUserId = int(user.Id)
  113. req.Details[index].UpdateBy = user.RealName
  114. req.Details[index].UpdateOn = now
  115. req.Details[index].Pid = int(id)
  116. }
  117. _, err = tx.Insert("settle_account_detail", req.Details)
  118. if err != nil {
  119. tx.Rollback()
  120. return err
  121. }
  122. return tx.Commit()
  123. }
  124. // 确认
  125. func (s Service) Confirm(req accountModel.AccountMainConfirmReq, user request.UserInfo) error {
  126. if req.MainId == 0 {
  127. return errors.New("参数缺失")
  128. }
  129. _, err := s.Dao.M.Update(fmt.Sprintf("SettleStatus='1',SettleUserId='%v',SettleUser='%v',SettleDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.MainId))
  130. return err
  131. }
  132. // 预约取消
  133. func (s Service) Cancel(req accountModel.AccountMainCancelReq, user request.UserInfo) error {
  134. if req.AppointId == 0 {
  135. return errors.New("参数缺失")
  136. }
  137. var main accountModel.SettleAccountMain
  138. var details []accountModel.SettleAccountDetail
  139. var rules []Param
  140. now := gtime.Now()
  141. per := float64(0)
  142. one, err := s.Dao.M.Where(fmt.Sprintf("AppointId='%v'", req.AppointId)).FindOne()
  143. if err != nil {
  144. return err
  145. }
  146. err = one.Struct(&main)
  147. if err != nil {
  148. return err
  149. }
  150. if main.Id == 0 { // 该预约未生成账单
  151. return nil
  152. }
  153. all, err := s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("pid='%v'", main.Id)).FindAll()
  154. if err != nil {
  155. return err
  156. }
  157. err = all.Structs(&details)
  158. if err != nil {
  159. return err
  160. }
  161. all, err = s.Dao.DB.Model("base_param").Where("Name LIKE '违约计费规则%'").Order("Code ASC").FindAll()
  162. if err != nil {
  163. return err
  164. }
  165. err = all.Structs(&rules)
  166. if err != nil {
  167. return err
  168. }
  169. mins := now.Sub(main.AppointStartDate).Minutes()
  170. if mins > 0 { // 正向超时,计算违约收费
  171. r := ""
  172. for _, rule := range rules {
  173. value1, _ := strconv.ParseFloat(rule.Code, 64)
  174. if mins < value1 {
  175. per, _ := strconv.ParseFloat(rule.Value, 64)
  176. per /= 100
  177. r += rule.Code + "分钟内" + rule.Value + "%"
  178. }
  179. }
  180. main.TotalPrice *= per
  181. main.ActualMachineHour = 0
  182. for index := range details {
  183. details[index].PaymentAccount = 0 // 取消,计费为0
  184. }
  185. if main.TotalPrice > 0 {
  186. var detail accountModel.SettleAccountDetail
  187. detail.PaymentType = "1"
  188. detail.PaymentAccount = main.TotalPrice
  189. detail.Data1 = main.AppointStartDate.Format("Y-m-d H:m:s")
  190. detail.Data2 = main.AppointEndDate.Format("Y-m-d H:m:s")
  191. detail.Data3 = now.Format("Y-m-d H:m:s")
  192. detail.Data4 = r
  193. details = append(details, detail)
  194. }
  195. tx, err := s.Dao.DB.Begin()
  196. if err != nil {
  197. return err
  198. }
  199. _, err = tx.Save("settle_account_main", main)
  200. if err != nil {
  201. tx.Rollback()
  202. return err
  203. }
  204. _, err = tx.Save("settle_account_detail", details)
  205. if err != nil {
  206. tx.Rollback()
  207. return err
  208. }
  209. return tx.Commit()
  210. }
  211. return nil
  212. }
  213. type Param struct {
  214. Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
  215. Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
  216. Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
  217. Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
  218. Sys string `protobuf:"bytes,5,opt,name=sys,proto3" json:"sys"`
  219. SortCode int32 `protobuf:"varint,6,opt,name=sort_code,json=sortCode,proto3" json:"sort_code"`
  220. Description string `protobuf:"bytes,7,opt,name=description,proto3" json:"description,omitempty"`
  221. CreateOn string `protobuf:"bytes,8,opt,name=create_on,json=createOn,proto3" json:"create_on,omitempty"`
  222. CreateUserId int32 `protobuf:"varint,9,opt,name=create_user_id,json=createUserId,proto3" json:"create_user_id,omitempty"`
  223. CreateBy string `protobuf:"bytes,10,opt,name=create_by,json=createBy,proto3" json:"create_by,omitempty"`
  224. ModifiedOn string `protobuf:"bytes,11,opt,name=modified_on,json=modifiedOn,proto3" json:"modified_on,omitempty"`
  225. ModifiedUserId int32 `protobuf:"varint,12,opt,name=modified_user_id,json=modifiedUserId,proto3" json:"modified_user_id,omitempty"`
  226. ModifiedBy string `protobuf:"bytes,13,opt,name=modified_by,json=modifiedBy,proto3" json:"modified_by,omitempty"`
  227. }