eta_business.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/hz_crm_api/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. 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) GetItemByCondition(condition string, pars []interface{}) (item *EtaBusiness, err error) {
  133. o := orm.NewOrm()
  134. sql := fmt.Sprintf(`SELECT * FROM %s WHERE 1=1 %s LIMIT 1`, m.TableName(), condition)
  135. err = o.Raw(sql, pars).QueryRow(&item)
  136. return
  137. }
  138. func (m *EtaBusiness) GetCountByCondition(condition string, pars []interface{}) (count int, err error) {
  139. o := orm.NewOrm()
  140. sql := fmt.Sprintf(`SELECT COUNT(1) FROM %s WHERE 1=1 %s`, m.TableName(), condition)
  141. err = o.Raw(sql, pars).QueryRow(&count)
  142. return
  143. }
  144. func (m *EtaBusiness) GetItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string) (items []*EtaBusiness, err error) {
  145. o := orm.NewOrm()
  146. fields := strings.Join(fieldArr, ",")
  147. if len(fieldArr) == 0 {
  148. fields = `*`
  149. }
  150. order := `ORDER BY create_time DESC`
  151. if orderRule != "" {
  152. order = ` ORDER BY ` + orderRule
  153. }
  154. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s`, fields, m.TableName(), condition, order)
  155. _, err = o.Raw(sql, pars).QueryRows(&items)
  156. return
  157. }
  158. func (m *EtaBusiness) GetPageItemsByCondition(condition string, pars []interface{}, fieldArr []string, orderRule string, startSize, pageSize int) (items []*EtaBusiness, err error) {
  159. o := orm.NewOrm()
  160. fields := strings.Join(fieldArr, ",")
  161. if len(fieldArr) == 0 {
  162. fields = `*`
  163. }
  164. order := `ORDER BY create_time DESC`
  165. if orderRule != "" {
  166. order = ` ORDER BY ` + orderRule
  167. }
  168. sql := fmt.Sprintf(`SELECT %s FROM %s WHERE 1=1 %s %s LIMIT ?,?`, fields, m.TableName(), condition, order)
  169. _, err = o.Raw(sql, pars, startSize, pageSize).QueryRows(&items)
  170. return
  171. }
  172. // CreateEtaBusinessAndContract 新增商家和签约
  173. func CreateEtaBusinessAndContract(businessItem *EtaBusiness, contractItem *EtaBusinessContract) (err error) {
  174. if businessItem == nil {
  175. err = fmt.Errorf("item empty")
  176. return
  177. }
  178. o := orm.NewOrm()
  179. tx, err := o.Begin()
  180. if err != nil {
  181. return
  182. }
  183. defer func() {
  184. if err != nil {
  185. _ = tx.Rollback()
  186. return
  187. }
  188. _ = tx.Commit()
  189. }()
  190. // 商家
  191. businessId, e := tx.Insert(businessItem)
  192. if e != nil {
  193. err = fmt.Errorf("business insert err: %s", e.Error())
  194. return
  195. }
  196. businessItem.EtaBusinessId = int(businessId)
  197. // 签约
  198. if contractItem != nil {
  199. contractItem.EtaBusinessId = businessItem.EtaBusinessId
  200. contractId, e := tx.Insert(contractItem)
  201. if e != nil {
  202. err = fmt.Errorf("contract insert err: %s", e.Error())
  203. return
  204. }
  205. // 更新商家当前签约ID
  206. businessItem.ContractId = int(contractId)
  207. cols := []string{"ContractId"}
  208. _, err = tx.Update(businessItem, cols...)
  209. }
  210. return
  211. }
  212. // EtaBusinessAddReq 新增商家请求体
  213. type EtaBusinessAddReq struct {
  214. BusinessName string `description:"商家名称"`
  215. CreditCode string `description:"社会统一信用码"`
  216. RegionType string `description:"所属区域:国内;海外"`
  217. Province string `description:"省份"`
  218. City string `description:"城市"`
  219. SellerId int `description:"销售ID"`
  220. SellerName string `description:"销售名称"`
  221. Leader string `description:"决策人"`
  222. IndustryId int `description:"行业ID"`
  223. IndustryName string `description:"行业名称"`
  224. CapitalScale string `description:"资金规模"`
  225. ResearchTeamSize string `description:"研究团队规模"`
  226. UserMax int `description:"用户上限"`
  227. SigningTime string `description:"签约时间"`
  228. ExpiredTime string `description:"到期时间"`
  229. IsCheck bool `description:"是否只做校验而不实际新增(业务操作上基础信息和签约时间分成两个步骤了)"`
  230. Nation string `description:"所属国家"`
  231. }
  232. // EtaBusinessEditReq 编辑商家请求体
  233. type EtaBusinessEditReq struct {
  234. EtaBusinessId int `description:"商家ID"`
  235. Province string `description:"省份"`
  236. City string `description:"城市"`
  237. Leader string `description:"决策人"`
  238. IndustryId int `description:"行业ID"`
  239. IndustryName string `description:"行业名称"`
  240. CapitalScale string `description:"资金规模"`
  241. ResearchTeamSize string `description:"研究团队规模"`
  242. UserMax int `description:"用户上限"`
  243. Nation string `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. Province string `description:"省份"`
  286. City string `description:"城市"`
  287. Address string `description:"商家地址"`
  288. SellerId int `description:"销售ID"`
  289. SellerName string `description:"销售名称"`
  290. Leader string `description:"决策人"`
  291. IndustryId int `description:"行业ID"`
  292. IndustryName string `description:"行业名称"`
  293. CapitalScale string `description:"资金规模"`
  294. ResearchTeamSize string `description:"研究团队规模"`
  295. UserMax int `description:"用户上限"`
  296. SigningStatus int `description:"签约状态:1-首次签约;2-续约中;3-已终止"`
  297. Enable int `description:"状态:0-禁用;1-启用"`
  298. ContractId int `description:"当前合约ID"`
  299. SigningTime string `description:"当前合约的签约时间"`
  300. ExpiredTime string `description:"当前合约的到期时间"`
  301. CreateTime string `description:"创建时间"`
  302. ModifyTime string `description:"更新时间"`
  303. Nation string `description:"所属国家"`
  304. }
  305. // EtaBusinessEditSignReq 编辑商家签约请求体
  306. type EtaBusinessEditSignReq struct {
  307. EtaBusinessContractId int `description:"商家合约ID"`
  308. SigningTime string `description:"当前合约的签约时间"`
  309. ExpiredTime string `description:"当前合约的到期时间"`
  310. }
  311. // EtaBusinessRemoveSignReq 删除商家签约请求体
  312. type EtaBusinessRemoveSignReq struct {
  313. EtaBusinessContractId int `description:"商家合约ID"`
  314. }