hsun пре 1 година
родитељ
комит
7ee85eb1f4

+ 259 - 39
controllers/eta_business/eta_business.go

@@ -24,6 +24,10 @@ type EtaBusinessController struct {
 // @Param   Keyword			query	string	false	"关键词: 商家名称/社会信用码/商家编码"
 // @Param   SellerIds		query	string	false	"销售IDs"
 // @Param   SigningStatus	query	string	false	"签约状态: 1-首次签约; 2-续约中; 3-已终止"
+// @Param   Province		query	string	false	"省份筛选"
+// @Param   City			query	string	false	"城市筛选"
+// @Param   SortParam		query	string	false	"排序字段: SignTime-签约时间; ExpireTime-到期时间; CreateTime-创建时间; UserMax-用户上限"
+// @Param   SortType		query	string	false	"排序类型: Asc-正序; Desc-倒序"
 // @Success 200 Ret=200 获取成功
 // @router /page_list [get]
 func (this *EtaBusinessController) PageList() {
@@ -99,6 +103,34 @@ func (this *EtaBusinessController) PageList() {
 		pars = append(pars, signingStatus)
 	}
 	// TODO:商家地址(多选)
+	province := this.GetString("Province", "")
+	province = strings.TrimSpace(province)
+	if province != "" {
+
+	}
+	//if province != "" {
+	//	var provinceSql string
+	//	slice := strings.Split(province, ",")
+	//	for _, v := range slice {
+	//		provinceSql += "'" + v + "'" + ","
+	//	}
+	//	provinceSql = strings.TrimRight(provinceSql, ",")
+	//	condition += ` AND a.province IN (` + provinceSql + `) `
+	//}
+	//
+	//if city != "" {
+	//	var citySql string
+	//	slice := strings.Split(city, ",")
+	//	for _, v := range slice {
+	//		citySql += "'" + v + "'" + ","
+	//	}
+	//	citySql = strings.TrimRight(citySql, ",")
+	//	condition += ` AND a.city IN (` + citySql + `) `
+	//}
+
+	// @Param   SortParam		query	string	false	"排序字段: SignTime-签约时间; ExpireTime-到期时间; CreateTime-创建时间; UserMax-用户上限"
+	// @Param   SortType		query	string	false	"排序类型: Asc-正序; Desc-倒序"
+	// TODO:排序
 
 	// 获取列表
 	businessOb := new(eta_business.EtaBusiness)
@@ -161,6 +193,20 @@ func (this *EtaBusinessController) Add() {
 		br.Ret = 408
 		return
 	}
+
+	// 操作权限校验
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Msg = "无权操作"
+		return
+	}
+
+	// 参数校验
 	var req eta_business.EtaBusinessAddReq
 	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
 		br.Msg = "参数解析异常!"
@@ -176,10 +222,17 @@ func (this *EtaBusinessController) Add() {
 		br.Msg = "社会统一信用码不可为空"
 		return
 	}
-	if req.Address == "" {
-		br.Msg = "商家地址不可为空"
+	req.Province = strings.TrimSpace(req.Province)
+	if req.Province == "" {
+		br.Msg = "省份不可为空"
+		return
+	}
+	req.City = strings.TrimSpace(req.City)
+	if req.City == "" {
+		br.Msg = "城市不可为空"
 		return
 	}
+	req.Leader = strings.TrimSpace(req.Leader)
 	if req.Leader == "" {
 		br.Msg = "决策人不可为空"
 		return
@@ -196,17 +249,35 @@ func (this *EtaBusinessController) Add() {
 		br.Msg = "用户上限不可小于0"
 		return
 	}
+	req.CapitalScale = strings.TrimSpace(req.CapitalScale)
 
-	// 权限校验
-	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
-	if e != nil {
-		br.Msg = "操作失败"
-		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
-		return
-	}
-	if !ok {
-		br.Msg = "无权操作"
-		return
+	// 如果仅校验不新增, 那么不做第二页签约时间的校验
+	var signTime, expiredTime time.Time
+	if !req.IsCheck {
+		if req.SigningTime == "" {
+			br.Msg = "签约时间不可为空"
+			return
+		}
+		if req.ExpiredTime == "" {
+			br.Msg = "到期时间不可为空"
+			return
+		}
+		signTime, e = time.ParseInLocation(utils.FormatDate, req.SigningTime, time.Local)
+		if e != nil {
+			br.Msg = "签约时间格式有误"
+			br.ErrMsg = "签约时间格式有误, Err: " + e.Error()
+			return
+		}
+		expiredTime, e = time.ParseInLocation(utils.FormatDate, req.ExpiredTime, time.Local)
+		if e != nil {
+			br.Msg = "到期时间格式有误"
+			br.ErrMsg = "到期时间格式有误, Err: " + e.Error()
+			return
+		}
+		if !expiredTime.After(signTime) {
+			br.Msg = "到期时间不得早于签约时间"
+			return
+		}
 	}
 
 	// 重名校验
@@ -227,6 +298,33 @@ func (this *EtaBusinessController) Add() {
 		}
 	}
 
+	// 社会信用码重复校验
+	{
+		item := new(eta_business.EtaBusiness)
+		cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessColumns.CreditCode)
+		pars := make([]interface{}, 0)
+		pars = append(pars, req.CreditCode)
+		exist, e := item.GetItemByCondition(cond, pars)
+		if e != nil && e.Error() != utils.ErrNoRow() {
+			br.Msg = "操作失败"
+			br.ErrMsg = "获取重复信用码商家失败, Err: " + e.Error()
+			return
+		}
+		if exist != nil {
+			br.Msg = "商家社会信用码已存在"
+			return
+		}
+	}
+
+	// 如果只做校验不新增, 此处校验通过后直接返回true
+	if req.IsCheck {
+		br.Data = true
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "操作成功"
+		return
+	}
+
 	// 商家编码
 	businessCode, e := eta_business.CreateEtaBusinessCode()
 	if e != nil {
@@ -234,33 +332,59 @@ func (this *EtaBusinessController) Add() {
 		br.ErrMsg = "生成商家编码失败, Err: " + e.Error()
 		return
 	}
-	business := new(eta_business.EtaBusiness)
-	business.BusinessName = req.BusinessName
-	business.BusinessCode = businessCode
-	business.CreditCode = req.CreditCode
-	business.Address = req.Address
-	business.SellerId = req.SellerId
-	business.SellerName = req.SellerName
-	business.Leader = req.Leader
-	business.IndustryId = req.IndustryId
-	business.IndustryName = req.IndustryName
-	business.CapitalScale = req.CapitalScale
-	business.ResearchTeamSize = req.ResearchTeamSize
-	business.UserMax = req.UserMax
-	business.Enable = 1
-	business.CreateTime = time.Now().Local()
-	business.CreateTime = time.Now().Local()
-	if e := business.Create(); e != nil {
+
+	now := time.Now().Local()
+	// 若当前时间不在首次签约时间内, 也算作已终止, 进入合约期时会由定时任务改为首次签约
+	status := eta_business.EtaBusinessSigningStatusFirst
+	isTerminate := true
+	if now.Equal(signTime) || now.Equal(expiredTime) {
+		isTerminate = false
+	}
+	if now.After(signTime) && now.Before(expiredTime) {
+		isTerminate = false
+	}
+	if isTerminate {
+		status = eta_business.EtaBusinessSigningStatusTerminate
+	}
+
+	// 新增商家和签约
+	businessItem := new(eta_business.EtaBusiness)
+	businessItem.BusinessName = req.BusinessName
+	businessItem.BusinessCode = businessCode
+	businessItem.CreditCode = req.CreditCode
+	businessItem.Province = req.Province
+	businessItem.City = req.City
+	businessItem.Address = req.Province + req.City
+	businessItem.SellerId = req.SellerId
+	businessItem.SellerName = req.SellerName
+	businessItem.Leader = req.Leader
+	businessItem.IndustryId = req.IndustryId
+	businessItem.IndustryName = req.IndustryName
+	businessItem.CapitalScale = req.CapitalScale
+	businessItem.ResearchTeamSize = req.ResearchTeamSize
+	businessItem.UserMax = req.UserMax
+	businessItem.Enable = 1
+	businessItem.SigningStatus = status
+	businessItem.SigningTime = signTime
+	businessItem.ExpiredTime = expiredTime
+	businessItem.CreateTime = now
+	businessItem.ModifyTime = now
+	contractItem := new(eta_business.EtaBusinessContract)
+	contractItem.SigningTime = signTime
+	contractItem.ExpiredTime = expiredTime
+	contractItem.CreateTime = now
+	contractItem.ModifyTime = now
+	if e = eta_business.CreateEtaBusinessAndContract(businessItem, contractItem); e != nil {
 		br.Msg = "操作失败"
-		br.ErrMsg = "新增商家失败, Err: " + e.Error()
+		br.ErrMsg = "新增商家和签约失败, Err: " + e.Error()
 		return
 	}
 
 	// 操作日志
 	go func() {
 		recordOb := new(eta_business.EtaBusinessOperationRecord)
-		recordOb.EtaBusinessId = business.EtaBusinessId
-		recordOb.SellerId = business.SellerId
+		recordOb.EtaBusinessId = businessItem.EtaBusinessId
+		recordOb.SellerId = businessItem.SellerId
 		recordOb.SysUserId = sysUser.AdminId
 		recordOb.SysRealName = sysUser.RealName
 		recordOb.OperationType = eta_business.EtaBusinessOperationTypeAdd
@@ -269,7 +393,6 @@ func (this *EtaBusinessController) Add() {
 		_ = recordOb.Create()
 	}()
 
-	br.Data = business.EtaBusinessId
 	br.Ret = 200
 	br.Success = true
 	br.Msg = "操作成功"
@@ -308,10 +431,17 @@ func (this *EtaBusinessController) Edit() {
 		br.ErrMsg = "参数有误, 商家ID为空"
 		return
 	}
-	if req.Address == "" {
-		br.Msg = "商家地址不可为空"
+	req.Province = strings.TrimSpace(req.Province)
+	if req.Province == "" {
+		br.Msg = "省份不可为空"
 		return
 	}
+	req.City = strings.TrimSpace(req.City)
+	if req.City == "" {
+		br.Msg = "城市不可为空"
+		return
+	}
+	req.Leader = strings.TrimSpace(req.Leader)
 	if req.Leader == "" {
 		br.Msg = "决策人不可为空"
 		return
@@ -328,6 +458,7 @@ func (this *EtaBusinessController) Edit() {
 		br.Msg = "用户上限不可小于0"
 		return
 	}
+	req.CapitalScale = strings.TrimSpace(req.CapitalScale)
 
 	// 权限校验
 	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
@@ -352,7 +483,9 @@ func (this *EtaBusinessController) Edit() {
 		br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
 		return
 	}
-	item.Address = req.Address
+	item.Province = req.Province
+	item.City = req.City
+	item.Address = req.Province + req.City
 	item.Leader = req.Leader
 	item.IndustryId = req.IndustryId
 	item.IndustryName = req.IndustryName
@@ -361,7 +494,7 @@ func (this *EtaBusinessController) Edit() {
 	item.UserMax = req.UserMax
 	item.ModifyTime = time.Now().Local()
 	cols := []string{
-		"Address", "Leader", "IndustryId", "IndustryName", "CapitalScale", "ResearchTeamSize", "UserMax", "ModifyTime",
+		"Province", "City", "Address", "Leader", "IndustryId", "IndustryName", "CapitalScale", "ResearchTeamSize", "UserMax", "ModifyTime",
 	}
 	if e := item.Update(cols); e != nil {
 		br.Msg = "操作失败"
@@ -818,15 +951,30 @@ func (this *EtaBusinessController) ContractList() {
 	list, e := contractOb.GetItemsByCondition(cond, pars, []string{}, order)
 	if e != nil {
 		br.Msg = "获取失败"
-		br.ErrMsg = "获取商家列表失败, Err: " + e.Error()
+		br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
 		return
 	}
+	now := time.Now().Local()
 	for _, v := range list {
 		b := new(eta_business.EtaBusinessContractItem)
 		b.EtaBusinessId = v.EtaBusinessId
 		b.SigningTime = v.SigningTime.Format(utils.FormatDate)
 		b.ExpiredTime = v.ExpiredTime.Format(utils.FormatDate)
-		// TODO:到期天数,当前合约
+		// 到期天数, 终止日按当天的23:59:59算
+		strEnd := v.ExpiredTime.Format(utils.FormatDate)
+		strEnd = strEnd + " 23:59:59"
+		endTime, e := time.ParseInLocation(utils.FormatDateTime, strEnd, time.Local)
+		if e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "签约日期有误, Err: " + e.Error()
+			return
+		}
+		b.ExpireDay = "-"
+		if now.After(v.SigningTime) && now.Before(endTime) {
+			diff := utils.GetDiffDays(now, v.ExpiredTime)
+			b.ExpireDay = strconv.Itoa(diff)
+			b.Using = true
+		}
 		resp = append(resp, b)
 	}
 
@@ -835,3 +983,75 @@ func (this *EtaBusinessController) ContractList() {
 	br.Success = true
 	br.Msg = "获取成功"
 }
+
+// OperateRecordList
+// @Title 操作日志列表
+// @Description 操作日志列表
+// @Param   EtaBusinessId	query	int		true	"商家ID"
+// @Success 200 Ret=200 获取成功
+// @router /operate_record_list [get]
+func (this *EtaBusinessController) OperateRecordList() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		if br.ErrMsg == "" {
+			br.IsSendEmail = false
+		}
+		this.Data["json"] = br
+		this.ServeJSON()
+	}()
+	sysUser := this.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	// 权限校验
+	resp := make([]*eta_business.EtaBusinessOperationRecordItem, 0)
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Data = resp
+		br.Ret = 200
+		br.Success = true
+		br.Msg = "获取成功"
+		return
+	}
+	businessId, _ := this.GetInt("EtaBusinessId", 0)
+	if businessId <= 0 {
+		br.Msg = "参数有误"
+		br.ErrMsg = "参数有误, 商家ID"
+		return
+	}
+
+	cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessOperationRecordColumns.EtaBusinessId)
+	pars := make([]interface{}, 0)
+	pars = append(pars, businessId)
+
+	recordOb := new(eta_business.EtaBusinessOperationRecord)
+	list, e := recordOb.GetItemsByCondition(cond, pars, []string{}, "")
+	if e != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = "获取商家操作日志列表失败, Err: " + e.Error()
+		return
+	}
+	for _, v := range list {
+		r := new(eta_business.EtaBusinessOperationRecordItem)
+		r.EtaBusinessId = v.EtaBusinessId
+		r.SysUserId = v.SysUserId
+		r.SysRealName = v.SysRealName
+		r.OperationType = v.OperationType
+		r.OperationRemark = v.OperationRemark
+		r.CreateTime = v.CreateTime.Format(utils.FormatDateTime)
+		resp = append(resp, r)
+	}
+
+	br.Data = resp
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "获取成功"
+}

+ 50 - 2
models/eta_business/eta_business.go

@@ -20,6 +20,8 @@ type EtaBusiness struct {
 	BusinessCode     string    `description:"商家编码"`
 	CreditCode       string    `description:"社会统一信用码"`
 	RegionType       string    `description:"所属区域:国内;海外"`
+	Province         string    `description:"省份"`
+	City             string    `description:"城市"`
 	Address          string    `description:"商家地址"`
 	SellerId         int       `description:"销售ID"`
 	SellerName       string    `description:"销售名称"`
@@ -51,6 +53,8 @@ var EtaBusinessColumns = struct {
 	BusinessCode     string
 	CreditCode       string
 	RegionType       string
+	Province         string
+	City             string
 	Address          string
 	SellerId         string
 	SellerName       string
@@ -72,6 +76,8 @@ var EtaBusinessColumns = struct {
 	BusinessCode:     "business_code",
 	CreditCode:       "credit_code",
 	RegionType:       "region_type",
+	Province:         "province",
+	City:             "city",
 	Address:          "address",
 	SellerId:         "seller_id",
 	SellerName:       "seller_name",
@@ -157,12 +163,50 @@ func (m *EtaBusiness) GetItemsByCondition(condition string, pars []interface{},
 	return
 }
 
+// CreateEtaBusinessAndContract 新增商家和签约
+func CreateEtaBusinessAndContract(businessItem *EtaBusiness, contractItem *EtaBusinessContract) (err error) {
+	if businessItem == nil || contractItem == nil {
+		err = fmt.Errorf("item empty")
+		return
+	}
+
+	o := orm.NewOrm()
+	tx, err := o.Begin()
+	if err != nil {
+		return
+	}
+	defer func() {
+		if err != nil {
+			_ = tx.Rollback()
+			return
+		}
+		_ = tx.Commit()
+	}()
+
+	// 商家
+	businessId, e := tx.Insert(businessItem)
+	if e != nil {
+		err = fmt.Errorf("business insert err: %s", e.Error())
+		return
+	}
+	businessItem.EtaBusinessId = int(businessId)
+
+	// 签约
+	contractItem.EtaBusinessId = businessItem.EtaBusinessId
+	_, e = tx.Insert(contractItem)
+	if e != nil {
+		err = fmt.Errorf("contract insert err: %s", e.Error())
+	}
+	return
+}
+
 // EtaBusinessAddReq 新增商家请求体
 type EtaBusinessAddReq struct {
 	BusinessName     string `description:"商家名称"`
 	CreditCode       string `description:"社会统一信用码"`
 	RegionType       string `description:"所属区域:国内;海外"`
-	Address          string `description:"商家地址"`
+	Province         string `description:"省份"`
+	City             string `description:"城市"`
 	SellerId         int    `description:"销售ID"`
 	SellerName       string `description:"销售名称"`
 	Leader           string `description:"决策人"`
@@ -171,12 +215,16 @@ type EtaBusinessAddReq struct {
 	CapitalScale     string `description:"资金规模"`
 	ResearchTeamSize string `description:"研究团队规模"`
 	UserMax          int    `description:"用户上限"`
+	SigningTime      string `description:"签约时间"`
+	ExpiredTime      string `description:"到期时间"`
+	IsCheck          bool   `description:"是否只做校验而不实际新增(业务操作上基础信息和签约时间分成两个步骤了)"`
 }
 
 // EtaBusinessEditReq 编辑商家请求体
 type EtaBusinessEditReq struct {
 	EtaBusinessId    int    `description:"商家ID"`
-	Address          string `description:"商家地址"`
+	Province         string `description:"省份"`
+	City             string `description:"城市"`
 	Leader           string `description:"决策人"`
 	IndustryId       int    `description:"行业ID"`
 	IndustryName     string `description:"行业名称"`

+ 1 - 1
models/eta_business/eta_business_contract.go

@@ -115,6 +115,6 @@ type EtaBusinessContractItem struct {
 	EtaBusinessId         int    `description:"ETA商家ID"`
 	SigningTime           string `description:"签约时间"`
 	ExpiredTime           string `description:"到期时间"`
-	ExpireDay             int    `description:"到期天数"`
+	ExpireDay             string `description:"到期天数"`
 	Using                 bool   `description:"是否当前合约"`
 }

+ 11 - 1
models/eta_business/eta_business_operation_record.go

@@ -17,7 +17,7 @@ const (
 	EtaBusinessOperationTypeRenewalContract
 )
 
-// EtaBusinessOperationRecord ETA商家合同
+// EtaBusinessOperationRecord ETA商家操作日志
 type EtaBusinessOperationRecord struct {
 	Id              int       `orm:"column(id);pk"`
 	EtaBusinessId   int       `description:"ETA商家ID"`
@@ -124,3 +124,13 @@ func (m *EtaBusinessOperationRecord) GetItemsByCondition(condition string, pars
 	_, err = o.Raw(sql, pars).QueryRows(&items)
 	return
 }
+
+// EtaBusinessOperationRecordItem ETA商家操作日志信息
+type EtaBusinessOperationRecordItem struct {
+	EtaBusinessId   int    `description:"ETA商家ID"`
+	SysUserId       int    `description:"操作人ID"`
+	SysRealName     string `description:"操作人姓名"`
+	OperationType   int    `description:"操作类型:1-新增;2-编辑;3-禁用;4-启用;5-移动销售;6-添加续约"`
+	OperationRemark string `description:"操作内容"`
+	CreateTime      string `description:"创建时间"`
+}

+ 7 - 0
utils/common.go

@@ -2014,3 +2014,10 @@ func GetPredictEdbDayListByNum(startDate time.Time, num int, frequency string) (
 	}
 	return
 }
+
+// GetDiffDays 计算两个日期相差的天数
+func GetDiffDays(t1, t2 time.Time) int {
+	t1 = time.Date(t1.Year(), t1.Month(), t1.Day(), 0, 0, 0, 0, time.Local)
+	t2 = time.Date(t2.Year(), t2.Month(), t2.Day(), 0, 0, 0, 0, time.Local)
+	return int(t1.Sub(t2).Hours() / 24)
+}