crm_company.go 14 KB

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