cust_customer.go 6.5 KB

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