eta_business.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. package eta_business
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "hongze/hongze_task/utils"
  7. "strings"
  8. "time"
  9. )
  10. const (
  11. EtaBusinessSigningStatusFirst = iota + 1
  12. EtaBusinessSigningStatusContinue
  13. EtaBusinessSigningStatusTerminate
  14. EtaBusinessSigningStatusWait
  15. )
  16. type EtaBusiness struct {
  17. EtaBusinessId int `orm:"column(eta_business_id);pk"`
  18. BusinessName string `description:"商家名称"`
  19. BusinessCode string `description:"商家编码"`
  20. CreditCode string `description:"社会统一信用码"`
  21. RegionType string `description:"所属区域:国内;海外"`
  22. Province string `description:"省份"`
  23. City string `description:"城市"`
  24. Address string `description:"商家地址"`
  25. SellerId int `description:"销售ID"`
  26. SellerName string `description:"销售名称"`
  27. Leader string `description:"决策人"`
  28. IndustryId int `description:"行业ID"`
  29. IndustryName string `description:"行业名称"`
  30. CapitalScale string `description:"资金规模"`
  31. ResearchTeamSize string `description:"研究团队规模"`
  32. UserMax int `description:"用户上限"`
  33. SigningStatus int `description:"签约状态:1-首次签约;2-续约中;3-已终止;4-待签约"`
  34. Enable int `description:"状态:0-禁用;1-启用"`
  35. ContractId int `description:"当前合约ID"`
  36. SigningTime time.Time `description:"当前合约的签约时间"`
  37. ExpiredTime time.Time `description:"当前合约的到期时间"`
  38. CreateTime time.Time `description:"创建时间"`
  39. ModifyTime time.Time `description:"更新时间"`
  40. }
  41. func (m *EtaBusiness) TableName() string {
  42. return "eta_business"
  43. }
  44. func (m *EtaBusiness) PrimaryId() string {
  45. return EtaBusinessColumns.EtaBusinessId
  46. }
  47. var EtaBusinessColumns = struct {
  48. EtaBusinessId string
  49. BusinessName string
  50. BusinessCode string
  51. CreditCode string
  52. RegionType string
  53. Province string
  54. City string
  55. Address string
  56. SellerId string
  57. SellerName string
  58. Leader string
  59. IndustryId string
  60. IndustryName string
  61. CapitalScale string
  62. ResearchTeamSize string
  63. UserMax string
  64. SigningStatus string
  65. Enable string
  66. SigningTime string
  67. ExpiredTime string
  68. CreateTime string
  69. ModifyTime string
  70. }{
  71. EtaBusinessId: "eta_business_id",
  72. BusinessName: "business_name",
  73. BusinessCode: "business_code",
  74. CreditCode: "credit_code",
  75. RegionType: "region_type",
  76. Province: "province",
  77. City: "city",
  78. Address: "address",
  79. SellerId: "seller_id",
  80. SellerName: "seller_name",
  81. Leader: "leader",
  82. IndustryId: "industry_id",
  83. IndustryName: "industry_name",
  84. CapitalScale: "capital_scale",
  85. ResearchTeamSize: "research_team_size",
  86. UserMax: "user_max",
  87. SigningStatus: "signing_status",
  88. Enable: "enable",
  89. SigningTime: "signing_time",
  90. ExpiredTime: "expired_time",
  91. CreateTime: "create_time",
  92. ModifyTime: "modify_time",
  93. }
  94. func (m *EtaBusiness) Create() (err error) {
  95. o := orm.NewOrm()
  96. id, err := o.Insert(m)
  97. if err != nil {
  98. return
  99. }
  100. m.EtaBusinessId = int(id)
  101. return
  102. }
  103. func (m *EtaBusiness) CreateMulti(items []*EtaBusiness) (err error) {
  104. if len(items) == 0 {
  105. return
  106. }
  107. o := orm.NewOrm()
  108. _, err = o.InsertMulti(len(items), items)
  109. return
  110. }
  111. func (m *EtaBusiness) Update(cols []string) (err error) {
  112. o := orm.NewOrm()
  113. _, err = o.Update(m, cols...)
  114. return
  115. }
  116. func (m *EtaBusiness) UpdateMulti(items []*EtaBusiness, cols []string) (err error) {
  117. if len(items) == 0 {
  118. return
  119. }
  120. o := orm.NewOrm()
  121. for _, v := range items {
  122. _, err = o.Update(v, cols...)
  123. if err != nil {
  124. return
  125. }
  126. }
  127. return
  128. }
  129. func (m *EtaBusiness) Del() (err error) {
  130. o := orm.NewOrm()
  131. sql := fmt.Sprintf(`DELETE FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  132. _, err = o.Raw(sql, m.EtaBusinessId).Exec()
  133. return
  134. }
  135. func (m *EtaBusiness) GetItemById(id int) (item *EtaBusiness, err error) {
  136. o := orm.NewOrm()
  137. sql := fmt.Sprintf(`SELECT * FROM %s WHERE %s = ? LIMIT 1`, m.TableName(), m.PrimaryId())
  138. err = o.Raw(sql, id).QueryRow(&item)
  139. return
  140. }
  141. func (m *EtaBusiness) GetItemByCondition(condition string, pars []interface{}) (item *EtaBusiness, err error) {
  142. o := orm.NewOrm()
  143. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  144. err = o.Raw(sql, pars).QueryRow(&item)
  145. return
  146. }
  147. func (m *EtaBusiness) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  148. o := orm.NewOrm()
  149. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  150. err = o.Raw(sql, pars).QueryRow(&count)
  151. return
  152. }
  153. func (m *EtaBusiness) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusiness, err error) {
  154. o := orm.NewOrm()
  155. fields := strings.Join(fieldArr, ",")
  156. if len(fieldArr) == 0 {
  157. fields = `*`
  158. }
  159. order := `ORDER BY create_time DESC`
  160. if orderRule != "" {
  161. order = ` ORDER BY ` + orderRule
  162. }
  163. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  164. _, err = o.Raw(sql, pars).QueryRows(&items)
  165. return
  166. }
  167. func (m *EtaBusiness) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaBusiness, err error) {
  168. o := orm.NewOrm()
  169. fields := strings.Join(fieldArr, ",")
  170. if len(fieldArr) == 0 {
  171. fields = `*`
  172. }
  173. order := `ORDER BY create_time DESC`
  174. if orderRule != "" {
  175. order = ` ORDER BY ` + orderRule
  176. }
  177. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  178. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  179. return
  180. }
  181. // CreateEtaBusinessAndContract 新增商家和签约
  182. func CreateEtaBusinessAndContract(businessItem *EtaBusiness, contractItem *EtaBusinessContract) (err error) {
  183. if businessItem == nil || contractItem == nil {
  184. err = fmt.Errorf("item empty")
  185. return
  186. }
  187. o := orm.NewOrm()
  188. tx, err := o.Begin()
  189. if err != nil {
  190. return
  191. }
  192. defer func() {
  193. if err != nil {
  194. _ = tx.Rollback()
  195. return
  196. }
  197. _ = tx.Commit()
  198. }()
  199. // 商家
  200. businessId, e := tx.Insert(businessItem)
  201. if e != nil {
  202. err = fmt.Errorf("business insert err: %s", e.Error())
  203. return
  204. }
  205. businessItem.EtaBusinessId = int(businessId)
  206. // 签约
  207. contractItem.EtaBusinessId = businessItem.EtaBusinessId
  208. _, e = tx.Insert(contractItem)
  209. if e != nil {
  210. err = fmt.Errorf("contract insert err: %s", e.Error())
  211. }
  212. return
  213. }
  214. // EtaBusinessAddReq 新增商家请求体
  215. type EtaBusinessAddReq struct {
  216. BusinessName string `description:"商家名称"`
  217. CreditCode string `description:"社会统一信用码"`
  218. RegionType string `description:"所属区域:国内;海外"`
  219. Province string `description:"省份"`
  220. City string `description:"城市"`
  221. SellerId int `description:"销售ID"`
  222. SellerName string `description:"销售名称"`
  223. Leader string `description:"决策人"`
  224. IndustryId int `description:"行业ID"`
  225. IndustryName string `description:"行业名称"`
  226. CapitalScale string `description:"资金规模"`
  227. ResearchTeamSize string `description:"研究团队规模"`
  228. UserMax int `description:"用户上限"`
  229. SigningTime string `description:"签约时间"`
  230. ExpiredTime string `description:"到期时间"`
  231. IsCheck bool `description:"是否只做校验而不实际新增(业务操作上基础信息和签约时间分成两个步骤了)"`
  232. }
  233. // EtaBusinessEditReq 编辑商家请求体
  234. type EtaBusinessEditReq struct {
  235. EtaBusinessId int `description:"商家ID"`
  236. Province string `description:"省份"`
  237. City string `description:"城市"`
  238. Leader string `description:"决策人"`
  239. IndustryId int `description:"行业ID"`
  240. IndustryName string `description:"行业名称"`
  241. CapitalScale string `description:"资金规模"`
  242. ResearchTeamSize string `description:"研究团队规模"`
  243. UserMax int `description:"用户上限"`
  244. }
  245. // EtaBusinessSigningReq 商家签约请求体
  246. type EtaBusinessSigningReq struct {
  247. EtaBusinessId int `description:"商家ID"`
  248. SigningTime string `description:"当前合约的签约时间"`
  249. ExpiredTime string `description:"当前合约的到期时间"`
  250. }
  251. // EtaBusinessEnableReq 禁启用商家请求体
  252. type EtaBusinessEnableReq struct {
  253. EtaBusinessId int `description:"商家ID"`
  254. }
  255. // EtaBusinessMoveSellerReq 移动商家销售请求体
  256. type EtaBusinessMoveSellerReq struct {
  257. EtaBusinessId int `description:"商家ID"`
  258. SellerId int `description:"销售ID"`
  259. SellerName string `description:"销售名称"`
  260. }
  261. // CreateEtaBusinessCode 生成ETA商家编码
  262. func CreateEtaBusinessCode() (code string, err error) {
  263. var num int
  264. o := orm.NewOrm()
  265. sql := `SELECT COUNT(1) AS num FROM eta_business WHERE create_time >= ? `
  266. err = o.Raw(sql, time.Now().Format(utils.FormatDate)).QueryRow(&num)
  267. if err != nil {
  268. return
  269. }
  270. code = "E" + time.Now().Format("20060102") + fmt.Sprintf("%02d", num)
  271. return
  272. }
  273. // EtaBusinessListResp 商家分页列表响应体
  274. type EtaBusinessListResp struct {
  275. List []*EtaBusinessItem `description:"商家列表数据"`
  276. Paging *paging.PagingItem `description:"分页数据"`
  277. }
  278. // EtaBusinessItem ETA商家信息
  279. type EtaBusinessItem struct {
  280. EtaBusinessId int
  281. BusinessName string `description:"商家名称"`
  282. BusinessCode string `description:"商家编码"`
  283. CreditCode string `description:"社会统一信用码"`
  284. RegionType string `description:"所属区域:国内;海外"`
  285. Address string `description:"商家地址"`
  286. SellerId int `description:"销售ID"`
  287. SellerName string `description:"销售名称"`
  288. Leader string `description:"决策人"`
  289. IndustryId int `description:"行业ID"`
  290. IndustryName string `description:"行业名称"`
  291. CapitalScale string `description:"资金规模"`
  292. ResearchTeamSize string `description:"研究团队规模"`
  293. UserMax int `description:"用户上限"`
  294. SigningStatus int `description:"签约状态:1-首次签约;2-续约中;3-已终止"`
  295. Enable int `description:"状态:0-禁用;1-启用"`
  296. SigningTime string `description:"当前合约的签约时间"`
  297. ExpiredTime string `description:"当前合约的到期时间"`
  298. CreateTime string `description:"创建时间"`
  299. ModifyTime string `description:"更新时间"`
  300. }