package contract import ( "fmt" "github.com/gin-gonic/gin" "github.com/go-playground/validator/v10" "hongze/fms_api/controller/resp" "hongze/fms_api/global" "hongze/fms_api/models/base" "hongze/fms_api/models/fms" "hongze/fms_api/models/system" fmsService "hongze/fms_api/services/fms" "hongze/fms_api/utils" "time" ) // // RegisterController 合同登记 type PrePaymentController struct{} // List // @Title 到款预登记列表 // @Description 到款预登记列表 // @Param Keyword query string false "关键词" // @Param StartDate query string false "约定开始时间" // @Param EndDate query string false "约定结束时间" // @Param SortType query string true "如何排序,是正序还是倒序,枚举值:`asc 正序`,`desc 倒叙`" // @Success 200 {object} fms.ContractRegisterItem // @router /contract/pre_pay/list [get] func (rg *PrePaymentController) List(c *gin.Context) { var req fms.PrePayListReq if e := c.BindQuery(&req); e != nil { err, ok := e.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+e.Error(), c) return } resp.FailData("参数解析失败", err.Translate(global.Trans), c) return } cond := `1 = 1` pars := make([]interface{}, 0) // 客户姓名/销售 if req.Keyword != "" { kw := "%" + req.Keyword + "%" cond += ` AND (company_name LIKE ? OR seller_name LIKE ? )` pars = append(pars, kw, kw) } if req.StartDate != "" && req.EndDate != "" { st := fmt.Sprint(req.StartDate, " 00:00:00") ed := fmt.Sprint(req.EndDate, " 23:59:59") cond += ` AND (create_time BETWEEN ? AND ?)` pars = append(pars, st, ed) } // 货币列表 currencyOB := new(fms.CurrencyUnit) currencyCond := `enable = 1` currencyPars := make([]interface{}, 0) currencyList, e := currencyOB.List(currencyCond, currencyPars) if e != nil { resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c) return } unitMap := make(map[string]string) for i := range currencyList { unitMap[currencyList[i].Code] = currencyList[i].UnitName } page := new(base.Page) page.SetPageSize(req.PageSize) page.SetCurrent(req.Current) sortTypeMap := map[int]bool{0: false, 1: true, 2: false} page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: sortTypeMap[req.SortType]}) total, list, e := fms.GetPrePayItemPageList(page, cond, pars) if e != nil { resp.FailMsg("获取失败", "获取预登记列表失败, Err: "+e.Error(), c) return } for i := range list { list[i].UnitName = unitMap[list[i].CurrencyUnit] } page.SetTotal(total) baseData := new(base.BaseData) baseData.SetPage(page) baseData.SetList(list) resp.OkData("获取成功", baseData, c) } // Add // @Title 新增到款预登记 // @Description 新增到款预登记 // @Param request body fms.PrepayAddReq true "type json string" // @Success 200 string "操作成功" // @router /contract/pre_pay/add [post] func (rg *PrePaymentController) Add(c *gin.Context) { req := new(fms.PrepayAddReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } claims, _ := c.Get("adminInfo") adminInfo := claims.(*system.SysAdmin) // 日期校验 startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local) if e != nil { resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c) return } endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local) if e != nil { resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c) return } // 货币及汇率 rateList, e := fmsService.GetTodayCurrencyRateList() if e != nil { resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c) return } var rate float64 for i := range rateList { if req.CurrencyUnit == rateList[i].Code { rate = rateList[i].RMBRate break } } if rate <= 0 { resp.FailMsg("操作失败", "货币汇率信息有误", c) return } payDate,err := time.Parse(utils.FormatDate,req.PaymentDate) if e != nil { resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c) return } ob := new(fms.ContractPrePayment) ob.CompanyName = req.CompanyName ob.CurrencyUnit = req.CurrencyUnit ob.StartDate = startDate ob.EndDate = endDate ob.AdminId = int(adminInfo.AdminId) ob.AdminName = adminInfo.AdminName ob.Amount = req.Amount / rate ob.OriginAmount = req.Amount ob.CurrencyUnit = req.CurrencyUnit ob.Remark = req.Remark ob.PaymentDate = payDate ob.Set() // 新增合同及套餐 if e = ob.Create(); e != nil { resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c) return } //// 开票到款汇总 //nowTime := time.Now().Local() //v := new(fms.InvoicePaymentSummary) //v.CreateTime = nowTime //v.ModifyTime = nowTime //v.PrePayid = ob.PrePayId // //if e = v.Create(); e != nil { // err = fmt.Errorf("新增汇总数据失败, Err: %s", e.Error()) //} resp.Ok("操作成功", c) } // Edit // @Title 编辑到款预登记 // @Description 编辑到款预登记 // @Param request body fms.ContractRegisterEditReq true "type json string" // @Success 200 string "操作成功" // @router /contract/pre_pay/edit [post] func (rg *PrePaymentController) Edit(c *gin.Context) { req := new(fms.PrepayEditReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } claims, _ := c.Get("adminInfo") adminInfo := claims.(*system.SysAdmin) // 日期校验 startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local) if e != nil { resp.FailMsg("约定开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c) return } endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local) if e != nil { resp.FailMsg("约定结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c) return } // 货币及汇率 rateList, e := fmsService.GetTodayCurrencyRateList() if e != nil { resp.FailMsg("操作失败", "获取今日货币汇率失败, Err: "+e.Error(), c) return } var rate float64 for i := range rateList { if req.CurrencyUnit == rateList[i].Code { rate = rateList[i].RMBRate break } } if rate <= 0 { resp.FailMsg("操作失败", "货币汇率信息有误", c) return } ob := new(fms.ContractPrePayment) item, e := ob.Fetch(req.PrePayId) if e != nil { if e == utils.ErrNoRow { resp.Fail("预到款登记记录不存在或已被删除", c) return } resp.FailMsg("操作失败", "获取预到款登记信息失败, Err:"+e.Error(), c) return } payDate,err := time.Parse(utils.FormatDate,req.PaymentDate) if e != nil { resp.FailMsg("日期转换失败", "日期转换失败, Err: "+e.Error(), c) return } item.CompanyName = req.CompanyName item.CurrencyUnit = req.CurrencyUnit item.StartDate = startDate item.EndDate = endDate item.AdminId = int(adminInfo.AdminId) item.AdminName = adminInfo.AdminName item.Amount = req.Amount / rate item.OriginAmount = req.Amount item.CurrencyUnit = req.CurrencyUnit item.Remark = req.Remark item.PaymentDate = payDate item.ModifyTime = time.Now().Local() updateCols := []string{ "ContractCode", "CompanyName", "SellerId", "SellerName", "Amount", "StartDate", "EndDate", "Remark", "ModifyTime","NewCompany", "OriginAmount", "CurrencyUnit", "PaymentDate", } if e = item.Update(updateCols); e != nil { resp.FailMsg("操作失败", "更新到预款登记失败, Err:"+e.Error(), c) return } resp.Ok("操作成功", c) } // Del // @Title 删除到款预登记 // @Description 删除到款预登记 // @Param request body fms.ContractRegisterDelReq true "type json string" // @Success 200 string "操作成功" // @router /contract/pre_pay/del [post] func (rg *PrePaymentController) Del(c *gin.Context) { req := new(fms.PrepayDelReq) err := c.ShouldBind(&req) if err != nil { errs, ok := err.(validator.ValidationErrors) if !ok { resp.FailData("参数解析失败", "Err:"+err.Error(), c) return } resp.FailData("参数解析失败", errs.Translate(global.Trans), c) return } ob := new(fms.ContractPrePayment) item, e := ob.Fetch(req.PrePayId) if e != nil { if e == utils.ErrNoRow { resp.Fail("合同登记不存在或已被删除", c) return } resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c) return } e = item.Delete() if e != nil { resp.FailMsg("删除记录失败", "Err:"+e.Error(), c) return } // 操作日志 //go func() { // opData := "" // opDataByte, e := json.Marshal(req) // if e != nil { // return // } // opData = string(opDataByte) // // logItem := new(fms.ContractRegisterLog) // logItem.ContractRegisterId = req.ContractRegisterId // logItem.AdminId = int(adminInfo.AdminId) // logItem.AdminName = adminInfo.RealName // logItem.OpData = opData // logItem.OpType = fms.ContractRegisterOpTypeDel // logItem.CreateTime = nowTime // if e = logItem.Create(); e != nil { // return // } //}() resp.Ok("操作成功", c) }