cust_customer.go 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264
  1. package cust
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "github.com/gogf/gf/container/garray"
  8. "math"
  9. "strconv"
  10. "strings"
  11. "time"
  12. "dashoo.cn/opms_libary/myerrors"
  13. "dashoo.cn/opms_libary/plugin/dingtalk/message"
  14. "dashoo.cn/opms_libary/request"
  15. "github.com/360EntSecGroup-Skylar/excelize"
  16. "github.com/gogf/gf/database/gdb"
  17. "github.com/gogf/gf/encoding/gjson"
  18. "github.com/gogf/gf/frame/g"
  19. "github.com/gogf/gf/os/gtime"
  20. "github.com/gogf/gf/util/gconv"
  21. "github.com/mozillazg/go-pinyin"
  22. "dashoo.cn/micro/app/dao/cust"
  23. platdao "dashoo.cn/micro/app/dao/plat"
  24. model "dashoo.cn/micro/app/model/cust"
  25. workflowModel "dashoo.cn/micro/app/model/workflow"
  26. "dashoo.cn/micro/app/service"
  27. workflowService "dashoo.cn/micro/app/service/workflow"
  28. )
  29. type CustomerService struct {
  30. *service.ContextService
  31. Dao *cust.CustCustomerDao
  32. BelongDao *cust.CustCustomerBelongDao
  33. DynamicsDao *cust.CustCustomerDynamicsDao
  34. ContactDao *cust.CustCustomerContactDao
  35. FollowDao *platdao.PlatFollowupDao
  36. BelongServer *CustomerbelongService
  37. ContanctServer *CustomercontactService
  38. }
  39. var isPublic, noPublic = "10", "20" // 公海,非公海
  40. var AllocaTion, OperaTion, Receive, Merge = "10", "20", "30", "40" // 10分配20转移30领取40合并
  41. type OpnType struct {
  42. OperaTion string
  43. }
  44. func NewCustomerService(ctx context.Context) (svc *CustomerService, err error) {
  45. svc = new(CustomerService)
  46. if svc.ContextService, err = svc.Init(ctx); err != nil {
  47. return nil, err
  48. }
  49. svc.Dao = cust.NewCustCustomerDao(svc.Tenant)
  50. svc.BelongDao = cust.NewCustCustomerBelongDao(svc.Tenant)
  51. svc.DynamicsDao = cust.NewCustCustomerDynamicsDao(svc.Tenant)
  52. svc.ContactDao = cust.NewCustCustomerContactDao(svc.Tenant)
  53. svc.FollowDao = platdao.NewPlatFollowupDao(svc.Tenant)
  54. svc.BelongServer, _ = NewCustomerBelongService(ctx)
  55. svc.ContanctServer, _ = NewCustomerContactService(ctx)
  56. return svc, nil
  57. }
  58. // GetList 客户列表列表
  59. func (s *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int, customerList []*model.CustList, err error) {
  60. Model := s.Dao.FieldsEx(s.Dao.C.DeletedTime)
  61. if req.IsPublic {
  62. Model = Model.Where(s.Dao.C.IsPublic, isPublic)
  63. } else {
  64. Model = Model.Where(s.Dao.C.IsPublic, noPublic)
  65. if req.TargetType == "self" {
  66. Model = Model.Where(s.Dao.C.SalesId, s.CxtUser.Id)
  67. } else if req.TargetType == "other" {
  68. Model = Model.Where(s.Dao.C.SalesId, s.DataScope["userIds"]).WhereNot(s.Dao.C.SalesId, s.CxtUser.Id)
  69. } else {
  70. Model = Model.DataScope(s.Ctx, "sales_id").Where(s.Dao.C.IsPublic, noPublic)
  71. }
  72. }
  73. //客户名称
  74. if req.CustName != "" {
  75. Model = Model.Where(s.Dao.C.CustName+" like ?", "%"+req.CustName+"%")
  76. }
  77. //客户编码
  78. if req.CustCode != "" {
  79. Model = Model.Where(s.Dao.C.CustCode+" like ?", "%"+req.CustCode+"%")
  80. }
  81. //客户行业
  82. if req.CustIndustry != "" {
  83. Model = Model.Where(s.Dao.C.CustIndustry, req.CustIndustry)
  84. }
  85. //客户级别
  86. if req.CustLevel != "" {
  87. Model = Model.Where(s.Dao.C.CustLevel, req.CustLevel)
  88. }
  89. //
  90. //if req.FollowUpDate != "" {
  91. // Model = Model.Where(s.Dao.C.FollowUpDate+" like ? ", req.FollowUpDate+"%")
  92. //}
  93. //省份
  94. if req.CustProvince != "" {
  95. Model = Model.Where(s.Dao.C.CustProvince, req.CustProvince)
  96. }
  97. //市
  98. if req.CustCity != "" {
  99. Model = Model.Where(s.Dao.C.CustCity, req.CustCity)
  100. }
  101. //所属销售
  102. if req.SalesName != "" {
  103. Model = Model.Where(s.Dao.C.SalesName+" like ?", "%"+req.SalesName+"%")
  104. }
  105. // 获取日期区间范围内的记录
  106. if req.BeginTime != "" && req.EndTime != "" {
  107. begin := strings.Split(req.BeginTime, " ")[0] + " 00:00:00"
  108. end := strings.Split(req.EndTime, " ")[0] + " 23:59:59"
  109. Model = Model.WhereBetween(s.Dao.C.FollowUpDate, begin, end)
  110. }
  111. if req.IsRemovePage {
  112. Model = Model
  113. } else {
  114. Model = Model.Page(req.GetPage())
  115. }
  116. total, err = Model.Count()
  117. if err != nil {
  118. g.Log().Error(err)
  119. err = myerrors.DbError("获取总行数失败。")
  120. return
  121. }
  122. err = Model.Order("id desc").Scan(&customerList)
  123. if err != nil {
  124. g.Log().Error(err)
  125. return
  126. }
  127. return
  128. }
  129. func (s *CustomerService) customerCode(province, industry string) (string, error) {
  130. sequence, err := service.Sequence(s.Dao.DB, "customer_code")
  131. if err != nil {
  132. return "", err
  133. }
  134. return s.customerProvinceCode(province) + industry + sequence, nil
  135. }
  136. func (s *CustomerService) customerProvinceCode(province string) string {
  137. if province == "重庆市" {
  138. return "CQ"
  139. }
  140. province = strings.Trim(province, "市")
  141. province = strings.Trim(province, "省")
  142. province = strings.Trim(province, "特别行政区")
  143. province = strings.Trim(province, "自治区")
  144. province = strings.Trim(province, "壮族")
  145. province = strings.Trim(province, "回族")
  146. province = strings.Trim(province, "维吾尔")
  147. pinyinArg := pinyin.NewArgs()
  148. pinyinArg.Style = pinyin.FIRST_LETTER
  149. provincePinyin := pinyin.Pinyin(province, pinyinArg)
  150. provinceFirstletter := []string{}
  151. for _, i := range provincePinyin {
  152. provinceFirstletter = append(provinceFirstletter, i[0])
  153. }
  154. provinceCode := strings.Join(provinceFirstletter, "")
  155. provinceCode = strings.ToUpper(provinceCode)
  156. fmt.Println(provincePinyin, provinceFirstletter, provinceCode)
  157. return provinceCode
  158. }
  159. // Create 创建客户
  160. func (s *CustomerService) Create(req *model.CustomerAddSeq) (insertId int64, err error) {
  161. cusTomer := new(model.CustCustomer)
  162. count, err := s.Dao.Where(s.Dao.C.CustName, req.CustName).Count()
  163. if err != nil {
  164. g.Log().Error(err)
  165. return
  166. }
  167. if count > 0 {
  168. return 0, myerrors.TipsError("该客户信息已存在,不可重复添加")
  169. }
  170. if err = gconv.Struct(req, cusTomer); err != nil {
  171. return
  172. }
  173. service.SetCreatedInfo(cusTomer, s.GetCxtUserId(), s.CxtUser.NickName)
  174. custCode, err := s.customerCode(req.CustProvince, req.CustIndustry)
  175. if err != nil {
  176. return 0, err
  177. }
  178. cusTomer.CustCode = custCode
  179. cusTomer.CustStatus = "10"
  180. roles := s.GetCxtUserRoles()
  181. isSales := false
  182. for _, v := range roles {
  183. if v == "SalesEngineer" || v == "ProductLineManager" { // 销售角色
  184. isSales = true
  185. break
  186. }
  187. }
  188. if _, ok := s.DataScope["is_big"]; ok {
  189. isSales = true
  190. }
  191. // 销售角色
  192. if isSales {
  193. cusTomer.IsPublic = noPublic
  194. cusTomer.SalesId = s.GetCxtUserId()
  195. cusTomer.SalesName = s.CxtUser.NickName
  196. cusTomer.CustStatus = "30"
  197. insertId, err = s.Dao.InsertAndGetId(cusTomer)
  198. if err != nil {
  199. g.Log().Error(err)
  200. return 0, err
  201. }
  202. err = s.CreateBelong(gconv.Int(insertId))
  203. return insertId, err
  204. } else {
  205. cusTomer.IsPublic = isPublic
  206. insertId, err = s.Dao.InsertAndGetId(cusTomer)
  207. if err != nil {
  208. g.Log().Error(err)
  209. return 0, err
  210. }
  211. }
  212. return insertId, err
  213. }
  214. // CreateBelong 创建客户归属信息
  215. func (s *CustomerService) CreateBelong(custId int) (err error) {
  216. belong := new(model.CustomerBelongAddSeq)
  217. belong.CustId = custId
  218. belong.SaleName = s.CxtUser.NickName
  219. belong.OpnType = AllocaTion
  220. belong.OpnPeople = s.CxtUser.NickName
  221. err = s.BelongServer.Create(belong)
  222. if err != nil {
  223. g.Log().Error(err)
  224. return
  225. }
  226. return
  227. }
  228. // 删除客户
  229. func (s *CustomerService) DeleteByIds(Ids []int64) (err error) {
  230. customerCount, err := s.Dao.Where(" id in (?)", Ids).Count()
  231. if err != nil {
  232. g.Log().Error(err)
  233. return
  234. }
  235. if customerCount == 0 {
  236. err = myerrors.TipsError("客户信息不存在")
  237. return
  238. }
  239. //删除客户表
  240. s.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  241. //客户表
  242. _, err = s.Dao.TX(tx).Where("id in (?)", Ids).Delete()
  243. if err != nil {
  244. g.Log().Error(err)
  245. return err
  246. }
  247. //客户联系人表
  248. _, err = s.ContactDao.Where("cust_id in (?)", Ids).Delete()
  249. if err != nil {
  250. g.Log().Error(err)
  251. return err
  252. }
  253. //客户归属表
  254. _, err = s.BelongDao.Where("cust_id in (?)", Ids).Delete()
  255. if err != nil {
  256. g.Log().Error(err)
  257. return err
  258. }
  259. return nil
  260. })
  261. return
  262. }
  263. // UpdateById 修改客户
  264. func (s *CustomerService) UpdateById(req *model.UpdateCustomer) (err error) {
  265. //判断数据是否存在
  266. count, err := s.Dao.Where("id = ", req.Id).Count()
  267. if err != nil {
  268. g.Log().Error(err)
  269. return
  270. }
  271. if count == 0 {
  272. return
  273. }
  274. //新的客户名字是否存在
  275. num, err := s.Dao.Where(s.Dao.C.CustName, req.CustName).WhereNot(s.Dao.C.Id, req.Id).Count()
  276. if err != nil {
  277. g.Log().Error(err)
  278. return err
  279. }
  280. if num > 0 {
  281. return myerrors.TipsError(fmt.Sprintf("客户名称[%s]已存在", req.CustName))
  282. }
  283. CustomertData := new(model.CustomerAddSeq)
  284. if err = gconv.Struct(req, CustomertData); err != nil {
  285. return
  286. }
  287. service.SetUpdatedInfo(CustomertData, s.GetCxtUserId(), s.CxtUser.NickName)
  288. _, err = s.Dao.FieldsEx(s.Dao.C.CreatedTime, s.Dao.C.CreatedBy, s.Dao.C.CreatedName, s.Dao.C.Id, s.Dao.C.CustCode, s.Dao.C.SalesName, s.Dao.C.SalesId).
  289. WherePri(s.Dao.C.Id, req.Id).Update(CustomertData)
  290. if err != nil {
  291. g.Log().Error(err)
  292. return
  293. }
  294. return
  295. }
  296. var AssignCustomerRequestProcessCode = "PROC-FE42B2D1-6097-4DE8-8AC5-23541B7D5C8A" // BizCode: 11
  297. var MoveToPubicRequestProcessCode = "PROC-C030BD3D-74A3-4FE5-9EBA-DF3BA9F8AEF9" // BizCode: 12
  298. var TransCustomerRequestProcessCode = "PROC-03BE93D3-9A44-43A1-BA6D-D1BB75702542" // BizCode: 13
  299. // AssignCustomerRequest 领取客户申请
  300. func (s *CustomerService) AssignCustomerRequest(ctx context.Context, req *model.AssignCustomerReq) error {
  301. if req.ApplyRemark == "" {
  302. return myerrors.TipsError("请输入申请说明")
  303. }
  304. data, err := s.Dao.Where("id in (?)", req.Ids).LockShared().All()
  305. if err != nil {
  306. return err
  307. }
  308. if len(data) == 0 {
  309. return myerrors.TipsError("领取用户不能为空")
  310. }
  311. for _, v := range data {
  312. if v.CustStatus != "10" {
  313. return myerrors.TipsError(fmt.Sprintf("客户: %s 已被领取", v.CustName))
  314. }
  315. if v.CustProvince == "" {
  316. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在省信息", v.CustName))
  317. }
  318. if v.CustCity == "" {
  319. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在市信息", v.CustName))
  320. }
  321. if v.CustIndustry == "" {
  322. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的客户类型信息", v.CustName))
  323. }
  324. }
  325. city := []string{}
  326. cityData := s.DataScope["cust_city_id"]
  327. if cityData != nil {
  328. for _, i := range cityData.([]interface{}) {
  329. city = append(city, i.(string))
  330. }
  331. }
  332. fmt.Println(city)
  333. cust := []*model.CustCustomer{}
  334. noNeedApprovalCust := []*model.CustCustomer{}
  335. noNeedApprovalCustId := []int64{}
  336. for _, v := range data {
  337. if service.StringSlicecontains(city, strconv.Itoa(v.CustCityId)) {
  338. noNeedApprovalCust = append(noNeedApprovalCust, v)
  339. noNeedApprovalCustId = append(noNeedApprovalCustId, int64(v.Id))
  340. } else {
  341. cust = append(cust, v)
  342. }
  343. }
  344. if len(noNeedApprovalCustId) != 0 {
  345. err = s.ChangeCustBelong(noNeedApprovalCustId, req.SalesId, req.SalesName)
  346. if err != nil {
  347. return err
  348. }
  349. req.Receive = AllocaTion
  350. err = s.BatchCreatebelong(data, req)
  351. if err != nil {
  352. return err
  353. }
  354. err = s.CreateDynamics("领取客户", &model.AssignCustomerReq{
  355. Ids: noNeedApprovalCustId,
  356. SalesId: req.SalesId,
  357. SalesName: req.SalesName,
  358. Remark: "",
  359. Receive: Receive,
  360. }, noNeedApprovalCustId...)
  361. if err != nil {
  362. return err
  363. }
  364. }
  365. remark, err := json.Marshal(map[string]string{
  366. "applyUserId": strconv.Itoa(s.GetCxtUserId()),
  367. "applyUserName": s.GetCxtUserName(),
  368. })
  369. if err != nil {
  370. return err
  371. }
  372. workflowSrv, err := workflowService.NewFlowService(ctx)
  373. if err != nil {
  374. return err
  375. }
  376. for _, u := range cust {
  377. bizCode := strconv.Itoa(u.Id) + ":" + strconv.Itoa(s.GetCxtUserId())
  378. applyData := gconv.Map(u)
  379. applyData["abstract"] = fmt.Sprintf("申请人:%v 领取公海客户:%v ", s.GetCxtUserName(), u.CustName)
  380. applyData["applyUser"] = s.GetCxtUserName()
  381. applyData["applyRemark"] = req.ApplyRemark
  382. _, err := workflowSrv.CheckStartProcessInstance(bizCode, workflowModel.CustomerReceive, string(remark), applyData)
  383. if err != nil {
  384. g.Log().Error(err)
  385. return err
  386. }
  387. _, err = s.Dao.Where("id = ?", u.Id).Data(map[string]interface{}{
  388. "is_public": noPublic,
  389. "cust_status": "20",
  390. }).Update()
  391. if err != nil {
  392. return err
  393. }
  394. }
  395. return nil
  396. }
  397. func (s *CustomerService) AssignCustomerRequestApproval(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  398. remark := map[string]string{}
  399. err := json.Unmarshal([]byte(flow.Remark), &remark)
  400. if err != nil {
  401. return err
  402. }
  403. userName := remark["applyUserName"]
  404. bizCode := strings.Split(flow.BizCode, ":")
  405. if len(bizCode) != 2 {
  406. return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  407. }
  408. custId, err := strconv.Atoi(bizCode[0])
  409. if err != nil {
  410. return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  411. }
  412. userId, err := strconv.Atoi(bizCode[1])
  413. if err != nil {
  414. return fmt.Errorf("客户领取审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  415. }
  416. cust, err := s.Dao.Where("id = ?", custId).One()
  417. if err != nil {
  418. return err
  419. }
  420. if cust == nil {
  421. return fmt.Errorf("客户不存在:%s Id: %d", flow.BizCode, flow.Id)
  422. }
  423. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  424. return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  425. }
  426. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  427. return fmt.Errorf("无法识别的 Result :%s", msg.Result)
  428. }
  429. if msg.ProcessType == "terminate" {
  430. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  431. "is_public": isPublic,
  432. "cust_status": "10",
  433. }).Update()
  434. return err
  435. }
  436. pass := msg.Result == "agree"
  437. if !pass {
  438. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  439. "is_public": isPublic,
  440. "cust_status": "10",
  441. }).Update()
  442. return err
  443. }
  444. s.CxtUser = &request.UserInfo{Id: userId, UserName: userName}
  445. err = s.ChangeCustBelong([]int64{int64(custId)}, int64(userId), remark["applyUserName"])
  446. if err != nil {
  447. return err
  448. }
  449. err = s.BatchCreatebelong([]*model.CustCustomer{cust}, &model.AssignCustomerReq{
  450. Ids: []int64{int64(custId)},
  451. SalesId: int64(userId),
  452. SalesName: userName,
  453. Remark: "",
  454. Receive: Receive,
  455. })
  456. if err != nil {
  457. return err
  458. }
  459. s.CxtUser = &request.UserInfo{Id: userId, UserName: userName}
  460. return s.CreateDynamics("领取客户", map[string]interface{}{
  461. "userId": userId,
  462. "custId": cust.Id,
  463. }, int64(cust.Id))
  464. }
  465. // 移回公海
  466. func (s *CustomerService) MoveToPublicRequest(ctx context.Context, req *model.MoveToPubicRep) error {
  467. if req.Remark == "" {
  468. return myerrors.TipsError("请输入移回原因")
  469. }
  470. data, err := s.Dao.Where("id in (?)", req.Ids).All()
  471. if err != nil {
  472. return err
  473. }
  474. if len(data) == 0 {
  475. return myerrors.TipsError("移回用户不能为空")
  476. }
  477. cusType, err := service.GetDictDataByType(ctx, "cust_idy")
  478. if err != nil {
  479. return err
  480. }
  481. for _, v := range data {
  482. if v.CustStatus == "10" {
  483. return myerrors.TipsError(fmt.Sprintf("客户: %s 已被移回公海", v.CustName))
  484. }
  485. if v.CustStatus == "20" {
  486. return myerrors.TipsError(fmt.Sprintf("客户: %s 正在等待审批", v.CustName))
  487. }
  488. if v.CustProvince == "" {
  489. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在省信息", v.CustName))
  490. }
  491. if v.CustCity == "" {
  492. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在市信息", v.CustName))
  493. }
  494. if v.CustIndustry == "" {
  495. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的客户类型信息", v.CustName))
  496. }
  497. }
  498. remark, err := json.Marshal(map[string]string{
  499. "applyUserId": strconv.Itoa(s.GetCxtUserId()),
  500. "applyUserName": s.GetCxtUserName(),
  501. })
  502. if err != nil {
  503. return err
  504. }
  505. workflowSrv, err := workflowService.NewFlowService(ctx)
  506. if err != nil {
  507. return err
  508. }
  509. for _, u := range data {
  510. bizCode := strconv.Itoa(u.Id) + ":" + strconv.Itoa(s.GetCxtUserId())
  511. applyData := gconv.Map(u)
  512. applyData["abstract"] = fmt.Sprintf("申请人:%v 将客户:%v 移回公海", s.GetCxtUserName(), u.CustName)
  513. applyData["custIndustry"] = cusType[u.CustIndustry]
  514. applyData["applyUser"] = s.GetCxtUserName()
  515. applyData["remark"] = req.Remark
  516. _, err := workflowSrv.CheckStartProcessInstance(bizCode, workflowModel.CustomerPublic, string(remark), applyData)
  517. if err != nil {
  518. g.Log().Error(err)
  519. return err
  520. }
  521. _, err = s.Dao.Where("id = ?", u.Id).Data(map[string]interface{}{
  522. "cust_status": "20",
  523. }).Update()
  524. if err != nil {
  525. return err
  526. }
  527. }
  528. return nil
  529. }
  530. // 移入公海回调
  531. func (s *CustomerService) MoveToPublicApproval(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  532. remark := map[string]string{}
  533. err := json.Unmarshal([]byte(flow.Remark), &remark)
  534. if err != nil {
  535. return err
  536. }
  537. userName := remark["applyUserName"]
  538. bizCode := strings.Split(flow.BizCode, ":")
  539. if len(bizCode) != 2 {
  540. return fmt.Errorf("客户移回审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  541. }
  542. custId, err := strconv.Atoi(bizCode[0])
  543. if err != nil {
  544. return fmt.Errorf("客户移回审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  545. }
  546. userId, err := strconv.Atoi(bizCode[1])
  547. if err != nil {
  548. return fmt.Errorf("客户移回审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  549. }
  550. cust, err := s.Dao.Where("id = ?", custId).One()
  551. if err != nil {
  552. return err
  553. }
  554. if cust == nil {
  555. return fmt.Errorf("客户不存在:%s Id: %d", flow.BizCode, flow.Id)
  556. }
  557. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  558. return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  559. }
  560. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  561. return fmt.Errorf("无法识别的 Result :%s", msg.Result)
  562. }
  563. if msg.ProcessType == "terminate" {
  564. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  565. "cust_status": "30",
  566. }).Update()
  567. return err
  568. }
  569. pass := msg.Result == "agree"
  570. if !pass {
  571. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  572. "cust_status": "30",
  573. }).Update()
  574. return err
  575. }
  576. now := gtime.Now()
  577. err = s.Dao.Transaction(context.TODO(), func(ctx context.Context, tx *gdb.TX) error {
  578. //更新客户信息
  579. _, err = s.Dao.TX(tx).Data(g.Map{
  580. "cust_status": "10",
  581. "is_public": isPublic,
  582. "sales_id": 0,
  583. "dept_id": 0,
  584. "dept_name": "",
  585. "updated_time": now,
  586. }).Where("id = ?", cust.Id).Update()
  587. if err != nil {
  588. return err
  589. }
  590. //更新销售归属表结束时间
  591. _, err = s.BelongDao.TX(tx).Data(g.Map{
  592. "end_date": now,
  593. }).Where("cust_id = ?", cust.Id).Update()
  594. if err != nil {
  595. return err
  596. }
  597. return nil
  598. })
  599. if err != nil {
  600. return err
  601. }
  602. s.CxtUser = &request.UserInfo{Id: userId, UserName: userName}
  603. return s.CreateDynamics("移入公海", map[string]interface{}{
  604. "userId": userId,
  605. "custId": cust.Id,
  606. }, int64(cust.Id))
  607. }
  608. // SysAdminTransCustomer 系统管理员转移客户(不走审批)
  609. func (s *CustomerService) SysAdminTransCustomer(ctx context.Context, req *model.AssignCustomerReq) error {
  610. // 校验用户是否有修改权限
  611. arr := garray.NewStrArrayFrom(s.CxtUser.Roles, true)
  612. if !arr.Contains("SysAdmin") {
  613. return myerrors.TipsError("权限不足")
  614. }
  615. if req.Remark == "" {
  616. return myerrors.TipsError("请输入转移原因")
  617. }
  618. customerList, err := s.Dao.WherePri(req.Ids).Where(s.Dao.C.IsPublic, "20").All()
  619. if err != nil {
  620. return err
  621. }
  622. if len(customerList) == 0 {
  623. return myerrors.TipsError("转移客户不能为空")
  624. }
  625. var customerIds []int64
  626. for _, item := range customerList {
  627. customerIds = append(customerIds, int64(item.Id))
  628. }
  629. err = s.ChangeCustBelong(customerIds, req.SalesId, req.SalesName)
  630. if err != nil {
  631. return err
  632. }
  633. err = s.BatchCreatebelong(customerList, &model.AssignCustomerReq{
  634. Ids: customerIds,
  635. SalesId: req.SalesId,
  636. SalesName: req.SalesName,
  637. Remark: req.Remark,
  638. Receive: OperaTion,
  639. })
  640. if err != nil {
  641. return err
  642. }
  643. s.CreateDynamics("转移客户", map[string]interface{}{
  644. "toUserId": req.SalesId,
  645. "toUserNickName": req.SalesName,
  646. "remark": "系统管理员转移客户",
  647. }, customerIds...)
  648. return nil
  649. }
  650. // 转移客户
  651. func (s *CustomerService) TransCustomerRequest(ctx context.Context, req *model.AssignCustomerReq) error {
  652. if req.Remark == "" {
  653. return myerrors.TipsError("请输入转移原因")
  654. }
  655. data, err := s.Dao.Where("id in (?)", req.Ids).All()
  656. if err != nil {
  657. return err
  658. }
  659. if len(data) == 0 {
  660. return myerrors.TipsError("转移用户不能为空")
  661. }
  662. for _, v := range data {
  663. if v.CustStatus == "10" {
  664. return myerrors.TipsError(fmt.Sprintf("客户: %s 为公海客户", v.CustName))
  665. }
  666. if v.CustStatus == "20" {
  667. return myerrors.TipsError(fmt.Sprintf("客户: %s 正在等待审批", v.CustName))
  668. }
  669. if v.CustProvince == "" {
  670. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在省信息", v.CustName))
  671. }
  672. if v.CustCity == "" {
  673. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的所在市信息", v.CustName))
  674. }
  675. if v.CustIndustry == "" {
  676. return myerrors.TipsError(fmt.Sprintf("请先完善客户: %s 的客户类型信息", v.CustName))
  677. }
  678. }
  679. remark, err := json.Marshal(map[string]string{
  680. "applyUserId": strconv.Itoa(s.GetCxtUserId()),
  681. "applyUserName": s.GetCxtUserName(),
  682. "toUserId": strconv.FormatInt(req.SalesId, 10),
  683. "toUserName": req.SalesName,
  684. })
  685. if err != nil {
  686. return err
  687. }
  688. workflowSrv, err := workflowService.NewFlowService(ctx)
  689. if err != nil {
  690. return err
  691. }
  692. for _, u := range data {
  693. bizCode := strings.Join([]string{
  694. strconv.Itoa(u.Id),
  695. strconv.Itoa(s.GetCxtUserId()),
  696. strconv.FormatInt(req.SalesId, 10),
  697. }, ":")
  698. applyData := gconv.Map(u)
  699. applyData["abstract"] = fmt.Sprintf("申请人:%v 申请转移客户:%v ", s.GetCxtUserName(), u.CustName)
  700. applyData["applyUser"] = s.GetCxtUserName()
  701. applyData["remark"] = req.Remark
  702. _, err := workflowSrv.CheckStartProcessInstance(bizCode, workflowModel.CustomerTrans, string(remark), applyData)
  703. if err != nil {
  704. g.Log().Error(err)
  705. return err
  706. }
  707. _, err = s.Dao.Where("id = ?", u.Id).Data(map[string]interface{}{
  708. "cust_status": "20",
  709. }).Update()
  710. if err != nil {
  711. return err
  712. }
  713. }
  714. return nil
  715. }
  716. // 转移客户回调
  717. func (s *CustomerService) TransCustomerApproval(flow *workflowModel.PlatWorkflow, msg *message.MixMessage) error {
  718. remark := map[string]string{}
  719. err := json.Unmarshal([]byte(flow.Remark), &remark)
  720. if err != nil {
  721. return err
  722. }
  723. userName := remark["applyUserName"]
  724. toUserName := remark["toUserName"]
  725. bizCode := strings.Split(flow.BizCode, ":")
  726. if len(bizCode) != 3 {
  727. return fmt.Errorf("转移客户审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  728. }
  729. custId, err := strconv.Atoi(bizCode[0])
  730. if err != nil {
  731. return fmt.Errorf("转移客户审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  732. }
  733. userId, err := strconv.Atoi(bizCode[1])
  734. if err != nil {
  735. return fmt.Errorf("转移客户审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  736. }
  737. toUserId, err := strconv.Atoi(bizCode[2])
  738. if err != nil {
  739. return fmt.Errorf("转移客户审批 bizCode 不合法:%s Id: %d", flow.BizCode, flow.Id)
  740. }
  741. // user, err := s.UserDao.Where("id = ?", userId).One()
  742. // if err != nil {
  743. // return err
  744. // }
  745. // if user == nil {
  746. // return fmt.Errorf("用户不存在:%s Id: %d", flow.BizCode, flow.Id)
  747. // }
  748. // toUser, err := s.UserDao.Where("id = ?", toUserId).One()
  749. // if err != nil {
  750. // return err
  751. // }
  752. // if toUser == nil {
  753. // return fmt.Errorf("用户不存在:%s Id: %d", flow.BizCode, flow.Id)
  754. // }
  755. cust, err := s.Dao.Where("id = ?", custId).One()
  756. if err != nil {
  757. return err
  758. }
  759. if cust == nil {
  760. return fmt.Errorf("客户不存在:%s Id: %d", flow.BizCode, flow.Id)
  761. }
  762. if msg.ProcessType != "finish" && msg.ProcessType != "terminate" {
  763. return fmt.Errorf("无法识别的 ProcessType :%s", msg.ProcessType)
  764. }
  765. if msg.Result != "agree" && msg.Result != "refuse" && msg.Result != "" {
  766. return fmt.Errorf("无法识别的 Result :%s", msg.Result)
  767. }
  768. if msg.ProcessType == "terminate" {
  769. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  770. "cust_status": "30",
  771. }).Update()
  772. return err
  773. }
  774. pass := msg.Result == "agree"
  775. if !pass {
  776. _, err = s.Dao.Where("id = ?", custId).Data(map[string]interface{}{
  777. "cust_status": "30",
  778. }).Update()
  779. return err
  780. }
  781. s.CxtUser = &request.UserInfo{Id: userId, UserName: userName}
  782. err = s.ChangeCustBelong([]int64{int64(custId)}, int64(toUserId), toUserName)
  783. if err != nil {
  784. return err
  785. }
  786. err = s.BatchCreatebelong([]*model.CustCustomer{cust}, &model.AssignCustomerReq{
  787. Ids: []int64{int64(custId)},
  788. SalesId: int64(toUserId),
  789. SalesName: toUserName,
  790. Remark: "",
  791. Receive: OperaTion,
  792. })
  793. if err != nil {
  794. return err
  795. }
  796. s.CxtUser = &request.UserInfo{Id: userId, UserName: userName}
  797. return s.CreateDynamics("转移客户", map[string]interface{}{
  798. "userId": userId,
  799. "custId": cust.Id,
  800. "toUserId": toUserId,
  801. }, int64(cust.Id))
  802. }
  803. // AssignCustomer 分配客户
  804. func (s *CustomerService) AssignCustomer(ctx context.Context, req *model.AssignCustomerReq) (err error) {
  805. if req.Receive != "" {
  806. return s.AssignCustomerRequest(ctx, req)
  807. }
  808. data, err := s.Dao.Where("id in (?)", req.Ids).LockShared().All()
  809. if err != nil {
  810. g.Log().Error(err)
  811. return
  812. }
  813. if len(data) == 0 {
  814. return myerrors.TipsError("无可分配客户")
  815. }
  816. customerNames := ""
  817. for _, v := range data {
  818. if v.SalesId != 0 {
  819. return myerrors.TipsError(fmt.Sprintf("客户名称[%s]已被领取或分配", v.CustName))
  820. }
  821. if customerNames == "" {
  822. customerNames = v.CustName
  823. } else {
  824. customerNames += "、" + v.CustName
  825. }
  826. }
  827. err = s.ChangeCustBelong(req.Ids, req.SalesId, req.SalesName)
  828. if err != nil {
  829. return
  830. }
  831. req.Receive = AllocaTion
  832. err = s.BatchCreatebelong(data, req)
  833. if err != nil {
  834. return err
  835. }
  836. // 消息提醒
  837. msg := g.MapStrStr{
  838. "msgTitle": "公海客户分配提醒",
  839. "msgContent": fmt.Sprintf("客户%v已分配给您,请前往查看", customerNames),
  840. "msgType": "20",
  841. "recvUserIds": gconv.String(req.SalesId),
  842. "msgStatus": "10",
  843. "sendType": "30",
  844. }
  845. if err := service.CreateSystemMessage(msg); err != nil {
  846. g.Log().Error("公海客户分配提醒异常:", err)
  847. }
  848. return s.CreateDynamics("分配客户", req, req.Ids...)
  849. }
  850. // GetEntityById 客户详情
  851. func (s *CustomerService) GetEntityById(Ids []int64) (entityInfo []*model.CustList, err error) {
  852. Model := s.Dao //
  853. err = Model.Where(" id in (?)", Ids).Scan(&entityInfo)
  854. if err != nil {
  855. g.Log().Error(err)
  856. return
  857. }
  858. return
  859. }
  860. // GetCustNameIsExist 判断客户名称是否存在
  861. func (s *CustomerService) GetCustNameIsExist(req *model.IsExistsCustName) (exist bool, err error) {
  862. custDao := s.Dao.M
  863. if req.Id > 0 {
  864. custDao = custDao.Where("cust_name = ", req.CustName).WhereNot(" id ", req.Id)
  865. } else {
  866. custDao = custDao.Where("cust_name = ", req.CustName)
  867. }
  868. count, err := custDao.Count()
  869. if err != nil {
  870. g.Log().Error(err)
  871. return
  872. }
  873. exist = false
  874. if count > 0 {
  875. exist = true
  876. }
  877. return
  878. }
  879. // CustAbstract 客户摘要
  880. func (s *CustomerService) CustAbstract(Id int64) (followInfo *model.Follow, err error) {
  881. count, err := s.FollowDao.Where(s.FollowDao.C.CustId, Id).Count()
  882. if err != nil {
  883. g.Log().Error(err)
  884. return
  885. }
  886. followInfo = new(model.Follow)
  887. followInfo.FollowCount = count
  888. followTime, err := s.Dao.Fields(s.Dao.C.FollowUpDate, s.Dao.C.CreatedTime).FindOne(Id)
  889. if err != nil {
  890. g.Log().Error(err)
  891. return
  892. }
  893. if followTime == nil {
  894. err = myerrors.TipsError("获取客户信息不存在")
  895. return
  896. }
  897. now := gtime.Now()
  898. var difference time.Duration
  899. if followTime.FollowUpDate.IsZero() {
  900. difference = now.Sub(gtime.New(followTime.CreatedTime))
  901. } else {
  902. difference = now.Sub(gtime.New(followTime.FollowUpDate))
  903. }
  904. days := difference.Hours() / 24
  905. if days < 0 {
  906. followInfo.NotFollowDay = 0
  907. } else {
  908. followInfo.NotFollowDay = int(math.Floor(days))
  909. }
  910. return
  911. }
  912. // // TransCustomer 转移客户
  913. // func (s *CustomerService) TransCustomer(req *model.AssignCustomerReq) (err error) {
  914. // data, err := s.Dao.Fields("sales_id,sales_name,id").Where("id in (?)", req.Ids).All()
  915. // if err != nil {
  916. // g.Log().Error(err)
  917. // return err
  918. // }
  919. // if len(data) == 0 {
  920. // return myerrors.TipsError("数据不存在")
  921. // }
  922. // err = s.ChangeCustBelong(req.Ids, req.SalesId, req.SalesName)
  923. // if err != nil {
  924. // g.Log().Error(err)
  925. // return
  926. // }
  927. // req.Receive = OperaTion
  928. // s.BatchCreatebelong(data, req)
  929. // return
  930. // }
  931. // ChangeCustBelong 变更客户所属关系
  932. func (s *CustomerService) ChangeCustBelong(Ids []int64, salesId int64, salesName string) (err error) {
  933. _, err = s.Dao.Data(g.Map{
  934. "cust_status": "30",
  935. "sales_id": salesId,
  936. "is_public": noPublic,
  937. "sales_name": salesName,
  938. "updated_by": s.GetCxtUserId(),
  939. "updated_name": s.GetCxtUserName(),
  940. "updated_time": gtime.Now(),
  941. }).Where("id in (?)", Ids).Update()
  942. return err
  943. }
  944. // CreateDynamics 创建客户动态信息
  945. func (s *CustomerService) CreateDynamics(opnTpye string, content interface{}, ids ...int64) (err error) {
  946. datas := make([]*model.CustCustomerDynamics, 0)
  947. for _, id := range ids {
  948. dynameics := new(model.CustCustomerDynamics)
  949. dynameics.CustId = int(id)
  950. dynameics.OpnPeopleId = s.GetCxtUserId()
  951. dynameics.OpnPeople = s.GetCxtUserName()
  952. dynameics.OpnDate = gtime.Now()
  953. dynameics.OpnType = opnTpye
  954. v, _ := gjson.Encode(content)
  955. dynameics.OpnContent = gconv.String(v)
  956. service.SetCreatedInfo(dynameics, s.GetCxtUserId(), s.GetCxtUserName())
  957. datas = append(datas, dynameics)
  958. }
  959. _, err = s.DynamicsDao.Insert(datas)
  960. if err != nil {
  961. g.Log().Error(err)
  962. return
  963. }
  964. return
  965. }
  966. // GetDynamicsList 客户动态
  967. func (s *CustomerService) GetDynamicsList(req *model.CustomerDynameicsReq) (total int, result []interface{}, err error) {
  968. total, err = s.DynamicsDao.Where("cust_id = ", req.CustId).Count()
  969. if err != nil {
  970. g.Log().Error(err)
  971. return
  972. }
  973. dynamics := []*model.CustomerDynameicsRep{}
  974. err = s.DynamicsDao.Where("cust_id = ", req.CustId).Order("created_time desc").Scan(&dynamics)
  975. if err != nil {
  976. g.Log().Error(err)
  977. return
  978. }
  979. dynamicsList := make(map[string][]*model.CustomerDynameicsRep)
  980. for _, v := range dynamics {
  981. opnDate := gtime.New(v.OpnDate).Format("Y-m-d")
  982. dynamicsList[opnDate] = append(dynamicsList[opnDate], &model.CustomerDynameicsRep{
  983. OpnPeople: v.OpnPeople,
  984. OpnDate: v.OpnDate,
  985. OpnType: v.OpnType,
  986. OpnContent: v.OpnContent,
  987. })
  988. }
  989. result = append(result, dynamicsList)
  990. return
  991. }
  992. // MergeCustomer 合并客户
  993. func (s *CustomerService) MergeCustomer(req *model.MergeCustomerRep) (err error) {
  994. //当前目标客户是否存在并取出合并前的销售
  995. customer, err := s.Dao.Where("id = ", req.Id).One()
  996. if err != nil {
  997. g.Log().Error(err)
  998. return
  999. }
  1000. if customer == nil {
  1001. return myerrors.TipsError("该客户不存在")
  1002. }
  1003. _, err = s.ContactDao.Where(" cust_id in (?)", req.ChooseId).Delete()
  1004. if err != nil {
  1005. g.Log().Error(err)
  1006. return
  1007. }
  1008. CustomertData := new(model.CustomerAddSeq)
  1009. if err = gconv.Struct(req, CustomertData); err != nil {
  1010. g.Log().Error(err)
  1011. return
  1012. }
  1013. service.SetUpdatedInfo(CustomertData, s.GetCxtUserId(), s.GetCxtUserName())
  1014. _, err = s.Dao.FieldsEx(s.Dao.C.CreatedTime, s.Dao.C.CreatedBy,
  1015. s.Dao.C.CreatedName, s.Dao.C.Id,
  1016. s.Dao.C.CustCode).WherePri(s.Dao.C.Id, req.Id).Update(CustomertData)
  1017. if err != nil {
  1018. g.Log().Error(err)
  1019. return
  1020. }
  1021. //删除被合并的客户信息
  1022. _, err = s.Dao.Where(" id in (?)", req.ChooseId).Delete()
  1023. if err != nil {
  1024. g.Log().Error(err)
  1025. return
  1026. }
  1027. //删除 所选客户销售联系人
  1028. _, err = s.BelongDao.Where(" cust_id in (?)", req.ChooseId).Delete()
  1029. if err != nil {
  1030. g.Log().Error(err)
  1031. return
  1032. }
  1033. //插入一条合并成功的归属记录
  1034. //更新销售归属表销售结束时间
  1035. _, err = s.BelongDao.Data(
  1036. g.Map{
  1037. "updated_by": s.GetCxtUserId(),
  1038. "updated_name": s.GetCxtUserName(),
  1039. "end_date": gtime.Now(),
  1040. }).WhereIn(s.BelongDao.C.CustId, req.Id).Update()
  1041. if err != nil {
  1042. g.Log().Error(err)
  1043. return
  1044. }
  1045. req.CustomerBelongAddSeq.CustId = int(req.Id)
  1046. req.CustomerBelongAddSeq.OpnType = Merge
  1047. req.CustomerBelongAddSeq.OpnPeople = s.GetCxtUserName()
  1048. req.CustomerBelongAddSeq.OrigSaleName = customer.SalesName
  1049. req.CustomerBelongAddSeq.SaleName = req.SalesName
  1050. s.BelongServer.Create(req.CustomerBelongAddSeq)
  1051. return
  1052. }
  1053. // CreateContact 联系人(合并)预留
  1054. func (s *CustomerService) CreateContact(Ids []int64, req *model.CustCustomerContactSeq) (err error) {
  1055. _, err = s.ContactDao.Where(" cust_id in (?)", Ids).Delete()
  1056. if err != nil {
  1057. g.Log().Error(err)
  1058. return
  1059. }
  1060. s.ContanctServer.Create(req)
  1061. return
  1062. }
  1063. // BatchCreatebelong 批量插入客户归属记录表
  1064. func (s *CustomerService) BatchCreatebelong(rep []*model.CustCustomer, req *model.AssignCustomerReq, n ...interface{}) (err error) {
  1065. //更新销售归属表销售结束时间
  1066. _, err = s.BelongDao.Data(
  1067. g.Map{
  1068. "updated_by": s.GetCxtUserId(),
  1069. "updated_name": s.GetCxtUserName(),
  1070. "end_date": gtime.Now(),
  1071. }).WhereIn(s.BelongDao.C.CustId, req.Ids).Update()
  1072. if err != nil {
  1073. return err
  1074. }
  1075. var belongData []*model.CustCustomerBelong
  1076. userName := s.GetCxtUserName()
  1077. for _, v := range rep {
  1078. orig_sale_name := v.SalesName
  1079. belong := new(model.CustCustomerBelong)
  1080. belong.CustId = v.Id
  1081. belong.SaleName = req.SalesName
  1082. belong.OrigSaleName = orig_sale_name
  1083. belong.OpnType = req.Receive
  1084. belong.OpnPeople = userName
  1085. belong.CreatedName = userName
  1086. belong.StartDate = gtime.Now() //新增开始时间
  1087. belong.OpnDatetime = gtime.Now()
  1088. belong.Remark = req.Remark
  1089. belong.CreatedBy = s.GetCxtUserId()
  1090. belongData = append(belongData, belong)
  1091. }
  1092. _, err = s.BelongDao.Insert(belongData)
  1093. return err
  1094. }
  1095. // 导出数据
  1096. func (s *CustomerService) Export(ctx context.Context, req *model.CustCustomerExport) (content *model.CustExport, err error) {
  1097. var con model.CustExport
  1098. req.IsRemovePage = true // 去掉分页标识
  1099. total, data, err := s.GetList(&req.CustCustomerSearchReq)
  1100. if err != nil {
  1101. return
  1102. }
  1103. cusType, err := service.GetDictDataByType(ctx, "cust_idy")
  1104. if err != nil {
  1105. return nil, err
  1106. }
  1107. f := excelize.NewFile()
  1108. index := f.NewSheet("Sheet1")
  1109. for index, item := range req.Columns {
  1110. sheetPosition := service.Div(index+1) + "1"
  1111. f.SetCellValue("Sheet1", sheetPosition, item)
  1112. }
  1113. if total > 0 {
  1114. for ck, item := range data {
  1115. for index, v := range req.Columns {
  1116. if v == "客户编码" {
  1117. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustCode)
  1118. }
  1119. if v == "客户名称" {
  1120. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustName)
  1121. }
  1122. if v == "助记名" {
  1123. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.AbbrName)
  1124. }
  1125. if v == "助记名" {
  1126. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.AbbrName)
  1127. }
  1128. if v == "所在地区" {
  1129. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustLocation)
  1130. }
  1131. if v == "客户类型" {
  1132. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), cusType[item.CustIndustry])
  1133. }
  1134. if v == "客户级别" {
  1135. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustLevel)
  1136. }
  1137. if v == "客户状态" {
  1138. var CustStatus string
  1139. CustStatus = "正常"
  1140. if item.CustStatus != "10" {
  1141. CustStatus = "异常"
  1142. }
  1143. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), CustStatus)
  1144. }
  1145. if v == "创建人" {
  1146. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CreatedName)
  1147. }
  1148. if v == "最后跟进时间" {
  1149. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.FollowUpDate)
  1150. }
  1151. if v == "创建时间" {
  1152. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CreatedTime)
  1153. }
  1154. if v == "所在省" {
  1155. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustProvince)
  1156. }
  1157. if v == "所在市" {
  1158. f.SetCellValue("Sheet1", service.Div(index+1)+strconv.Itoa(ck+2), item.CustCity)
  1159. }
  1160. }
  1161. }
  1162. }
  1163. f.SetActiveSheet(index)
  1164. var buffer *bytes.Buffer
  1165. buffer, _ = f.WriteToBuffer()
  1166. con.Content = buffer.Bytes()
  1167. return &con, err
  1168. }