settle_account_bill.go 10 KB

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