Browse Source

Merge branch 'cygx_10.9.1'

ziwen 1 year ago
parent
commit
4bf809bb58

+ 108 - 0
controllers/company.go

@@ -9226,3 +9226,111 @@ func (this *CompanyController) DelRemark() {
 	br.Success = true
 	br.Msg = "操作成功"
 }
+
+// @Title 获取流水信息接口
+// @Description 获取流水信息接口
+// @Param   CompanyId   query   int  true       "客户id"
+// @Success 200 {object} company.CygxActivityPointsBillListResp
+// @router /company/activity/special/points/bill [get]
+func (this *CompanyTodoController) CompanyActivitySpecialPointsBill() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	companyId, _ := this.GetInt("CompanyId")
+	if companyId <= 0 {
+		br.Msg = "请选择客户"
+		return
+	}
+
+	// 获取流水信息
+	var condition string
+	var pars []interface{}
+
+	condition += ` AND company_id = ? `
+	pars = append(pars, companyId)
+
+	//查询当年的数据
+	condition += ` AND b.create_time >= ?  `
+	pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
+	list, err := cygx.GetCygxActivitySpecialTripBillDetailListAll(condition, pars)
+	if err != nil {
+		br.Msg = "获取任务信息失败"
+		br.ErrMsg = "获取任务信息失败,err:" + err.Error()
+		return
+	}
+
+	//userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(companyId)
+	//if err != nil {
+	//	br.Msg = "获取专项调研剩余次数失败"
+	//	br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+	//	return
+	//}
+	//chartMap := map[int]string{utils.YI_YAO_ID: utils.YI_YAO_NAME, utils.XIAO_FEI_ID: utils.XIAO_FEI_NAME, utils.KE_JI_ID: utils.KE_JI_NAME, utils.ZHI_ZAO_ID: utils.ZHI_ZAO_NAME}
+	resp := new(cygx.CygxActivitySpecialPointsBillResp)
+	//lastTripRemaining := 0
+	for _, v := range list {
+		item := cygx.CygxActivitySpecialPointsBillRespItem{
+			Id:                  v.Id,
+			CreateTime:          v.CreateTime.Format(utils.FormatDateTime),
+			CompanyId:           v.CompanyId,
+			CompanyName:         v.CompanyName,
+			RealName:            v.RealName,
+			BillDetailed:        v.BillDetailed,
+			ActivityName:        v.ResearchTheme,
+			ChartPermissionId:   v.ChartPermissionId,
+			ChartPermissionName: v.ChartPermissionName,
+			Total:               v.Total,
+		}
+		if v.ActivityId == 0 {
+			item.Content = v.Content
+		} else {
+			if v.DoType == 1 && v.Way != 4 {
+				item.Content = item.ActivityName + "--报名"
+			} else if v.DoType == 2 && v.Way != 4 {
+				item.Content = item.ActivityName + "--取消报名"
+			} else if v.Way == 4 {
+				item.Content = item.ActivityName + "--活动取消"
+			}
+		}
+		//if userType == 2 {
+		//	if i== 0 {
+		//		item.Total += strconv.Itoa(tripRemaining) + "次"
+		//	} else {
+		//		item.Total += strconv.Itoa(tripRemaining-list[i-1].BillDetailed) + "次"
+		//	}
+		//	//if v.BillDetailed < 4 {
+		//	//	item.Total += strconv.Itoa(lastTripRemaining-list[i-1].BillDetailed) + "次"
+		//	//} else {
+		//	//	item.Total += strconv.Itoa(lastTripRemaining) + "次"
+		//	//}
+		//} else {
+		//	for k, num := range mapChartName {
+		//		if i== 0 {
+		//			item.Total += k + strconv.Itoa(num) + "次+"
+		//		} else {
+		//			if list[i-1].ActivityId == 0 {
+		//				item.Total += k + strconv.Itoa(num) + "次+"
+		//			} else {
+		//				item.Total += k + strconv.Itoa(num-list[i-1].BillDetailed) + "次+"
+		//			}
+		//		}
+		//		//if num > 0 {
+		//		//	if i== 0{
+		//		//		item.Total += k + strconv.Itoa(num) + "次+"
+		//		//	} else {
+		//		//		item.Total += k + strconv.Itoa(num-v.BillDetailed) + "次+"
+		//		//	}
+		//		//}
+		//	}
+		//	item.Total = strings.TrimRight(item.Total, "+")
+		//}
+		resp.List = append(resp.List, &item)
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+	br.Data = resp
+}

+ 2 - 0
controllers/cygx/activity_special.go

@@ -521,6 +521,8 @@ func (this *ActivitySpecialCoAntroller) PublishAndCancel() {
 		updateParams["PublishDate"] = time.Now()
 	} else {
 		item.PublishStatus = 0
+		// 取消专项调研返点
+		go cygxService.ActivitySpecialPublishAndCancel(activityInfo)
 	}
 	updateParams["PublishStatus"] = item.PublishStatus
 	item.ActivityId = req.ActivityId

+ 118 - 26
controllers/cygx/activity_special_trip.go

@@ -63,7 +63,7 @@ func (this *ActivitySpecialTripCoAntroller) AddUser() {
 		uids += strconv.Itoa(v.UserId) + ","
 	}
 	uids = strings.TrimRight(uids, ",")
