sign_up.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package upcrm
  2. import (
  3. "time"
  4. "go-common/app/admin/main/up/model/signmodel"
  5. "go-common/app/admin/main/up/model/upcrmmodel"
  6. "go-common/library/log"
  7. xtime "go-common/library/time"
  8. "github.com/jinzhu/gorm"
  9. )
  10. const (
  11. // PayStateUnpay not pay
  12. PayStateUnpay = 0
  13. // PayStatePayed payed
  14. PayStatePayed = 1
  15. )
  16. // InsertSignUp insert sign up
  17. func (d *Dao) InsertSignUp(db *gorm.DB, up *signmodel.SignUp) (affectedRow int64, err error) {
  18. var handle = db.Save(up)
  19. err = handle.Error
  20. affectedRow = handle.RowsAffected
  21. return
  22. }
  23. // InsertPayInfo inert pay
  24. func (d *Dao) InsertPayInfo(db *gorm.DB, info *signmodel.SignPay) (affectedRow int64, err error) {
  25. var handle = db.Save(info)
  26. err = handle.Error
  27. affectedRow = handle.RowsAffected
  28. return
  29. }
  30. // InsertTaskInfo insert task
  31. func (d *Dao) InsertTaskInfo(db *gorm.DB, info *signmodel.SignTask) (affectedRow int64, err error) {
  32. var handle = db.Save(info)
  33. err = handle.Error
  34. affectedRow = handle.RowsAffected
  35. return
  36. }
  37. // InsertContractInfo insert contract
  38. func (d *Dao) InsertContractInfo(db *gorm.DB, info interface{}) (affectedRow int64, err error) {
  39. var handle = db.Save(info)
  40. err = handle.Error
  41. affectedRow = handle.RowsAffected
  42. return
  43. }
  44. // DelPayInfo update payinfo
  45. func (d *Dao) DelPayInfo(db *gorm.DB, ids []int64) (affectedRow int64, err error) {
  46. var handle = db.Model(&signmodel.SignPay{}).Where("id IN (?)", ids).Update("state", 100)
  47. err = handle.Error
  48. affectedRow = handle.RowsAffected
  49. return
  50. }
  51. // DelTaskInfo update taskInfo
  52. func (d *Dao) DelTaskInfo(db *gorm.DB, ids []int64) (affectedRow int64, err error) {
  53. var handle = db.Model(&signmodel.SignTask{}).Where("id IN (?)", ids).Update("state", 100)
  54. err = handle.Error
  55. affectedRow = handle.RowsAffected
  56. return
  57. }
  58. // DelSignContract update signcontract
  59. func (d *Dao) DelSignContract(db *gorm.DB, ids []int64) (affectedRow int64, err error) {
  60. var handle = db.Model(&signmodel.SignContract{}).Where("id IN (?)", ids).Update("state", 100)
  61. err = handle.Error
  62. affectedRow = handle.RowsAffected
  63. return
  64. }
  65. // SignUpID .
  66. func (d *Dao) SignUpID(sigID int64) (su *signmodel.SignUp, msp map[int64]*signmodel.SignPay, mst map[int64]*signmodel.SignTask, msc map[int64]*signmodel.SignContract, err error) {
  67. su = &signmodel.SignUp{}
  68. if err = d.crmdb.Table(signmodel.TableSignUp).Where("id = ? AND state IN (0,1)", sigID).Find(su).Error; err != nil && err != gorm.ErrRecordNotFound {
  69. log.Error("db fail, err=%+v", err)
  70. return
  71. }
  72. if err == gorm.ErrRecordNotFound {
  73. err = nil
  74. su, msp, mst, msc = nil, nil, nil, nil
  75. return
  76. }
  77. var (
  78. sps []*signmodel.SignPay
  79. sts []*signmodel.SignTask
  80. scs []*signmodel.SignContract
  81. )
  82. msp = make(map[int64]*signmodel.SignPay)
  83. if err = d.crmdb.Table(signmodel.TableSignPay).Where("sign_id = ? AND state IN (0,1)", sigID).Find(&sps).Error; err != nil && err != gorm.ErrRecordNotFound {
  84. log.Error("db fail, err=%+v", err)
  85. return
  86. }
  87. for _, v := range sps {
  88. msp[v.ID] = v
  89. }
  90. mst = make(map[int64]*signmodel.SignTask)
  91. if err = d.crmdb.Table(signmodel.TableSignTask).Where("sign_id = ? AND state = 0", sigID).Find(&sts).Error; err != nil && err != gorm.ErrRecordNotFound {
  92. log.Error("db fail, err=%+v", err)
  93. return
  94. }
  95. for _, v := range sts {
  96. mst[v.ID] = v
  97. }
  98. msc = make(map[int64]*signmodel.SignContract)
  99. if err = d.crmdb.Table(signmodel.TableSignContract).Where("sign_id = ? AND state = 0", sigID).Find(&scs).Error; err != nil && err != gorm.ErrRecordNotFound {
  100. log.Error("db fail, err=%+v", err)
  101. return
  102. }
  103. for _, v := range scs {
  104. msc[v.ID] = v
  105. }
  106. return
  107. }
  108. // GetSignIDByCondition get sign id
  109. // arg query args
  110. func (d *Dao) GetSignIDByCondition(arg *signmodel.SignQueryArg) (signIDs []uint32, err error) {
  111. var signIDMap = map[uint32]struct{}{}
  112. switch {
  113. default:
  114. // 如果是mid,则不进行其他的查询
  115. if arg.Mid != 0 {
  116. var result []signmodel.SignUpOnlyID
  117. var db = d.crmdb.Table("sign_up").Where("mid=?", arg.Mid)
  118. err = db.Select("id").Find(&result).Error
  119. if err != nil && err != gorm.ErrRecordNotFound {
  120. log.Error("db fail, err=%+v", err)
  121. return
  122. }
  123. for _, v := range result {
  124. signIDMap[v.ID] = struct{}{}
  125. }
  126. break
  127. }
  128. var now = time.Now()
  129. // 1.增加查询条件
  130. if arg.DuePay == 1 {
  131. var duedate = now.AddDate(0, 0, 7)
  132. var result []signmodel.SignUpOnlySignID
  133. // due_date and state = PayStateUnpay
  134. err = d.crmdb.Table("sign_pay").Select("sign_id").Where("due_date <= ? and state = 0", duedate.Format(upcrmmodel.TimeFmtDate)).Where("state != 100").
  135. Find(&result).Error
  136. if err != nil && err != gorm.ErrRecordNotFound {
  137. log.Error("db fail, err=%+v", err)
  138. } else {
  139. for _, v := range result {
  140. signIDMap[v.SignID] = struct{}{}
  141. }
  142. }
  143. }
  144. if arg.DueSign == 1 || arg.ExpireSign == 1 {
  145. var duedate = now.AddDate(0, 0, 30)
  146. var result []signmodel.SignUpOnlyID
  147. var db = d.crmdb.Table("sign_up").Select("id")
  148. if arg.DueSign == 1 && arg.ExpireSign == 1 {
  149. db = db.Where("end_date < ?", duedate.Format(upcrmmodel.TimeFmtDate)).Where("state != 100") // 去掉已删除的
  150. } else {
  151. if arg.DueSign == 1 {
  152. db = db.Where("end_date >= ? and end_date <= ?", now.Format(upcrmmodel.TimeFmtDate), duedate.Format(upcrmmodel.TimeFmtDate))
  153. } else if arg.ExpireSign == 1 {
  154. db = db.Where("end_date < ?", now.Format(upcrmmodel.TimeFmtDate))
  155. }
  156. }
  157. err = db.
  158. Find(&result).Error
  159. if err != nil && err != gorm.ErrRecordNotFound {
  160. log.Error("db fail, err=%+v", err)
  161. } else {
  162. for _, v := range result {
  163. signIDMap[v.ID] = struct{}{}
  164. }
  165. }
  166. }
  167. }
  168. for k := range signIDMap {
  169. signIDs = append(signIDs, k)
  170. }
  171. return
  172. }
  173. //GetSignUpByID signid 可以是nil,如果是nil,则会取所有的信息
  174. // query, args, 额外的查询条件
  175. func (d *Dao) GetSignUpByID(signID []uint32, order string, offset int, limit int, query interface{}, args ...interface{}) (result []signmodel.SignUp, err error) {
  176. var db = d.crmdb.Table("sign_up")
  177. if signID != nil {
  178. db = db.Where("id in (?)", signID)
  179. }
  180. if query != nil {
  181. db = db.Where(query, args)
  182. }
  183. if order != "" {
  184. db = db.Order(order)
  185. }
  186. err = db.
  187. Offset(offset).
  188. Limit(limit).
  189. Find(&result).Error
  190. if err == gorm.ErrRecordNotFound {
  191. err = nil
  192. }
  193. return
  194. }
  195. //GetSignUpCount get sign up's count
  196. func (d *Dao) GetSignUpCount(query string, args ...interface{}) (count int) {
  197. d.crmdb.Table(signmodel.TableSignUp).Where(query, args).Count(&count)
  198. return
  199. }
  200. //GetTask get task by sign id and state
  201. func (d *Dao) GetTask(signID []uint32, state ...int) (result []signmodel.SignTask, err error) {
  202. err = d.crmdb.Table(signmodel.TableSignTask).Where("sign_id in (?) and state = ?", signID, state).Find(&result).Error
  203. if err == gorm.ErrRecordNotFound {
  204. err = nil
  205. }
  206. return
  207. }
  208. //GetPay get get sign id
  209. func (d *Dao) GetPay(signID []uint32) (result []signmodel.SignPay, err error) {
  210. err = d.crmdb.Table(signmodel.TableSignPay).Where("sign_id in (?)", signID).Find(&result).Error
  211. if err == gorm.ErrRecordNotFound {
  212. err = nil
  213. }
  214. return
  215. }
  216. //GetContract get get sign id
  217. func (d *Dao) GetContract(signID []uint32) (result []signmodel.SignContract, err error) {
  218. err = d.crmdb.Table(signmodel.TableSignContract).Where("sign_id in (?)", signID).Find(&result).Error
  219. if err == gorm.ErrRecordNotFound {
  220. err = nil
  221. }
  222. return
  223. }
  224. //PayComplete finish pay by pay id
  225. func (d *Dao) PayComplete(ids []int64) (affectedRow int64, err error) {
  226. var db = d.crmdb.Table(signmodel.TableSignPay).Where("id in (?)", ids).Updates(map[string]interface{}{"state": PayStatePayed})
  227. err = db.Error
  228. if err == nil {
  229. affectedRow = db.RowsAffected
  230. }
  231. return
  232. }
  233. //SignWithName sign with name, used to send mail
  234. type SignWithName struct {
  235. signmodel.SignUp
  236. Name string
  237. }
  238. //GetDueSignUp check due
  239. // expireAfterDays : how many days to expire
  240. func (d *Dao) GetDueSignUp(now time.Time, expireAfterDays int) (result []*SignWithName, err error) {
  241. var dueDate = now.AddDate(0, 0, expireAfterDays)
  242. var db = d.crmdb.Table(signmodel.TableSignUp).Select("id, mid, end_date, admin_id, admin_name")
  243. // email_state = 0 是未发过邮件的意思
  244. db = db.Where("end_date <= ? and end_date >= ? and email_state = 0", dueDate.Format(upcrmmodel.TimeFmtDate), now.Format(upcrmmodel.TimeFmtDate))
  245. err = db.Find(&result).Error
  246. return
  247. }
  248. //PayWithAdmin pay with name, used to send mail
  249. type PayWithAdmin struct {
  250. signmodel.SignPay
  251. Name string
  252. AdminID int
  253. AdminName string
  254. }
  255. //GetDuePay check due
  256. func (d *Dao) GetDuePay(now time.Time, expireAfterDays int) (result []*PayWithAdmin, err error) {
  257. var dueDate = now.AddDate(0, 0, expireAfterDays)
  258. err = d.crmdb.Raw("select p.id, "+
  259. "p.sign_id, "+
  260. "p.mid, "+
  261. "p.due_date, "+
  262. "p.pay_value,"+
  263. "s.admin_id, "+
  264. "s.admin_name"+
  265. " from sign_pay as p left join sign_up as s on p.sign_id = s.id "+
  266. " where p.due_date <= ? and p.email_state = 0 and p.state = 0", dueDate.Format(upcrmmodel.TimeFmtDate)).
  267. Scan(&result).Error
  268. return
  269. }
  270. //UpdateEmailState update email send state
  271. // state : @
  272. func (d *Dao) UpdateEmailState(table string, ids []int64, state int8) (affectedRow int64, err error) {
  273. var db = d.crmdb.Table(table).Where("id in (?)", ids).Update("email_state", state)
  274. err = db.Error
  275. if err == nil {
  276. affectedRow = db.RowsAffected
  277. } else {
  278. log.Error("update email state fail, err=%+v", err)
  279. }
  280. return
  281. }
  282. //CheckUpHasValidContract check if has valid contract
  283. func (d *Dao) CheckUpHasValidContract(mid int64, date time.Time) (exist bool, err error) {
  284. var ids []struct {
  285. ID int
  286. }
  287. err = d.crmdb.Table(signmodel.TableSignUp).Select("id").Where("mid=? and end_date>=?", mid, date.Format(upcrmmodel.TimeFmtDate)).Limit(1).Find(&ids).Error
  288. if err != nil && err != gorm.ErrRecordNotFound {
  289. log.Error("check exist from db fail, err=%+v", err)
  290. return
  291. }
  292. exist = len(ids) > 0
  293. return
  294. }
  295. // GetOrCreateTaskHistory .
  296. func (d *Dao) GetOrCreateTaskHistory(db *gorm.DB, st *signmodel.SignTask) (sth *signmodel.SignTaskHistory, init bool, err error) {
  297. sDate, _ := signmodel.GetTaskDuration(time.Now(), st.TaskType)
  298. sth = new(signmodel.SignTaskHistory)
  299. err = db.Select("*").Where("task_template_id=? and generate_date=?", st.ID, sDate).Find(&sth).Error
  300. // 创建一条,如果没找到的话
  301. if err == gorm.ErrRecordNotFound {
  302. sth = &signmodel.SignTaskHistory{
  303. Mid: st.Mid,
  304. SignID: int64(st.SignID),
  305. TaskTemplateID: int(st.ID),
  306. TaskType: st.TaskType,
  307. TaskCondition: int(st.TaskCondition),
  308. GenerateDate: xtime.Time(sDate.Unix()),
  309. Attribute: st.Attribute,
  310. State: signmodel.SignTaskStateRunning,
  311. }
  312. if err = db.Save(&sth).Error; err != nil {
  313. log.Error("create task history fail, err=%v, task=%v", err, st)
  314. }
  315. init = true
  316. }
  317. return
  318. }
  319. // UpSignTaskHistory .
  320. func (d *Dao) UpSignTaskHistory(db *gorm.DB, sth *signmodel.SignTaskHistory) (err error) {
  321. if err = db.Table(signmodel.TableSignTaskHistory).Where("id = ?", sth.ID).UpdateColumns(
  322. map[string]interface{}{
  323. "task_type": sth.TaskType,
  324. "task_condition": sth.TaskCondition,
  325. "attribute": sth.Attribute,
  326. "mtime": time.Now(),
  327. }).Error; err != nil {
  328. log.Error("dao.UpSignTaskHistory(%+v) , err=%+v", sth, err)
  329. }
  330. return
  331. }