eta_business.go 12 KB

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