crm_company.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. package models
  2. import (
  3. "fmt"
  4. "github.com/beego/beego/v2/client/orm"
  5. "hongze/hongze_cygx/utils"
  6. "time"
  7. )
  8. type Company struct {
  9. CompanyId int `orm:"column(company_id);pk"`
  10. CompanyName string `description:"客户名称"`
  11. CompanyType int `orm:"column(type)"`
  12. CreditCode string `description:"社会统一信用码"`
  13. CompanyCode string `description:"客户编码"`
  14. Sort int `description:"优先级"`
  15. IsFeeCustomer int `description:"是否付费用户"`
  16. Country string `description:"国家编码"`
  17. Province string `description:"省"`
  18. City string `description:"市"`
  19. Address string `description:"详细地址"`
  20. Enabled int `description:"用户状态"`
  21. CreatedTime time.Time `description:"创建时间"`
  22. LastUpdatedTime time.Time `description:"最后一次阅读时间"`
  23. Seller string `description:"销售员"`
  24. SellsId int `description:"销售员id"`
  25. CompanyBelong string `description:"客户所属,ficc:ficc客户,public_offering:公募客户,partner:合作伙伴"`
  26. StartDate string `description:"合同开始日期"`
  27. EndDate string `description:"合同结束日期"`
  28. LastType int `description:"原客户标签"`
  29. IsVip int `description:"0:普通用户,1:大客户"`
  30. FirstStartDate string `description:"首次设置为试用客户开始时间"`
  31. FirstEndDate string `description:"首次设置为试用客户结束时间"`
  32. DateType int `description:"设置流失类型,1:1个月,2:2个月,3:3个月"`
  33. Remark string `description:"备注信息"`
  34. RegionType string `description:"地区类型,国内,国外"`
  35. OpenCompanyCode string `description:"开放给第三方的客户编码,不让第三方定位我们的客户信息"`
  36. TripartiteCompanyCode string `description:"第三方给过来的客户编码,判断客户是否存在"`
  37. InteractionNum int `description:"互动总量"`
  38. InteractionNumSeller int `description:"销售可以查看的互动总量"`
  39. }
  40. type CompanyLog struct {
  41. Id int `orm:"column(id);pk"`
  42. CompanyId int `description:"客户id"`
  43. CompanyName string `description:"客户名称"`
  44. CreditCode string `description:"统一社会信用码"`
  45. CompanyCode string `description:"客户编码"`
  46. Sort int `description:"优先级"`
  47. IsFeeCustomer int `description:"是否付费用户"`
  48. Country string `description:"国家编码"`
  49. ProvinceId int `description:"省id"`
  50. CityId int `description:"市id"`
  51. Address string `description:"详细地址"`
  52. Ctype int `orm:"column(type)" description:"客户标签,1:付费客户,2:试用客户,3:流失客户,4:潜在客户" `
  53. Enabled int `description:"用户状态"`
  54. CreatedTime time.Time `description:"创建时间"`
  55. LastUpdatedTime time.Time `description:"最后修改时间"`
  56. Seller string `description:"销售员"`
  57. SellsId int `description:"销售员ID"`
  58. CompanyBelong string `description:"客户所属,ficc:ficc客户,public_offering:公募客户,partner:合作伙伴"`
  59. StartDate string `description:"合同开始日期"`
  60. EndDate string `description:"合同结束日期"`
  61. LoseReason string `description:"流失原因"`
  62. LastType int `description:"原客户标签"`
  63. FirstStartDate string `description:"首次设置为试用客户开始时间"`
  64. FirstEndDate string `description:"首次设置为试用客户结束时间"`
  65. DateType int `description:"日期类型"`
  66. AdminId int `description:"操作人id"`
  67. LogType string `description:"日志类型"`
  68. LogCreateTime time.Time `description:"日志创建时间"`
  69. Province string `description:"省"`
  70. City string `description:"市"`
  71. }
  72. type CompanyOperationRecord struct {
  73. Id int `orm:"column(id);pk"`
  74. CompanyId int `description:"客户id"`
  75. CompanyName string `description:"客户名称"`
  76. SellerId int `description:"销售员id"`
  77. SysUserId int `description:"操作者id"`
  78. SysRealName string `description:"操作者名称"`
  79. Remark string `description:"备注"`
  80. Operation string `description:"操作"`
  81. CreateTime time.Time `description:"操作时间"`
  82. ProductId int `description:"产品id"`
  83. ProductName string `description:"产品名称"`
  84. ApproveUserId int `description:"审批人id"`
  85. ApproveRealName string `description:"审批人姓名"`
  86. ApproveContent string `description:"审批人内容"`
  87. ApproveRemark string `description:"审批人内容"`
  88. Status string `description:"状态"`
  89. }
  90. type CompanyPermissionLog struct {
  91. Id int `orm:"column(id);pk"`
  92. CompanyId int
  93. ChartPermissionId int
  94. CreateTime time.Time
  95. SysUserId int
  96. SysUserName string
  97. StartDate string
  98. EndDate string
  99. ProductId int
  100. ProductName string
  101. }
  102. type CompanyProduct struct {
  103. CompanyProductId int `orm:"column(company_product_id);pk" description:"客户产品id"`
  104. CompanyId int `description:"客户id"`
  105. ProductId int `description:"产品id"`
  106. ProductName string `description:"产品名称"`
  107. CompanyName string `description:"客户名称"`
  108. Source string `description:"来源"`
  109. Reasons string `description:"新增理由"`
  110. Status string `description:"客户状态"`
  111. IndustryId int `description:"行业id"`
  112. IndustryName string `description:"行业名称"`
  113. SellerId int `description:"销售id"`
  114. SellerName string `description:"销售名称"`
  115. GroupId int `description:"销售分组id"`
  116. DepartmentId int `description:"销售部门id"`
  117. IsSuspend int `description:"1:暂停,0:启用"`
  118. SuspendTime time.Time `description:"暂停启用时间"`
  119. TryOutTime time.Time `description:"正式转试用时间"`
  120. RenewalReason string `description:"正式转试用后的续约情况说明"`
  121. LastDescriptionTime time.Time `description:"上次添加说明时间"`
  122. RenewalIntention int `description:"是否勾选无续约意向,1:确认,0:未确认"`
  123. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  124. FreezeTime time.Time `description:"冻结时间"`
  125. FreezeReason time.Time `description:"冻结理由"`
  126. Remark string `description:"备注信息"`
  127. CreateTime time.Time `description:"创建时间"`
  128. ModifyTime time.Time `description:"修改时间"`
  129. StartDate string `description:"开始日期"`
  130. EndDate string `description:"结束日期"`
  131. ContractEndDate time.Time `description:"合同结束日期"`
  132. LoseReason string `description:"流失原因"`
  133. LossTime time.Time `description:"流失时间"`
  134. CompanyType string `description:"客户类型"`
  135. OpenCode string `description:"开放给第三方的编码,不让第三方定位我们的客户信息"`
  136. }
  137. type CompanyProductLog struct {
  138. Id int `orm:"column(id);pk"`
  139. CompanyProductId int `description:"产品id"`
  140. CompanyId int `description:"客户id"`
  141. ProductId int `description:"产品id"`
  142. ProductName string `description:"产品名称"`
  143. CompanyName string `description:"客户名称"`
  144. Source string `description:"来源"`
  145. Reasons string `description:"新增理由"`
  146. Status string `description:"客户状态"`
  147. IndustryId int `description:"行业id"`
  148. IndustryName string `description:"行业名称"`
  149. SellerId int `description:"所属销售id"`
  150. SellerName string `description:"所属销售名称"`
  151. GroupId int `description:"所属销售分组id"`
  152. DepartmentId int `description:"所属销售部门id"`
  153. IsSuspend int `description:"1:暂停,0:启用"`
  154. SuspendTime time.Time `description:"暂停时间"`
  155. FreezeTime time.Time `description:"冻结时间"`
  156. Remark string `description:"备注信息"`
  157. StartDate string `description:"开始日期"`
  158. EndDate string `description:"结束日期"`
  159. CreateTime time.Time `description:"创建时间"`
  160. ModifyTime time.Time `description:"修改时间"`
  161. LoseReason string `description:"流失原因"`
  162. LossTime time.Time `description:"流失时间"`
  163. LogType string `description:"日志类型"`
  164. LogCreateTime time.Time `description:"日志创建时间"`
  165. AdminId int `description:"操作人id"`
  166. ApproveStatus string `description:"审批状态:'审批中','通过','驳回'"`
  167. FreezeStartDate string `description:"冻结开始日期"`
  168. FreezeEndDate string `description:"冻结结束日期"`
  169. }
  170. //type CompanyReportPermission struct {
  171. //之前的结构体已存在
  172. //}
  173. type CompanySource struct {
  174. SourceId int `orm:"column(source_id);pk"`
  175. SourceName string `description:"来源名称"`
  176. }
  177. // 判断公司名称是否存在
  178. func GetCompanyCountByName(companyName string) (count int, err error) {
  179. o := orm.NewOrmUsingDB("weekly_report")
  180. sql := `SELECT COUNT(1) AS count FROM company WHERE company_name = ?`
  181. err = o.Raw(sql, companyName).QueryRow(&count)
  182. return
  183. }
  184. // 判断三方公司公司名称是否存在
  185. func GetCompanyCountByThirdName(tripartiteCompanyCode string) (count int, err error) {
  186. o := orm.NewOrmUsingDB("weekly_report")
  187. sql := `SELECT COUNT(1) AS count FROM company WHERE tripartite_company_code = ?`
  188. err = o.Raw(sql, tripartiteCompanyCode).QueryRow(&count)
  189. return
  190. }
  191. // 获取来源详情
  192. func GetCompanySourceByName(companyName string) (item *CompanySource, err error) {
  193. o := orm.NewOrmUsingDB("weekly_report")
  194. sql := `SELECT * FROM company_source WHERE source_name = ?`
  195. err = o.Raw(sql, companyName).QueryRow(&item)
  196. return
  197. }
  198. func GetCompanyCode() (companyCode string, err error) {
  199. var num int
  200. o := orm.NewOrmUsingDB("weekly_report")
  201. sql := `SELECT COUNT(1) AS num FROM company WHERE created_time >=? `
  202. err = o.Raw(sql, time.Now().Format(utils.FormatDate)).QueryRow(&num)
  203. if err != nil {
  204. return
  205. }
  206. companyCode = "KH" + time.Now().Format("20060102") + fmt.Sprintf("%03d", num)
  207. return
  208. }
  209. // 新增客户
  210. func AddCompany(item *Company) (lastId int64, err error) {
  211. o := orm.NewOrmUsingDB("weekly_report")
  212. lastId, err = o.Insert(item)
  213. return
  214. }
  215. // 更新客户基础信息
  216. func (company *Company) Update(cols []string) (err error) {
  217. o := orm.NewOrmUsingDB("weekly_report")
  218. _, err = o.Update(company, cols...)
  219. fmt.Println(company)
  220. fmt.Println(err)
  221. return
  222. }
  223. // 新增客户产品
  224. func AddCompanyProduct(item *CompanyProduct) (newId int64, err error) {
  225. o := orm.NewOrmUsingDB("weekly_report")
  226. newId, err = o.Insert(item)
  227. return
  228. }
  229. // 新增客户产品
  230. func AddCompanyProductLog(item *CompanyProductLog) (err error) {
  231. o := orm.NewOrmUsingDB("weekly_report")
  232. _, err = o.Insert(item)
  233. return
  234. }
  235. func GetCompanyReportPermissionCount(companyId, productId int) (count int, err error) {
  236. o := orm.NewOrmUsingDB("weekly_report")
  237. sql := `SELECT COUNT(1) AS count FROM company_report_permission WHERE company_id=? AND product_id=? `
  238. err = o.Raw(sql, companyId, productId).QueryRow(&count)
  239. return
  240. }
  241. func DeleteCompanyReportPermission(companyId, productId int) (err error) {
  242. o := orm.NewOrmUsingDB("weekly_report")
  243. sql := ` DELETE FROM company_report_permission WHERE company_id=? AND product_id=? `
  244. _, err = o.Raw(sql, companyId, productId).Exec()
  245. return
  246. }
  247. // 新增客户权限
  248. func AddCompanyReportPermission(item *CompanyReportPermission) (err error) {
  249. o := orm.NewOrmUsingDB("weekly_report")
  250. _, err = o.Insert(item)
  251. return
  252. }
  253. // 新增客户权限日志
  254. func AddCompanyPermissionLog(item *CompanyPermissionLog) (err error) {
  255. o := orm.NewOrmUsingDB("weekly_report")
  256. _, err = o.Insert(item)
  257. return
  258. }
  259. // 新增客户操作记录
  260. func AddCompanyOperationRecord(item *CompanyOperationRecord) (lastId int64, err error) {
  261. o := orm.NewOrmUsingDB("weekly_report")
  262. lastId, err = o.Insert(item)
  263. return
  264. }
  265. // 新增客户产品
  266. func AddCompanyLog(item *CompanyLog) (err error) {
  267. o := orm.NewOrmUsingDB("weekly_report")
  268. _, err = o.Insert(item)
  269. return
  270. }
  271. type UserSellerRelation struct {
  272. RelationId int64 `orm:"column(relation_id);pk"`
  273. UserId int `description:"用户id"`
  274. CompanyId int `description:"企业用户id"`
  275. SellerId int `description:"销售id"`
  276. Seller string `description:"销售员名称"`
  277. ProductId int `description:"产品id"`
  278. Mobile string `description:"手机号"`
  279. Email string `description:"邮箱"`
  280. ModifyTime time.Time `description:"修改时间"`
  281. CreateTime time.Time `description:"创建时间"`
  282. }
  283. // 添加销售员与员工的关系
  284. func AddUserSellerRelation(userId int64, companyId, sellerId, productId int, seller, mobile, email string) (lastId int64, err error) {
  285. o := orm.NewOrmUsingDB("weekly_report")
  286. relation := UserSellerRelation{
  287. UserId: int(userId),
  288. SellerId: sellerId,
  289. CompanyId: companyId,
  290. Seller: seller,
  291. ProductId: productId,
  292. Mobile: mobile,
  293. Email: email,
  294. CreateTime: time.Now(),
  295. ModifyTime: time.Now(),
  296. }
  297. lastId, err = o.Insert(&relation)
  298. return
  299. }