123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- 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
- }
- ob := new(fms.ContractPrePayment)
- ob.CompanyName = req.CompanyName
- ob.SellerId = req.SellerId
- ob.SellerName = req.SellerName
- 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.NewCompany = req.NewCompany
- 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
- }
- item.CompanyName = req.CompanyName
- item.SellerId = req.SellerId
- item.SellerName = req.SellerName
- 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.NewCompany = req.NewCompany
- item.ModifyTime = time.Now().Local()
- updateCols := []string{
- "ContractCode", "CompanyName", "SellerId", "SellerName", "Amount", "StartDate", "EndDate",
- "Remark", "ModifyTime","NewCompany", "OriginAmount", "CurrencyUnit",
- }
- 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)
- }
|