settle_account_main.go 11 KB

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