settle_account_main.go 11 KB

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