wangxingcheng 3 years ago
parent
commit
cc1c518be3

+ 35 - 0
opms_parent/app/handler/cust/customer.go

@@ -7,6 +7,7 @@ import (
 	"dashoo.cn/opms_libary/myerrors"
 	"dashoo.cn/opms_libary/myerrors"
 	"github.com/gogf/gf/errors/gerror"
 	"github.com/gogf/gf/errors/gerror"
 	"github.com/gogf/gf/frame/g"
 	"github.com/gogf/gf/frame/g"
+	"github.com/gogf/gf/net/ghttp"
 	"github.com/gogf/gf/os/gtime"
 	"github.com/gogf/gf/os/gtime"
 	"github.com/gogf/gf/util/gconv"
 	"github.com/gogf/gf/util/gconv"
 	"github.com/gogf/gf/util/gvalid"
 	"github.com/gogf/gf/util/gvalid"
@@ -267,6 +268,22 @@ func (c *CustomerHeader) UpdateById(ctx context.Context, req *model.UpdateCustom
 	return nil
 	return nil
 }
 }
 
 
+//客户摘要
+func (c *CustomerHeader) CustAbstract(ctx context.Context, req *comm_def.IdReq, rsp *comm_def.CommonMsg) error {
+	if req.Id == 0 {
+		return gerror.New("参数有误")
+	}
+	customerServer, err := server.NewCustomerService(ctx)
+	if err != nil {
+		g.Log().Error(err)
+		return gerror.New("系统异常,请重新尝试")
+	}
+	list, err := customerServer.CustAbstract(req.Id)
+	g.Log().Info("CustAbstract", list)
+	rsp.Data = g.Map{"list": list}
+	return nil
+}
+
 //操作日志
 //操作日志
 func (c *CustomerHeader) WriteCustLog(ctx context.Context, custType string, custId []int64, req interface{}) {
 func (c *CustomerHeader) WriteCustLog(ctx context.Context, custType string, custId []int64, req interface{}) {
 	CustomerService, _ := server.NewCustomerService(ctx)
 	CustomerService, _ := server.NewCustomerService(ctx)
@@ -276,3 +293,21 @@ func (c *CustomerHeader) WriteCustLog(ctx context.Context, custType string, cust
 	custDynameics.OpnContent = req
 	custDynameics.OpnContent = req
 	CustomerService.OperationLog(ctx, custId, custDynameics)
 	CustomerService.OperationLog(ctx, custId, custDynameics)
 }
 }
+
+func (c *CustomerHeader) DeriveList(ctx context.Context, req *model.CustCustomerSearchReq, rsp *comm_def.CommonMsg) error {
+	//s := g.Server()
+	customerServer, err := server.NewCustomerService(ctx)
+	if err != nil {
+		g.Log().Error(err)
+		return gerror.New("系统异常,请重新尝试")
+	}
+	g.Server()
+	r := new(ghttp.Request)
+	buffer, _ := customerServer.Derive(req)
+	fileName := gconv.String(gtime.Now()) + ".csv"
+	r.Response.Header().Set("Content-Type", "text/csv")
+	r.Response.Header().Set("Content-Disposition", "attachment;filename="+fileName)
+	r.Response.Write(buffer.Bytes())
+
+	return nil
+}

+ 13 - 0
opms_parent/app/model/cust/cust_customer.go

@@ -71,7 +71,20 @@ type CustList struct {
 	CustLevel    string      `orm:"cust_level"     json:"custLevel"`    // 客户级别(10 重点客户 20 普通客户 30非优客户)
 	CustLevel    string      `orm:"cust_level"     json:"custLevel"`    // 客户级别(10 重点客户 20 普通客户 30非优客户)
 	CreatedName  string      `orm:"created_name"   json:"createdName"`  // 创建人
 	CreatedName  string      `orm:"created_name"   json:"createdName"`  // 创建人
 	CreatedTime  *gtime.Time `orm:"created_time"   json:"createdTime"`  // 创建时间
 	CreatedTime  *gtime.Time `orm:"created_time"   json:"createdTime"`  // 创建时间
+	//Follow       *Follow     `json:"follow"`
+}
 
 
+//跟进摘要
+type Follow struct {
+	FollowCount     int     `json:"followCount"`     // 跟进次数
+	NotFollowDay    int     `json:"notFollowDay"`    // 未跟进天数
+	Business        int     `json:"business"`        // 商机数量
+	BusinessTotal   float64 `json:"businessTotal"`   // 商机总额
+	DealCount       int     `json "dealCotal"`       // 成交次数
+	DealTotal       float64 `json "dealTotal"`       // 成交总额
+	PaymentTotal    float64 `json "paymentTotal"`    // 回款总额
+	NotPaymentTotal float64 `json "notPaymentTotal"` // 未回款总额
+	DrawTotal       float64 `json "drawTotal"`       // 开票总额
 }
 }
 
 
 //转移客户参数
 //转移客户参数

