Prechádzať zdrojové kódy

商家签约-编辑/删除签约

hsun 1 rok pred
rodič
commit
32b8fd69ce

+ 290 - 0
controllers/eta_business/eta_business.go

@@ -1204,3 +1204,293 @@ func (this *EtaBusinessController) OperateRecordList() {
 	br.Success = true
 	br.Msg = "获取成功"
 }
+
+// EditSign
+// @Title 编辑签约
+// @Description 编辑签约
+// @Param	request	body eta_business.EtaBusinessEditSignReq true "type json string"
+// @Success 200 Ret=200 操作成功
+// @router /edit_sign [post]
+func (this *EtaBusinessController) EditSign() {
+	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
+	}
+	var req eta_business.EtaBusinessEditSignReq
+	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + e.Error()
+		return
+	}
+	if req.EtaBusinessContractId <= 0 {
+		br.Msg = "参数有误"
+		br.ErrMsg = "参数有误, 商家签约ID为空"
+		return
+	}
+	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
+	}
+
+	// 权限校验
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Msg = "无权操作"
+		return
+	}
+
+	contractOb := new(eta_business.EtaBusinessContract)
+	contract, e := contractOb.GetItemById(req.EtaBusinessContractId)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			br.Msg = "签约信息不存在, 请刷新页面"
+			return
+		}
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取商家签约信息失败, Err: " + e.Error()
+		return
+	}
+	businessOb := new(eta_business.EtaBusiness)
+	business, e := businessOb.GetItemById(contract.EtaBusinessId)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			br.Msg = "商家不存在, 请刷新页面"
+			return
+		}
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
+		return
+	}
+
+	// 续约不可早于当前生效合约的到期时间
+	if business.ContractId > 0 && business.ContractId != req.EtaBusinessContractId {
+		{
+			ob := new(eta_business.EtaBusinessContract)
+			item, e := ob.GetItemById(business.ContractId)
+			if e != nil {
+				br.Msg = "操作失败"
+				br.ErrMsg = "获取商家当前合同失败, Err: " + e.Error()
+				return
+			}
+			if !signTime.After(item.ExpiredTime) {
+				br.Msg = "签约时间不可早于当前合同的到期时间"
+				return
+			}
+		}
+	}
+
+	// 获取历史签约, 签约日期不可交叠
+	cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
+	pars := make([]interface{}, 0)
+	pars = append(pars, contract.EtaBusinessId)
+	contracts, e := contractOb.GetItemsByCondition(cond, pars, []string{}, "signing_time ASC")
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取商家签约列表失败, Err: " + e.Error()
+		return
+	}
+
+	// 校验签约时间是否重叠
+	isFirst := true      // 是否为首次签约
+	changeFirst := false // 是否需要更新首次签约合同
+	if len(contracts) > 0 {
+		isFirst = false
+		for k, c := range contracts {
+			if c.EtaBusinessContractId == req.EtaBusinessContractId {
+				continue
+			}
+			pass := false
+			if expiredTime.Before(c.SigningTime) {
+				pass = true
+			}
+			if signTime.After(c.ExpiredTime) {
+				pass = true
+			}
+			if !pass {
+				br.Msg = "签约时间在存续期内, 请检查"
+				return
+			}
+			// 若该合同签约时间早于第一份合同(业务上未限制所以可能会出现这种情况, contracts已排序), 则更新为首次签约合同
+			if k == 0 && signTime.Before(c.SigningTime) {
+				changeFirst = true
+				isFirst = true
+			}
+		}
+	}
+
+	// 新增签约
+	contract.SigningTime = signTime
+	contract.ExpiredTime = expiredTime
+	contract.ModifyTime = time.Now().Local()
+	if isFirst {
+		contract.IsFirst = 1
+	}
+	updateCols := []string{"SigningTime", "ExpiredTime", "ModifyTime"}
+	if e = contract.UpdateMaybeUpdateFirst(contract, changeFirst, updateCols); e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "编辑签约失败, Err: " + e.Error()
+		return
+	}
+
+	// 签约后续操作
+	go func() {
+		_ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
+	}()
+
+	// 续约操作日志
+	if !isFirst {
+		go func() {
+			recordOb := new(eta_business.EtaBusinessOperationRecord)
+			recordOb.EtaBusinessId = business.EtaBusinessId
+			recordOb.SellerId = business.SellerId
+			recordOb.SysUserId = sysUser.AdminId
+			recordOb.SysRealName = sysUser.RealName
+			recordOb.OperationType = eta_business.EtaBusinessOperationTypeEditContract
+			recordOb.OperationRemark = fmt.Sprintf("%s编辑续约", sysUser.RealName)
+			recordOb.CreateTime = time.Now().Local()
+			_ = recordOb.Create()
+		}()
+	}
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "操作成功"
+}
+
+// RemoveSign
+// @Title 删除签约
+// @Description 删除签约
+// @Param	request	body eta_business.EtaBusinessRemoveSignReq true "type json string"
+// @Success 200 Ret=200 操作成功
+// @router /remove_sign [post]
+func (this *EtaBusinessController) RemoveSign() {
+	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
+	}
+	var req eta_business.EtaBusinessRemoveSignReq
+	if e := json.Unmarshal(this.Ctx.Input.RequestBody, &req); e != nil {
+		br.Msg = "参数解析异常!"
+		br.ErrMsg = "参数解析失败,Err:" + e.Error()
+		return
+	}
+	if req.EtaBusinessContractId <= 0 {
+		br.Msg = "参数有误"
+		br.ErrMsg = "参数有误, 商家签约ID为空"
+		return
+	}
+
+	// 权限校验
+	ok, e := etaBusinessService.CheckEtaBusinessOperateAuth(sysUser.RoleTypeCode)
+	if e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "操作权限校验失败, ErrMsg: " + e.Error()
+		return
+	}
+	if !ok {
+		br.Msg = "无权操作"
+		return
+	}
+
+	// 获取商家信息
+	contractOb := new(eta_business.EtaBusinessContract)
+	contract, e := contractOb.GetItemById(req.EtaBusinessContractId)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			br.Msg = "签约信息不存在, 请刷新页面"
+			return
+		}
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取商家签约信息失败, Err: " + e.Error()
+		return
+	}
+
+	// 获取商家信息
+	businessOb := new(eta_business.EtaBusiness)
+	business, e := businessOb.GetItemById(contract.EtaBusinessId)
+	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			br.Msg = "商家不存在, 请检查"
+			return
+		}
+		br.Msg = "操作失败"
+		br.ErrMsg = "获取商家信息失败, Err: " + e.Error()
+		return
+	}
+
+	// 删除签约
+	if e = contract.Del(); e != nil {
+		br.Msg = "操作失败"
+		br.ErrMsg = "删除签约失败, Err: " + e.Error()
+		return
+	}
+
+	// 签约后续操作
+	go func() {
+		_ = etaBusinessService.UpdateEtaBusinessAfterSigning(business.EtaBusinessId)
+	}()
+
+	// 操作日志
+	go func() {
+		recordOb := new(eta_business.EtaBusinessOperationRecord)
+		recordOb.EtaBusinessId = business.EtaBusinessId
+		recordOb.SellerId = business.SellerId
+		recordOb.SysUserId = sysUser.AdminId
+		recordOb.SysRealName = sysUser.RealName
+		recordOb.OperationType = eta_business.EtaBusinessOperationTypeRemoveContract
+		recordOb.OperationRemark = fmt.Sprintf("%s删除签约", sysUser.RealName)
+		recordOb.CreateTime = time.Now().Local()
+		_ = recordOb.Create()
+	}()
+
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "操作成功"
+}

