settle_account_bill.go 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package settle_account_bill
  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. "strconv"
  13. "strings"
  14. )
  15. // Service 会议室服务
  16. type Service struct {
  17. Dao *account.SettleAccountBillDao
  18. Tenant string
  19. }
  20. // NewSrv 服务初始化
  21. func NewService(tenant string) Service {
  22. return Service{Dao: account.NewSettleAccountBillDao(tenant), Tenant: tenant}
  23. }
  24. // List 会议室列表
  25. func (s Service) List(req model.ListReq) ([]accountModel.SettleAccountBill, int, error) {
  26. entityModel := s.Dao.M
  27. where := "1=1"
  28. if req.Entity != nil {
  29. entity := new(accountModel.SettleAccountBillReq)
  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.Status != "" {
  38. where += fmt.Sprintf(" AND Status='%v'", entity.Status)
  39. }
  40. if entity.SettleDate != "" {
  41. timelist := strings.Split(entity.SettleDate,",")
  42. if len(timelist) == 2 {
  43. where += fmt.Sprintf(" AND SettleDate>'%v' AND SettleDate<'%v'", timelist[0],timelist[1])
  44. }
  45. }
  46. if entity.SettleUser != "" {
  47. where += fmt.Sprintf(" AND SettleUser LIKE '%%%v%%'", entity.SettleUser)
  48. }
  49. if entity.MainUser != "" {
  50. where += fmt.Sprintf(" AND MainUser LIKE '%%%v%%'", entity.MainUser)
  51. }
  52. if entity.StartDate != "" && entity.EndDate != "" {
  53. where += fmt.Sprintf(" AND StartDate>'%v' AND EndDate<'%v'", entity.StartDate, entity.EndDate)
  54. }
  55. }
  56. entityModel = entityModel.Where(where)
  57. total, err := entityModel.Count()
  58. if err != nil {
  59. return nil, 0, err
  60. }
  61. if total == 0 {
  62. return nil, 0, nil
  63. }
  64. res, err := entityModel.Page(req.Current, req.Size).Order("settle_account_bill.CreateOn DESC").Fields("settle_account_bill.*").FindAll()
  65. if err != nil {
  66. return nil, 0, err
  67. }
  68. if res.IsEmpty() {
  69. return nil, 0, nil
  70. }
  71. list := make([]accountModel.SettleAccountBill, 0)
  72. err = res.Structs(&list)
  73. if err != nil {
  74. return nil, 0, err
  75. }
  76. return list, total, nil
  77. }
  78. // 结算
  79. func (s Service) Settle(req accountModel.AccountBillSettleReq, user request.UserInfo) error {
  80. if req.BillId == 0 || req.Amount == 0 {
  81. return errors.New("参数缺失")
  82. }
  83. tx, err := s.Dao.DB.Begin()
  84. if err != nil {
  85. return err
  86. }
  87. _, err = tx.Update("settle_account_bill", fmt.Sprintf("Status='2',SettleUserId='%v',SettleUser='%v',SettleDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.BillId))
  88. if err != nil {
  89. tx.Rollback()
  90. return err
  91. }
  92. _, err = tx.Update("base_account", fmt.Sprintf("Surplus=Surplus-%v", req.Amount), fmt.Sprintf("Id='%v'", req.Amount))
  93. if err != nil {
  94. tx.Rollback()
  95. return err
  96. }
  97. return tx.Commit()
  98. }
  99. // 确认
  100. func (s Service) Confirm(req accountModel.AccountBillConfirmReq, user request.UserInfo) error {
  101. if req.BillId == 0 {
  102. return errors.New("参数缺失")
  103. }
  104. _, err := s.Dao.Update(fmt.Sprintf("Status='1',VerificationUserId='%v',VerificationUser='%v',VerificationDate='%v'", user.Id, user.RealName, gtime.Now()), fmt.Sprintf("Id='%v'", req.BillId))
  105. return err
  106. }
  107. // 自动生成账单
  108. func (s Service) GenerateBill() error {
  109. var rules []Param
  110. var rule1 Param
  111. var rule2 Param
  112. var rule3 Param
  113. var mains []accountModel.SettleAccountMain
  114. //var details []accountModel.SettleAccountDetail
  115. var accounts []accountModel.BaseAccount
  116. var auto AutoConfirmRecord
  117. mainMap := make(map[int][]accountModel.SettleAccountMain, 0)
  118. //detailMap := make(map[int][]accountModel.SettleAccountDetail, 0)
  119. accountMap := make(map[int]accountModel.BaseAccount, 0)
  120. now := gtime.Now()
  121. all, err := s.Dao.DB.Model("base_param").Where("Name='已出账单生成日期' OR Name='账单明细自动确认天数' OR Name='付款截止日期参数'").FindAll()
  122. if err != nil {
  123. return err
  124. }
  125. err = all.Structs(&rules)
  126. if err != nil {
  127. if err == sql.ErrNoRows {
  128. return errors.New("参数未配置")
  129. }
  130. return err
  131. }
  132. for _, item := range rules {
  133. if item.Name == "已出账单生成日期" {
  134. rule1 = item
  135. continue
  136. }
  137. if item.Name == "账单明细自动确认天数" {
  138. rule2 = item
  139. continue
  140. }
  141. if item.Name == "付款截止日期参数" {
  142. rule3 = item
  143. continue
  144. }
  145. }
  146. if rule1.Id == 0 || rule2.Id == 0 || rule3.Id == 0 {
  147. return errors.New("参数未配置")
  148. }
  149. all, err = s.Dao.DB.Model("base_account").FindAll()
  150. if err != nil {
  151. return err
  152. }
  153. err = all.Structs(&accounts)
  154. if err != nil {
  155. return err
  156. }
  157. for _, item := range accounts {
  158. if accountMap[item.MainUserId].Id == 0 {
  159. accountMap[item.MainUserId] = item
  160. } else if accountMap[item.MainUserId].Advance > item.Advance {
  161. accountMap[item.MainUserId] = item
  162. }
  163. }
  164. pDay, _ := strconv.Atoi(rule1.Value)
  165. diffDay, _ := strconv.Atoi(rule2.Value)
  166. endDay, _ := strconv.Atoi(rule3.Value)
  167. if now.Day() != pDay {
  168. return nil
  169. }
  170. endDate := now.AddDate(0, 0, -1).Format("Y-m-d 23:59:59")
  171. startDate := now.AddDate(0, -1, 0).Format("Y-m-d 00:00:00")
  172. all, err = s.Dao.DB.Model("settle_account_main").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
  173. if err != nil {
  174. return err
  175. }
  176. err = all.Structs(&mains)
  177. if err != nil {
  178. if err == sql.ErrNoRows {
  179. return nil
  180. }
  181. return err
  182. }
  183. //all, err = s.Dao.DB.Model("settle_account_detail").Where(fmt.Sprintf("CreateOn>='%v' AND CreateOn<='%v'", startDate, endDate)).FindAll()
  184. //if err != nil {
  185. // return err
  186. //}
  187. //err = all.Structs(&details)
  188. //if err != nil {
  189. // return err
  190. //}
  191. for _, item := range mains { // 统计结算明细主表
  192. if mainMap[item.MainUserId] == nil {
  193. mainMap[item.MainUserId] = make([]accountModel.SettleAccountMain, 0)
  194. }
  195. mainMap[item.MainUserId] = append(mainMap[item.MainUserId], item)
  196. }
  197. //for _, item := range details { // 统计结算明细子表
  198. // if detailMap[item.Pid] == nil {
  199. // detailMap[item.Pid] = make([]accountModel.SettleAccountDetail, 0)
  200. // }
  201. // detailMap[item.Pid] = append(detailMap[item.Pid], item)
  202. //}
  203. tx, err := s.Dao.DB.Begin()
  204. if err != nil {
  205. return err
  206. }
  207. auto.BillIds = "-1"
  208. auto.AutoSettleDate = now.AddDate(0, 0, diffDay)
  209. for key, value := range mainMap {
  210. var bill accountModel.SettleAccountBill
  211. bill.PaymentDueDate = now.AddDate(0, 0, endDay)
  212. bill.StartDate = gtime.NewFromStr(startDate)
  213. bill.EndDate = gtime.NewFromStr(endDate)
  214. bill.MainUserId = key
  215. bill.MainUser = value[0].MainUser
  216. bill.AccountId = accountMap[bill.MainUserId].Id
  217. ids := "-1"
  218. for _, item := range value {
  219. ids += fmt.Sprintf(",%v", item.Id)
  220. bill.TotalCount += item.TotalPrice
  221. }
  222. r, err := tx.Save("settle_account_bill", bill)
  223. if err != nil {
  224. tx.Rollback()
  225. return err
  226. }
  227. id, _ := r.LastInsertId()
  228. auto.BillIds += fmt.Sprintf(",%v", id)
  229. _, err = tx.Update("settle_account_main", fmt.Sprintf("BillId='%v',Status='1'", id), fmt.Sprintf("Id IN (%v)", ids))
  230. if err != nil {
  231. tx.Rollback()
  232. return err
  233. }
  234. }
  235. _, err = tx.Save("auto_confirm_record", auto) // 自动确认辅助表
  236. if err != nil {
  237. tx.Rollback()
  238. return err
  239. }
  240. return tx.Commit()
  241. }
  242. // 自动确认
  243. func (s Service) AutoConfirm() error {
  244. now := gtime.Now()
  245. fmt.Println(now.Format("Y-m-d H:m:s"))
  246. endDate := now.Format("Y-m-d 23:59:59")
  247. startDate := now.Format("Y-m-d 00:00:00")
  248. var autos []AutoConfirmRecord
  249. all, err := s.Dao.DB.Model("auto_confirm_record").Where(fmt.Sprintf("AutoSettleDate>='%v' AND AutoSettleDate<='%v'", startDate, endDate)).FindAll()
  250. if err != nil {
  251. return err
  252. }
  253. err = all.Structs(&autos)
  254. if err != nil {
  255. if err == sql.ErrNoRows {
  256. return nil
  257. }
  258. return err
  259. }
  260. if len(autos) == 0 {
  261. return nil
  262. }
  263. tx, err := s.Dao.DB.Begin()
  264. if err != nil {
  265. return err
  266. }
  267. ids := "-1"
  268. for _, item := range autos {
  269. ids += fmt.Sprintf(",%v", item.Id)
  270. _, err = tx.Update("settle_account_main", fmt.Sprintf("SettleUserId=0,SettleUser='系统自动确认',SettleDate='%v',SettleStatus='1'", now.Format("Y-m-d H:m:s")), fmt.Sprintf("BillId IN (%v) AND (SettleUserId IS NULL OR SettleUser='')", item.BillIds))
  271. if err != nil {
  272. tx.Rollback()
  273. return err
  274. }
  275. }
  276. _, err = tx.Delete("auto_confirm_record", fmt.Sprintf("Id IN (%v)", ids))
  277. if err != nil {
  278. tx.Rollback()
  279. return err
  280. }
  281. return tx.Commit()
  282. }
  283. type Param struct {
  284. Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"`
  285. Code string `protobuf:"bytes,2,opt,name=code,proto3" json:"code,omitempty"`
  286. Name string `protobuf:"bytes,3,opt,name=name,proto3" json:"name,omitempty"`
  287. Value string `protobuf:"bytes,4,opt,name=value,proto3" json:"value,omitempty"`
  288. Sys string `protobuf:"bytes,5,opt,name=sys,proto3" json:"sys"`
  289. SortCode int32 `protobuf:"varint,6,opt,name=sort_code,json=sortCode,proto3" json:"sort_code"`
  290. Description string `protobuf:"bytes,7,opt,name=description,proto3" json:"description,omitempty"`
  291. CreateOn string `protobuf:"bytes,8,opt,name=create_on,json=createOn,proto3" json:"create_on,omitempty"`
  292. CreateUserId int32 `protobuf:"varint,9,opt,name=create_user_id,json=createUserId,proto3" json:"create_user_id,omitempty"`
  293. CreateBy string `protobuf:"bytes,10,opt,name=create_by,json=createBy,proto3" json:"create_by,omitempty"`
  294. ModifiedOn string `protobuf:"bytes,11,opt,name=modified_on,json=modifiedOn,proto3" json:"modified_on,omitempty"`
  295. ModifiedUserId int32 `protobuf:"varint,12,opt,name=modified_user_id,json=modifiedUserId,proto3" json:"modified_user_id,omitempty"`
  296. ModifiedBy string `protobuf:"bytes,13,opt,name=modified_by,json=modifiedBy,proto3" json:"modified_by,omitempty"`
  297. }
  298. // auto_confirm_record
  299. type AutoConfirmRecord struct {
  300. Id int `orm:"Id,primary" json:"id"` // 主键
  301. BillIds string `orm:"BillIds" json:"bill_ids"` // 账单Ids
  302. AutoSettleDate *gtime.Time `orm:"AutoSettleDate" json:"auto_settle_date"` // 自动确认时间
  303. }