+ 110 - 2
opms_parent/app/service/cust/cust_customer.go

@@ -1,8 +1,13 @@
 package cust
 package cust
 
 
 import (
 import (
+	"bytes"
 	"context"
 	"context"
+	"encoding/csv"
+	"math"
+	"sort"
 	"strconv"
 	"strconv"
+	"time"
 
 
 	"github.com/gogf/gf/errors/gerror"
 	"github.com/gogf/gf/errors/gerror"
 	"github.com/gogf/gf/frame/g"
 	"github.com/gogf/gf/frame/g"
@@ -10,16 +15,22 @@ import (
 	"github.com/gogf/gf/util/gconv"
 	"github.com/gogf/gf/util/gconv"
 
 
 	"dashoo.cn/micro/app/dao/cust"
 	"dashoo.cn/micro/app/dao/cust"
+	platdao "dashoo.cn/micro/app/dao/plat"
 	model "dashoo.cn/micro/app/model/cust"
 	model "dashoo.cn/micro/app/model/cust"
 	"dashoo.cn/micro/app/service"
 	"dashoo.cn/micro/app/service"
 )
 )
 
 
+const TIME_LAYOUT = "2006-01-02 15:04:05"
+
+var derivetable = []string{"客户编码", "客户名称", "助记名", "所在地区", "客户行业", "客户行业", "客户状态", "最后跟进时间", "创建人", "创建时间"}
+
 type CustomerService struct {
 type CustomerService struct {
 	*service.ContextService
 	*service.ContextService
 	Dao         *cust.CustCustomerDao
 	Dao         *cust.CustCustomerDao
 	BelongDao   *cust.CustCustomerBelongDao
 	BelongDao   *cust.CustCustomerBelongDao
 	DynamicsDao *cust.CustCustomerDynamicsDao
 	DynamicsDao *cust.CustCustomerDynamicsDao
 	ContactDao  *cust.CustCustomerContactDao
 	ContactDao  *cust.CustCustomerContactDao
+	FollowDao   *platdao.PlatFollowupDao
 }
 }
 
 
 var isPublic, noPublic = "10", "20" // 公海,非公海
 var isPublic, noPublic = "10", "20" // 公海,非公海
@@ -40,9 +51,51 @@ func NewCustomerService(ctx context.Context) (svc *CustomerService, err error) {
 	svc.BelongDao = cust.NewCustCustomerBelongDao(svc.Tenant)
 	svc.BelongDao = cust.NewCustCustomerBelongDao(svc.Tenant)
 	svc.DynamicsDao = cust.NewCustCustomerDynamicsDao(svc.Tenant)
 	svc.DynamicsDao = cust.NewCustCustomerDynamicsDao(svc.Tenant)
 	svc.ContactDao = cust.NewCustCustomerContactDao(svc.Tenant)
 	svc.ContactDao = cust.NewCustCustomerContactDao(svc.Tenant)
+	svc.FollowDao = platdao.NewPlatFollowupDao(svc.Tenant)
 	return svc, nil
 	return svc, nil
 }
 }
 
 
