settle_account_main.go 10 KB

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