settle_account_main.go 11 KB

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