+//导出数据
+func (c *CustomerService) Derive(req *model.CustCustomerSearchReq) (bytesBuffer *bytes.Buffer, err error) {
+	//file, err := os.Create("cust_list.csv")
+	//g.Log().Info("xxxx", "cust_list")
+	//if err != nil {
+	//	fmt.Println("open file is failed, err: ", err)
+	//}
+	req.TargetType = "11"
+	_, data, _ := c.GetList(req)
+	// 延迟关闭
+	//buffer := &bytes.Buffer{}
+	bytesBuffer = &bytes.Buffer{}
+	bytesBuffer.WriteString("xEFxBBxBF")
+	//defer file.Close()
+	//// 写入UTF-8 BOM,防止中文乱码
+	//	buffer.WriteString("\xEF\xBB\xBF")
+	w := csv.NewWriter(bytesBuffer)
+	// 写入数据
+	w.Write(derivetable)
+	w.Flush()
+	// Map写入
+	m := make(map[int][]string)
+	for k, v := range data {
+		m[k] = []string{v.CustCode, v.CustName, v.AbbrName, v.CustLocation, v.CustIndustry, v.CustLevel, v.CustStatus,
+			gconv.String(v.FollowUpDate), v.CreatedName, gconv.String(v.CreatedTime)}
+	}
+	// 按照key排序
+	var keys []int
+	for k := range m {
+		keys = append(keys, k)
+	}
+	sort.Ints(keys)
+	for _, key := range keys {
+		w.Write(m[key])
+		// 刷新缓冲
+	}
+	w.Flush()
+
+	return
+}
+
 //创建客户
 //创建客户
 func (c *CustomerService) Create(req *model.Customer) (insertId int64, err error) {
 func (c *CustomerService) Create(req *model.Customer) (insertId int64, err error) {
 	cusTomer := new(model.CustCustomer)
 	cusTomer := new(model.CustCustomer)
@@ -123,8 +176,9 @@ func (c *CustomerService) GetList(req *model.CustCustomerSearchReq) (total int,
 	g.Log().Info("serverS", req)
 	g.Log().Info("serverS", req)
 	Model := c.Dao.M
 	Model := c.Dao.M
 	Model = Model.Where(c.Dao.Columns.DeletedTime + " is null")
 	Model = Model.Where(c.Dao.Columns.DeletedTime + " is null")
-	if req.TargetType != "" {
+	if req.TargetType == "" {
 		if !req.IsPublic {
 		if !req.IsPublic {
+			g.Log().Info("ISPUblic", "xxxxxx")
 			Model = Model.Where(c.Dao.Columns.SalesId, c.CxtUser.Id).Where(c.Dao.Columns.IsPublic, noPublic)
 			Model = Model.Where(c.Dao.Columns.SalesId, c.CxtUser.Id).Where(c.Dao.Columns.IsPublic, noPublic)
 		} else {
 		} else {
 			g.Log().Info("serverS", req)
 			g.Log().Info("serverS", req)
@@ -282,7 +336,8 @@ func (c *CustomerService) DistriCustomer(req *model.DistriCustomer) error {
 
 
 //客户详情
 //客户详情
 func (c *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustList, err error) {
 func (c *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustList, err error) {
-	Model := c.Dao.M
+	Model := c.Dao.M //
+	//FollowModel := c.FollowDao.M
 
 
 	err = Model.Where(cust.CustCustomer.Columns.Id+" in (?)", ids).Scan(&entityInfo)
 	err = Model.Where(cust.CustCustomer.Columns.Id+" in (?)", ids).Scan(&entityInfo)
 	if err != nil {
 	if err != nil {
@@ -292,6 +347,59 @@ func (c *CustomerService) GetEntityById(ids []int64) (entityInfo []*model.CustLi
 	return
 	return
 }
 }
 
 
+//客户摘要
+func (c *CustomerService) CustAbstract(id int64) (followInfo *model.Follow, err error) {
+	custModel := c.Dao.M //
+	Model := c.FollowDao.M
+
+	count, err := Model.Where(c.FollowDao.Columns.CustId, id).Count()
+	g.Log().Info("count", count)
+	if err != nil {
+		g.Log().Error(err)
+		return nil, gerror.New("获取用户数据失败")
+	}
+
+	followInfo = new(model.Follow)
+	followInfo.FollowCount = count
+	//
+	find, err := custModel.Fields(c.Dao.Columns.FollowUpDate).Where(c.Dao.Columns.Id, id).FindOne()
+
+	if err != nil {
+		g.Log().Error(err)
+		return nil, gerror.New("获取用户数据失败")
+	}
+	if find.IsEmpty() {
+		followInfo.NotFollowDay = 0
+	}
+	findOne := find.Map()
+	//followDate, err := Model.Fields("max(next_time) as next_time").Where(c.FollowDao.Columns.CustId, id).Where(c.FollowDao.Columns.FollowDate, findOne["follow_up_date"]).FindOne()
+	//if followDate.IsEmpty() {
+	//	followInfo.NotFollowDay = 0
+	//}
+	//followNextTime := followDate.Map()
+	//next_time := gconv.String(followNextTime["next_time"])
+	upDate := gconv.String(findOne["follow_up_date"])
+	follow_up, err1 := time.Parse(TIME_LAYOUT, upDate)
+	now := gtime.Now()
+	follow_next, err2 := time.Parse(TIME_LAYOUT, gconv.String(now))
+	if err1 != nil || err2 != nil {
+		followInfo.NotFollowDay = 0
+	}
+	poor := follow_next.Sub(follow_up)
+	g.Log().Info("xxxxfdsaf", poor)
+	hours := float64(poor.Hours() / 24)
+	if hours < 0 {
+		followInfo.NotFollowDay = 0
+	} else {
+		followInfo.NotFollowDay = int(math.Floor(hours))
+	}
+
+	//g.Log().Info("findOne_time", findOne["follow_up_date"])
+	//g.Log().Info("next_time", next_time)
+	//g.Log().Info("ceil", math.Ceil(hours))
+	return
+}
+
 //转移客户
 //转移客户
 func (c *CustomerService) UpdateBytransfer(req *model.CustSalesReq) (entityInfo []*model.CustCustomer, err error) {
 func (c *CustomerService) UpdateBytransfer(req *model.CustSalesReq) (entityInfo []*model.CustCustomer, err error) {
 	custModel := c.Dao.M
 	custModel := c.Dao.M