mcn_account.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package up
  2. import (
  3. "context"
  4. "database/sql"
  5. "fmt"
  6. "time"
  7. "go-common/app/admin/main/mcn/model"
  8. xsql "go-common/library/database/sql"
  9. xtime "go-common/library/time"
  10. "go-common/library/xstr"
  11. "github.com/pkg/errors"
  12. )
  13. const (
  14. _inMcnSignEntrySQL = "INSERT mcn_sign(mcn_mid,begin_date,end_date,permission) VALUES (?,?,?,?)"
  15. _inMcnSignPaySQL = "INSERT mcn_sign_pay(mcn_mid,sign_id,due_date,pay_value,note) VALUES (?,?,?,?,?)"
  16. _upMcnSignOPSQL = "UPDATE mcn_sign SET state = ?, reject_time = ?, reject_reason = ? WHERE id = ?"
  17. _upMcnUpOPSQL = "UPDATE mcn_up SET state = ?, reject_time = ?, reject_reason = ?, state_change_time = ? WHERE id = ?"
  18. _mcnUpPermitOPSQL = "UPDATE mcn_up SET permission = ?, up_auth_link = ? WHERE sign_id = ? AND mcn_mid = ? AND up_mid = ?"
  19. _upPermitApplyOPSQL = "UPDATE mcn_up_permission_apply SET state = ?, reject_reason = ?, reject_time = ?, admin_id = ?, admin_name = ? WHERE id = ?"
  20. _selMcnSignsSQL = `SELECT id,mcn_mid,company_name,company_license_id,company_license_link,contract_link,contact_name,
  21. contact_title,contact_idcard,contact_phone,begin_date,end_date,reject_reason,reject_time,state,ctime,mtime,permission FROM mcn_sign WHERE state = ? ORDER BY mtime DESC limit ?,?`
  22. _selMcnSignSQL = `SELECT id,mcn_mid,company_name,company_license_id,company_license_link,contract_link,contact_name,
  23. contact_title,contact_idcard,contact_phone,begin_date,end_date,reject_reason,reject_time,state,ctime,mtime,permission FROM mcn_sign WHERE id = ?`
  24. _selMcnSignPayMapSQL = "SELECT id,sign_id,due_date,pay_value,state FROM mcn_sign_pay WHERE sign_id IN (%s) AND state IN (0,1)"
  25. _selMcnUpsSQL = "SELECT id,sign_id,mcn_mid,up_mid,begin_date,end_date,contract_link,up_auth_link,reject_reason,reject_time,state,ctime,mtime,up_type,site_link,confirm_time,publication_price,permission FROM mcn_up WHERE state = ? ORDER BY mtime DESC limit ?,?"
  26. _selMcnUpSQL = "SELECT id,sign_id,mcn_mid,up_mid,begin_date,end_date,contract_link,up_auth_link,reject_reason,reject_time,state,ctime,mtime,up_type,site_link,confirm_time,publication_price,permission FROM mcn_up WHERE id = ?"
  27. _selMcnSignCountUQTimeSQL = "SELECT COUNT(1) FROM mcn_sign WHERE mcn_mid = ? AND state IN (0,1,2,10,13,15) AND (end_date >= ? OR end_date >= ?) AND begin_date <= ?"
  28. _mcnSignTotalSQL = "SELECT COUNT(1) FROM mcn_sign WHERE state = ?"
  29. _mcnUpTotalSQL = "SELECT COUNT(1) FROM mcn_up WHERE state = ?"
  30. _mcnSignNoStateCountSQL = "SELECT COUNT(1) FROM mcn_sign WHERE mcn_mid = ? AND state IN (0,1,2,10,13,15)"
  31. _mcnUpPermitReviewsSQL = "SELECT id,mcn_mid,up_mid,sign_id,new_permission,old_permission,reject_reason,reject_time,state,ctime,mtime,admin_id,admin_name,up_auth_link FROM mcn_up_permission_apply WHERE state = ? ORDER BY mtime DESC limit ?,?"
  32. _mcnUpPermitReviewSQL = "SELECT id,mcn_mid,up_mid,sign_id,new_permission,old_permission,reject_reason,reject_time,state,ctime,mtime,admin_id,admin_name,up_auth_link FROM mcn_up_permission_apply WHERE id = ?"
  33. _mcnUpPermitTotalSQL = "SELECT COUNT(1) FROM mcn_up_permission_apply WHERE state = ?"
  34. )
  35. // TxAddMcnSignEntry .
  36. func (d *Dao) TxAddMcnSignEntry(tx *xsql.Tx, mcnMid int64, beginDate, endDate string, permission uint32) (lastID int64, err error) {
  37. var res sql.Result
  38. if res, err = tx.Exec(_inMcnSignEntrySQL, mcnMid, beginDate, endDate, permission); err != nil {
  39. return lastID, err
  40. }
  41. if lastID, err = res.LastInsertId(); err != nil {
  42. return lastID, errors.Errorf("res.LastInsertId(%d,%s,%s,%d) error(%+v)", mcnMid, beginDate, endDate, permission, err)
  43. }
  44. return lastID, nil
  45. }
  46. // TxAddMcnSignPay .
  47. func (d *Dao) TxAddMcnSignPay(tx *xsql.Tx, mcnMid, signID, payValue int64, dueDate, note string) (rows int64, err error) {
  48. var res sql.Result
  49. if res, err = tx.Exec(_inMcnSignPaySQL, mcnMid, signID, dueDate, payValue, note); err != nil {
  50. return rows, err
  51. }
  52. return res.RowsAffected()
  53. }
  54. // UpMcnSignOP .
  55. func (d *Dao) UpMcnSignOP(c context.Context, signID int64, state int8, rejectTime xtime.Time, rejectReason string) (rows int64, err error) {
  56. var res sql.Result
  57. if res, err = d.db.Exec(c, _upMcnSignOPSQL, state, rejectTime, rejectReason, signID); err != nil {
  58. return rows, err
  59. }
  60. return res.RowsAffected()
  61. }
  62. // UpMcnUpOP .
  63. func (d *Dao) UpMcnUpOP(c context.Context, signUpID int64, state int8, rejectTime xtime.Time, rejectReason string) (rows int64, err error) {
  64. var res sql.Result
  65. if res, err = d.db.Exec(c, _upMcnUpOPSQL, state, rejectTime, rejectReason, time.Now(), signUpID); err != nil {
  66. return rows, err
  67. }
  68. return res.RowsAffected()
  69. }
  70. // TxMcnUpPermitOP .
  71. func (d *Dao) TxMcnUpPermitOP(tx *xsql.Tx, signID, mcnMid, upMid int64, permission uint32, upAuthLink string) (rows int64, err error) {
  72. var res sql.Result
  73. if res, err = tx.Exec(_mcnUpPermitOPSQL, permission, upAuthLink, signID, mcnMid, upMid); err != nil {
  74. return rows, err
  75. }
  76. return res.RowsAffected()
  77. }
  78. // TxUpPermitApplyOP .
  79. func (d *Dao) TxUpPermitApplyOP(tx *xsql.Tx, arg *model.McnUpPermissionApply) (rows int64, err error) {
  80. var res sql.Result
  81. if res, err = tx.Exec(_upPermitApplyOPSQL, arg.State, arg.RejectReason, arg.RejectTime, arg.AdminID, arg.AdminName, arg.ID); err != nil {
  82. return rows, err
  83. }
  84. return res.RowsAffected()
  85. }
  86. // McnSigns .
  87. func (d *Dao) McnSigns(c context.Context, arg *model.MCNSignStateReq) (ms []*model.MCNSignInfoReply, err error) {
  88. var rows *xsql.Rows
  89. limit, offset := arg.PageArg.CheckPageValidation()
  90. if rows, err = d.db.Query(c, _selMcnSignsSQL, arg.State, offset, limit); err != nil {
  91. return
  92. }
  93. defer rows.Close()
  94. for rows.Next() {
  95. m := new(model.MCNSignInfoReply)
  96. if err = rows.Scan(&m.SignID, &m.McnMid, &m.CompanyName, &m.CompanyLicenseID, &m.CompanyLicenseLink, &m.ContractLink, &m.ContactName,
  97. &m.ContactTitle, &m.ContactIdcard, &m.ContactPhone, &m.BeginDate, &m.EndDate, &m.RejectReason, &m.RejectTime, &m.State, &m.Ctime, &m.Mtime, &m.Permission); err != nil {
  98. return
  99. }
  100. ms = append(ms, m)
  101. }
  102. err = rows.Err()
  103. return
  104. }
  105. // McnSign .
  106. func (d *Dao) McnSign(c context.Context, signID int64) (m *model.MCNSignInfoReply, err error) {
  107. row := d.db.QueryRow(c, _selMcnSignSQL, signID)
  108. m = new(model.MCNSignInfoReply)
  109. if err = row.Scan(&m.SignID, &m.McnMid, &m.CompanyName, &m.CompanyLicenseID, &m.CompanyLicenseLink, &m.ContractLink, &m.ContactName,
  110. &m.ContactTitle, &m.ContactIdcard, &m.ContactPhone, &m.BeginDate, &m.EndDate, &m.RejectReason, &m.RejectTime, &m.State, &m.Ctime, &m.Mtime, &m.Permission); err != nil {
  111. if err == xsql.ErrNoRows {
  112. err = nil
  113. m = nil
  114. return
  115. }
  116. }
  117. return
  118. }
  119. // McnSignPayMap .
  120. func (d *Dao) McnSignPayMap(c context.Context, signIDs []int64) (sm map[int64][]*model.SignPayInfoReply, err error) {
  121. var rows *xsql.Rows
  122. if rows, err = d.db.Query(c, fmt.Sprintf(_selMcnSignPayMapSQL, xstr.JoinInts(signIDs))); err != nil {
  123. return
  124. }
  125. defer rows.Close()
  126. sm = make(map[int64][]*model.SignPayInfoReply)
  127. for rows.Next() {
  128. s := new(model.SignPayInfoReply)
  129. if err = rows.Scan(&s.SignPayID, &s.SignID, &s.DueDate, &s.PayValue, &s.State); err != nil {
  130. return
  131. }
  132. sm[s.SignID] = append(sm[s.SignID], s)
  133. }
  134. err = rows.Err()
  135. return
  136. }
  137. // McnUps .
  138. func (d *Dao) McnUps(c context.Context, arg *model.MCNUPStateReq) (ups []*model.MCNUPInfoReply, err error) {
  139. var rows *xsql.Rows
  140. limit, offset := arg.PageArg.CheckPageValidation()
  141. if rows, err = d.db.Query(c, _selMcnUpsSQL, arg.State, offset, limit); err != nil {
  142. return
  143. }
  144. defer rows.Close()
  145. for rows.Next() {
  146. up := new(model.MCNUPInfoReply)
  147. if err = rows.Scan(&up.SignUpID, &up.SignID, &up.McnMid, &up.UpMid, &up.BeginDate, &up.EndDate, &up.ContractLink,
  148. &up.UpAuthLink, &up.RejectReason, &up.RejectTime, &up.State, &up.Ctime, &up.Mtime, &up.UPType, &up.SiteLink,
  149. &up.ConfirmTime, &up.PubPrice, &up.Permission); err != nil {
  150. return
  151. }
  152. ups = append(ups, up)
  153. }
  154. err = rows.Err()
  155. return
  156. }
  157. // McnUp .
  158. func (d *Dao) McnUp(c context.Context, signUpID int64) (up *model.MCNUPInfoReply, err error) {
  159. row := d.db.QueryRow(c, _selMcnUpSQL, signUpID)
  160. up = new(model.MCNUPInfoReply)
  161. if err = row.Scan(&up.SignUpID, &up.SignID, &up.McnMid, &up.UpMid, &up.BeginDate, &up.EndDate, &up.ContractLink,
  162. &up.UpAuthLink, &up.RejectReason, &up.RejectTime, &up.State, &up.Ctime, &up.Mtime, &up.UPType, &up.SiteLink,
  163. &up.ConfirmTime, &up.PubPrice, &up.Permission); err != nil {
  164. if err == xsql.ErrNoRows {
  165. err = nil
  166. up = nil
  167. return
  168. }
  169. }
  170. return
  171. }
  172. // McnSignCountUQTime .
  173. func (d *Dao) McnSignCountUQTime(c context.Context, mcnMid int64, stime, etime xtime.Time) (count int64, err error) {
  174. row := d.db.QueryRow(c, _selMcnSignCountUQTimeSQL, mcnMid, stime, etime, etime)
  175. if err = row.Scan(&count); err != nil {
  176. if err == xsql.ErrNoRows {
  177. err = nil
  178. return
  179. }
  180. }
  181. return
  182. }
  183. // McnSignTotal .
  184. func (d *Dao) McnSignTotal(c context.Context, arg *model.MCNSignStateReq) (count int64, err error) {
  185. row := d.db.QueryRow(c, _mcnSignTotalSQL, arg.State)
  186. if err = row.Scan(&count); err != nil {
  187. if err == xsql.ErrNoRows {
  188. err = nil
  189. return
  190. }
  191. }
  192. return
  193. }
  194. // McnUpTotal .
  195. func (d *Dao) McnUpTotal(c context.Context, arg *model.MCNUPStateReq) (count int64, err error) {
  196. row := d.db.QueryRow(c, _mcnUpTotalSQL, arg.State)
  197. if err = row.Scan(&count); err != nil {
  198. if err == xsql.ErrNoRows {
  199. err = nil
  200. return
  201. }
  202. }
  203. return
  204. }
  205. // McnSignNoOKState .
  206. func (d *Dao) McnSignNoOKState(c context.Context, mcnMid int64) (count int64, err error) {
  207. row := d.db.QueryRow(c, _mcnSignNoStateCountSQL, mcnMid)
  208. if err = row.Scan(&count); err != nil {
  209. if err == xsql.ErrNoRows {
  210. err = nil
  211. return
  212. }
  213. }
  214. return
  215. }
  216. // McnUpPermits .
  217. func (d *Dao) McnUpPermits(c context.Context, arg *model.MCNUPPermitStateReq) (ms []*model.McnUpPermissionApply, err error) {
  218. var rows *xsql.Rows
  219. limit, offset := arg.PageArg.CheckPageValidation()
  220. if rows, err = d.db.Query(c, _mcnUpPermitReviewsSQL, arg.State, offset, limit); err != nil {
  221. return
  222. }
  223. defer rows.Close()
  224. for rows.Next() {
  225. m := new(model.McnUpPermissionApply)
  226. if err = rows.Scan(&m.ID, &m.McnMid, &m.UpMid, &m.SignID, &m.NewPermission, &m.OldPermission, &m.RejectReason,
  227. &m.RejectTime, &m.State, &m.Ctime, &m.Mtime, &m.AdminID, &m.AdminName, &m.UpAuthLink); err != nil {
  228. return
  229. }
  230. ms = append(ms, m)
  231. }
  232. err = rows.Err()
  233. return
  234. }
  235. // McnUpPermit .
  236. func (d *Dao) McnUpPermit(c context.Context, id int64) (m *model.McnUpPermissionApply, err error) {
  237. row := d.db.QueryRow(c, _mcnUpPermitReviewSQL, id)
  238. m = new(model.McnUpPermissionApply)
  239. if err = row.Scan(&m.ID, &m.McnMid, &m.UpMid, &m.SignID, &m.NewPermission, &m.OldPermission, &m.RejectReason,
  240. &m.RejectTime, &m.State, &m.Ctime, &m.Mtime, &m.AdminID, &m.AdminName, &m.UpAuthLink); err != nil {
  241. if err == xsql.ErrNoRows {
  242. err = nil
  243. return
  244. }
  245. }
  246. return
  247. }
  248. // McnUpPermitTotal .
  249. func (d *Dao) McnUpPermitTotal(c context.Context, arg *model.MCNUPPermitStateReq) (count int64, err error) {
  250. row := d.db.QueryRow(c, _mcnUpPermitTotalSQL, arg.State)
  251. if err = row.Scan(&count); err != nil {
  252. if err == xsql.ErrNoRows {
  253. err = nil
  254. return
  255. }
  256. }
  257. return
  258. }