sign_up.go 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. package upcrmservice
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "sort"
  7. "strings"
  8. "time"
  9. "go-common/app/admin/main/up/dao/global"
  10. "go-common/app/admin/main/up/model"
  11. "go-common/app/admin/main/up/model/signmodel"
  12. "go-common/app/admin/main/up/model/upcrmmodel"
  13. "go-common/app/admin/main/up/service/cache"
  14. "go-common/app/admin/main/up/util"
  15. accgrpc "go-common/app/service/main/account/api"
  16. "go-common/library/log"
  17. "go-common/library/net/http/blademaster"
  18. "go-common/library/net/metadata"
  19. xtime "go-common/library/time"
  20. "github.com/jinzhu/gorm"
  21. )
  22. // SignUpAuditLogs .
  23. func (s *Service) SignUpAuditLogs(c context.Context, arg *signmodel.SignOpSearchArg) (res *signmodel.SignAuditListReply, err error) {
  24. return s.mng.SignUpAuditLogs(c, arg)
  25. }
  26. // SignAdd add sign info
  27. func (s *Service) SignAdd(context context.Context, arg *signmodel.SignUpArg) (result signmodel.CommonResponse, err error) {
  28. if arg == nil {
  29. log.Error("sign add arg is nil")
  30. return
  31. }
  32. // 处理合同信息
  33. var contractInfo []*signmodel.SignContractInfoArg
  34. for _, v := range arg.ContractInfo {
  35. if v == nil || strings.Trim(v.Filename, " ") == "" {
  36. continue
  37. }
  38. if strings.Trim(v.Filelink, " \n\r") == "" {
  39. err = model.ErrNoFileLink
  40. log.Error("no file link for contract, please upload file, arg=%v", arg)
  41. return
  42. }
  43. contractInfo = append(contractInfo, v)
  44. }
  45. arg.ContractInfo = contractInfo
  46. // 从context 里拿后台登录信息
  47. var bmContext, ok = context.(*blademaster.Context)
  48. if ok {
  49. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  50. if ok {
  51. arg.AdminID = int(uid)
  52. }
  53. name, ok := util.GetContextValueString(bmContext, "username")
  54. if ok {
  55. arg.AdminName = name
  56. }
  57. }
  58. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  59. // 事物
  60. tx := s.crmdb.BeginTran(context)
  61. defer func() {
  62. if err != nil {
  63. tx.Rollback()
  64. } else {
  65. tx.Commit()
  66. }
  67. }()
  68. // 获取up主基础信息中的分区id
  69. upInfo, _ := s.crmdb.QueryUpBaseInfo(arg.Mid, "active_tid")
  70. // 1.先去sign up里插入一条记录,然后获得对应的id
  71. var dbSignUp signmodel.SignUp
  72. // 把请求信息制作签约up的db内容
  73. arg.CopyTo(&dbSignUp)
  74. if upInfo.ActiveTid != 0 {
  75. // 把up主的分区id赋值到签约up主信息
  76. dbSignUp.ActiveTid = int16(upInfo.ActiveTid)
  77. }
  78. _, e := s.crmdb.InsertSignUp(tx, &dbSignUp)
  79. err = e
  80. if err != nil {
  81. log.Error("fail to add into sign up db, req=%+v, err=%+v", arg, err)
  82. return
  83. }
  84. log.Info("add sign up ok, new id=%d, next add other info", dbSignUp.ID)
  85. // 2.把id写入pay/task/contract的sign_id字段中,然后分别将这三种信息插入到数据库
  86. for _, v := range arg.PayInfo {
  87. v.SignID = dbSignUp.ID
  88. v.Mid = dbSignUp.Mid
  89. if _, err = s.addPayInfo(tx, v); err != nil {
  90. log.Error("insert payinfo db fail, err=%+v", err)
  91. break
  92. }
  93. }
  94. for _, v := range arg.TaskInfo {
  95. v.SignID = dbSignUp.ID
  96. v.Mid = dbSignUp.Mid
  97. if _, err = s.addTaskInfo(tx, v); err != nil {
  98. log.Error("insert payinfo db fail, err=%+v", err)
  99. break
  100. }
  101. }
  102. for _, v := range arg.ContractInfo {
  103. v.SignID = dbSignUp.ID
  104. v.Mid = dbSignUp.Mid
  105. if _, err = s.addContractInfo(tx, v); err != nil {
  106. log.Error("insert payinfo db fail, err=%+v", err)
  107. break
  108. }
  109. }
  110. log.Info("add sign up, new id=%d, all info finish", dbSignUp.ID)
  111. index := []interface{}{}
  112. content := map[string]interface{}{
  113. "new": arg,
  114. "old": nil,
  115. "change_type": new([]int8),
  116. }
  117. // 上报添加的签约日志
  118. s.AddAuditLog(signmodel.SignUpLogBizID, signmodel.SignUpMidAdd, "新增", int64(arg.AdminID), arg.AdminName, []int64{int64(arg.Mid)}, index, content)
  119. return
  120. }
  121. // SignUpdate .
  122. func (s *Service) SignUpdate(context context.Context, arg *signmodel.SignUpArg) (result signmodel.CommonResponse, err error) {
  123. if arg == nil || arg.ID == 0 {
  124. log.Error("sign up arg is nil")
  125. return
  126. }
  127. // 处理合同信息
  128. var contractInfo []*signmodel.SignContractInfoArg
  129. for _, v := range arg.ContractInfo {
  130. if v == nil || strings.Trim(v.Filename, " ") == "" {
  131. continue
  132. }
  133. if strings.Trim(v.Filelink, " \n\r") == "" {
  134. err = model.ErrNoFileLink
  135. log.Error("no file link for contract, please upload file, arg=%v", arg)
  136. return
  137. }
  138. contractInfo = append(contractInfo, v)
  139. }
  140. arg.ContractInfo = contractInfo
  141. // 从context 里拿后台登录信息
  142. var bmContext, ok = context.(*blademaster.Context)
  143. if ok {
  144. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  145. if ok {
  146. arg.AdminID = int(uid)
  147. }
  148. name, ok := util.GetContextValueString(bmContext, "username")
  149. if ok {
  150. arg.AdminName = name
  151. }
  152. }
  153. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  154. // 预处理声明参数
  155. var (
  156. oriSignUp *signmodel.SignUp
  157. oriSignPayMap map[int64]*signmodel.SignPay
  158. oriSignTaskMap map[int64]*signmodel.SignTask
  159. oriSignContractMap map[int64]*signmodel.SignContract
  160. signPayIDMap, signTaskIDMap, signContractIDMap map[int64]struct{}
  161. delSignPayIDs, delSignTaskIDs, delSignContractIDs []int64
  162. changeType []int8
  163. signPays []*signmodel.SignPay
  164. signTasks []*signmodel.SignTask
  165. signContracts []*signmodel.SignContract
  166. fields = make(map[int8]struct{})
  167. signUp = &signmodel.SignUp{}
  168. oriSignUpArg = new(signmodel.SignUpArg)
  169. )
  170. // 从db获取签约信息 、付款信息、任务信息、合同信息
  171. if oriSignUp, oriSignPayMap, oriSignTaskMap, oriSignContractMap, err = s.crmdb.SignUpID(arg.ID); err != nil {
  172. log.Error("s.crmdb.SignUpID(%+d) error(%+v)", arg.ID, err)
  173. return
  174. }
  175. if oriSignUp.State == 1 || oriSignUp.State == 100 {
  176. err = fmt.Errorf("up签约已过期或者已被删除")
  177. return
  178. }
  179. // 把请求参数制作成签约up的db
  180. arg.CopyTo(signUp)
  181. // 请求的签约up信息和db的签约up信息做diff, fields包含了修改哪些信息
  182. signUp.Diff(oriSignUp, fields)
  183. // 把db内的签约信息制作成请求参数类型数据, 放入上报日志需要
  184. oriSignUpArg.SignUpBaseInfo.CopyFrom(oriSignUp)
  185. // 需要更新的付款信息的db id
  186. signPayIDMap = map[int64]struct{}{}
  187. // 对比请求的付款信息,diff 出变更的数据
  188. for _, v := range arg.PayInfo {
  189. var sp = &signmodel.SignPay{}
  190. v.CopyTo(sp)
  191. sp.Diff(oriSignPayMap, fields)
  192. sp.SignID = arg.ID
  193. sp.Mid = arg.Mid
  194. v.SignID = arg.ID
  195. v.Mid = arg.Mid
  196. signPays = append(signPays, sp)
  197. signPayIDMap[v.ID] = struct{}{}
  198. }
  199. // 把db内付款信息制作成上报日志需要结构
  200. for _, v := range oriSignPayMap {
  201. var pi = &signmodel.SignPayInfoArg{}
  202. pi.CopyFrom(v)
  203. pi.SignID = arg.ID
  204. pi.Mid = arg.Mid
  205. oriSignUpArg.PayInfo = append(oriSignUpArg.PayInfo, pi)
  206. if _, ok := signPayIDMap[v.ID]; !ok && v.ID != 0 {
  207. delSignPayIDs = append(delSignPayIDs, int64(v.ID))
  208. }
  209. }
  210. // 付款信息是否存在删除
  211. if len(delSignPayIDs) > 0 {
  212. fields[signmodel.ChangeSignPayHistory] = struct{}{}
  213. }
  214. // 需要更新的任务的db id
  215. signTaskIDMap = map[int64]struct{}{}
  216. // 对比请求的任务信息,diff 出变更的数据
  217. for _, v := range arg.TaskInfo {
  218. var st = &signmodel.SignTask{SignID: arg.ID, Mid: arg.Mid}
  219. v.CopyTo(st)
  220. st.Diff(oriSignTaskMap, fields)
  221. st.SignID = arg.ID
  222. st.Mid = arg.Mid
  223. v.SignID = arg.ID
  224. v.Mid = arg.Mid
  225. signTasks = append(signTasks, st)
  226. signTaskIDMap[v.ID] = struct{}{}
  227. }
  228. // 把db内任务信息制作成上报日志需要结构
  229. for _, v := range oriSignTaskMap {
  230. var ti = &signmodel.SignTaskInfoArg{}
  231. ti.CopyFrom(v)
  232. ti.SignID = arg.ID
  233. ti.Mid = arg.Mid
  234. oriSignUpArg.TaskInfo = append(oriSignUpArg.TaskInfo, ti)
  235. if _, ok := signTaskIDMap[v.ID]; !ok && v.ID != 0 {
  236. delSignTaskIDs = append(delSignTaskIDs, int64(v.ID))
  237. }
  238. }
  239. // 任务是否存在删除
  240. if len(delSignTaskIDs) > 0 {
  241. fields[signmodel.ChangeSignTaskHistory] = struct{}{}
  242. }
  243. // 需要更新的合同的db id
  244. signContractIDMap = map[int64]struct{}{}
  245. // 对比请求的合同信息,diff 出变更的数据
  246. for _, v := range arg.ContractInfo {
  247. var sc = &signmodel.SignContract{SignID: arg.ID, Mid: arg.Mid}
  248. v.CopyTo(sc)
  249. sc.Diff(oriSignContractMap, fields)
  250. sc.SignID = arg.ID
  251. sc.Mid = arg.Mid
  252. v.SignID = arg.ID
  253. v.Mid = arg.Mid
  254. signContracts = append(signContracts, sc)
  255. signContractIDMap[v.ID] = struct{}{}
  256. }
  257. // 把db内合同信息制作成上报日志需要结构
  258. for _, v := range oriSignContractMap {
  259. var si = &signmodel.SignContractInfoArg{}
  260. si.CopyFrom(v)
  261. si.SignID = arg.ID
  262. si.Mid = arg.Mid
  263. oriSignUpArg.ContractInfo = append(oriSignUpArg.ContractInfo, si)
  264. if _, ok := signContractIDMap[v.ID]; !ok && v.ID != 0 {
  265. delSignContractIDs = append(delSignContractIDs, int64(v.ID))
  266. }
  267. }
  268. // 合同是否存在删除
  269. if len(delSignContractIDs) > 0 {
  270. fields[signmodel.ChangeSignContractHistory] = struct{}{}
  271. }
  272. for k := range fields {
  273. changeType = append(changeType, k)
  274. }
  275. if len(changeType) == 0 {
  276. err = fmt.Errorf("up签约信息暂无修改")
  277. return
  278. }
  279. tx := s.crmdb.BeginTran(context)
  280. defer func() {
  281. if err != nil {
  282. tx.Rollback()
  283. } else {
  284. tx.Commit()
  285. }
  286. }()
  287. if _, err = s.crmdb.InsertSignUp(tx, signUp); err != nil {
  288. log.Error("fail to add into sign up db, req=%+v, err=%+v", arg, err)
  289. return
  290. }
  291. for _, v := range signPays {
  292. if _, err = s.crmdb.InsertPayInfo(tx, v); err != nil {
  293. log.Error("insert pay info db fail, err=%+v", err)
  294. break
  295. }
  296. }
  297. for _, v := range signTasks {
  298. if _, err = s.crmdb.InsertTaskInfo(tx, v); err != nil {
  299. log.Error("insert task info db fail, err=%+v", err)
  300. break
  301. }
  302. var (
  303. init bool
  304. sth *signmodel.SignTaskHistory
  305. )
  306. if sth, init, err = s.crmdb.GetOrCreateTaskHistory(tx, v); err != nil {
  307. log.Error("s.crmdb.GetOrCreateTaskHistory, err=%+v", err)
  308. break
  309. }
  310. if !init {
  311. sth.Attribute = v.Attribute
  312. sth.TaskCondition = v.TaskCondition
  313. sth.TaskType = v.TaskType
  314. if err = s.crmdb.UpSignTaskHistory(tx, sth); err != nil {
  315. log.Error("s.crmdb.UpSignTaskHistory, err=%+v", err)
  316. break
  317. }
  318. }
  319. }
  320. for _, v := range signContracts {
  321. if _, err = s.crmdb.InsertContractInfo(tx, v); err != nil {
  322. log.Error("insert contract info db fail, err=%+v", err)
  323. break
  324. }
  325. }
  326. if _, err = s.crmdb.DelPayInfo(tx, delSignPayIDs); err != nil {
  327. log.Error("delete task info db fail, err=%+v", err)
  328. return
  329. }
  330. if _, err = s.crmdb.DelTaskInfo(tx, delSignTaskIDs); err != nil {
  331. log.Error("delete task info db fail, err=%+v", err)
  332. return
  333. }
  334. if _, err = s.crmdb.DelSignContract(tx, delSignContractIDs); err != nil {
  335. log.Error("delete task info db fail, err=%+v", err)
  336. return
  337. }
  338. index := []interface{}{int64(arg.ID)}
  339. content := map[string]interface{}{
  340. "new": arg,
  341. "old": oriSignUpArg,
  342. "change_type": changeType,
  343. }
  344. // 上报变更信息
  345. s.AddAuditLog(signmodel.SignUpLogBizID, signmodel.SignUpMidUpdate, "修改", int64(arg.AdminID), arg.AdminName, []int64{int64(arg.Mid)}, index, content)
  346. return
  347. }
  348. // ViolationAdd .
  349. func (s *Service) ViolationAdd(context context.Context, arg *signmodel.ViolationArg) (result signmodel.CommonResponse, err error) {
  350. if arg == nil || arg.SignID == 0 {
  351. log.Error("violation add arg is nil")
  352. return
  353. }
  354. su := &signmodel.SignUp{}
  355. if err = s.crmdb.GetDb().Table(signmodel.TableSignUp).Where("id = ? AND state = 0", arg.SignID).Find(su).Error; err != nil && err != gorm.ErrRecordNotFound {
  356. log.Error("db fail, err=%+v", err)
  357. return
  358. }
  359. if err == gorm.ErrRecordNotFound {
  360. err = fmt.Errorf("签约ID(%d)不存在", arg.SignID)
  361. return
  362. }
  363. var bmContext, ok = context.(*blademaster.Context)
  364. if ok {
  365. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  366. if ok {
  367. arg.AdminID = uid
  368. }
  369. name, ok := util.GetContextValueString(bmContext, "username")
  370. if ok {
  371. arg.AdminName = name
  372. }
  373. }
  374. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  375. tx := s.crmdb.BeginTran(context)
  376. defer func() {
  377. if err != nil {
  378. tx.Rollback()
  379. } else {
  380. tx.Commit()
  381. }
  382. }()
  383. // 新增违约历史
  384. vh := &signmodel.SignViolationHistory{}
  385. arg.CopyTo(vh)
  386. if err = tx.Save(vh).Error; err != nil {
  387. log.Error("sign violation up fail, err=%+v", err)
  388. return
  389. }
  390. // 新增违约次数
  391. if err = tx.Table(signmodel.TableSignUp).Where("id = ?", arg.SignID).UpdateColumns(
  392. map[string]interface{}{
  393. "violation_times": gorm.Expr("violation_times + ?", 1),
  394. "admin_id": arg.AdminID,
  395. "admin_name": arg.AdminName,
  396. }).Error; err != nil {
  397. log.Error("sign up add violation times fail, err=%+v", err)
  398. }
  399. return
  400. }
  401. // ViolationRetract .
  402. func (s *Service) ViolationRetract(context context.Context, arg *signmodel.IDArg) (result signmodel.CommonResponse, err error) {
  403. var bmContext, ok = context.(*blademaster.Context)
  404. if ok {
  405. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  406. if ok {
  407. arg.AdminID = uid
  408. }
  409. name, ok := util.GetContextValueString(bmContext, "username")
  410. if ok {
  411. arg.AdminName = name
  412. }
  413. }
  414. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  415. tx := s.crmdb.BeginTran(context)
  416. defer func() {
  417. if err != nil {
  418. tx.Rollback()
  419. } else {
  420. tx.Commit()
  421. }
  422. }()
  423. // 删除违约历史
  424. if err = tx.Table(signmodel.TableSignViolationHistory).Where("id = ?", arg.ID).UpdateColumns(
  425. map[string]interface{}{
  426. "state": 100,
  427. "admin_id": arg.AdminID,
  428. "admin_name": arg.AdminName,
  429. }).Error; err != nil {
  430. log.Error("sign violation Retract fail, err=%+v", err)
  431. }
  432. // 减少违约次数
  433. if err = tx.Table(signmodel.TableSignUp).Where("id = ?", arg.SignID).UpdateColumns(
  434. map[string]interface{}{
  435. "violation_times": gorm.Expr("violation_times - ?", 1),
  436. "admin_id": arg.AdminID,
  437. "admin_name": arg.AdminName,
  438. }).Error; err != nil {
  439. log.Error("sign up dec violation times fail, err=%+v", err)
  440. }
  441. return
  442. }
  443. // ViolationList .
  444. func (s *Service) ViolationList(context context.Context, arg *signmodel.PageArg) (result *signmodel.ViolationResult, err error) {
  445. if arg == nil {
  446. log.Error("arg is nil")
  447. return
  448. }
  449. if arg.Page <= 0 {
  450. arg.Page = 1
  451. }
  452. if arg.Size <= 0 || arg.Size >= 50 {
  453. arg.Size = 20
  454. }
  455. result = new(signmodel.ViolationResult)
  456. result.Result = []*signmodel.ViolationArg{}
  457. result.Page = arg.Page
  458. result.Size = arg.Size
  459. var (
  460. count int
  461. offset = (arg.Page - 1) * arg.Size
  462. vhs = []*signmodel.SignViolationHistory{}
  463. )
  464. if err = s.crmdb.GetDb().Table(signmodel.TableSignViolationHistory).Where("sign_id = ?", arg.SignID).Count(&count).Error; err != nil {
  465. log.Error("violation count fail, err=%+v", err)
  466. return
  467. }
  468. if count <= 0 {
  469. return
  470. }
  471. result.TotalCount = count
  472. if err = s.crmdb.GetDb().Table(signmodel.TableSignViolationHistory).Where("sign_id = ?", arg.SignID).Order(fmt.Sprintf("%s %s", "mtime", "DESC")).
  473. Offset(offset).
  474. Limit(arg.Size).
  475. Find(&vhs).Error; err != nil && err != gorm.ErrRecordNotFound {
  476. log.Error("violationList fail, err=%+v", err)
  477. }
  478. for _, v := range vhs {
  479. var re = &signmodel.ViolationArg{}
  480. re.CopyFrom(v)
  481. result.Result = append(result.Result, re)
  482. }
  483. return
  484. }
  485. // AbsenceAdd .
  486. func (s *Service) AbsenceAdd(context context.Context, arg *signmodel.AbsenceArg) (result signmodel.CommonResponse, err error) {
  487. if arg == nil || arg.SignID == 0 {
  488. log.Error("violation add arg is nil")
  489. return
  490. }
  491. su := &signmodel.SignUp{}
  492. if err = s.crmdb.GetDb().Table(signmodel.TableSignUp).Where("id = ? AND state = 0", arg.SignID).Find(su).Error; err != nil && err != gorm.ErrRecordNotFound {
  493. log.Error("db fail, err=%+v", err)
  494. return
  495. }
  496. if err == gorm.ErrRecordNotFound {
  497. err = fmt.Errorf("签约ID(%d)不存在", arg.SignID)
  498. return
  499. }
  500. var Result struct {
  501. LeaveTimes int
  502. }
  503. if err = s.crmdb.GetDb().Table(signmodel.TableSignTaskAbsence).Select("SUM(absence_count) as leave_times").Where("sign_id = ? AND state = 0", arg.SignID).Scan(&Result).Error; err != nil {
  504. log.Error("sign task absence sum fail, err=%+v", err)
  505. return
  506. }
  507. var bmContext, ok = context.(*blademaster.Context)
  508. if ok {
  509. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  510. if ok {
  511. arg.AdminID = uid
  512. }
  513. name, ok := util.GetContextValueString(bmContext, "username")
  514. if ok {
  515. arg.AdminName = name
  516. }
  517. }
  518. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  519. tx := s.crmdb.BeginTran(context)
  520. defer func() {
  521. if err != nil {
  522. tx.Rollback()
  523. } else {
  524. tx.Commit()
  525. }
  526. }()
  527. var sth *signmodel.SignTaskHistory
  528. // 查询签约周期任务历史数据,没有新增
  529. if sth, err = s.getOrCreateTaskHistory(tx, arg.SignID); err != nil {
  530. log.Error("s.getOrCreateTaskHistory(%d), err=%+v", arg.SignID, err)
  531. return
  532. }
  533. // 增加请假历史
  534. ta := &signmodel.SignTaskAbsence{}
  535. arg.CopyTo(ta)
  536. ta.TaskHistoryID = sth.ID
  537. if err = tx.Save(ta).Error; err != nil {
  538. log.Error("sign task absence fail, err=%+v", err)
  539. return
  540. }
  541. // 新增请假历史
  542. if err = tx.Table(signmodel.TableSignUp).Where("id = ?", arg.SignID).UpdateColumns(
  543. map[string]interface{}{
  544. "leave_times": Result.LeaveTimes + arg.AbsenceCount,
  545. "admin_id": arg.AdminID,
  546. "admin_name": arg.AdminName,
  547. }).Error; err != nil {
  548. log.Error("sign up add leave time fail, err=%+v", err)
  549. }
  550. return
  551. }
  552. // get task history, if not exist, then will create it
  553. func (s *Service) getOrCreateTaskHistory(tx *gorm.DB, signID int64) (res *signmodel.SignTaskHistory, err error) {
  554. st := new(signmodel.SignTask)
  555. if err = s.crmdb.GetDb().Select("*").Where("sign_id = ?", signID).Find(&st).Error; err != nil {
  556. return
  557. }
  558. if res, _, err = s.crmdb.GetOrCreateTaskHistory(tx, st); err != nil {
  559. log.Error("s.crmdb.GetOrCreateTaskHistory, err=%+v", err)
  560. }
  561. return
  562. }
  563. // AbsenceRetract .
  564. func (s *Service) AbsenceRetract(context context.Context, arg *signmodel.IDArg) (result signmodel.CommonResponse, err error) {
  565. var bmContext, ok = context.(*blademaster.Context)
  566. if ok {
  567. uid, ok := util.GetContextValueInt64(bmContext, "uid")
  568. if ok {
  569. arg.AdminID = uid
  570. }
  571. name, ok := util.GetContextValueString(bmContext, "username")
  572. if ok {
  573. arg.AdminName = name
  574. }
  575. }
  576. log.Info("add sign up, req=%+v, admin id=%d, admin name=%s", arg, arg.AdminID, arg.AdminName)
  577. tx := s.crmdb.BeginTran(context)
  578. defer func() {
  579. if err != nil {
  580. tx.Rollback()
  581. } else {
  582. tx.Commit()
  583. }
  584. }()
  585. signTaskAbsences := &signmodel.SignTaskAbsence{}
  586. if err = s.crmdb.GetDb().Table(signmodel.TableSignTaskAbsence).Select("absence_count").Where("id = ? AND state = 0", arg.ID).Find(&signTaskAbsences).Error; err != nil && err != gorm.ErrRecordNotFound {
  587. log.Error("sign task absence_count fail, err=%+v", err)
  588. return
  589. }
  590. if err == gorm.ErrRecordNotFound {
  591. err = fmt.Errorf("签约ID(%d)的请假ID(%d)不存在", arg.SignID, arg.ID)
  592. return
  593. }
  594. // 删除请假历史
  595. if err = tx.Table(signmodel.TableSignTaskAbsence).Where("id = ?", arg.ID).UpdateColumns(
  596. map[string]interface{}{
  597. "state": 100,
  598. "admin_id": arg.AdminID,
  599. "admin_name": arg.AdminName,
  600. }).Error; err != nil {
  601. log.Error("task absence Retract fail, err=%+v", err)
  602. }
  603. // 减少请假次数
  604. if err = tx.Table(signmodel.TableSignUp).Where("id = ?", arg.SignID).UpdateColumns(
  605. map[string]interface{}{
  606. "leave_times": gorm.Expr("leave_times - ?", signTaskAbsences.AbsenceCount),
  607. "admin_id": arg.AdminID,
  608. "admin_name": arg.AdminName,
  609. }).Error; err != nil {
  610. log.Error("sign up dec leave times fail, err=%+v", err)
  611. }
  612. return
  613. }
  614. // AbsenceList .
  615. func (s *Service) AbsenceList(context context.Context, arg *signmodel.PageArg) (result *signmodel.AbsenceResult, err error) {
  616. if arg == nil {
  617. log.Error("arg is nil")
  618. return
  619. }
  620. if arg.Page <= 0 {
  621. arg.Page = 1
  622. }
  623. if arg.Size <= 0 || arg.Size >= 50 {
  624. arg.Size = 20
  625. }
  626. result = new(signmodel.AbsenceResult)
  627. result.Result = []*signmodel.AbsenceArg{}
  628. result.Page = arg.Page
  629. result.Size = arg.Size
  630. var (
  631. count int
  632. taskHisIDs []int64
  633. mst = make(map[int64]*signmodel.SignTaskHistory)
  634. offset = (arg.Page - 1) * arg.Size
  635. tas = []*signmodel.SignTaskAbsence{}
  636. sts = []*signmodel.SignTaskHistory{}
  637. su = &signmodel.SignUp{}
  638. )
  639. if err = s.crmdb.GetDb().Table(signmodel.TableSignUp).Where("id = ? AND state IN (0,1)", arg.SignID).Find(su).Error; err != nil && err != gorm.ErrRecordNotFound {
  640. log.Error("db fail, err=%+v", err)
  641. return
  642. }
  643. if err == gorm.ErrRecordNotFound {
  644. err = fmt.Errorf("up签约不存在")
  645. return
  646. }
  647. if err = s.crmdb.GetDb().Table(signmodel.TableSignTaskAbsence).Where("sign_id = ?", arg.SignID).Count(&count).Error; err != nil {
  648. log.Error("sign task absence count fail, err=%+v", err)
  649. return
  650. }
  651. if count <= 0 {
  652. return
  653. }
  654. result.TotalCount = count
  655. if err = s.crmdb.GetDb().Table(signmodel.TableSignTaskAbsence).Where("sign_id = ?", arg.SignID).Order(fmt.Sprintf("%s %s", "mtime", "DESC")).
  656. Offset(offset).
  657. Limit(arg.Size).
  658. Find(&tas).Error; err != nil && err != gorm.ErrRecordNotFound {
  659. log.Error("absenceList fail, err=%+v", err)
  660. }
  661. for _, v := range tas {
  662. taskHisIDs = append(taskHisIDs, v.TaskHistoryID)
  663. }
  664. if err = s.crmdb.GetDb().Table(signmodel.TableSignTaskHistory).Where("id IN (?) AND sign_id = ?", taskHisIDs, arg.SignID).Find(&sts).Error; err != nil && err != gorm.ErrRecordNotFound {
  665. log.Error("sign task history fail, err=%+v", err)
  666. return
  667. }
  668. for _, v := range sts {
  669. mst[v.ID] = v
  670. }
  671. for _, v := range tas {
  672. var (
  673. ok bool
  674. sDate, eDate time.Time
  675. st *signmodel.SignTaskHistory
  676. re = &signmodel.AbsenceArg{}
  677. )
  678. // 从任务历史里面获取签约周期
  679. if st, ok = mst[int64(v.TaskHistoryID)]; ok {
  680. if st.TaskType != signmodel.TaskTypeAccumulate {
  681. sDate, eDate = signmodel.GetTaskDuration(st.GenerateDate.Time(), st.TaskType)
  682. re.TaskBegin = xtime.Time(sDate.Unix())
  683. re.TaskEnd = xtime.Time(eDate.Unix())
  684. } else {
  685. re.TaskBegin = su.BeginDate
  686. re.TaskEnd = su.EndDate
  687. }
  688. }
  689. re.CopyFrom(v)
  690. result.Result = append(result.Result, re)
  691. }
  692. return
  693. }
  694. // ViewCheck .
  695. func (s *Service) ViewCheck(context context.Context, arg *signmodel.PowerCheckArg) (res *signmodel.PowerCheckReply, err error) {
  696. res = &signmodel.PowerCheckReply{}
  697. if arg == nil || arg.Mid == 0 {
  698. log.Error("view arg is nil")
  699. return
  700. }
  701. var count int64
  702. if err = s.crmdb.GetDb().Table(signmodel.TableSignUp).Where("mid = ?", arg.Mid).Count(&count).Error; err != nil {
  703. log.Error("db fail, err=%+v", err)
  704. return
  705. }
  706. if count > 0 {
  707. res.IsSign = true
  708. }
  709. var baseInfo upcrmmodel.UpBaseInfo
  710. if baseInfo, err = s.crmdb.QueryUpBaseInfo(arg.Mid, "active_tid"); err != nil && err != gorm.ErrRecordNotFound {
  711. log.Error("s.crmdb.QueryUpBaseInfo(%d), err=%+v", arg.Mid, err)
  712. return
  713. }
  714. if err == gorm.ErrRecordNotFound {
  715. err = nil
  716. return
  717. }
  718. for _, tid := range arg.TIDs {
  719. if tid == int16(baseInfo.ActiveTid) {
  720. res.IsPower = true
  721. return
  722. }
  723. }
  724. return
  725. }
  726. /*
  727. id, 最后插入的id
  728. */
  729. func (s *Service) addPayInfo(tx *gorm.DB, arg *signmodel.SignPayInfoArg) (id int64, err error) {
  730. if arg == nil {
  731. err = errors.New("add pay info nil pointer")
  732. return
  733. }
  734. var dbInfo signmodel.SignPay
  735. arg.CopyTo(&dbInfo)
  736. var _, e = s.crmdb.InsertPayInfo(tx, &dbInfo)
  737. err = e
  738. id = dbInfo.ID
  739. return
  740. }
  741. func (s *Service) addTaskInfo(tx *gorm.DB, arg *signmodel.SignTaskInfoArg) (id int64, err error) {
  742. if arg == nil {
  743. err = errors.New("add task info nil pointer")
  744. return
  745. }
  746. var dbInfo signmodel.SignTask
  747. arg.CopyTo(&dbInfo)
  748. if _, err = s.crmdb.InsertTaskInfo(tx, &dbInfo); err != nil {
  749. log.Error("s.crmdb.InsertTaskInfo(%+v) error(%+v)", &dbInfo, err)
  750. return
  751. }
  752. id = dbInfo.ID
  753. if _, _, err = s.crmdb.GetOrCreateTaskHistory(tx, &dbInfo); err != nil {
  754. log.Error("s.crmdb.GetOrCreateTaskHistory(%+v) error(%+v)", &dbInfo, err)
  755. }
  756. return
  757. }
  758. func (s *Service) addContractInfo(tx *gorm.DB, arg *signmodel.SignContractInfoArg) (id int64, err error) {
  759. if arg == nil {
  760. err = errors.New("add contract info nil pointer")
  761. return
  762. }
  763. var dbInfo signmodel.SignContract
  764. arg.CopyTo(&dbInfo)
  765. var _, e = s.crmdb.InsertContractInfo(tx, &dbInfo)
  766. err = e
  767. id = dbInfo.ID
  768. return
  769. }
  770. // type sortPayFunc func(p1, p2 *signmodel.SignPayInfoArg) bool
  771. // type paySorter struct {
  772. // datas []*signmodel.SignPayInfoArg
  773. // by sortPayFunc // Closure used in the Less method.
  774. // }
  775. // Len is part of sort.Interface.
  776. // func (s *paySorter) Len() int {
  777. // return len(s.datas)
  778. // }
  779. // // Swap is part of sort.Interface.
  780. // func (s *paySorter) Swap(i, j int) {
  781. // s.datas[i], s.datas[j] = s.datas[j], s.datas[i]
  782. // }
  783. // // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
  784. // func (s *paySorter) Less(i, j int) bool {
  785. // return s.by(s.datas[i], s.datas[j])
  786. // }
  787. // func sortPayInfo(planets []*signmodel.SignPayInfoArg, sortfunc sortPayFunc) {
  788. // ps := &paySorter{
  789. // datas: planets,
  790. // by: sortfunc, // The Sort method's receiver is the function (closure) that defines the sort order.
  791. // }
  792. // sort.Sort(ps)
  793. // }
  794. // func sortByDueAsc(p1, p2 *signmodel.SignPayInfoArg) bool {
  795. // var v1, _ = time.Parse(upcrmmodel.TimeFmtDate, p1.DueDate)
  796. // var v2, _ = time.Parse(upcrmmodel.TimeFmtDate, p2.DueDate)
  797. // return v1.Before(v2)
  798. // }
  799. //SignQuery sign query
  800. func (s *Service) SignQuery(c context.Context, arg *signmodel.SignQueryArg) (res *signmodel.SignQueryResult, err error) {
  801. if arg == nil {
  802. log.Error("arg is nil")
  803. return
  804. }
  805. if arg.Page <= 0 {
  806. arg.Page = 1
  807. }
  808. if arg.IsDetail == signmodel.SignUpDetail {
  809. arg.Size = 5
  810. } else {
  811. if arg.Size <= 0 || arg.Size > 20 {
  812. arg.Size = 20
  813. }
  814. }
  815. var (
  816. count int
  817. mids []int64
  818. tids []int64
  819. signIDs []int64
  820. signTaskIDs []int64
  821. signTaskHistoryIDs []int64
  822. tpNames map[int64]string
  823. db = s.crmdb.GetDb()
  824. signUpHandle = db.Table(signmodel.TableSignUp)
  825. signPayHandle = db.Table(signmodel.TableSignPay)
  826. signContractHandle = db.Table(signmodel.TableSignContract)
  827. signTaskHistoryHandle = db.Table(signmodel.TableSignTaskHistory)
  828. signAbsenceHandle = db.Table(signmodel.TableSignTaskAbsence)
  829. signTaskHandle = db.Table(signmodel.TableSignTask)
  830. signUps = []*signmodel.SignUp{}
  831. signPayInfos = []*signmodel.SignPay{}
  832. signContractInfos = []*signmodel.SignContract{}
  833. signTaskHistorys = []*signmodel.SignTaskHistory{}
  834. signTaskAbsences = []*signmodel.SignTaskAbsence{}
  835. signTasks = []*signmodel.SignTask{}
  836. signUpBaseInfos = []*signmodel.SignUpBaseInfo{}
  837. signPayInfoMap = make(map[int64][]*signmodel.SignPayInfoArg)
  838. signContractInfoMap = make(map[int64][]*signmodel.SignContractInfoArg)
  839. signTaskHistoryMap = make(map[int64][]*signmodel.SignTaskHistoryArg)
  840. offset = (arg.Page - 1) * arg.Size
  841. )
  842. res = new(signmodel.SignQueryResult)
  843. res.Page = arg.Page
  844. res.Size = arg.Size
  845. res.Result = []*signmodel.SignUpsArg{}
  846. if len(arg.Tids) != 0 {
  847. signUpHandle = signUpHandle.Where("active_tid IN (?)", arg.Tids)
  848. }
  849. if arg.Mid != 0 {
  850. signUpHandle = signUpHandle.Where("mid = ?", arg.Mid)
  851. }
  852. if arg.DueSign != 0 {
  853. signUpHandle = signUpHandle.Where("due_warn = ?", 2)
  854. }
  855. if arg.DuePay != 0 {
  856. signUpHandle = signUpHandle.Where("pay_expire_state = ?", 2)
  857. }
  858. if arg.ExpireSign != 0 {
  859. signUpHandle = signUpHandle.Where("state = ?", 1)
  860. }
  861. if arg.Sex != -1 {
  862. signUpHandle = signUpHandle.Where("sex = ?", arg.Sex)
  863. }
  864. if len(arg.Country) != 0 {
  865. signUpHandle = signUpHandle.Where("country IN (?)", arg.Country)
  866. }
  867. if arg.ActiveTID != 0 {
  868. signUpHandle = signUpHandle.Where("active_tid = ?", arg.ActiveTID)
  869. }
  870. if arg.SignType != 0 {
  871. signUpHandle = signUpHandle.Where("sign_type = ?", arg.SignType)
  872. }
  873. if arg.TaskState != 0 {
  874. signUpHandle = signUpHandle.Where("task_state = ?", arg.TaskState)
  875. }
  876. if arg.SignBegin != 0 {
  877. signUpHandle = signUpHandle.Where("begin_date >= ?", arg.SignBegin)
  878. }
  879. if arg.SignEnd != 0 {
  880. signUpHandle = signUpHandle.Where("end_date <= ?", arg.SignEnd)
  881. }
  882. signUpHandle = signUpHandle.Where("state IN (0,1)")
  883. if err = signUpHandle.Count(&count).Error; err != nil {
  884. log.Error("signUps count fail, err=%+v", err)
  885. return
  886. }
  887. if count <= 0 {
  888. return
  889. }
  890. res.TotalCount = count
  891. if arg.IsDetail == signmodel.SignUpDetail {
  892. signUpHandle = signUpHandle.Order(fmt.Sprintf("%s %s", "id", "DESC"))
  893. } else {
  894. signUpHandle = signUpHandle.Order(fmt.Sprintf("%s %s", "mtime", "DESC"))
  895. }
  896. if err = signUpHandle.Offset(offset).Limit(arg.Size).Find(&signUps).Error; err != nil && err != gorm.ErrRecordNotFound {
  897. log.Error("signUps fail, err=%+v", err)
  898. return
  899. }
  900. for _, v := range signUps {
  901. var signUpBaseInfo = &signmodel.SignUpBaseInfo{}
  902. signUpBaseInfo.CopyFrom(v)
  903. signUpBaseInfos = append(signUpBaseInfos, signUpBaseInfo)
  904. signIDs = append(signIDs, v.ID)
  905. mids = append(mids, v.Mid)
  906. tids = append(tids, int64(v.ActiveTid))
  907. }
  908. tpNames = cache.GetTidName(tids...)
  909. var infosReply *accgrpc.InfosReply
  910. if infosReply, err = global.GetAccClient().Infos3(c, &accgrpc.MidsReq{Mids: mids, RealIp: metadata.String(c, metadata.RemoteIP)}); err != nil {
  911. log.Error("global.GetAccClient().Infos3(%+v) error(%+v)", mids, err)
  912. err = nil
  913. }
  914. if err = signPayHandle.Where("sign_id IN (?) AND state IN (0,1)", signIDs).Order(fmt.Sprintf("%s %s", "due_date", "ASC")).Find(&signPayInfos).Error; err != nil && err != gorm.ErrRecordNotFound {
  915. log.Error("sign pays fail, err=%+v", err)
  916. return
  917. }
  918. for _, v := range signPayInfos {
  919. var signPayInfo = &signmodel.SignPayInfoArg{}
  920. signPayInfo.CopyFrom(v)
  921. signPayInfoMap[v.SignID] = append(signPayInfoMap[v.SignID], signPayInfo)
  922. }
  923. if err = signContractHandle.Where("sign_id IN (?) AND state = 0", signIDs).Order(fmt.Sprintf("%s %s", "id", "ASC")).Find(&signContractInfos).Error; err != nil && err != gorm.ErrRecordNotFound {
  924. log.Error("sign contract fail, err=%+v", err)
  925. return
  926. }
  927. for _, v := range signContractInfos {
  928. var signContractInfo = &signmodel.SignContractInfoArg{}
  929. signContractInfo.CopyFrom(v)
  930. signContractInfoMap[v.SignID] = append(signContractInfoMap[v.SignID], signContractInfo)
  931. }
  932. // task
  933. if err = signTaskHandle.Where("sign_id IN (?) AND state = 0", signIDs).Find(&signTasks).Error; err != nil && err != gorm.ErrRecordNotFound {
  934. log.Error("sign task fail, err=%+v", err)
  935. return
  936. }
  937. for _, v := range signTasks {
  938. signTaskIDs = append(signTaskIDs, v.ID)
  939. }
  940. // task history NOTE: mtime 解决generate_date为累积时没用办法排序 有一定风险会错乱
  941. if arg.IsDetail == signmodel.SignUpList {
  942. var signTaskHistorySQL = `SELECT * FROM sign_task_history WHERE task_template_id IN (?) AND state IN (1,2) AND mtime = (SELECT MAX(mtime) FROM sign_task_history s
  943. WHERE s.sign_id=sign_task_history.sign_id AND s.task_template_id IN (?) AND s.state IN (1,2))`
  944. if err = signTaskHistoryHandle.Raw(signTaskHistorySQL, signTaskIDs, signTaskIDs).Find(&signTaskHistorys).Error; err != nil && err != gorm.ErrRecordNotFound {
  945. log.Error("sign task history fail, err=%+v", err)
  946. return
  947. }
  948. } else {
  949. if err = signTaskHistoryHandle.Where("task_template_id IN (?) AND state IN (1,2)", signTaskIDs).Order(fmt.Sprintf("%s %s", "id", "DESC")).Find(&signTaskHistorys).Error; err != nil && err != gorm.ErrRecordNotFound {
  950. log.Error("sign task history fail, err=%+v", err)
  951. return
  952. }
  953. }
  954. for _, v := range signTaskHistorys {
  955. signTaskHistoryIDs = append(signTaskHistoryIDs, v.ID)
  956. }
  957. if err = signAbsenceHandle.Raw("select sum(absence_count)as absence_count, task_history_id from sign_task_absence where task_history_id IN (?) AND state = 0 group by task_history_id", signTaskHistoryIDs).Find(&signTaskAbsences).Error; err != nil && err != gorm.ErrRecordNotFound {
  958. log.Error("sign absence fail, err=%+v", err)
  959. return
  960. }
  961. var hidMap = make(map[int64]int)
  962. for _, v := range signTaskAbsences {
  963. hidMap[v.TaskHistoryID] = v.AbsenceCount
  964. }
  965. for _, v := range signTaskHistorys {
  966. var (
  967. absenceCounter int
  968. signTaskHistoryArg = &signmodel.SignTaskHistoryArg{}
  969. )
  970. if count, ok := hidMap[v.ID]; ok {
  971. absenceCounter = count
  972. }
  973. signTaskHistoryArg.CopyFrom(v, absenceCounter)
  974. signTaskHistoryMap[v.SignID] = append(signTaskHistoryMap[v.SignID], signTaskHistoryArg)
  975. }
  976. for _, v := range signUpBaseInfos {
  977. if tpName, ok := tpNames[int64(v.ActiveTid)]; ok {
  978. v.TypeName = tpName
  979. }
  980. if infosReply != nil && infosReply.Infos != nil {
  981. if info, ok := infosReply.Infos[v.Mid]; ok {
  982. v.Name = info.Name
  983. }
  984. }
  985. if arg.IsDetail == signmodel.SignUpDetail {
  986. res.SignBaseInfo = v
  987. }
  988. var signUpsArg = &signmodel.SignUpsArg{}
  989. signUpsArg.SignUpBaseInfo = *v
  990. if signPayInfoArg, ok := signPayInfoMap[v.ID]; ok {
  991. signUpsArg.PayInfo = signPayInfoArg
  992. }
  993. if signContractInfoArg, ok := signContractInfoMap[v.ID]; ok {
  994. signUpsArg.ContractInfo = signContractInfoArg
  995. }
  996. if signTaskHistoryArg, ok := signTaskHistoryMap[v.ID]; ok {
  997. for _, sth := range signTaskHistoryArg {
  998. if sth.TaskType == signmodel.TaskTypeAccumulate {
  999. sth.TaskBegin = v.BeginDate
  1000. sth.TaskEnd = v.EndDate
  1001. }
  1002. }
  1003. signUpsArg.TaskHistoryInfo = signTaskHistoryArg
  1004. }
  1005. res.Result = append(res.Result, signUpsArg)
  1006. }
  1007. return
  1008. }
  1009. // SignQueryID .
  1010. func (s *Service) SignQueryID(c context.Context, arg *signmodel.SignIDArg) (res *signmodel.SignUpArg, err error) {
  1011. var (
  1012. tpNames map[int64]string
  1013. signUp *signmodel.SignUp
  1014. signPayMap map[int64]*signmodel.SignPay
  1015. signTaskMap map[int64]*signmodel.SignTask
  1016. signContractMap map[int64]*signmodel.SignContract
  1017. )
  1018. res = new(signmodel.SignUpArg)
  1019. if signUp, signPayMap, signTaskMap, signContractMap, err = s.crmdb.SignUpID(arg.ID); err != nil {
  1020. log.Error("s.crmdb.SignUpID(%+d) error(%+v)", arg.ID, err)
  1021. return
  1022. }
  1023. if signUp == nil {
  1024. return
  1025. }
  1026. var infoReply *accgrpc.InfoReply
  1027. if infoReply, err = global.GetAccClient().Info3(c, &accgrpc.MidReq{Mid: signUp.Mid, RealIp: metadata.String(c, metadata.RemoteIP)}); err != nil {
  1028. log.Error("global.GetAccClient().Infos3(%d) error(%+v)", signUp.Mid, err)
  1029. err = nil
  1030. }
  1031. tpNames = cache.GetTidName(int64(signUp.ActiveTid))
  1032. res.SignUpBaseInfo.CopyFrom(signUp)
  1033. if infoReply != nil && infoReply.Info != nil {
  1034. res.SignUpBaseInfo.Name = infoReply.Info.Name
  1035. }
  1036. if tpName, ok := tpNames[int64(signUp.ActiveTid)]; ok {
  1037. res.SignUpBaseInfo.TypeName = tpName
  1038. }
  1039. for _, v := range signPayMap {
  1040. var payInfo = &signmodel.SignPayInfoArg{}
  1041. payInfo.CopyFrom(v)
  1042. res.PayInfo = append(res.PayInfo, payInfo)
  1043. }
  1044. if signPayMap != nil {
  1045. sort.Slice(res.PayInfo, func(i int, j int) bool {
  1046. return res.PayInfo[i].DueDate < res.PayInfo[j].DueDate
  1047. })
  1048. }
  1049. for _, v := range signTaskMap {
  1050. var taskInfo = &signmodel.SignTaskInfoArg{}
  1051. taskInfo.CopyFrom(v)
  1052. res.TaskInfo = append(res.TaskInfo, taskInfo)
  1053. }
  1054. if signTaskMap != nil {
  1055. sort.Slice(res.TaskInfo, func(i int, j int) bool {
  1056. return res.TaskInfo[i].ID < res.TaskInfo[j].ID
  1057. })
  1058. }
  1059. for _, v := range signContractMap {
  1060. var contractInfo = &signmodel.SignContractInfoArg{}
  1061. contractInfo.CopyFrom(v)
  1062. res.ContractInfo = append(res.ContractInfo, contractInfo)
  1063. }
  1064. if signContractMap != nil {
  1065. sort.Slice(res.ContractInfo, func(i int, j int) bool {
  1066. return res.ContractInfo[i].ID < res.ContractInfo[j].ID
  1067. })
  1068. }
  1069. return
  1070. }
  1071. // SignPayComplete complete sign pay
  1072. func (s *Service) SignPayComplete(con context.Context, arg *signmodel.SignPayCompleteArg) (result signmodel.SignPayCompleteResult, err error) {
  1073. var affectedrow, e = s.crmdb.PayComplete(arg.IDs)
  1074. if e != nil {
  1075. err = e
  1076. log.Error("fail to complete pay task, err=%+v", e)
  1077. return
  1078. }
  1079. log.Info("complete pay, id=%+v, affected row=%d", arg.IDs, affectedrow)
  1080. return
  1081. }
  1082. // SignCheckExist check sign up has an valid contract
  1083. func (s *Service) SignCheckExist(c context.Context, arg *signmodel.SignCheckExsitArg) (result signmodel.SignCheckExsitResult, err error) {
  1084. result.Exist, err = s.crmdb.CheckUpHasValidContract(arg.Mid, time.Now())
  1085. if err != nil {
  1086. log.Error("check up has valid contract fail, err=%+v", err)
  1087. }
  1088. return
  1089. }
  1090. // Countrys .
  1091. func (s *Service) Countrys(c context.Context, arg *signmodel.CommonArg) (res *signmodel.SignCountrysReply, err error) {
  1092. var (
  1093. signUp []*signmodel.SignUp
  1094. db = s.crmdb.GetDb()
  1095. signUpHandle = db.Table(signmodel.TableSignUp)
  1096. )
  1097. res = new(signmodel.SignCountrysReply)
  1098. if err = signUpHandle.Raw("select DISTINCT(country) from sign_up").Find(&signUp).Error; err != nil && err != gorm.ErrRecordNotFound {
  1099. log.Error("sign up fail, err=%+v", err)
  1100. }
  1101. if err == gorm.ErrRecordNotFound {
  1102. err = nil
  1103. return
  1104. }
  1105. for _, v := range signUp {
  1106. if v.Country == "" {
  1107. continue
  1108. }
  1109. res.List = append(res.List, v.Country)
  1110. }
  1111. return
  1112. }
  1113. // Tids .
  1114. func (s *Service) Tids(c context.Context, arg *signmodel.CommonArg) (res *signmodel.SignTidsReply, err error) {
  1115. var (
  1116. tids []int64
  1117. signUp []*signmodel.SignUp
  1118. db = s.crmdb.GetDb()
  1119. signUpHandle = db.Table(signmodel.TableSignUp)
  1120. )
  1121. res = new(signmodel.SignTidsReply)
  1122. if err = signUpHandle.Raw("select DISTINCT(active_tid) from sign_up").Find(&signUp).Error; err != nil && err != gorm.ErrRecordNotFound {
  1123. log.Error("sign up fail, err=%+v", err)
  1124. }
  1125. if err == gorm.ErrRecordNotFound {
  1126. err = nil
  1127. return
  1128. }
  1129. for _, v := range signUp {
  1130. if v.ActiveTid == 0 {
  1131. continue
  1132. }
  1133. tids = append(tids, int64(v.ActiveTid))
  1134. }
  1135. res.List = cache.GetTidName(tids...)
  1136. return
  1137. }