cust_customer.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package cust
  2. import (
  3. "context"
  4. "strconv"
  5. "github.com/gogf/gf/database/gdb"
  6. "github.com/gogf/gf/errors/gerror"
  7. "github.com/gogf/gf/frame/g"
  8. "github.com/gogf/gf/os/gtime"
  9. "github.com/gogf/gf/util/gconv"
  10. "dashoo.cn/micro/app/dao/cust"
  11. model "dashoo.cn/micro/app/model/cust"
  12. "dashoo.cn/micro/app/service"
  13. )
  14. type customerService struct {
  15. *service.ContextService
  16. Dao *cust.CustCustomerDao
  17. BelongDao *cust.CustCustomerBelongDao
  18. }
  19. func NewCustomerService(ctx context.Context) (svc *customerService, err error) {
  20. svc = new(customerService)
  21. if svc.ContextService, err = svc.Init(ctx); err != nil {
  22. return nil, err
  23. }
  24. svc.Dao = cust.NewCustCustomerDao(svc.Tenant)
  25. svc.BelongDao = cust.NewCustCustomerBelongDao(svc.Tenant)
  26. return svc, nil
  27. }
  28. //创建客户
  29. func (c *customerService) Create(req *model.AddCustCustomerReq) (err error) {
  30. cusTomer := new(model.CustCustomer)
  31. conTact := new(model.CustCustomerContact)
  32. if err = gconv.Struct(req.Cust, cusTomer); err != nil {
  33. g.Log().Info("error", err)
  34. return
  35. }
  36. if err = gconv.Struct(req.Info, conTact); err != nil {
  37. return
  38. }
  39. g.Log().Info(err)
  40. Model := c.Dao.M
  41. record, err := Model.As("c").LeftJoin("cust_customer_contact ct", "c.id=ct.cust_id").Fields("c.*").Where(
  42. g.Map{
  43. "ct.telephone": req.Info.TelePhone,
  44. "ct.cuct_name": req.Info.CuctName,
  45. "c.cust_name": req.Cust.CustName,
  46. },
  47. ).One()
  48. g.Log().Info("recordE", record.IsEmpty())
  49. if err != nil || !record.IsEmpty() {
  50. err = gerror.New("该客户信息已存在,不可重复添加")
  51. return err
  52. }
  53. cusTomer.CustCode = strconv.Itoa(int(gtime.Timestamp()))
  54. cusTomer.IsPublic = "10"
  55. service.SetCreatedInfo(cusTomer, c.GetCxtUserId(), c.GetCxtUserName())
  56. var tx *gdb.TX
  57. tx, err = g.DB().Begin()
  58. if err != nil {
  59. err = gerror.New("事务开启失败")
  60. return
  61. }
  62. cus_res, err := Model.Insert(cusTomer)
  63. if err != nil {
  64. g.Log().Error(err)
  65. err = gerror.New("cusTomer表插入失败")
  66. tx.Rollback()
  67. return
  68. }
  69. InsertId, _ := cus_res.LastInsertId()
  70. conTact.CustId = int(InsertId)
  71. service.SetCreatedInfo(conTact, c.GetCxtUserId(), c.GetCxtUserName())
  72. _, err = cust.CustCustomerContact.Insert(conTact)
  73. if err != nil {
  74. g.Log().Error(err)
  75. err = gerror.New("conTact表插入失败")
  76. tx.Rollback()
  77. return
  78. }
  79. /**
  80. 预留逻辑判断当前登录角色是否是销售人员
  81. if 销售人员 {
  82. cusTomer.SalesId = 当前销售人员id
  83. cusTomer.SalesName = 当前销售人员姓名
  84. c.BelongDao.M.Insert()
  85. }
  86. */
  87. tx.Commit()
  88. return
  89. }
  90. //客户列表列表
  91. func (c *customerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
  92. Model := c.Dao.M.As("c").LeftJoin("cust_customer_contact ct", "c.id=ct.cust_id").Where("c.is_public ", 20) //非公海客户
  93. //客户名称
  94. if req.CustName != "" {
  95. Model = Model.Where("c.cust_name ", req.CustName)
  96. }
  97. //联系人
  98. if req.CuctName != "" {
  99. Model = Model.Where("ct.cuct_name", req.CuctName)
  100. }
  101. // 联系人姓名
  102. if req.CustStatus != "" {
  103. Model = Model.Where("ct.cuct_name", req.CuctName)
  104. }
  105. //所属销售
  106. if req.SalesName != "" {
  107. Model = Model.Where("c.sales_name ?", req.SalesName)
  108. }
  109. //联系人电话
  110. if req.Telephone != "" {
  111. Model = Model.Where("ct.telephone ?", req.Telephone)
  112. }
  113. total, err = Model.Fields("c.id").Count()
  114. if err != nil {
  115. g.Log().Error(err)
  116. err = gerror.New("获取总行数失败")
  117. return
  118. }
  119. if req.PageNum == 0 {
  120. req.PageNum = 1
  121. }
  122. fields := "c.*,ct.cuct_name,ct.telephone"
  123. err = Model.Fields(fields).Page(req.PageNum, req.PageSize).Order("id asc").Scan(&customerList)
  124. return
  125. }
  126. //分配客户
  127. func (c *customerService) DistriCustomer(req *model.DistriCustomer) error {
  128. /**
  129. 待写逻辑(销售总监或销售助理将公海客户分配给指定销售工程师)
  130. if c.user.id != 销售总监 || c.user.id!=销售助理 {
  131. err = gerror.New("该账号无权限操作!")
  132. return
  133. }
  134. */
  135. custModel := c.Dao.M
  136. rep, err := custModel.Where(cust.CustCustomer.Columns.Id+" in (?) ", req.Ids).Where(cust.CustCustomer.Columns.IsPublic, 10).All()
  137. if err != nil || rep.IsEmpty() {
  138. err = gerror.New("该数据不存在")
  139. return err
  140. }
  141. err = c.Customer(req.Ids, req.SalesId, 2)
  142. if err != nil {
  143. err = gerror.New("可配客户失败")
  144. return err
  145. }
  146. return nil
  147. }
  148. //客户详情
  149. func (c *customerService) GetEntityById(id int64) (entityInfo []*model.CustCustomer, err error) {
  150. Model := c.Dao.M
  151. err = Model.Where(cust.CustCustomer.Columns.Id, id).WithAll().Scan(&entityInfo)
  152. if err != nil {
  153. g.Log().Error(err)
  154. return nil, gerror.New("获取用户数据失败")
  155. }
  156. return
  157. }
  158. //转移客户
  159. func (c *customerService) UpdateBytransfer(req *model.CustSalesReq) (entityInfo []*model.CustCustomer, err error) {
  160. custModel := c.Dao.M
  161. rep, err := custModel.Fields("sales_id,sales_name,id").Where(cust.CustCustomer.Columns.Id+" in (?)", req.Ids).All()
  162. if err != nil || rep.IsEmpty() {
  163. err = gerror.New("该数据不存在")
  164. return
  165. }
  166. c.Customer(req.Ids, req.SalesIds, 1)
  167. belongModel := c.BelongDao.M
  168. maps := []map[string]interface{}{}
  169. date_time := gtime.Now()
  170. for _, v := range rep {
  171. old_id := v.GMap().Get("id")
  172. orig_sale_name := v.GMap().Get("sales_name")
  173. g.Log().Info("orig_sale_name", orig_sale_name)
  174. belong := map[string]interface{}{}
  175. belong["cust_id"] = old_id.(int)
  176. belong["sale_name"] = ""
  177. belong["orig_sale_name"] = orig_sale_name
  178. belong["opn_type"] = "20"
  179. belong["opn_people"] = 1
  180. belong["opn_datetime"] = date_time
  181. belong["created_by"] = 1
  182. belong["remark"] = req.Remark
  183. belong["created_name"] = "admin"
  184. belong["created_time"] = date_time
  185. belong["opn_datetime"] = date_time
  186. maps = append(maps, belong)
  187. }
  188. _, err = belongModel.Insert(maps)
  189. if err != nil {
  190. err = gerror.New("转移客户失败")
  191. return
  192. }
  193. return
  194. }
  195. func (c *customerService) Customer(ids []int64, salesId int64, behavior int8) error {
  196. custModel := c.Dao.M
  197. custCustomerData := new(model.CustCustomer)
  198. custCustomerData.SalesId = int(salesId)
  199. switch behavior {
  200. case 1:
  201. custCustomerData.SalesName = "转移销售"
  202. custCustomerData.IsPublic = "10"
  203. case 2:
  204. custCustomerData.SalesName = "分配销售"
  205. custCustomerData.IsPublic = "20"
  206. }
  207. service.SetUpdatedInfo(custCustomerData, c.GetCxtUserId(), c.GetCxtUserName())
  208. _, err := custModel.FieldsEx(
  209. cust.CustCustomer.Columns.CustCode,
  210. cust.CustCustomer.Columns.CustName,
  211. cust.CustCustomer.Columns.AbbrName,
  212. cust.CustCustomer.Columns.CustLocation,
  213. cust.CustCustomer.Columns.Id,
  214. cust.CustCustomer.Columns.CustAddress,
  215. cust.CustCustomer.Columns.CreatedTime,
  216. cust.CustCustomer.Columns.CustStatus,
  217. cust.CustCustomer.Columns.CreatedBy,
  218. cust.CustCustomer.Columns.CreatedName).
  219. WherePri(cust.CustCustomer.Columns.Id+" in (?)", ids).Update(custCustomerData)
  220. if err != nil {
  221. g.Log().Error(err)
  222. err = gerror.New("转移客户变更失败")
  223. return err
  224. }
  225. return nil
  226. }