+ 12 - 0
models/eta_business/eta_business.go

@@ -326,3 +326,15 @@ type EtaBusinessItem struct {
 	CreateTime       string `description:"创建时间"`
 	ModifyTime       string `description:"更新时间"`
 }
+
+// EtaBusinessEditSignReq 编辑商家签约请求体
+type EtaBusinessEditSignReq struct {
+	EtaBusinessContractId int    `description:"商家合约ID"`
+	SigningTime           string `description:"当前合约的签约时间"`
+	ExpiredTime           string `description:"当前合约的到期时间"`
+}
+
+// EtaBusinessRemoveSignReq 删除商家签约请求体
+type EtaBusinessRemoveSignReq struct {
+	EtaBusinessContractId int `description:"商家合约ID"`
+}

+ 40 - 0
models/eta_business/eta_business_contract.go

@@ -154,6 +154,46 @@ func (m *EtaBusinessContract) CreateMaybeUpdateFirst(item *EtaBusinessContract,
 	return
 }
 
+// UpdateMaybeUpdateFirst 编辑签约合同, 或同时更新首次签约合同
+func (m *EtaBusinessContract) UpdateMaybeUpdateFirst(item *EtaBusinessContract, changeFirst bool, updateCols []string) (err error) {
+	// 无需修改首次签约则直接更新
+	o := orm.NewOrm()
+	if !changeFirst {
+		e := item.Update(updateCols)
+		if e != nil {
+			return e
+		}
+		return
+	}
+
+	// 更新首次签约合同
+	tx, e := o.Begin()
+	if e != nil {
+		return e
+	}
+	defer func() {
+		if err != nil {
+			_ = tx.Rollback()
+			return
+		}
+		_ = tx.Commit()
+	}()
+
+	_, e = tx.Update(item, updateCols...)
+	if e != nil {
+		err = fmt.Errorf("contract update err: %s", e.Error())
+		return
+	}
+
+	sql := fmt.Sprintf(`UPDATE %s SET %s = 0 WHERE %s = ? AND %s <> ?`, m.TableName(), EtaBusinessContractColumns.IsFirst, EtaBusinessContractColumns.EtaBusinessId, EtaBusinessContractColumns.EtaBusinessContractId)
+	_, e = tx.Raw(sql, item.EtaBusinessId, item.EtaBusinessContractId).Exec()
+	if e != nil {
+		err = fmt.Errorf("update first contract err: %s", e.Error())
+		return
+	}
+	return
+}
+
 // EtaBusinessContractItem 商家签约信息
 type EtaBusinessContractItem struct {
 	EtaBusinessContractId int

+ 3 - 1
models/eta_business/eta_business_operation_record.go

@@ -15,6 +15,8 @@ const (
 	EtaBusinessOperationTypeEnable
 	EtaBusinessOperationTypeMoveSeller
 	EtaBusinessOperationTypeRenewalContract
+	EtaBusinessOperationTypeEditContract
+	EtaBusinessOperationTypeRemoveContract
 )
 
 // EtaBusinessOperationRecord ETA商家操作日志表
@@ -24,7 +26,7 @@ type EtaBusinessOperationRecord struct {
 	SellerId        int       `description:"操作时所对应的销售ID"`
 	SysUserId       int       `description:"操作人ID"`
 	SysRealName     string    `description:"操作人姓名"`
-	OperationType   int       `description:"操作类型:1-新增;2-编辑;3-禁用;4-启用;5-移动销售;6-添加续约"`
+	OperationType   int       `description:"操作类型:1-新增;2-编辑;3-禁用;4-启用;5-移动销售;6-添加续约;7-编辑签约;8-删除签约"`
 	OperationRemark string    `description:"操作内容"`
 	CreateTime      time.Time `description:"创建时间"`
 }

+ 18 - 0
routers/commentsRouter.go

@@ -6838,6 +6838,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"],
+        beego.ControllerComments{
+            Method: "EditSign",
+            Router: `/edit_sign`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"],
         beego.ControllerComments{
             Method: "Enable",
@@ -6874,6 +6883,15 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"],
+        beego.ControllerComments{
+            Method: "RemoveSign",
+            Router: `/remove_sign`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"] = append(beego.GlobalControllerRouter["hongze/hz_crm_api/controllers/eta_business:EtaBusinessController"],
         beego.ControllerComments{
             Method: "Signing",

+ 27 - 11
services/eta_business/eta_business.go

@@ -45,6 +45,9 @@ func UpdateEtaBusinessAfterSigning(businessId int) (err error) {
 	businessOb := new(eta_business.EtaBusiness)
 	business, e := businessOb.GetItemById(businessId)
 	if e != nil {
+		if e.Error() == utils.ErrNoRow() {
+			return
+		}
 		err = fmt.Errorf("获取商家信息失败, Err: %s", e.Error())
 		return
 	}
@@ -55,7 +58,8 @@ func UpdateEtaBusinessAfterSigning(businessId int) (err error) {
 		cond := fmt.Sprintf(` AND %s = ?`, eta_business.EtaBusinessContractColumns.EtaBusinessId)
 		pars := make([]interface{}, 0)
 		pars = append(pars, businessId)
-		list, e := ob.GetItemsByCondition(cond, pars, []string{}, "")
+		// 按签约时间倒序
+		list, e := ob.GetItemsByCondition(cond, pars, []string{}, fmt.Sprintf("%s DESC", eta_business.EtaBusinessContractColumns.SigningTime))
 		if e != nil {
 			err = fmt.Errorf("获取商家合同列表失败, Err: " + e.Error())
 			return
@@ -63,6 +67,16 @@ func UpdateEtaBusinessAfterSigning(businessId int) (err error) {
 		contracts = list
 	}
 	if len(contracts) == 0 {
+		// 重置签约信息
+		cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"}
+		business.ContractId = 0
+		business.SigningStatus = eta_business.EtaBusinessSigningStatusWait
+		business.SigningTime = time.Time{}
+		business.ExpiredTime = time.Time{}
+		business.ModifyTime = time.Now().Local()
+		if e = business.Update(cols); e != nil {
+			err = fmt.Errorf("更新商家信息失败, Err: %s", e.Error())
+		}
 		return
 	}
 
@@ -86,18 +100,20 @@ func UpdateEtaBusinessAfterSigning(businessId int) (err error) {
 			break
 		}
 	}
-	// 不存在任一合同期内, 当前合同不变, 更新状态
-	if !using {
-		business.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate
-		business.ModifyTime = time.Now().Local()
-		cols := []string{"SigningStatus", "ModifyTime"}
-		if e = business.Update(cols); e != nil {
-			err = fmt.Errorf("更新商家信息失败, Err: %s", e.Error())
-			return
+
+	// 不存在任一合同期内, 取签约时间最后的合同(已按时间排序)
+	if !using && len(contracts) > 0 {
+		business.ContractId = contracts[0].EtaBusinessContractId
+		business.SigningTime = contracts[0].SigningTime
+		business.ExpiredTime = contracts[0].ExpiredTime
+		if contracts[0].IsFirst == 1 {
+			business.SigningStatus = eta_business.EtaBusinessSigningStatusFirst
+		} else {
+			business.SigningStatus = eta_business.EtaBusinessSigningStatusTerminate
 		}
-		return
+		business.ModifyTime = time.Now().Local()
 	}
-	// 合同期内
+
 	cols := []string{"ContractId", "SigningStatus", "SigningTime", "ExpiredTime", "ModifyTime"}
 	if e = business.Update(cols); e != nil {
 		err = fmt.Errorf("更新商家信息失败, Err: %s", e.Error())