6
0

settle_account_main.go 12 KB

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