-	for _, v := range uidList {
+	for i, v := range uidList {
 		uid := v.UserId
 		wxUser, userErr := models.GetWxUserByUserId(uid)
 		if userErr != nil {
@@ -128,6 +128,12 @@ func (this *ActivitySpecialTripCoAntroller) AddUser() {
 				return
 			}
 			if total == 0 {
+				totalAll, err := cygx.GetUserActivitySpecialTripCount(uid, activityId)
+				if err != nil {
+					br.Msg = "获取信息失败"
+					br.ErrMsg = "获取日程数量信息失败,Err:" + err.Error()
+					return
+				}
 				//流水记录表
 				itemBill := new(cygx.CygxActivitySpecialTripBill)
 				itemBill.UserId = infoUser.UserId
@@ -145,36 +151,68 @@ func (this *ActivitySpecialTripCoAntroller) AddUser() {
 				itemBill.ChartPermissionId = activityInfo.ChartPermissionId
 				itemBill.AdminId = AdminUser.AdminId
 
-				item := new(cygx.CygxActivitySpecialTrip)
-				item.UserId = infoUser.UserId
-				item.RealName = infoUser.RealName
-				item.SellerName = infoUser.SellerName
-				item.ActivityId = activityId
-				item.CreateTime = time.Now()
-				item.Mobile = infoUser.Mobile
-				item.Email = infoUser.Email
-				item.Email = infoUser.Email
-				item.CompanyId = infoUser.CompanyId
-				item.CompanyName = infoUser.CompanyName
-				item.AdminId = AdminUser.AdminId
-				item.Source = 2
-				//优先绑定用户修改过的外呼手机号
-				if infoUser.OutboundMobile != "" {
-					item.OutboundMobile = infoUser.OutboundMobile
-					if infoUser.OutboundCountryCode == "" {
-						item.CountryCode = "86"
+				if totalAll == 0 {
+					item := new(cygx.CygxActivitySpecialTrip)
+					item.UserId = infoUser.UserId
+					item.RealName = infoUser.RealName
+					item.SellerName = infoUser.SellerName
+					item.ActivityId = activityId
+					item.CreateTime = time.Now()
+					item.Mobile = infoUser.Mobile
+					item.Email = infoUser.Email
+					item.Email = infoUser.Email
+					item.CompanyId = infoUser.CompanyId
+					item.CompanyName = infoUser.CompanyName
+					item.AdminId = AdminUser.AdminId
+					item.Source = 2
+					//优先绑定用户修改过的外呼手机号
+					if infoUser.OutboundMobile != "" {
+						item.OutboundMobile = infoUser.OutboundMobile
+						if infoUser.OutboundCountryCode == "" {
+							item.CountryCode = "86"
+						} else {
+							item.CountryCode = infoUser.OutboundCountryCode
+						}
 					} else {
-						item.CountryCode = infoUser.OutboundCountryCode
+						item.OutboundMobile = infoUser.Mobile
+						if infoUser.CountryCode == "" {
+							item.CountryCode = "86"
+						} else {
+							item.CountryCode = infoUser.CountryCode
+						}
 					}
+					items = append(items, item)
 				} else {
-					item.OutboundMobile = infoUser.Mobile
-					if infoUser.CountryCode == "" {
-						item.CountryCode = "86"
-					} else {
-						item.CountryCode = infoUser.CountryCode
+					err = cygx.UpdateSpecialTrip(1,0,uid, activityId)
+					if err != nil {
+						br.Msg = "报名失败,"
+						br.ErrMsg = "二次报名,更改报名是否有效状态失败,Err:" + err.Error()
+						return
+					}
+					resultTime := utils.StrTimeToTime(activityInfo.ActivityTime)
+					//48小时之内的取消也扣除一次参会记录
+					if time.Now().Add(+time.Hour * 48).After(resultTime) {
+						itemBill.BillDetailed = 0 //48小时之内,取消报名之后二次报名,不扣除流水记录
 					}
 				}
-				items = append(items, item)
+				userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(infoUser.CompanyId)
+				if err != nil {
+					br.Msg = "获取专项调研剩余次数失败"
+					br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+					return
+				}
+				if userType == 2 {
+					tripRemaining = tripRemaining + itemBill.BillDetailed-i
+					itemBill.Total = strconv.Itoa(tripRemaining) + "次"
+				} else {
+					for k, num := range mapChartName {
+						if activityInfo.ChartPermissionName == k {
+							num = num + itemBill.BillDetailed - i
+						}
+						itemBill.Total += k + strconv.Itoa(num) + "次+"
+					}
+					itemBill.Total = strings.TrimRight(itemBill.Total, "+")
+				}
 				itemsBill = append(itemsBill, itemBill)
 			}
 
@@ -508,6 +546,24 @@ func (this *ActivitySpecialTripCoAntroller) TripCancel() {
 		br.ErrMsg = "修改失败 Err:" + err.Error()
 		return
 	}
+	userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(infoUser.CompanyId)
+	if err != nil {
+		br.Msg = "获取专项调研剩余次数失败"
+		br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+		return
+	}
+	if userType == 2 {
+		tripRemaining += itemBill.BillDetailed
+		itemBill.Total = strconv.Itoa(tripRemaining) + "次"
+	} else {
+		for k, num := range mapChartName {
+			if activityInfo.ChartPermissionName == k {
+				num += itemBill.BillDetailed
+			}
+			itemBill.Total += k + strconv.Itoa(num) + "次+"
+		}
+		itemBill.Total = strings.TrimRight(itemBill.Total, "+")
+	}
 	err = cygx.AddCygxActivitySpecialTripBill(itemBill)
 	if err != nil {
 		br.Msg = "修改失败"
@@ -736,6 +792,24 @@ func (this *ActivitySpecialTripCoAntroller) MeetingDo() {
 				itemBill.ChartPermissionId = activityInfo.ChartPermissionId
 				itemBill.AdminId = AdminUser.AdminId
 				itemBill.Way = 2
+				userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
+				if err != nil {
+					br.Msg = "获取专项调研剩余次数失败"
+					br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+					return
+				}
+				if userType == 2 {
+					tripRemaining += itemBill.BillDetailed
+					itemBill.Total = strconv.Itoa(tripRemaining) + "次"
+				} else {
+					for k, num := range mapChartName {
+						if activityInfo.ChartPermissionName == k {
+							num += itemBill.BillDetailed
+						}
+						itemBill.Total += k + strconv.Itoa(num) + "次+"
+					}
+					itemBill.Total = strings.TrimRight(itemBill.Total, "+")
+				}
 				itemsBill = append(itemsBill, itemBill)
 			}
 		}
@@ -775,6 +849,24 @@ func (this *ActivitySpecialTripCoAntroller) MeetingDo() {
 			itemBill.ChartPermissionId = activityInfo.ChartPermissionId
 			itemBill.AdminId = AdminUser.AdminId
 			itemBill.Way = 2
+			userType, tripRemaining, mapChartName, err := cygxService.GetChartPermissionSpecialSurplusByCompany(v.CompanyId)
+			if err != nil {
+				br.Msg = "获取专项调研剩余次数失败"
+				br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+				return
+			}
+			if userType == 2 {
+				tripRemaining -= 1
+				itemBill.Total = strconv.Itoa(tripRemaining) + "次"
+			} else {
+				for k, num := range mapChartName {
+					if activityInfo.ChartPermissionName == k {
+						num -= 1
+					}
+					itemBill.Total += k + strconv.Itoa(num) + "次+"
+				}
+				itemBill.Total = strings.TrimRight(itemBill.Total, "+")
+			}
 			itemsBill = append(itemsBill, itemBill)
 		}
 		items = append(items, item)

+ 17 - 0
models/company/company_permission.go

@@ -623,3 +623,20 @@ func GetReportPermissionIdsByCompanyIds(companyIds []int) (items []*CompanyListP
 	_, err = o.Raw(sql, companyIds).QueryRows(&items)
 	return
 }
+
+
+// GetCompanyReportPermissionByCompanyIdAndProductId 获取权限类型
+func GetCompanyReportPermissionByCompanyIdAndProductId(companyId, productId int) (items []*CompanyReportPermission, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+	re.*
+FROM
+	company_report_permission  as re 
+	INNER JOIN chart_permission as  c ON c.chart_permission_id = re.chart_permission_id
+WHERE
+	re.company_id = ? 
+	AND re.product_id = ? 
+	AND STATUS = '正式' `
+	_, err = o.Raw(sql, companyId, productId).QueryRows(&items)
+	return
+}

+ 19 - 2
models/cygx/activity_special_trip.go

@@ -113,8 +113,9 @@ func AddCygxActivitySpecialTrip(items []*CygxActivitySpecialTrip, itemsBill []*C
 	}
 
 	//添加流水记录
-	for _, item := range itemsBill {
-		_, err = to.Insert(item)
+	//倒序插入,让流水表看起来是正的
+	for i := len(itemsBill) - 1; i >= 0; i-- {
+		_, err = to.Insert(itemsBill[i])
 		if err != nil {
 			return
 		}
@@ -280,3 +281,19 @@ func GetCygxActivitySpecialTripListCondition(condition string, pars []interface{
 	}
 	return
 }
+
+//获取某一用户的报名的数量
+func GetUserActivitySpecialTripCount(uid, activityId int) (count int, err error) {
+	sqlCount := `SELECT COUNT(1) AS count FROM cygx_activity_special_trip  WHERE  user_id=?  AND   activity_id =? `
+	o := orm.NewOrm()
+	err = o.Raw(sqlCount, uid, activityId).QueryRow(&count)
+	return
+}
+
+
+func UpdateSpecialTrip(isValid, isCancel, userId, activityId int) (err error) {
+	o := orm.NewOrm()
+	sql := `UPDATE cygx_activity_special_trip SET is_valid = ?,create_time=NOW(),is_cancel=?  WHERE user_id=? AND activity_id=? `
+	_, err = o.Raw(sql, isValid, isCancel, userId, activityId).Exec()
+	return
+}

+ 110 - 2
models/cygx/activity_special_trip_bill.go

@@ -21,7 +21,9 @@ type CygxActivitySpecialTripBill struct {
 	DoType            int       `description:"操作方式,1报名,2取消报名"`
 	RegisterPlatform  int       `description:"来源 1小程序,2:网页"`
 	ChartPermissionId int       `description:"行业id"`
-	Way               int       `description:"1报名,取消报名。2到会取消到会"`
+	Way               int       `description:"1报名,取消报名。2到会取消到会 3转正或清零 4取消活动"`
+	Content           string    `description:"内容"`
+	Total             string    `description:"总和"`
 }
 
 type CygxActivitySpecialTripBillList struct {
@@ -41,6 +43,8 @@ type CygxActivitySpecialTripBillList struct {
 	RegisterPlatform    int       `description:"来源 1小程序,2:网页"`
 	ChartPermissionId   int       `description:"行业id"`
 	ChartPermissionName string    `description:"行业名称"`
+	Way                 int       `description:"1报名,取消报名。2到会取消到会 3转正或清零 4取消活动"`
+	Content             string    `description:"内容"`
 }
 
 // 添加
@@ -70,13 +74,31 @@ func GetCygxActivitySpecialTripBillList(condition string, pars []interface{}) (i
 			c.chart_permission_name 
 		FROM
 			cygx_activity_special_trip_bill AS b
-			INNER JOIN chart_permission AS c ON c.chart_permission_id = b.chart_permission_id 
+			LEFT JOIN chart_permission AS c ON c.chart_permission_id = b.chart_permission_id 
 		WHERE
 			1 = 1` + condition
 	_, err = o.Raw(sql, pars).QueryRows(&item)
 	return
 }
 
+type AirborneCount struct {
+	Count             int
+	ChartPermissionId int ` description:"品种权限ID"`
+}
+
+// 获取空降的公司报名的记录
+func GetActivitySpecialTripAirborneListByActivitySpecial(condition string, pars []interface{}) (items []*AirborneCount, err error) {
+	sqlCount := ` SELECT chart_permission_id,COUNT(1) AS count
+		FROM
+			cygx_activity_special_meeting_detail AS t
+			INNER JOIN cygx_activity_special AS a ON a.activity_id = t.activity_id 
+		WHERE
+			 1= 1  	AND YEAR ( t.create_time )= YEAR (NOW()) ` + condition + `GROUP BY chart_permission_id`
+	o := orm.NewOrm()
+	_, err = o.Raw(sqlCount, pars).QueryRows(&items)
+	return
+}
+
 // 获取空降的公司报名的记录
 func GetActivitySpecialTripAirborneCountByActivitySpecial(condition string, pars []interface{}) (count int, err error) {
 	sqlCount := ` SELECT COUNT(1) AS count
@@ -89,3 +111,89 @@ func GetActivitySpecialTripAirborneCountByActivitySpecial(condition string, pars
 	err = o.Raw(sqlCount, pars).QueryRow(&count)
 	return
 }
+
+type CygxActivitySpecialTripBillDetailList struct {
+	Id                  int       `orm:"column(id);pk"`
+	UserId              int       `description:"用户id,多个用,隔开"`
+	ActivityId          int       `description:"活动ID"`
+	ResearchTheme       string    `description:"调研主题"`
+	CreateTime          time.Time `description:"创建时间"`
+	Mobile              string    `description:"手机号"`
+	Email               string    `description:"邮箱号"`
+	CompanyId           int       `description:"公司ID"`
+	CompanyName         string    `description:"公司名称"`
+	RealName            string    `description:"用户实际名称"`
+	AdminId             int       `description:"销售/管理员ID"`
+	Source              int       `description:"来源,1小程序,2后台添加, 3开发人员手动添加"`
+	BillDetailed        int       `description:"流水明细,判断是进账还是出账"`
+	DoType              int       `description:"操作方式,1报名,2取消报名"`
+	RegisterPlatform    int       `description:"来源 1小程序,2:网页"`
+	ChartPermissionId   int       `description:"行业id"`
+	ChartPermissionName string    `description:"行业名称"`
+	Content             string    `description:"内容"`
+	Way                 int       `description:"1报名,取消报名。2到会取消到会 3转正或清零 4取消活动"`
+	Total               string    `description:"总和"`
+}
+
+func GetCygxActivitySpecialTripBillDetailList(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBillDetailList, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			b.*,
+			a.research_theme,
+			c.chart_permission_name 
+		FROM
+			cygx_activity_special_trip_bill AS b
+			INNER JOIN chart_permission AS c ON c.chart_permission_id = b.chart_permission_id 
+			INNER JOIN cygx_activity_special AS a ON a.activity_id = b.activity_id
+		WHERE
+			1 = 1` + condition
+	_, err = o.Raw(sql, pars).QueryRows(&item)
+	return
+}
+
+type CygxActivitySpecialPointsBillRespItem struct {
+	Id                  int    `gorm:"column:id;primary_key;AUTO_INCREMENT"`
+	Content             string `gorm:"column:content" `                                 // 内容说明
+	Total               string `gorm:"column:points;default:0;NOT NULL" `               // 合计
+	CreateTime          string `gorm:"column:create_time" `                             // 创建时间
+	CompanyId           int    `gorm:"column:company_id;default:0" `                    // 公司ID
+	CompanyName         string `gorm:"column:company_name" `                            // 公司名称
+	RealName            string `gorm:"column:real_name"`                                // 用户实际名称
+	BillDetailed        int    `gorm:"column:bill_detailed;default:0;NOT NULL" json:""` // 流水明细,判断是进账还是出账
+	ActivityName        string `description:"活动标题"`
+	ChartPermissionId   int    `description:"行业id"`
+	ChartPermissionName string `description:"行业名称"`
+}
+
+type CygxActivitySpecialPointsBillResp struct {
+	List []*CygxActivitySpecialPointsBillRespItem
+}
+
+func GetCygxActivitySpecialTripBillDetailListAll(condition string, pars []interface{}) (item []*CygxActivitySpecialTripBillDetailList, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			b.*,
+			a.research_theme,
+			c.chart_permission_name 
+		FROM
+			cygx_activity_special_trip_bill AS b
+			LEFT JOIN chart_permission AS c ON c.chart_permission_id = b.chart_permission_id 
+			LEFT JOIN cygx_activity_special AS a ON a.activity_id = b.activity_id
+		WHERE
+			1 = 1` + condition
+
+	sql += ` ORDER BY b.create_time DESC`
+	_, err = o.Raw(sql, pars).QueryRows(&item)
+	return
+}
+
+func GetCygxActivitySpecialTripBillByCompanyId(companyId int) (item *CygxActivitySpecialTripBill, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT
+			*
+		FROM
+			cygx_activity_special_trip_bill WHERE company_id = ? ORDER BY create_time DESC LIMIT 1
+		`
+	err = o.Raw(sql, companyId).QueryRow(&item)
+	return
+}

+ 39 - 0
models/cygx/cygx_activity_special_points_company.go

@@ -0,0 +1,39 @@
+package cygx
+
+import (
+	"github.com/beego/beego/v2/client/orm"
+	"time"
+)
+
+type CygxActivitySpecialInheritPointsCompany struct {
+	Id                  int       `orm:"column(id);pk"`
+	CompanyId           int       // 公司ID
+	CompanyName         string    // 公司名称
+	Points              int   // 公司剩余点数
+	CreateTime          time.Time // 创建时间
+	ModifyTime          time.Time // 更新时间
+	ChartPermissionId   int       // 品种ID
+	ChartPermissionName string    // 品种名称
+}
+
+func AddCygxActivitySpecialPointsCompany(item *CygxActivitySpecialInheritPointsCompany) (err error) {
+	o := orm.NewOrm()
+	_, err = o.Insert(item)
+	if err != nil {
+		return
+	}
+	return
+}
+
+func AddCygxActivitySpecialInheritPointsCompanyMulti(items []*CygxActivitySpecialInheritPointsCompany) (err error) {
+	o := orm.NewOrm()
+	_, err = o.InsertMulti(1, items)
+	return
+}
+
+func GetCygxActivitySpecialInheritPointsByCompanyId(companyId int) (list []*CygxActivitySpecialInheritPointsCompany, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT * FROM cygx_activity_special_inherit_points_company WHERE company_id = ?  `
+	_, err = o.Raw(sql,companyId).QueryRows(&list)
+	return
+}

+ 9 - 0
routers/commentsRouter.go

@@ -8899,6 +8899,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:CompanyTodoController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:CompanyTodoController"],
+        beego.ControllerComments{
+            Method: "CompanyActivitySpecialPointsBill",
+            Router: `/company/activity/special/points/bill`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:CompanyTodoController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers:CompanyTodoController"],
         beego.ControllerComments{
             Method: "CompanyTryOutDayList",

+ 24 - 0
services/company_apply/company_approval.go

@@ -391,6 +391,29 @@ func Approved(approvalRecord *contract.ContractApprovalRecord, opUser *system.Ad
 				// 处理权益客户的身份信息()
 				cygxService.UpdateCygxCompanyUserType(recordInfo.CompanyContractId)
 
+				if recordInfo.ApplyMethod == 1{
+					//获取合同信息
+					contractInfo, tmpErr := company.GetCompanyContractDetail(recordInfo.CompanyId, recordInfo.ProductId, recordInfo.CompanyContractId)
+					if tmpErr != nil {
+						err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId, ";Err:"+tmpErr.Error()))
+						return
+					}
+					if contractInfo == nil {
+						err = errors.New(fmt.Sprint("待审批信息不存在,CompanyId:", recordInfo.CompanyId, ";productId:", recordInfo.ProductId))
+						return
+					}
+
+					//格式化合同开始时间
+					contractStartDate, tmpErr := time.Parse(utils.FormatDate, contractInfo.StartDate)
+					if tmpErr != nil {
+						err = errors.New("合同开始时间转换失败,Err:" + tmpErr.Error())
+						return
+					}
+					if time.Now().After(contractStartDate) {
+						cygxService.ActivitySpecialCompanyApproval(recordInfo.CompanyId, companyInfo.CompanyName) //审批通过的时候专项调研次数更新
+					}
+				}
+
 				//权益销售客户申请转正后,消息群发给所有销售
 				services.AddCompanyApprovalMessageRai(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
 
@@ -398,6 +421,7 @@ func Approved(approvalRecord *contract.ContractApprovalRecord, opUser *system.Ad
 				services.AddCompanyApprovalMessageWangYang(recordInfo.CompanyId, recordInfo.CompanyContractId, recordInfo.ApplyRealName, companyInfo.CompanyName)
 
 				go cygxService.YanXuanCompanyApproval(recordInfo.CompanyId) //研选审批通过的时候研选扣点更新
+
 			}
 		}()
 	} else {

+ 367 - 61
services/cygx/activity_special.go

@@ -223,48 +223,55 @@ func GetSpecialSurplusByCompany(companyId int) (specialSurplus string, err error
 	if companyDetail.Status == "永续" {
 		specialSurplus = "不限次数"
 	}
+	// 获取继承点数
+	inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
+	}
+	//chartMap := map[int]string{utils.YI_YAO_ID:utils.YI_YAO_NAME, utils.XIAO_FEI_ID:utils.XIAO_FEI_NAME, utils.KE_JI_ID:utils.KE_JI_NAME, utils.ZHI_ZAO_ID:utils.ZHI_ZAO_NAME}
+	//chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
+	chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
+	var condition string
+	var pars []interface{}
+
+	condition += ` AND company_id = ? `
+	pars = append(pars, companyId)
+
+	//查询当年的数据
+	condition += ` AND b.create_time >= ?  `
+	pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
+	listTripBill, e := cygx.GetCygxActivitySpecialTripBillList(condition, pars)
+	if e != nil {
+		err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
+		return
+	}
 
 	if companyDetail.Status == "正式" {
-		userType, packageType, _, _, _ := GetUserType(companyId)
-		var airborneTota int
+		userType, _, _, _, _ := GetUserType(companyId)
 		if userType == 2 {
-			var condition string
-			var pars []interface{}
-
-			condition += ` AND company_id = ? `
-			pars = append(pars, companyId)
-
-			//到会空降的也加入流水记录表,这里不在做单独计算 2023-07-10
-			//airborneTota, e = cygx.GetActivitySpecialTripAirborneCountByActivitySpecial(condition, pars)
-			//if e != nil {
-			//	err = errors.New("GetActivitySpecialTripAirborneCountByActivitySpecial, Err: " + e.Error())
-			//	return
-			//}
-			//condition += ` AND is_valid = 1 `
-			//查询当年的数据
-			condition += ` AND create_time >= ?  `
-			pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
-			listTripBill, e := cygx.GetCygxActivitySpecialTripBill(condition, pars)
-			if e != nil {
-				err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
-				return
-			}
-			var TripBillNum int
+			var tripBillNum int
 			for _, v := range listTripBill {
-				TripBillNum += v.BillDetailed
+				//if v.ActivityId == 0 {
+				//	continue
+				//}
+				tripBillNum += v.BillDetailed
 			}
-			// CRM13.2 70w客户有16次专项调研, 45w有10次
-			packageTypeMap := map[int]int{1: 16, 2: 10}
-			totalTrip := packageTypeMap[packageType]
 
-			tripRemaining := totalTrip - airborneTota + TripBillNum
+			if len(inheritList) > 0 {
+				for _, v := range inheritList {
+					if v.ChartPermissionId == 0 {
+						tripBillNum += v.Points
+					}
+				}
+			}
+			tripRemaining := tripBillNum
 			if tripRemaining < 0 {
 				tripRemaining = 0
 			}
+
 			specialSurplus = strconv.Itoa(tripRemaining) + "次"
-			//tripRemaining = 12 - tripTota
 		} else {
-			list, e := company.GetCompanyReportPermissionUpgrade(companyId, 2)
+			list, e := company.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
 			if e != nil && e.Error() != utils.ErrNoRow() {
 				err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
 			}
@@ -273,51 +280,57 @@ func GetSpecialSurplusByCompany(companyId int) (specialSurplus string, err error
 			}
 			var chartPermissionIdSlice []string
 			mapChartName := make(map[string]int)
+			mapUpgradeId := make(map[int]int)
 			mapPermissionNameTrip := make(map[string]int)
+			mapInheritChartName := make(map[string]int)
 			//mapPermissionName := make(map[int]string)
 			for _, v := range list {
 				chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
-
+				//是升级套餐才有点数
+				if v.IsUpgrade == 1 {
+					mapUpgradeId[v.ChartPermissionId] = 1
+				}
 			}
-			chartList, e := models.GetChartPermissionByIds(chartPermissionIdSlice)
-			if e != nil {
-				err = errors.New("获取品种信息失败, Err:" + e.Error())
-				return
+			chartList := make([]*models.ChartPermission, 0)
+			if len(chartPermissionIdSlice) > 0 {
+				chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
+				if e != nil {
+					err = errors.New("获取品种信息失败, Err:" + e.Error())
+					return
+				}
 			}
+
 			if len(chartList) == 0 {
 				return
 			}
-			//到会空降的也加入流水记录表,这里不在做单独计算 2023-07-10
-			//airborneList, e := cygx.GetCygxActivitySpecialTripAirborneListByComapnyId(companyId)
-			//if e != nil {
-			//	err = errors.New("获取品种信息失败, Err:" + e.Error())
-			//	return
-			//}
-			//for _, v := range airborneList {
-			//	mapPermissionNameTrip[v.ChartPermissionName] += 1
-			//}
-
-			var condition string
-			var pars []interface{}
-
-			condition += ` AND company_id = ? `
-			pars = append(pars, companyId)
-
-			//查询当年的数据
-			condition += ` AND b.create_time >= ?  `
-			pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
-			listTripBill, e := cygx.GetCygxActivitySpecialTripBillList(condition, pars)
-			if e != nil {
-				err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
-				return
-			}
+
 			//var TripBillNum int
 			for _, v := range listTripBill {
+				if v.ActivityId == 0 {
+					continue
+				}
 				mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
 			}
 			for _, v := range chartList {
-				mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
+				//如果是升级则加点
+				if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
+					mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
+				} else {
+					mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
+				}
+			}
+			// 通过继承获得的加点
+			for _, v := range inheritList {
+				mapInheritChartName[v.ChartPermissionName] = v.Points
+			}
+			for k, _ := range chartNameMap {
+				if _, ok := mapChartName[k]; ok {
+					if inherit, ok2 := mapInheritChartName[k]; ok2 {
+						mapChartName[k] += inherit
+					}
+				}
 			}
+
 			for k, v := range mapChartName {
 				if v > 0 {
 					specialSurplus += k + strconv.Itoa(v) + "次+"
@@ -391,3 +404,296 @@ func CheckActivitySpecialUpdatePower(adminId int, activityInfo *cygx.ActivitySpe
 	havePower = true
 	return
 }
+
+// GetChartPermissionSpecialSurplusByCompany 获取公司专项调研次数-分品种
+func GetChartPermissionSpecialSurplusByCompany(companyId int) (userType int, tripRemaining int, mapChartName map[string]int, err error) {
+	companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
+	if e != nil {
+		err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
+	}
+	if companyDetail == nil {
+		return
+	}
+	if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
+		return
+	}
+	var specialSurplus string
+	if companyDetail.Status == "永续" {
+		specialSurplus = "不限次数"
+	}
+	//chartMap := map[int]string{utils.YI_YAO_ID:utils.YI_YAO_NAME, utils.XIAO_FEI_ID:utils.XIAO_FEI_NAME, utils.KE_JI_ID:utils.KE_JI_NAME, utils.ZHI_ZAO_ID:utils.ZHI_ZAO_NAME}
+	//chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
+	chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
+	if companyDetail.Status == "正式" {
+		//var packageType int
+		userType, _, _, _, _ = GetUserType(companyId)
+		var condition string
+		var pars []interface{}
+
+		condition += ` AND company_id = ? `
+		pars = append(pars, companyId)
+
+		//查询当年的数据
+		condition += ` AND b.create_time >= ?  `
+		pars = append(pars, time.Now().Format(utils.FormatYearDate)+"-01-01")
+		listTripBill, e := cygx.GetCygxActivitySpecialTripBillList(condition, pars)
+		if e != nil {
+			err = errors.New("GetActivitySpecialTripCountByActivitySpecial, Err: " + e.Error())
+			return
+		}
+		// 获取继承点数
+		inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
+		}
+		if userType == 2 {
+
+			var tripBillNum int
+			for _, v := range listTripBill {
+				//if v.ActivityId == 0 {
+				//	continue
+				//}
+				tripBillNum += v.BillDetailed
+			}
+			if len(inheritList) > 0 {
+				for _, v := range inheritList {
+					if v.ChartPermissionId == 0 {
+						tripBillNum += v.Points
+					}
+				}
+			}
+
+			tripRemaining = tripBillNum
+			if tripRemaining < 0 {
+				tripRemaining = 0
+			}
+
+		} else {
+			list, e := company.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
+			if e != nil && e.Error() != utils.ErrNoRow() {
+				err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
+			}
+			if len(list) == 0 {
+				return
+			}
+			var chartPermissionIdSlice []string
+			mapChartName = make(map[string]int)
+			mapUpgradeId := make(map[int]int)
+			mapPermissionNameTrip := make(map[string]int)
+			mapInheritChartName := make(map[string]int)
+			//mapPermissionName := make(map[int]string)
+			for _, v := range list {
+				chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
+				//是升级套餐才有点数
+				if v.IsUpgrade == 1 {
+					mapUpgradeId[v.ChartPermissionId] = 1
+				}
+			}
+			chartList := make([]*models.ChartPermission, 0)
+			if len(chartPermissionIdSlice) > 0 {
+				chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
+				if e != nil {
+					err = errors.New("获取品种信息失败, Err:" + e.Error())
+					return
+				}
+			}
+
+			if len(chartList) == 0 {
+				return
+			}
+
+			for _, v := range listTripBill {
+				if v.ActivityId == 0 {
+					continue
+				}
+				mapPermissionNameTrip[v.ChartPermissionName] += v.BillDetailed
+			}
+			for _, v := range chartList {
+				//如果是升级则加点
+				if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
+					mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
+				} else {
+					mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
+				}
+			}
+			// 通过继承获得的加点
+			for _, v := range inheritList {
+				mapInheritChartName[v.ChartPermissionName] = v.Points
+			}
+			for k, _ := range chartNameMap {
+				if _, ok := mapChartName[k]; ok {
+					if inherit, ok2 := mapInheritChartName[k]; ok2 {
+						mapChartName[k] += inherit
+					}
+				}
+			}
+
+			for k, v := range mapChartName {
+				if v > 0 {
+					specialSurplus += k + strconv.Itoa(v) + "次+"
+				}
+			}
+			specialSurplus = strings.TrimRight(specialSurplus, "+")
+		}
+	}
+	return
+}
+
+// 审批通过的时候专项调研次数更新
+func ActivitySpecialCompanyApproval(companyId int, companyName string) (err error) {
+	userType, packageType, _, _, _ := GetUserType(companyId)
+	// 获取继承点数
+	inheritList, e := cygx.GetCygxActivitySpecialInheritPointsByCompanyId(companyId)
+	if e != nil && e.Error() != utils.ErrNoRow() {
+		err = errors.New("GetCygxActivitySpecialInheritPointsByCompanyId, Err: " + e.Error())
+	}
+	chartNameMap := map[string]int{utils.YI_YAO_NAME: 0, utils.XIAO_FEI_NAME: 0, utils.KE_JI_NAME: 0, utils.ZHI_ZAO_NAME: 0}
+
+	itemBill := new(cygx.CygxActivitySpecialTripBill)
+	itemBill.CreateTime = time.Now()
+	itemBill.CompanyId = companyId
+	itemBill.CompanyName = companyName
+	itemBill.Source = 2
+	itemBill.DoType = 2
+	itemBill.Way = 3
+	if userType == 2{
+		packageTypeMap := map[int]int{1: 16, 2: 10}
+		totalTrip := packageTypeMap[packageType]
+		if len(inheritList) > 0 {
+			for _, v := range inheritList {
+				if v.ChartPermissionId == 0 {
+					itemBill.BillDetailed = totalTrip + v.Points
+				} else {
+					itemBill.BillDetailed = totalTrip
+				}
+			}
+		} else {
+			itemBill.BillDetailed = totalTrip
+		}
+
+		itemBill.Total = strconv.Itoa(itemBill.BillDetailed) + "次"
+		if totalTrip == 10 {
+			itemBill.Content = "45w大套餐转正"
+		} else {
+			itemBill.Content = "70w大套餐转正"
+		}
+	} else {
+		list, e := company.GetCompanyReportPermissionByCompanyIdAndProductId(companyId, 2)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = errors.New("GetCompanyReportPermissionUpgrade, Err: " + e.Error())
+		}
+		if len(list) == 0 {
+			return
+		}
+		var chartPermissionIdSlice []string
+		mapChartName := make(map[string]int)
+		mapUpgradeId := make(map[int]int)
+		mapInheritChartName := make(map[string]int)
+		mapPermissionNameTrip := make(map[string]int)
+		//mapPermissionName := make(map[int]string)
+		for _, v := range list {
+			chartPermissionIdSlice = append(chartPermissionIdSlice, strconv.Itoa(v.ChartPermissionId))
+			//是升级套餐才有点数
+			if v.IsUpgrade == 1 {
+				mapUpgradeId[v.ChartPermissionId] = 1
+			}
+		}
+		chartList := make([]*models.ChartPermission, 0)
+		if len(chartPermissionIdSlice) > 0 {
+			chartList, e = models.GetChartPermissionByIds(chartPermissionIdSlice)
+			if e != nil {
+				err = errors.New("获取品种信息失败, Err:" + e.Error())
+				return
+			}
+		}
+		if len(chartList) == 0 {
+			return
+		}
+		for _, v := range chartList {
+			//如果是升级则加点
+			if _, ok := mapUpgradeId[v.ChartPermissionId]; ok {
+				mapChartName[v.PermissionName] = 5 + mapPermissionNameTrip[v.ChartPermissionName]
+			} else {
+				mapChartName[v.PermissionName] = mapPermissionNameTrip[v.ChartPermissionName]
+			}
+		}
+		// 通过继承获得的加点
+		for _, v := range inheritList {
+			mapInheritChartName[v.ChartPermissionName] = v.Points
+		}
+		for k, _ := range chartNameMap {
+			if _, ok := mapChartName[k]; ok {
+				if inherit, ok2 := mapInheritChartName[k]; ok2 {
+					mapChartName[k] += inherit
+				}
+			}
+		}
+		for k, v := range mapChartName {
+			if v > 0 {
+				itemBill.BillDetailed += v
+				itemBill.Total += k + strconv.Itoa(v) + "次+"
+			}
+		}
+		itemBill.Content = "行业升级套餐转正"
+		itemBill.Total = strings.TrimRight(itemBill.Total, "+")
+	}
+	err = cygx.AddCygxActivitySpecialTripBill(itemBill)
+	if err != nil {
+		return
+	}
+	return
+}
+
+// 取消专项调研返点
+func ActivitySpecialPublishAndCancel(activityInfo *cygx.ActivitySpecialDetail) (err error) {
+	//userType, tripRemaining, mapChartName, err := GetChartPermissionSpecialSurplusByCompany(companyId)
+	//if err != nil {
+	//	br.Msg = "获取专项调研剩余次数失败"
+	//	br.ErrMsg = "获取专项调研剩余次数失败,err:" + err.Error()
+	//	return
+	//}
+	//itemBill := new(cygx.CygxActivitySpecialTripBill)
+	//itemBill.CreateTime = time.Now()
+	//itemBill.ActivityId = activityInfo.ActivityId
+	//itemBill.Content = activityInfo.ResearchTheme + "--活动取消"
+	//itemBill.Source = 2
+	//itemBill.DoType = 2
+	//itemBill.Way = 4
+	//itemBill.BillDetailed = 1
+	//err = cygx.AddCygxActivitySpecialTripBill(itemBill)
+	//if err != nil {
+	//	return
+	//}
+	return
+}
+
+// GetSpecialSurplusByCompanyNew 获取公司专项调研剩余次数-用流水表数据不计算了,计算都丢在流水里
+func GetSpecialSurplusByCompanyNew(companyId int) (specialSurplus string, err error) {
+	companyDetail, e := cygx.GetCompanyDetailByIdGroup(companyId)
+	if e != nil {
+		err = errors.New("GetCompanyDetailByIdGroup, Err: " + e.Error())
+	}
+	if companyDetail == nil {
+		return
+	}
+	if companyDetail.Status != "永续" && companyDetail.Status != "正式" {
+		return
+	}
+	if companyDetail.Status == "永续" {
+		specialSurplus = "不限次数"
+	}
+	//chartMap := map[int]string{utils.YI_YAO_ID:utils.YI_YAO_NAME, utils.XIAO_FEI_ID:utils.XIAO_FEI_NAME, utils.KE_JI_ID:utils.KE_JI_NAME, utils.ZHI_ZAO_ID:utils.ZHI_ZAO_NAME}
+	//chartNumMap := map[int]int{utils.YI_YAO_ID:0, utils.XIAO_FEI_ID:0, utils.KE_JI_ID:0, utils.ZHI_ZAO_ID:0}
+	if companyDetail.Status == "正式" {
+		billItem,e := cygx.GetCygxActivitySpecialTripBillByCompanyId(companyId)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			err = e
+			return
+		}
+		if billItem != nil {
+			specialSurplus = billItem.Total
+		}
+		return
+	}
+	return
+}