settle_account_main.go 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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.InstrumentName != "" {
  48. where += fmt.Sprintf(" AND InstrumentName LIKE '%%%v%%'", entity.InstrumentName)
  49. }
  50. if entity.AppointUserId != 0 {
  51. where += fmt.Sprintf(" AND AppointUserId='%v'", entity.AppointUserId)
  52. }
  53. if entity.AppointUser != "" {
  54. where += fmt.Sprintf(" AND AppointUser LIKE '%%%v%%'", entity.AppointUser)
  55. }
  56. if entity.Status != "" {
  57. where += fmt.Sprintf(" AND Status='%v'", entity.Status)
  58. }
  59. if entity.SettleStatus != "" {
  60. where += fmt.Sprintf(" AND SettleStatus='%v'", entity.SettleStatus)
  61. }
  62. if entity.SettleUser != "" {
  63. where += fmt.Sprintf(" AND SettleUser LIKE '%%%v%%'", entity.SettleUser)
  64. }
  65. if entity.FeeType != "" {
  66. where += fmt.Sprintf(" AND FeeType='%v'", entity.FeeType)
  67. }
  68. if entity.AppointStartDate != "" && entity.AppointEndDate != "" {
  69. where += fmt.Sprintf(" AND AppointStartDate>'%v' AND AppointEndDate<'%v'", entity.AppointStartDate, entity.AppointEndDate)
  70. }
  71. }
  72. entityModel = entityModel.Where(where)
  73. total, err := entityModel.Count()
  74. if err != nil {
  75. return nil, 0, err
  76. }
  77. if total == 0 {
  78. return nil, 0, nil
  79. }
  80. res, err := entityModel.Page(req.Current, req.Size).Order("settle_account_main.CreateOn DESC").Fields("settle_account_main.*").FindAll()
  81. if err != nil {
  82. return nil, 0, err
  83. }
  84. if res.IsEmpty() {
  85. return nil, 0, nil
  86. }
  87. list := make([]accountModel.SettleAccountMain, 0)
  88. err = res.Structs(&list)
  89. if err != nil {
  90. return nil, 0, err
  91. }
  92. return list, total, nil
  93. }
  94. // 新增
  95. func (s Service) Add(req accountModel.AccountMainAddReq, user request.UserInfo) error {
  96. now := gtime.Now() // 获取当前时间
  97. var baseAccount accountModel.BaseAccount
  98. // 更新必要信息
  99. req.Main.CreateUserId = int(user.Id)
  100. req.Main.CreateBy = user.RealName
  101. req.Main.CreateOn = now
  102. req.Main.UpdateUserId = int(user.Id)
  103. req.Main.UpdateBy = user.RealName
  104. req.Main.UpdateOn = now
  105. // 获取账户
  106. result1, err := s.Dao.DB.Model("base_account").Where(fmt.Sprintf("MainUserId='%v'", req.Main.MainUserId)).Order("Advance ASC").FindOne()
  107. if err != nil {
  108. return err
  109. }
  110. err = result1.Struct(&baseAccount)
  111. if err != nil {
  112. return err
  113. }
  114. baseAccount.Available -= req.Main.TotalPrice // 账户可用金额计算
  115. tx, err := s.Dao.DB.Begin()
  116. if err != nil {
  117. return err
  118. }
  119. result, err := tx.Insert("settle_account_main", req.Main)
  120. if err != nil {
  121. tx.Rollback()
  122. return err
  123. }
  124. id , _ := result.LastInsertId()
  125. for index := range req.Details { // 更新必要信息
  126. req.Details[index].CreateUserId = int(user.Id)
  127. req.Details[index].CreateBy = user.RealName
  128. req.Details[index].CreateOn = now
  129. req.Details[index].UpdateUserId = int(user.Id)
  130. req.Details[index].UpdateBy = user.RealName
  131. req.Details[index].UpdateOn = now
  132. req.Details[index].Pid = int(id)
  133. }
  134. _, err = tx.Insert("settle_account_detail", req.Details)
  135. if err != nil {
  136. tx.Rollback()
  137. return err
  138. }
  139. _, err = tx.Save("base_account", baseAccount)
  140. if err != nil {
  141. tx.Rollback()
  142. return err
  143. }
  144. return tx.Commit()
  145. }
  146. // 确认
  147. func (s Service) Confirm(req accountModel.AccountMainConfirmReq, user request.UserInfo) error {
  148. if req.MainId == 0 {
  149. return errors.New("参数缺失")
  150. }
  151. _, 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))
  152. return err
  153. }
  154. // 预约取消
  155. func (s Service) Cancel(req accountModel.AccountMainCancelReq, user request.UserInfo) error {
  156. if req.AppointId == 0 {
  157. return errors.New("参数缺失")
  158. }
  159. var main accountModel.SettleAccountMain
  160. var details []accountModel.SettleAccountDetail
  161. var baseAccount accountModel.BaseAccount
  162. var rules []Param
  163. now := gtime.Now()
  164. per := float64(0)
  165. userNumber := 0
  166. one, err := s.Dao.M.Where(fmt.Sprintf("AppointId='%v'", req.AppointId)).FindOne()
  167. if err != nil {
  168. return err
  169. }
  170. err = one.Struct(&main)
  171. if err != nil {
  172. return err
  173. }
  174. if main.Id == 0 { // 该预约未生成账单
  175. return nil
  176. }
  177. all, err := s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("pid='%v' AND PaymentType='0'", main.Id)).FindAll()
  178. if err != nil {
  179. return err
  180. }
  181. err = all.Structs(&details)
  182. if err != nil {
  183. return err
  184. }
  185. all, err = s.Dao.DB.Model("base_param").Where("Name LIKE '违约计费规则%'").Order("Code ASC").FindAll()
  186. if err != nil {
  187. return err
  188. }
  189. err = all.Structs(&rules)
  190. if err != nil {
  191. return err
  192. }
  193. // 计算当前机器占用数量
  194. userNumber, err = s.Dao.DB.Model("appointment").Where(fmt.Sprintf("RelevanceId='%v' AND SignInTime IS NOT NULL AND SignOutTime IS NULL", main.InstrumentId)).Count()
  195. if err != nil {
  196. return err
  197. }
  198. // 获取账户
  199. result1, err := s.Dao.DB.Model("base_account").Where(fmt.Sprintf("MainUserId='%v'", main.MainUserId)).Order("Advance ASC").FindOne()
  200. if err != nil {
  201. return err
  202. }
  203. err = result1.Struct(&baseAccount)
  204. if err != nil {
  205. return err
  206. }
  207. var detail accountModel.SettleAccountDetail
  208. detail.PaymentType = "1"
  209. detail.PaymentAccount = 0
  210. detail.Data1 = main.AppointStartDate.Format("Y-m-d H:m:s")
  211. detail.Data2 = main.AppointEndDate.Format("Y-m-d H:m:s")
  212. detail.Data3 = now.Format("Y-m-d H:m:s") // 取消时间
  213. mins := now.Sub(main.AppointStartDate).Minutes()
  214. if mins > 0 { // 正向超时,计算违约收费
  215. r := ""
  216. for _, rule := range rules {
  217. value1, _ := strconv.ParseFloat(rule.Code, 64)
  218. if mins < value1 {
  219. per, _ := strconv.ParseFloat(rule.Value, 64)
  220. per /= 100
  221. r += rule.Code + "分钟内" + rule.Value + "%"
  222. }
  223. }
  224. detail.Data4 = r // 扣费规则
  225. } else {
  226. detail.Data4 = "已开始之后取消,100%" // 扣费规则
  227. per = 1
  228. }
  229. if userNumber > 0 { // 机器被占用,机器被占用,取消不扣费
  230. detail.Data4 = "机器被占用,取消不扣费"
  231. per = 0
  232. }
  233. main.ActualMachineHour = 0
  234. for _, item := range details {
  235. detail.PaymentAccount += item.PaymentAccount * per
  236. }
  237. oldAmount := main.TotalPrice
  238. main.TotalPrice = detail.PaymentAccount
  239. diffValue := oldAmount - detail.PaymentAccount
  240. baseAccount.Available += diffValue // 账户可用金额返还
  241. tx, err := s.Dao.DB.Begin()
  242. if err != nil {
  243. return err
  244. }
  245. // 更新账单明细信息
  246. _, err = tx.Save("settle_account_main", main)
  247. if err != nil {
  248. tx.Rollback()
  249. return err
  250. }
  251. // 计费明细费用设置为0
  252. _, err = tx.Update("settle_account_detail", "PaymentAccount=0", fmt.Sprintf("pid='%v'", main.Id))
  253. if err != nil {
  254. tx.Rollback()
  255. return err
  256. }
  257. // 新增违约计费
  258. _, err = tx.Save("settle_account_detail", detail)
  259. if err != nil {
  260. tx.Rollback()
  261. return err
  262. }
  263. _, err = tx.Save("base_account", baseAccount)
  264. if err != nil {
  265. tx.Rollback()
  266. return err
  267. }
  268. return tx.Commit()
  269. }
  270. type Param struct {
  271. Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
  272. Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
  273. Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
  274. Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
  275. Sys string `protobuf:"bytes,5,opt,name=sys,proto3" json:"sys"`
  276. SortCode int32 `protobuf:"varint,6,opt,name=sort_code,json=sortCode,proto3" json:"sort_code"`
  277. Description string `protobuf:"bytes,7,opt,name=description,proto3" json:"description,omitempty"`
  278. CreateOn string `protobuf:"bytes,8,opt,name=create_on,json=createOn,proto3" json:"create_on,omitempty"`
  279. CreateUserId int32 `protobuf:"varint,9,opt,name=create_user_id,json=createUserId,proto3" json:"create_user_id,omitempty"`
  280. CreateBy string `protobuf:"bytes,10,opt,name=create_by,json=createBy,proto3" json:"create_by,omitempty"`
  281. ModifiedOn string `protobuf:"bytes,11,opt,name=modified_on,json=modifiedOn,proto3" json:"modified_on,omitempty"`
  282. ModifiedUserId int32 `protobuf:"varint,12,opt,name=modified_user_id,json=modifiedUserId,proto3" json:"modified_user_id,omitempty"`
  283. ModifiedBy string `protobuf:"bytes,13,opt,name=modified_by,json=modifiedBy,proto3" json:"modified_by,omitempty"`
  284. }