register.go 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807
  1. package contract
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/gin-gonic/gin"
  7. "github.com/go-playground/validator/v10"
  8. "github.com/shopspring/decimal"
  9. "github.com/tealeg/xlsx"
  10. "hongze/fms_api/controller/resp"
  11. "hongze/fms_api/global"
  12. "hongze/fms_api/models/base"
  13. "hongze/fms_api/models/crm"
  14. "hongze/fms_api/models/fms"
  15. "hongze/fms_api/models/system"
  16. fmsService "hongze/fms_api/services/fms"
  17. "hongze/fms_api/utils"
  18. "net/http"
  19. "os"
  20. "path"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // RegisterController 合同登记
  26. type RegisterController struct{}
  27. // List
  28. // @Title 合同登记列表
  29. // @Description 合同登记列表
  30. // @Param Keyword query string false "关键词"
  31. // @Param StartDate query string false "合同开始日期"
  32. // @Param EndDate query string false "合同结束日期"
  33. // @Param ServiceType query int false "套餐类型"
  34. // @Param ContractType query int false "合同类型"
  35. // @Param RegisterStatus query int false "登记状态"
  36. // @Success 200 {object} fms.ContractRegisterItem
  37. // @router /contract/register/list [get]
  38. func (rg *RegisterController) List(c *gin.Context) {
  39. var req fms.ContractRegisterListReq
  40. if e := c.BindQuery(&req); e != nil {
  41. err, ok := e.(validator.ValidationErrors)
  42. if !ok {
  43. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  44. return
  45. }
  46. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  47. return
  48. }
  49. cond := `1 = 1`
  50. pars := make([]interface{}, 0)
  51. // 合同编号/客户姓名/销售/代付方
  52. if req.Keyword != "" {
  53. kw := "%" + req.Keyword + "%"
  54. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ? OR pay_company_name LIKE ?)`
  55. pars = append(pars, kw, kw, kw, kw)
  56. }
  57. if req.StartDate != "" && req.EndDate != "" {
  58. st := fmt.Sprint(req.StartDate, " 00:00:00")
  59. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  60. cond += ` AND (create_time BETWEEN ? AND ?)`
  61. pars = append(pars, st, ed)
  62. }
  63. if req.ContractType != 0 {
  64. cond += ` AND contract_type = ?`
  65. pars = append(pars, req.ContractType)
  66. }
  67. if req.RegisterStatus != 0 {
  68. cond += ` AND register_status = ?`
  69. pars = append(pars, req.RegisterStatus)
  70. }
  71. // 套餐筛选
  72. if req.ServiceType != 0 {
  73. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  74. if e != nil {
  75. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  76. return
  77. }
  78. if len(registerIds) > 0 {
  79. cond += ` AND contract_register_id IN ?`
  80. pars = append(pars, registerIds)
  81. } else {
  82. cond += ` AND 1 = 2`
  83. }
  84. }
  85. page := new(base.Page)
  86. page.SetPageSize(req.PageSize)
  87. page.SetCurrent(req.Current)
  88. page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: false})
  89. total, list, e := fms.GetContractRegisterItemPageList(page, cond, pars)
  90. if e != nil {
  91. resp.FailMsg("获取失败", "获取合同登记列表失败, Err: "+e.Error(), c)
  92. return
  93. }
  94. registerIds := make([]int, 0)
  95. for i := range list {
  96. registerIds = append(registerIds, list[i].ContractRegisterId)
  97. }
  98. serviceMap := make(map[int]string, 0)
  99. invoiceMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  100. paymentMap := make(map[int][]*fms.ContractInvoiceItem, 0)
  101. if len(registerIds) > 0 {
  102. // 获取服务套餐
  103. servicesNameList, e := fms.GetContractRegisterServicesNameByRegisterIds(registerIds)
  104. if e != nil {
  105. resp.FailMsg("获取失败", "获取套餐拼接字符串失败, Err: "+e.Error(), c)
  106. return
  107. }
  108. for i := range servicesNameList {
  109. serviceMap[servicesNameList[i].ContractRegisterId] = servicesNameList[i].ServicesName
  110. }
  111. // 获取开票/到款列表
  112. invoiceCond := `contract_register_id IN ?`
  113. invoicePars := make([]interface{}, 0)
  114. invoicePars = append(invoicePars, registerIds)
  115. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  116. if e != nil {
  117. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  118. return
  119. }
  120. for i := range invoiceList {
  121. if invoiceMap[invoiceList[i].ContractRegisterId] == nil {
  122. invoiceMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  123. }
  124. if paymentMap[invoiceList[i].ContractRegisterId] == nil {
  125. paymentMap[invoiceList[i].ContractRegisterId] = make([]*fms.ContractInvoiceItem, 0)
  126. }
  127. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  128. invoiceMap[invoiceList[i].ContractRegisterId] = append(invoiceMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  129. }
  130. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  131. paymentMap[invoiceList[i].ContractRegisterId] = append(paymentMap[invoiceList[i].ContractRegisterId], invoiceList[i])
  132. }
  133. }
  134. }
  135. respList := make([]*fms.ContractRegisterList, 0)
  136. for i := range list {
  137. v := new(fms.ContractRegisterList)
  138. v.ContractRegisterItem = list[i]
  139. v.ServicesName = serviceMap[list[i].ContractRegisterId]
  140. v.InvoiceList = invoiceMap[list[i].ContractRegisterId]
  141. v.PaymentList = paymentMap[list[i].ContractRegisterId]
  142. respList = append(respList, v)
  143. }
  144. page.SetTotal(total)
  145. baseData := new(base.BaseData)
  146. baseData.SetPage(page)
  147. baseData.SetList(respList)
  148. resp.OkData("获取成功", baseData, c)
  149. }
  150. // Add
  151. // @Title 新增合同登记
  152. // @Description 新增合同登记
  153. // @Param request body fms.ContractRegisterAddReq true "type json string"
  154. // @Success 200 string "操作成功"
  155. // @router /contract/register/add [post]
  156. func (rg *RegisterController) Add(c *gin.Context) {
  157. req := new(fms.ContractRegisterAddReq)
  158. err := c.ShouldBind(&req)
  159. if err != nil {
  160. errs, ok := err.(validator.ValidationErrors)
  161. if !ok {
  162. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  163. return
  164. }
  165. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  166. return
  167. }
  168. claims, _ := c.Get("adminInfo")
  169. adminInfo := claims.(*system.SysAdmin)
  170. // 日期校验
  171. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  172. if e != nil {
  173. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  174. return
  175. }
  176. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  177. if e != nil {
  178. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  179. return
  180. }
  181. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  182. if req.SignDate != "" {
  183. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  184. if e != nil {
  185. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  186. return
  187. }
  188. signDate = signDateTime
  189. }
  190. // 是否存在相同合同编号的登记
  191. ob := new(fms.ContractRegister)
  192. existCond := `contract_code = ?`
  193. existPars := make([]interface{}, 0)
  194. existPars = append(existPars, req.ContractCode)
  195. exist, e := ob.FetchByCondition(existCond, existPars)
  196. if e != nil && e != utils.ErrNoRow {
  197. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  198. return
  199. }
  200. if exist != nil && exist.ContractRegisterId > 0 {
  201. resp.Fail("合同编号已存在", c)
  202. return
  203. }
  204. nowTime := time.Now().Local()
  205. ob.ContractCode = req.ContractCode
  206. ob.PayContractCode = req.PayContractCode
  207. ob.CrmContractId = req.CrmContractId
  208. ob.ContractSource = req.ContractSource
  209. ob.CompanyName = req.CompanyName
  210. ob.PayCompanyName = req.PayCompanyName
  211. ob.ProductId = req.ProductId
  212. ob.SellerId = req.SellerId
  213. ob.SellerName = req.SellerName
  214. ob.ContractType = req.ContractType
  215. ob.ContractAmount = req.ContractAmount
  216. ob.StartDate = startDate
  217. ob.EndDate = endDate
  218. ob.SignDate = signDate
  219. ob.AgreedPayTime = req.AgreedPayTime
  220. ob.ContractStatus = req.ContractStatus
  221. ob.RegisterStatus = fms.ContractRegisterStatusIng
  222. ob.Remark = req.Remark
  223. ob.ServiceRemark = req.ServiceRemark
  224. ob.Set()
  225. // 补充协议直接完成
  226. if req.ContractType == fms.ContractTypePlus {
  227. ob.RegisterStatus = fms.ContractRegisterStatusComplete
  228. }
  229. // 套餐信息
  230. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  231. if e != nil {
  232. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  233. return
  234. }
  235. // 新增合同及套餐
  236. if e = fms.CreateContractRegisterAndServices(ob, serviceList); e != nil {
  237. resp.FailMsg("操作失败", "新增合同及套餐失败, Err: "+e.Error(), c)
  238. return
  239. }
  240. // 操作日志
  241. go func() {
  242. opData := ""
  243. opDataByte, e := json.Marshal(req)
  244. if e != nil {
  245. return
  246. }
  247. opData = string(opDataByte)
  248. logItem := new(fms.ContractRegisterLog)
  249. logItem.ContractRegisterId = ob.ContractRegisterId
  250. logItem.AdminId = int(adminInfo.AdminId)
  251. logItem.AdminName = adminInfo.RealName
  252. logItem.OpData = opData
  253. logItem.OpType = fms.ContractRegisterOpTypeSave
  254. logItem.CreateTime = nowTime
  255. logItem.Remark = req.Remark
  256. if e = logItem.Create(); e != nil {
  257. return
  258. }
  259. }()
  260. resp.Ok("操作成功", c)
  261. }
  262. // Edit
  263. // @Title 编辑合同登记
  264. // @Description 编辑合同登记
  265. // @Param request body fms.ContractRegisterEditReq true "type json string"
  266. // @Success 200 string "操作成功"
  267. // @router /contract/register/edit [post]
  268. func (rg *RegisterController) Edit(c *gin.Context) {
  269. req := new(fms.ContractRegisterEditReq)
  270. err := c.ShouldBind(&req)
  271. if err != nil {
  272. errs, ok := err.(validator.ValidationErrors)
  273. if !ok {
  274. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  275. return
  276. }
  277. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  278. return
  279. }
  280. claims, _ := c.Get("adminInfo")
  281. adminInfo := claims.(*system.SysAdmin)
  282. // 日期校验
  283. startDate, e := time.ParseInLocation(utils.FormatDate, req.StartDate, time.Local)
  284. if e != nil {
  285. resp.FailMsg("合同开始日期格式有误", "合同开始日期格式有误, Err: "+e.Error(), c)
  286. return
  287. }
  288. endDate, e := time.ParseInLocation(utils.FormatDate, req.EndDate, time.Local)
  289. if e != nil {
  290. resp.FailMsg("合同结束日期格式有误", "合同结束日期格式有误, Err: "+e.Error(), c)
  291. return
  292. }
  293. signDate, _ := time.ParseInLocation(utils.FormatDate, "0000-00-00", time.Local)
  294. if req.SignDate != "" {
  295. signDateTime, e := time.ParseInLocation(utils.FormatDate, req.SignDate, time.Local)
  296. if e != nil {
  297. resp.FailMsg("合同签订日期格式有误", "合同签订日期格式有误, Err: "+e.Error(), c)
  298. return
  299. }
  300. signDate = signDateTime
  301. }
  302. ob := new(fms.ContractRegister)
  303. item, e := ob.Fetch(req.ContractRegisterId)
  304. if e != nil {
  305. if e == utils.ErrNoRow {
  306. resp.Fail("登记记录不存在或已被删除", c)
  307. return
  308. }
  309. resp.FailMsg("操作失败", "获取合同登记信息失败, Err:"+e.Error(), c)
  310. return
  311. }
  312. // 是否存在相同合同编号的登记
  313. existCond := `contract_code = ?`
  314. existPars := make([]interface{}, 0)
  315. existPars = append(existPars, req.ContractCode)
  316. exist, e := ob.FetchByCondition(existCond, existPars)
  317. if e != nil && e != utils.ErrNoRow {
  318. resp.FailMsg("操作失败", "获取相同登记号失败, Err: "+e.Error(), c)
  319. return
  320. }
  321. if exist != nil && exist.ContractRegisterId > 0 && exist.ContractRegisterId != item.ContractRegisterId {
  322. resp.Fail("合同编号已存在", c)
  323. return
  324. }
  325. nowTime := time.Now().Local()
  326. item.ContractCode = req.ContractCode
  327. item.PayContractCode = req.PayContractCode
  328. item.CrmContractId = req.CrmContractId
  329. item.ContractSource = req.ContractSource
  330. item.CompanyName = req.CompanyName
  331. item.PayCompanyName = req.PayCompanyName
  332. item.SellerId = req.SellerId
  333. item.SellerName = req.SellerName
  334. item.ContractType = req.ContractType
  335. item.ContractAmount = req.ContractAmount
  336. item.StartDate = startDate
  337. item.EndDate = endDate
  338. item.SignDate = signDate
  339. item.AgreedPayTime = req.AgreedPayTime
  340. item.ContractStatus = req.ContractStatus
  341. item.RegisterStatus = fms.ContractRegisterStatusIng
  342. // 补充协议直接完成
  343. if req.ContractType == fms.ContractTypePlus {
  344. item.RegisterStatus = fms.ContractRegisterStatusComplete
  345. }
  346. item.Remark = req.Remark
  347. item.ServiceRemark = req.ServiceRemark
  348. item.ModifyTime = nowTime
  349. updateCols := []string{
  350. "ContractCode", "CrmContractId", "ContractSource", "CompanyName", "SellerId", "SellerName",
  351. "ContractType", "ContractAmount", "StartDate", "EndDate", "SignDate", "AgreedPayTime", "ContractStatus",
  352. "RegisterStatus", "Remark", "ModifyTime", "ServiceRemark",
  353. }
  354. // 套餐信息
  355. serviceList, e := fmsService.HandleContractServiceAndDetail(req.ProductId, req.Services, true)
  356. if e != nil {
  357. resp.FailMsg("操作失败", "获取合同套餐详情失败, Err: "+e.Error(), c)
  358. return
  359. }
  360. // 更新合同及套餐
  361. if e = fms.UpdateContractRegisterAndServices(item, updateCols, serviceList); e != nil {
  362. resp.FailMsg("操作失败", "更新合同及套餐失败, Err: "+e.Error(), c)
  363. return
  364. }
  365. // 非补充协议校验金额-是否修改状态
  366. if item.ContractType != fms.ContractTypePlus {
  367. go fmsService.CheckContractRegisterAmount(item.ContractRegisterId)
  368. }
  369. // 操作日志
  370. go func() {
  371. opData := ""
  372. opDataByte, e := json.Marshal(req)
  373. if e != nil {
  374. return
  375. }
  376. opData = string(opDataByte)
  377. logItem := new(fms.ContractRegisterLog)
  378. logItem.ContractRegisterId = item.ContractRegisterId
  379. logItem.AdminId = int(adminInfo.AdminId)
  380. logItem.AdminName = adminInfo.RealName
  381. logItem.OpData = opData
  382. logItem.OpType = fms.ContractRegisterOpTypeEdit
  383. logItem.CreateTime = nowTime
  384. logItem.Remark = req.Remark
  385. if e = logItem.Create(); e != nil {
  386. return
  387. }
  388. }()
  389. resp.Ok("操作成功", c)
  390. }
  391. // Del
  392. // @Title 删除合同登记
  393. // @Description 删除合同登记
  394. // @Param request body fms.ContractRegisterDelReq true "type json string"
  395. // @Success 200 string "操作成功"
  396. // @router /contract/register/del [post]
  397. func (rg *RegisterController) Del(c *gin.Context) {
  398. req := new(fms.ContractRegisterDelReq)
  399. err := c.ShouldBind(&req)
  400. if err != nil {
  401. errs, ok := err.(validator.ValidationErrors)
  402. if !ok {
  403. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  404. return
  405. }
  406. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  407. return
  408. }
  409. claims, _ := c.Get("adminInfo")
  410. adminInfo := claims.(*system.SysAdmin)
  411. ob := new(fms.ContractRegister)
  412. item, e := ob.Fetch(req.ContractRegisterId)
  413. if e != nil {
  414. if e == utils.ErrNoRow {
  415. resp.Fail("合同登记不存在或已被删除", c)
  416. return
  417. }
  418. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  419. return
  420. }
  421. nowTime := time.Now().Local()
  422. item.IsDeleted = 1
  423. item.ModifyTime = nowTime
  424. updateCols := []string{"IsDeleted", "ModifyTime"}
  425. if e = item.Update(updateCols); e != nil {
  426. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  427. return
  428. }
  429. // 操作日志
  430. go func() {
  431. opData := ""
  432. opDataByte, e := json.Marshal(req)
  433. if e != nil {
  434. return
  435. }
  436. opData = string(opDataByte)
  437. logItem := new(fms.ContractRegisterLog)
  438. logItem.ContractRegisterId = req.ContractRegisterId
  439. logItem.AdminId = int(adminInfo.AdminId)
  440. logItem.AdminName = adminInfo.RealName
  441. logItem.OpData = opData
  442. logItem.OpType = fms.ContractRegisterOpTypeDel
  443. logItem.CreateTime = nowTime
  444. if e = logItem.Create(); e != nil {
  445. return
  446. }
  447. }()
  448. resp.Ok("操作成功", c)
  449. }
  450. // Detail
  451. // @Title 合同登记详情
  452. // @Description 合同登记详情
  453. // @Param ContractRegisterId query int false "合同登记ID"
  454. // @Success 200 {object} fms.ContractRegisterDetail
  455. // @router /contract/register/detail [get]
  456. func (rg *RegisterController) Detail(c *gin.Context) {
  457. var req fms.ContractRegisterDetailReq
  458. if e := c.BindQuery(&req); e != nil {
  459. err, ok := e.(validator.ValidationErrors)
  460. if !ok {
  461. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  462. return
  463. }
  464. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  465. return
  466. }
  467. result := new(fms.ContractRegisterDetail)
  468. // 合同登记信息
  469. item, e := fms.GetContractRegisterItemById(req.ContractRegisterId)
  470. if e != nil {
  471. resp.FailData("获取失败", "获取合同登记详情失败, Err:"+e.Error(), c)
  472. return
  473. }
  474. result.ContractRegisterItem = item
  475. // 套餐信息
  476. serviceList, e := fmsService.GetContractServiceAndDetail(req.ContractRegisterId)
  477. if e != nil {
  478. resp.FailData("获取失败", "获取合同套餐信息失败, Err: "+e.Error(), c)
  479. return
  480. }
  481. result.ServiceList = serviceList
  482. // 开票/到款信息
  483. invoiceCond := `contract_register_id = ?`
  484. invoicePars := make([]interface{}, 0)
  485. invoicePars = append(invoicePars, req.ContractRegisterId)
  486. invoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  487. if e != nil {
  488. resp.FailData("获取失败", "获取合同开票/到款信息失败, Err: "+e.Error(), c)
  489. return
  490. }
  491. result.InvoiceList = make([]*fms.ContractInvoiceItem, 0)
  492. result.PaymentList = make([]*fms.ContractInvoiceItem, 0)
  493. for i := range invoiceList {
  494. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypeMake {
  495. result.InvoiceList = append(result.InvoiceList, invoiceList[i])
  496. continue
  497. }
  498. if invoiceList[i].InvoiceType == fms.ContractInvoiceTypePay {
  499. result.PaymentList = append(result.PaymentList, invoiceList[i])
  500. }
  501. }
  502. // 合同登记进度
  503. logCond := `contract_register_id = ?`
  504. logPars := make([]interface{}, 0)
  505. logPars = append(logPars, req.ContractRegisterId)
  506. logList, e := fms.GetContractRegisterLogItemList(logCond, logPars)
  507. if e != nil {
  508. resp.FailData("获取失败", "获取合同登记进度失败, Err: "+e.Error(), c)
  509. return
  510. }
  511. result.Logs = logList
  512. resp.OkData("获取成功", result, c)
  513. }
  514. // UpdateStatus
  515. // @Title 修改合同状态
  516. // @Description 修改合同状态
  517. // @Param request body fms.ContractRegisterUpdateStatusReq true "type json string"
  518. // @Success 200 string "操作成功"
  519. // @router /contract/register/update_status [post]
  520. func (rg *RegisterController) UpdateStatus(c *gin.Context) {
  521. req := new(fms.ContractRegisterUpdateStatusReq)
  522. err := c.ShouldBind(&req)
  523. if err != nil {
  524. errs, ok := err.(validator.ValidationErrors)
  525. if !ok {
  526. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  527. return
  528. }
  529. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  530. return
  531. }
  532. claims, _ := c.Get("adminInfo")
  533. adminInfo := claims.(*system.SysAdmin)
  534. ob := new(fms.ContractRegister)
  535. item, e := ob.Fetch(req.ContractRegisterId)
  536. if e != nil {
  537. if e == utils.ErrNoRow {
  538. resp.Fail("合同登记不存在或已被删除", c)
  539. return
  540. }
  541. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  542. return
  543. }
  544. nowTime := time.Now().Local()
  545. item.ContractStatus = req.ContractStatus
  546. item.ModifyTime = nowTime
  547. updateCols := []string{"ContractStatus", "ModifyTime"}
  548. if e = item.Update(updateCols); e != nil {
  549. resp.FailMsg("操作失败", "更新合同登记失败, Err:"+e.Error(), c)
  550. return
  551. }
  552. // 非补充协议校验金额-是否修改状态
  553. if item.ContractType != fms.ContractTypePlus {
  554. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  555. }
  556. // 操作日志
  557. go func() {
  558. opData := ""
  559. opDataByte, e := json.Marshal(req)
  560. if e != nil {
  561. return
  562. }
  563. opData = string(opDataByte)
  564. logItem := new(fms.ContractRegisterLog)
  565. logItem.ContractRegisterId = req.ContractRegisterId
  566. logItem.AdminId = int(adminInfo.AdminId)
  567. logItem.AdminName = adminInfo.RealName
  568. logItem.OpData = opData
  569. logItem.OpType = fms.ContractRegisterOpTypeStatus
  570. logItem.CreateTime = nowTime
  571. if e = logItem.Create(); e != nil {
  572. return
  573. }
  574. }()
  575. resp.Ok("操作成功", c)
  576. }
  577. // Invoice
  578. // @Title 开票/到款登记
  579. // @Description 开票/到款登记
  580. // @Param request body fms.ContractInvoiceSaveReq true "type json string"
  581. // @Success 200 string "操作成功"
  582. // @router /contract/register/invoice [post]
  583. func (rg *RegisterController) Invoice(c *gin.Context) {
  584. req := new(fms.ContractInvoiceSaveReq)
  585. err := c.ShouldBind(&req)
  586. if err != nil {
  587. errs, ok := err.(validator.ValidationErrors)
  588. if !ok {
  589. resp.FailData("参数解析失败", "Err:"+err.Error(), c)
  590. return
  591. }
  592. resp.FailData("参数解析失败", errs.Translate(global.Trans), c)
  593. return
  594. }
  595. claims, _ := c.Get("adminInfo")
  596. adminInfo := claims.(*system.SysAdmin)
  597. registerOB := new(fms.ContractRegister)
  598. item, e := registerOB.Fetch(req.ContractRegisterId)
  599. if e != nil {
  600. if e == utils.ErrNoRow {
  601. resp.Fail("合同登记不存在或已被删除", c)
  602. return
  603. }
  604. resp.FailMsg("获取合同登记失败", "Err:"+e.Error(), c)
  605. return
  606. }
  607. if item.ContractType == fms.ContractTypePlus {
  608. resp.Fail("补充协议不允许添加开票/到款登记", c)
  609. return
  610. }
  611. noChangeInvoiceIds := make([]int, 0)
  612. newInvoice := make([]*fms.ContractInvoice, 0)
  613. if len(req.AmountList) > 0 {
  614. for i := range req.AmountList {
  615. if req.AmountList[i].Amount <= 0 {
  616. resp.Fail("登记金额有误", c)
  617. return
  618. }
  619. if req.AmountList[i].InvoiceDate == "" {
  620. resp.Fail("请选择日期", c)
  621. return
  622. }
  623. t, e := time.ParseInLocation(utils.FormatDate, req.AmountList[i].InvoiceDate, time.Local)
  624. if e != nil {
  625. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  626. return
  627. }
  628. if req.AmountList[i].InvoiceId > 0 {
  629. noChangeInvoiceIds = append(noChangeInvoiceIds, req.AmountList[i].InvoiceId)
  630. } else {
  631. v := &fms.ContractInvoice{
  632. ContractRegisterId: req.ContractRegisterId,
  633. ContractCode: item.ContractCode,
  634. Amount: req.AmountList[i].Amount,
  635. InvoiceType: req.InvoiceType,
  636. InvoiceDate: t,
  637. AdminId: int(adminInfo.AdminId),
  638. AdminName: adminInfo.RealName,
  639. Remark: req.AmountList[i].Remark,
  640. }
  641. v.Set()
  642. newInvoice = append(newInvoice, v)
  643. }
  644. }
  645. }
  646. // 获取原有的登记信息
  647. invoiceCond := `contract_register_id = ? AND invoice_type = ?`
  648. invoicePars := make([]interface{}, 0)
  649. invoicePars = append(invoicePars, req.ContractRegisterId, req.InvoiceType)
  650. originInvoiceList, e := fms.GetContractInvoiceItemList(invoiceCond, invoicePars)
  651. if e != nil {
  652. resp.FailMsg("获取失败", "获取开票/到款列表失败, Err: "+e.Error(), c)
  653. return
  654. }
  655. // 比对原有和现有的登记信息
  656. logList := make([]*fms.ContractRegisterLog, 0)
  657. opData := ""
  658. opDataByte, e := json.Marshal(req)
  659. if e != nil {
  660. return
  661. }
  662. opData = string(opDataByte)
  663. opType := fms.ContractRegisterOpTypeInvoice
  664. if req.InvoiceType == fms.ContractInvoiceTypePay {
  665. opType = fms.ContractRegisterOpTypePayment
  666. }
  667. nowTime := time.Now().Local()
  668. // 需要删除的记录
  669. deleteInvoiceIds := make([]int, 0)
  670. for i := range originInvoiceList {
  671. if !utils.InArrayByInt(noChangeInvoiceIds, originInvoiceList[i].ContractInvoiceId) {
  672. deleteInvoiceIds = append(deleteInvoiceIds, originInvoiceList[i].ContractInvoiceId)
  673. logList = append(logList, &fms.ContractRegisterLog{
  674. ContractRegisterId: req.ContractRegisterId,
  675. AdminId: int(adminInfo.AdminId),
  676. AdminName: adminInfo.RealName,
  677. OpData: opData,
  678. OpType: opType,
  679. CreateTime: nowTime,
  680. AmountRemark: fmt.Sprint("删除", fms.ContractInvoiceKeyNameMap[opType], "金额", originInvoiceList[i].Amount, "元"),
  681. })
  682. }
  683. }
  684. // 新增的记录
  685. if len(newInvoice) > 0 {
  686. newAmount := decimal.NewFromFloat(0).Round(2)
  687. for i := range newInvoice {
  688. a := decimal.NewFromFloat(newInvoice[i].Amount).Round(2)
  689. newAmount = newAmount.Add(a)
  690. }
  691. ia, _ := newAmount.Round(2).Float64()
  692. logList = append(logList, &fms.ContractRegisterLog{
  693. ContractRegisterId: req.ContractRegisterId,
  694. AdminId: int(adminInfo.AdminId),
  695. AdminName: adminInfo.RealName,
  696. OpData: opData,
  697. OpType: opType,
  698. CreateTime: nowTime,
  699. AmountRemark: fmt.Sprint("新增", fms.ContractInvoiceKeyNameMap[opType], "金额", ia, "元"),
  700. })
  701. }
  702. // 删除并新增登记
  703. ob := new(fms.ContractInvoice)
  704. if e := ob.DeleteAndCreateNewInvoice(req.ContractRegisterId, req.InvoiceType, deleteInvoiceIds, newInvoice); e != nil {
  705. resp.FailData("日期格式有误", "Err:"+e.Error(), c)
  706. return
  707. }
  708. // 校验金额-是否修改状态
  709. go fmsService.CheckContractRegisterAmount(req.ContractRegisterId)
  710. // 操作日志
  711. go func() {
  712. logOB := new(fms.ContractRegisterLog)
  713. if e := logOB.AddInBatches(logList); e != nil {
  714. return
  715. }
  716. }()
  717. resp.Ok("操作成功", c)
  718. }
  719. // Export
  720. // @Title 合同登记-导出
  721. // @Description 合同登记-导出
  722. // @Param Keyword query string false "关键词"
  723. // @Param StartDate query string false "合同开始日期"
  724. // @Param EndDate query string false "合同结束日期"
  725. // @Param ServiceType query int false "套餐类型"
  726. // @Param ContractType query int false "合同类型"
  727. // @Param RegisterStatus query int false "登记状态"
  728. // @Success 200 string "操作成功"
  729. // @router /contract/register/export [get]
  730. func (rg *RegisterController) Export(c *gin.Context) {
  731. var req fms.ContractRegisterListReq
  732. if e := c.BindQuery(&req); e != nil {
  733. err, ok := e.(validator.ValidationErrors)
  734. if !ok {
  735. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  736. return
  737. }
  738. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  739. return
  740. }
  741. cond := `1 = 1`
  742. pars := make([]interface{}, 0)
  743. if req.Keyword != "" {
  744. kw := "%" + req.Keyword + "%"
  745. cond += ` AND (company_name LIKE ? OR contract_code LIKE ? OR seller_name LIKE ?)`
  746. pars = append(pars, kw, kw, kw)
  747. }
  748. if req.StartDate != "" && req.EndDate != "" {
  749. st := fmt.Sprint(req.StartDate, " 00:00:00")
  750. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  751. cond += ` AND (create_time BETWEEN ? AND ?)`
  752. pars = append(pars, st, ed)
  753. }
  754. if req.ContractType != 0 {
  755. cond += ` AND contract_type = ?`
  756. pars = append(pars, req.ContractType)
  757. }
  758. if req.RegisterStatus != 0 {
  759. cond += ` AND register_status = ?`
  760. pars = append(pars, req.RegisterStatus)
  761. }
  762. if req.ServiceType != 0 {
  763. registerIds, e := fms.GetContractRegisterIdsByTempId(req.ServiceType)
  764. if e != nil {
  765. resp.FailMsg("获取失败", "获取合同登记IDs失败, Err: "+e.Error(), c)
  766. return
  767. }
  768. if len(registerIds) > 0 {
  769. cond += ` AND contract_register_id IN ?`
  770. pars = append(pars, registerIds)
  771. } else {
  772. cond += ` AND 1 = 2`
  773. }
  774. }
  775. // 获取列表数据
  776. cr := new(fms.ContractRegister)
  777. list, e := cr.List(cond, pars)
  778. if e != nil {
  779. resp.FailData("获取合同列表失败", "Err:"+e.Error(), c)
  780. return
  781. }
  782. if len(list) == 0 {
  783. resp.Fail("无有效数据可导出", c)
  784. return
  785. }
  786. registerIds := make([]int, 0)
  787. for i := range list {
  788. registerIds = append(registerIds, list[i].ContractRegisterId)
  789. }
  790. // 获取小套餐品种
  791. cpCond := `product_id = ? AND permission_name <> ?`
  792. cpPars := make([]interface{}, 0)
  793. cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
  794. cp := new(crm.ChartPermission)
  795. permissionList, e := cp.List(cpCond, cpPars)
  796. if e != nil {
  797. resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
  798. return
  799. }
  800. permissionLen := len(permissionList)
  801. permissionNameIdMap := make(map[string]int)
  802. for i := range permissionList {
  803. permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  804. }
  805. // 获取套餐服务
  806. //serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
  807. //if e != nil {
  808. // resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
  809. // return
  810. //}
  811. // 套餐/开票/到款列表
  812. serviceMap := make(map[int][]*fms.ContractService)
  813. serviceChartPermissionsMap := make(map[int][]int)
  814. invoiceMap := make(map[int][]*fms.ContractInvoice)
  815. paymentMap := make(map[int][]*fms.ContractInvoice)
  816. maxInvoice := 0
  817. maxPayment := 0
  818. if len(registerIds) > 0 {
  819. // 获取套餐信息
  820. csCond := `contract_register_id IN ?`
  821. csPars := make([]interface{}, 0)
  822. csPars = append(csPars, registerIds)
  823. cs := new(fms.ContractService)
  824. serviceList, e := cs.List(csCond, csPars)
  825. if e != nil {
  826. resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
  827. return
  828. }
  829. for i := range serviceList {
  830. cid := serviceList[i].ContractRegisterId
  831. if serviceMap[cid] == nil {
  832. serviceMap[cid] = make([]*fms.ContractService, 0)
  833. }
  834. serviceMap[cid] = append(serviceMap[cid], serviceList[i])
  835. // 小套餐权限
  836. if serviceChartPermissionsMap[cid] == nil {
  837. serviceChartPermissionsMap[cid] = make([]int, 0)
  838. }
  839. if serviceList[i].ChartPermissionIds != "" {
  840. ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
  841. serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
  842. }
  843. }
  844. // 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
  845. ci := new(fms.ContractInvoice)
  846. invoiceList, e := ci.List(csCond, csPars, "")
  847. if e != nil {
  848. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  849. return
  850. }
  851. for k := range invoiceList {
  852. cid := invoiceList[k].ContractRegisterId
  853. if invoiceMap[cid] == nil {
  854. invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
  855. }
  856. if paymentMap[cid] == nil {
  857. paymentMap[cid] = make([]*fms.ContractInvoice, 0)
  858. }
  859. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
  860. invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
  861. continue
  862. }
  863. if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
  864. paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
  865. continue
  866. }
  867. }
  868. // 取最大开票/到款数
  869. for j := range invoiceMap {
  870. if len(invoiceMap[j]) > maxInvoice {
  871. maxInvoice = len(invoiceMap[j])
  872. }
  873. }
  874. for p := range paymentMap {
  875. if len(paymentMap[p]) > maxPayment {
  876. maxPayment = len(paymentMap[p])
  877. }
  878. }
  879. }
  880. // 生成Excel文件
  881. //dir, err := os.Executable()
  882. //exPath := filepath.Dir(dir)
  883. //filePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  884. xlsxFile := xlsx.NewFile()
  885. //if err != nil {
  886. // resp.FailData("生成文件失败", "Err:"+err.Error(), c)
  887. // return
  888. //}
  889. style := xlsx.NewStyle()
  890. alignment := xlsx.Alignment{
  891. Horizontal: "center",
  892. Vertical: "center",
  893. WrapText: true,
  894. }
  895. style.Alignment = alignment
  896. style.ApplyAlignment = true
  897. sheet, err := xlsxFile.AddSheet("合同登记")
  898. if err != nil {
  899. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  900. return
  901. }
  902. //_ = sheet.SetColWidth(0, 0, 60)
  903. //_ = sheet.SetColWidth(4, 10, 60)
  904. // 1行表头
  905. titleRow := sheet.AddRow()
  906. titleRow.SetHeight(40)
  907. // 1行1列-右合并两格
  908. cell1 := titleRow.AddCell()
  909. cell1.HMerge = 2
  910. cell1.SetValue("FICC客户签约表 2022")
  911. cell1.SetStyle(style)
  912. // 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
  913. titleRow.AddCell().SetValue("")
  914. titleRow.AddCell().SetValue("")
  915. // 1行2列
  916. cell2 := titleRow.AddCell()
  917. cell2.SetValue("FICC大套餐")
  918. cell2.SetStyle(style)
  919. // 1行3列-右合并小套餐数
  920. if permissionLen >= 1 {
  921. cell3 := titleRow.AddCell()
  922. cell3.HMerge = permissionLen - 1
  923. cell3.SetValue("FICC小套餐")
  924. cell3.SetStyle(style)
  925. // 同上右增单元格小套餐数-1的空白单元格用于合并
  926. for i := 0; i < permissionLen-1; i++ {
  927. titleRow.AddCell().SetValue("")
  928. }
  929. }
  930. cell4 := titleRow.AddCell()
  931. cell4.SetValue("市场策略")
  932. cell4.SetStyle(style)
  933. cell5 := titleRow.AddCell()
  934. cell5.SetValue("财富管理")
  935. cell5.SetStyle(style)
  936. // 第二行表头
  937. titleRow2 := sheet.AddRow()
  938. titleRow2.SetHeight(60)
  939. row2Title := make([]string, 0)
  940. row2Title = append(row2Title, "客户名称", "续约-0\n新增-1", "销售", "FICC大套餐")
  941. for i := range permissionList {
  942. row2Title = append(row2Title, permissionList[i].PermissionName)
  943. }
  944. row2Title = append(row2Title, "市场策略", "财富管理", "套餐备注", "开始时间", "到期时间", "2022年合同金额", "约定付款时间", "签订日",
  945. "签订月", "合同状态", "合同编号", "备注")
  946. for i := range row2Title {
  947. v := titleRow2.AddCell()
  948. v.SetValue(row2Title[i])
  949. v.SetStyle(style)
  950. }
  951. // 第二行表头-开票/收款(动态添加)
  952. for i := 0; i < maxInvoice; i++ {
  953. n := i + 1
  954. c1 := titleRow2.AddCell()
  955. t1 := fmt.Sprintf("%s%d", "开票日", n)
  956. c1.SetValue(t1)
  957. c1.SetStyle(style)
  958. c2 := titleRow2.AddCell()
  959. t2 := fmt.Sprintf("%s%d", "开票金额", n)
  960. c2.SetValue(t2)
  961. c2.SetStyle(style)
  962. row2Title = append(row2Title, t1, t2)
  963. }
  964. for i := 0; i < maxPayment; i++ {
  965. n := i + 1
  966. c1 := titleRow2.AddCell()
  967. t1 := fmt.Sprintf("%s%d", "收款日", n)
  968. c1.SetValue(t1)
  969. c1.SetStyle(style)
  970. c2 := titleRow2.AddCell()
  971. t2 := fmt.Sprintf("%s%d", "收款金额", n)
  972. c2.SetValue(t2)
  973. c2.SetStyle(style)
  974. row2Title = append(row2Title, t1, t2)
  975. }
  976. // 此处取第二行标题NameKeyMap, 后面的动态匹配
  977. row2NameKeyMap := make(map[string]int)
  978. for i := range row2Title {
  979. row2NameKeyMap[row2Title[i]] = i
  980. }
  981. // TODO:代付和补充协议
  982. contractTMap := map[int]int{
  983. fms.ContractTypeNew: 1,
  984. fms.ContractTypeRenew: 0,
  985. }
  986. for _, v := range list {
  987. k := -1
  988. dataRow := sheet.AddRow()
  989. dataRow.SetHeight(20)
  990. k += 3
  991. dataRow.AddCell().SetString(v.CompanyName)
  992. dataRow.AddCell().SetString(fmt.Sprint(contractTMap[v.ContractType]))
  993. dataRow.AddCell().SetString(v.SellerName)
  994. // 大套餐
  995. k += 1
  996. col4Name := row2Title[k]
  997. svList := serviceMap[v.ContractRegisterId]
  998. col4 := ""
  999. if svList != nil && len(svList) > 0 {
  1000. for isv := range svList {
  1001. if svList[isv].Title == col4Name {
  1002. col4 = "是"
  1003. break
  1004. }
  1005. }
  1006. }
  1007. dataRow.AddCell().SetString(col4)
  1008. // 小套餐
  1009. serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
  1010. for i := 0; i < permissionLen; i++ {
  1011. k += 1
  1012. colName := row2Title[k]
  1013. chartPermissionId := permissionNameIdMap[colName]
  1014. if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
  1015. dataRow.AddCell().SetString("是")
  1016. } else {
  1017. dataRow.AddCell().SetString("")
  1018. }
  1019. }
  1020. // 财富管理/市场策略(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
  1021. k += 1
  1022. col5Name := row2Title[k]
  1023. col5 := ""
  1024. if svList != nil && len(svList) > 0 {
  1025. for isv := range svList {
  1026. if svList[isv].Title == col5Name {
  1027. col5 = "是"
  1028. break
  1029. }
  1030. }
  1031. }
  1032. dataRow.AddCell().SetString(col5)
  1033. k += 1
  1034. col6Name := row2Title[k]
  1035. col6 := ""
  1036. if svList != nil && len(svList) > 0 {
  1037. for isv := range svList {
  1038. if svList[isv].Title == col6Name {
  1039. col6 = "是"
  1040. break
  1041. }
  1042. }
  1043. }
  1044. dataRow.AddCell().SetString(col6)
  1045. // 其他信息
  1046. dataRow.AddCell().SetString(v.ServiceRemark) // 套餐备注
  1047. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.StartDate)) // 开始时间
  1048. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.EndDate)) // 到期时间
  1049. dataRow.AddCell().SetString(fmt.Sprint("¥", v.ContractAmount)) // 2022年合同金额
  1050. dataRow.AddCell().SetString(v.AgreedPayTime) // 约定付款时间
  1051. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", v.SignDate)) // 签订日
  1052. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01", v.SignDate)) // 签订月
  1053. dataRow.AddCell().SetString(fms.ContractStatusKeyNameMap[v.ContractStatus]) // 合同状态
  1054. dataRow.AddCell().SetString(v.ContractCode) // 合同编号
  1055. dataRow.AddCell().SetString(v.Remark) // 备注
  1056. // 开票/到款信息
  1057. ivList := invoiceMap[v.ContractRegisterId]
  1058. ivListLen := len(ivList)
  1059. if ivList != nil && len(ivList) > 0 {
  1060. for ia := 0; ia < maxInvoice; ia++ {
  1061. if ia < ivListLen {
  1062. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", ivList[ia].InvoiceDate)) // 开票日
  1063. dataRow.AddCell().SetString(fmt.Sprint(ivList[ia].Amount)) // 开票金额
  1064. } else {
  1065. // 这里要把不够的填充为空
  1066. dataRow.AddCell().SetString("")
  1067. dataRow.AddCell().SetString("")
  1068. }
  1069. }
  1070. }
  1071. pyList := paymentMap[v.ContractRegisterId]
  1072. pyListLen := len(pyList)
  1073. if pyList != nil && pyListLen > 0 {
  1074. for ib := 0; ib < maxInvoice; ib++ {
  1075. if ib < pyListLen {
  1076. dataRow.AddCell().SetString(utils.TimeTransferString("2006/01/02", pyList[ib].InvoiceDate)) // 收款日
  1077. dataRow.AddCell().SetString(fmt.Sprint(pyList[ib].Amount)) // 收款金额
  1078. } else {
  1079. // 已经是最后的几列了其实可以不用填充空, 万一后面要加列此处还是填充上吧
  1080. dataRow.AddCell().SetString("")
  1081. dataRow.AddCell().SetString("")
  1082. }
  1083. }
  1084. }
  1085. }
  1086. // 输出文件
  1087. var buffer bytes.Buffer
  1088. _ = xlsxFile.Write(&buffer)
  1089. content := bytes.NewReader(buffer.Bytes())
  1090. //err = xlsxFile.Save(filePath)
  1091. //if err != nil {
  1092. // resp.FailData("保存文件失败", "Err:"+err.Error(), c)
  1093. // return
  1094. //}
  1095. //defer func() {
  1096. // _ = os.Remove(filePath)
  1097. //}()
  1098. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1099. fileName := "财务列表_" + randStr + ".xlsx"
  1100. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1101. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1102. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1103. }
  1104. // InvoiceList
  1105. // @Title 开票/到款列表
  1106. // @Description 开票/到款列表
  1107. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1108. // @Param ContractCode query string false "合同编号"
  1109. // @Param StartDate query string false "开始日期"
  1110. // @Param EndDate query string false "结束日期"
  1111. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1112. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1113. // @Success 200 {object} fms.ContractInvoiceItem
  1114. // @router /contract/register/invoice_list [get]
  1115. func (rg *RegisterController) InvoiceList(c *gin.Context) {
  1116. var req fms.ContractInvoiceListReq
  1117. if e := c.BindQuery(&req); e != nil {
  1118. err, ok := e.(validator.ValidationErrors)
  1119. if !ok {
  1120. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1121. return
  1122. }
  1123. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1124. return
  1125. }
  1126. cond := `invoice_type = ?`
  1127. pars := make([]interface{}, 0)
  1128. pars = append(pars, req.InvoiceType)
  1129. // 合同编号
  1130. if req.ContractCode != "" {
  1131. kw := fmt.Sprint("%", req.ContractCode, "%")
  1132. cond += ` AND contract_code LIKE ?`
  1133. pars = append(pars, kw)
  1134. }
  1135. if req.StartDate != "" && req.EndDate != "" {
  1136. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1137. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1138. cond += ` AND (invoice_time BETWEEN ? AND ?)`
  1139. pars = append(pars, st, ed)
  1140. }
  1141. if req.MinAmount > 0 {
  1142. cond += ` AND amount >= ?`
  1143. pars = append(pars, req.MinAmount)
  1144. }
  1145. if req.MaxAmount > 0 {
  1146. cond += ` AND amount <= ?`
  1147. pars = append(pars, req.MaxAmount)
  1148. }
  1149. page := new(base.Page)
  1150. page.SetPageSize(req.PageSize)
  1151. page.SetCurrent(req.Current)
  1152. page.AddOrderItem(base.OrderItem{Column: "invoice_time", Asc: false})
  1153. total, list, e := fms.GetContractInvoiceItemPageList(page, cond, pars)
  1154. if e != nil {
  1155. resp.FailMsg("获取失败", "获取合同开票/到款列表失败, Err: "+e.Error(), c)
  1156. return
  1157. }
  1158. page.SetTotal(total)
  1159. baseData := new(base.BaseData)
  1160. baseData.SetPage(page)
  1161. baseData.SetList(list)
  1162. resp.OkData("获取成功", baseData, c)
  1163. }
  1164. // InvoiceExport
  1165. // @Title 开票/到款列表-导出
  1166. // @Description 合同登记-导出
  1167. // @Param InvoiceType query int false "类型: 1-开票登记; 2-到款登记"
  1168. // @Param ContractCode query string false "合同编号"
  1169. // @Param StartDate query string false "开始日期"
  1170. // @Param EndDate query string false "结束日期"
  1171. // @Param MinAmount query float64 false "开票金额区间-最小值"
  1172. // @Param MaxAmount query float64 false "开票金额区间-最大值"
  1173. // @Success 200 string "操作成功"
  1174. // @router /contract/register/invoice_export [get]
  1175. func (rg *RegisterController) InvoiceExport(c *gin.Context) {
  1176. var req fms.ContractInvoiceListReq
  1177. if e := c.BindQuery(&req); e != nil {
  1178. err, ok := e.(validator.ValidationErrors)
  1179. if !ok {
  1180. resp.FailData("参数解析失败", "Err:"+e.Error(), c)
  1181. return
  1182. }
  1183. resp.FailData("参数解析失败", err.Translate(global.Trans), c)
  1184. return
  1185. }
  1186. cond := `invoice_type = ?`
  1187. pars := make([]interface{}, 0)
  1188. pars = append(pars, req.InvoiceType)
  1189. // 合同编号
  1190. if req.ContractCode != "" {
  1191. cond += ` AND contract_code = ?`
  1192. pars = append(pars, req.ContractCode)
  1193. }
  1194. if req.StartDate != "" && req.EndDate != "" {
  1195. st := fmt.Sprint(req.StartDate, " 00:00:00")
  1196. ed := fmt.Sprint(req.EndDate, " 23:59:59")
  1197. cond += ` AND (create_time BETWEEN ? AND ?)`
  1198. pars = append(pars, st, ed)
  1199. }
  1200. if req.MinAmount > 0 && req.MaxAmount > 0 {
  1201. cond += ` AND (amount BETWEEN ? AND ?)`
  1202. pars = append(pars, req.MinAmount, req.MaxAmount)
  1203. }
  1204. // 获取列表数据
  1205. cr := new(fms.ContractInvoice)
  1206. orderRule := `invoice_time DESC`
  1207. list, e := cr.List(cond, pars, orderRule)
  1208. if e != nil {
  1209. resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
  1210. return
  1211. }
  1212. if len(list) == 0 {
  1213. resp.Fail("无有效数据可导出", c)
  1214. return
  1215. }
  1216. // 生成Excel文件
  1217. xlsxFile := xlsx.NewFile()
  1218. style := xlsx.NewStyle()
  1219. alignment := xlsx.Alignment{
  1220. Horizontal: "center",
  1221. Vertical: "center",
  1222. WrapText: true,
  1223. }
  1224. style.Alignment = alignment
  1225. style.ApplyAlignment = true
  1226. sheet, err := xlsxFile.AddSheet("开票列表")
  1227. if err != nil {
  1228. resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
  1229. return
  1230. }
  1231. // 表头
  1232. titleRow := sheet.AddRow()
  1233. titleRow.SetHeight(40)
  1234. cell1 := titleRow.AddCell()
  1235. cell1.SetValue("合同编号")
  1236. cell1.SetStyle(style)
  1237. cell2 := titleRow.AddCell()
  1238. cell2.SetValue("开票金额")
  1239. cell2.SetStyle(style)
  1240. cell3 := titleRow.AddCell()
  1241. cell3.SetValue("开票日期")
  1242. cell3.SetStyle(style)
  1243. for _, v := range list {
  1244. dataRow := sheet.AddRow()
  1245. dataRow.SetHeight(20)
  1246. dataRow.AddCell().SetString(v.ContractCode)
  1247. dataRow.AddCell().SetString(fmt.Sprint(v.Amount))
  1248. dataRow.AddCell().SetString(utils.TimeTransferString("2006-01-02", v.InvoiceDate))
  1249. }
  1250. // 输出文件
  1251. var buffer bytes.Buffer
  1252. _ = xlsxFile.Write(&buffer)
  1253. content := bytes.NewReader(buffer.Bytes())
  1254. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1255. fileName := "开票列表_" + randStr + ".xlsx"
  1256. c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
  1257. c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  1258. http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
  1259. }
  1260. // Import
  1261. // @Title 合同登记-导入
  1262. // @Description 合同登记-导入
  1263. // @Success 200 string "操作成功"
  1264. // @router /contract/register/import [post]
  1265. func (rg *RegisterController) Import(c *gin.Context) {
  1266. h, err := c.FormFile("File")
  1267. if err != nil {
  1268. resp.FailData("获取文件失败", "Err:"+err.Error(), c)
  1269. return
  1270. }
  1271. ext := path.Ext(h.Filename)
  1272. if ext != ".xlsx" && ext != ".xls" {
  1273. resp.Fail("请上传Excel文件", c)
  1274. return
  1275. }
  1276. uploadDir := "static/xls"
  1277. err = os.MkdirAll(uploadDir, 766)
  1278. if err != nil {
  1279. resp.FailData("存储目录创建失败", "Err:"+err.Error(), c)
  1280. return
  1281. }
  1282. uploadPath := uploadDir + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + h.Filename
  1283. err = c.SaveUploadedFile(h, uploadPath)
  1284. if err != nil {
  1285. resp.FailData("保存本地文件失败", "Err:"+err.Error(), c)
  1286. return
  1287. }
  1288. defer func() {
  1289. _ = os.Remove(uploadPath)
  1290. }()
  1291. xlFile, err := xlsx.OpenFile(uploadPath)
  1292. if err != nil {
  1293. resp.FailData("打开文件失败", "Err:"+err.Error(), c)
  1294. return
  1295. }
  1296. // 获取所有已登记,根据合同编号去重
  1297. contractCodeArr := make([]string, 0)
  1298. registerOB := new(fms.ContractRegister)
  1299. registerCond := ``
  1300. registerPars := make([]interface{}, 0)
  1301. registerList, e := registerOB.List(registerCond, registerPars)
  1302. if e != nil {
  1303. resp.FailData("获取合同登记列表失败", "Err:"+e.Error(), c)
  1304. return
  1305. }
  1306. for i := range registerList {
  1307. contractCodeArr = append(contractCodeArr, registerList[i].ContractCode)
  1308. }
  1309. // 获取所有销售名称Map
  1310. sellerNameIdMap := make(map[string]int)
  1311. sellerCond := ``
  1312. sellerPars := make([]interface{}, 0)
  1313. sellerList, e := crm.GetCompanySellerByRoleCodes(sellerCond, sellerPars)
  1314. if e != nil {
  1315. resp.FailData("获取销售列表失败", "Err:"+e.Error(), c)
  1316. return
  1317. }
  1318. for i := range sellerList {
  1319. sellerNameIdMap[sellerList[i].RealName] = sellerList[i].AdminId
  1320. }
  1321. // 获取品种权限Map
  1322. chartPermissionNameIdMap := make(map[string]int)
  1323. cpCond := `product_id = ?`
  1324. cpPars := make([]interface{}, 0)
  1325. cpPars = append(cpPars, crm.CompanyProductFicc)
  1326. cp := new(crm.ChartPermission)
  1327. permissionList, e := cp.List(cpCond, cpPars)
  1328. if e != nil {
  1329. resp.FailData("获取权限列表失败", "Err:"+e.Error(), c)
  1330. return
  1331. }
  1332. for i := range permissionList {
  1333. chartPermissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
  1334. }
  1335. // 获取所有套餐模板
  1336. serviceTempCond := ``
  1337. serviceTempPars := make([]interface{}, 0)
  1338. serviceTempOB := new(fms.ContractServiceTemplate)
  1339. serviceTempList, e := serviceTempOB.List(serviceTempCond, serviceTempPars)
  1340. if e != nil {
  1341. resp.FailData("获取套餐模板列表失败", "Err:"+e.Error(), c)
  1342. return
  1343. }
  1344. serviceTempNameMap := make(map[string]*fms.ContractServiceTemplate)
  1345. for i := range serviceTempList {
  1346. serviceTempNameMap[serviceTempList[i].Title] = serviceTempList[i]
  1347. }
  1348. titleMap := make(map[int]string)
  1349. newIds := make([]int, 0)
  1350. for _, sheet := range xlFile.Sheets {
  1351. // 遍历行读取
  1352. maxRow := sheet.MaxRow
  1353. for i := 0; i < maxRow; i++ {
  1354. // 第二行开始读取表头
  1355. if i == 1 {
  1356. row := sheet.Row(i)
  1357. cells := row.Cells
  1358. for k, cell := range cells {
  1359. text := cell.String()
  1360. titleMap[k] = text
  1361. // 只检验前面部分表头
  1362. if k == 0 {
  1363. if text != "客户名称" {
  1364. resp.Fail("匹配失败, 请按模板导入", c)
  1365. return
  1366. }
  1367. }
  1368. if k == 1 {
  1369. if !strings.Contains(text, "续约") {
  1370. resp.Fail("匹配失败, 请按模板导入", c)
  1371. return
  1372. }
  1373. }
  1374. if k == 2 {
  1375. if text != "销售" {
  1376. resp.Fail("匹配失败, 请按模板导入", c)
  1377. return
  1378. }
  1379. }
  1380. if k == 3 {
  1381. if text != "FICC大套餐" {
  1382. resp.Fail("匹配失败, 请按模板导入", c)
  1383. return
  1384. }
  1385. }
  1386. }
  1387. }
  1388. // 数据行
  1389. if i >= 2 {
  1390. row := sheet.Row(i)
  1391. cells := row.Cells
  1392. // 登记信息
  1393. rowRegister := new(fms.ContractRegister)
  1394. // 套餐
  1395. rowServices := make([]*fms.ContractService, 0)
  1396. // 开票/到款
  1397. rowInvoices := make([]*fms.ContractInvoice, 0)
  1398. rowChartPermissionIdArr := make([]string, 0)
  1399. rowInvoice1 := new(fms.ContractInvoice)
  1400. rowInvoice2 := new(fms.ContractInvoice)
  1401. rowInvoice3 := new(fms.ContractInvoice)
  1402. rowInvoice4 := new(fms.ContractInvoice)
  1403. rowInvoice5 := new(fms.ContractInvoice)
  1404. rowInvoice6 := new(fms.ContractInvoice)
  1405. for k, cell := range cells {
  1406. // 客户名称
  1407. if k == 0 {
  1408. rowRegister.CompanyName = utils.TrimStr(cell.String())
  1409. }
  1410. // 续约/新增
  1411. if k == 1 {
  1412. if utils.TrimStr(cell.String()) == "0" {
  1413. rowRegister.ContractType = fms.ContractTypeRenew
  1414. }
  1415. if utils.TrimStr(cell.String()) == "1" {
  1416. rowRegister.ContractType = fms.ContractTypeNew
  1417. }
  1418. }
  1419. // 销售
  1420. if k == 2 {
  1421. rowRegister.SellerName = utils.TrimStr(cell.String())
  1422. rowRegister.SellerId = sellerNameIdMap[rowRegister.SellerName]
  1423. }
  1424. // FICC大套餐
  1425. if k == 3 {
  1426. if utils.TrimStr(cell.String()) == "是" {
  1427. tempItem := serviceTempNameMap[titleMap[k]]
  1428. if tempItem == nil {
  1429. resp.Fail("套餐名称不匹配, 请按模板导入", c)
  1430. return
  1431. }
  1432. cs := &fms.ContractService{
  1433. ProductId: crm.CompanyProductFicc,
  1434. ServiceTemplateId: tempItem.ServiceTemplateId,
  1435. Title: tempItem.Title,
  1436. Value: tempItem.Value,
  1437. TableValue: tempItem.TableValue,
  1438. ChartPermissionId: tempItem.ChartPermissionId,
  1439. }
  1440. cs.Set()
  1441. rowServices = append(rowServices, cs)
  1442. }
  1443. }
  1444. // FICC小套餐
  1445. if k >= 4 && k <= 23 {
  1446. // 小套餐权限
  1447. if utils.TrimStr(cell.String()) == "是" {
  1448. rowChartPermissionIdArr = append(rowChartPermissionIdArr, strconv.Itoa(chartPermissionNameIdMap[titleMap[k]]))
  1449. }
  1450. }
  1451. // 市场策略/财富管理
  1452. if k == 24 || k == 25 {
  1453. if utils.TrimStr(cell.String()) == "是" {
  1454. tempItem := serviceTempNameMap[titleMap[k]]
  1455. if tempItem == nil {
  1456. resp.Fail("市场策略/财富管理套餐名称不匹配, 请按模板导入", c)
  1457. return
  1458. }
  1459. cs := &fms.ContractService{
  1460. ProductId: crm.CompanyProductFicc,
  1461. ServiceTemplateId: tempItem.ServiceTemplateId,
  1462. Title: tempItem.Title,
  1463. Value: tempItem.Value,
  1464. TableValue: tempItem.TableValue,
  1465. ChartPermissionId: tempItem.ChartPermissionId,
  1466. }
  1467. cs.Set()
  1468. rowServices = append(rowServices, cs)
  1469. }
  1470. }
  1471. // 套餐备注
  1472. if k == 26 {
  1473. rowRegister.ServiceRemark = utils.TrimStr(cell.String())
  1474. }
  1475. // 开始时间/到期时间
  1476. if k == 27 {
  1477. startDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1478. if e != nil {
  1479. resp.Fail("开始时间格式有误, 请按模板导入", c)
  1480. return
  1481. }
  1482. rowRegister.StartDate = startDate
  1483. }
  1484. if k == 28 {
  1485. endDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1486. if e != nil {
  1487. resp.Fail("到期时间格式有误, 请按模板导入", c)
  1488. return
  1489. }
  1490. rowRegister.EndDate = endDate
  1491. }
  1492. // 合同金额
  1493. if k == 29 {
  1494. amountStr := utils.TrimStr(cell.String())
  1495. amount, e := strconv.ParseFloat(amountStr, 64)
  1496. if e != nil {
  1497. resp.Fail("合同金额有误, 请按模板导入", c)
  1498. return
  1499. }
  1500. rowRegister.ContractAmount = amount
  1501. }
  1502. // 约定付款日期
  1503. if k == 30 {
  1504. rowRegister.AgreedPayTime = utils.TrimStr(cell.String())
  1505. }
  1506. // 签订日
  1507. // k == 32为签订月,可忽略
  1508. if k == 31 {
  1509. signDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1510. if e != nil {
  1511. resp.Fail("开始时间格式有误, 请按模板导入", c)
  1512. return
  1513. }
  1514. rowRegister.SignDate = signDate
  1515. }
  1516. // 合同状态
  1517. if k == 33 {
  1518. rowRegister.ContractStatus = fms.ContractStatusNameKeyMap[utils.TrimStr(cell.String())]
  1519. }
  1520. // 合同编号
  1521. if k == 34 {
  1522. rowContractCode := utils.TrimStr(cell.String())
  1523. if utils.InArrayByStr(contractCodeArr, rowContractCode) {
  1524. // 此合同已登记, 跳过本行的读取
  1525. break
  1526. }
  1527. rowRegister.ContractCode = rowContractCode
  1528. }
  1529. // 备注
  1530. if k == 35 {
  1531. rowRegister.Remark = utils.TrimStr(cell.String())
  1532. }
  1533. // 开票日/开票金额
  1534. if k == 36 {
  1535. if utils.TrimStr(cell.String()) != "" {
  1536. fmt.Println("36", utils.TrimStr(cell.String()))
  1537. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1538. if e != nil {
  1539. resp.Fail("开票时间1格式有误, 请按模板导入", c)
  1540. return
  1541. }
  1542. rowInvoice1.InvoiceDate = invoiceDate
  1543. rowInvoice1.ContractCode = rowRegister.ContractCode
  1544. rowInvoice1.InvoiceType = fms.ContractInvoiceTypeMake
  1545. }
  1546. }
  1547. if k == 37 && utils.TrimStr(cell.String()) != "" {
  1548. amountStr := utils.TrimStr(cell.String())
  1549. amount, e := strconv.ParseFloat(amountStr, 64)
  1550. if e != nil {
  1551. resp.Fail("开票金额1有误, 请按模板导入", c)
  1552. return
  1553. }
  1554. rowInvoice1.Amount = amount
  1555. rowInvoices = append(rowInvoices, rowInvoice1)
  1556. }
  1557. if k == 38 {
  1558. if utils.TrimStr(cell.String()) != "" {
  1559. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1560. if e != nil {
  1561. resp.Fail("开票时间2格式有误, 请按模板导入", c)
  1562. return
  1563. }
  1564. rowInvoice2.InvoiceDate = invoiceDate
  1565. rowInvoice2.ContractCode = rowRegister.ContractCode
  1566. rowInvoice2.InvoiceType = fms.ContractInvoiceTypeMake
  1567. }
  1568. }
  1569. if k == 39 && utils.TrimStr(cell.String()) != "" {
  1570. amountStr := utils.TrimStr(cell.String())
  1571. amount, e := strconv.ParseFloat(amountStr, 64)
  1572. if e != nil {
  1573. resp.Fail("开票金额2有误, 请按模板导入", c)
  1574. return
  1575. }
  1576. rowInvoice2.Amount = amount
  1577. rowInvoices = append(rowInvoices, rowInvoice2)
  1578. }
  1579. if k == 40 {
  1580. if utils.TrimStr(cell.String()) != "" {
  1581. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1582. if e != nil {
  1583. resp.Fail("开票时间3格式有误, 请按模板导入", c)
  1584. return
  1585. }
  1586. rowInvoice3.InvoiceDate = invoiceDate
  1587. rowInvoice3.ContractCode = rowRegister.ContractCode
  1588. rowInvoice3.InvoiceType = fms.ContractInvoiceTypeMake
  1589. }
  1590. }
  1591. if k == 41 && utils.TrimStr(cell.String()) != "" {
  1592. amountStr := utils.TrimStr(cell.String())
  1593. amount, e := strconv.ParseFloat(amountStr, 64)
  1594. if e != nil {
  1595. resp.Fail("开票金额3有误, 请按模板导入", c)
  1596. return
  1597. }
  1598. rowInvoice3.Amount = amount
  1599. rowInvoices = append(rowInvoices, rowInvoice3)
  1600. }
  1601. // 收款日/收款金额
  1602. if k == 42 {
  1603. if utils.TrimStr(cell.String()) != "" {
  1604. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1605. if e != nil {
  1606. resp.Fail("收款时间1格式有误, 请按模板导入", c)
  1607. return
  1608. }
  1609. rowInvoice4.InvoiceDate = invoiceDate
  1610. rowInvoice4.ContractCode = rowRegister.ContractCode
  1611. rowInvoice4.InvoiceType = fms.ContractInvoiceTypePay
  1612. }
  1613. }
  1614. if k == 43 && utils.TrimStr(cell.String()) != "" {
  1615. amountStr := utils.TrimStr(cell.String())
  1616. amount, e := strconv.ParseFloat(amountStr, 64)
  1617. if e != nil {
  1618. resp.Fail("收款金额1有误, 请按模板导入", c)
  1619. return
  1620. }
  1621. rowInvoice4.Amount = amount
  1622. rowInvoices = append(rowInvoices, rowInvoice4)
  1623. }
  1624. if k == 44 {
  1625. if utils.TrimStr(cell.String()) != "" {
  1626. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1627. if e != nil {
  1628. resp.Fail("收款时间2格式有误, 请按模板导入", c)
  1629. return
  1630. }
  1631. rowInvoice5.InvoiceDate = invoiceDate
  1632. rowInvoice5.ContractCode = rowRegister.ContractCode
  1633. rowInvoice5.InvoiceType = fms.ContractInvoiceTypePay
  1634. }
  1635. }
  1636. if k == 45 && utils.TrimStr(cell.String()) != "" {
  1637. amountStr := utils.TrimStr(cell.String())
  1638. amount, e := strconv.ParseFloat(amountStr, 64)
  1639. if e != nil {
  1640. resp.Fail("收款金额2有误, 请按模板导入", c)
  1641. return
  1642. }
  1643. rowInvoice5.Amount = amount
  1644. rowInvoices = append(rowInvoices, rowInvoice5)
  1645. }
  1646. if k == 46 {
  1647. if utils.TrimStr(cell.String()) != "" {
  1648. invoiceDate, e := time.ParseInLocation("2006/01/02", utils.TrimStr(cell.String()), time.Local)
  1649. if e != nil {
  1650. resp.Fail("收款时间3格式有误, 请按模板导入", c)
  1651. return
  1652. }
  1653. rowInvoice6.InvoiceDate = invoiceDate
  1654. rowInvoice6.ContractCode = rowRegister.ContractCode
  1655. rowInvoice6.InvoiceType = fms.ContractInvoiceTypePay
  1656. }
  1657. }
  1658. if k == 47 && utils.TrimStr(cell.String()) != "" {
  1659. amountStr := utils.TrimStr(cell.String())
  1660. amount, e := strconv.ParseFloat(amountStr, 64)
  1661. if e != nil {
  1662. resp.Fail("收款金额3有误, 请按模板导入", c)
  1663. return
  1664. }
  1665. rowInvoice6.Amount = amount
  1666. rowInvoices = append(rowInvoices, rowInvoice6)
  1667. }
  1668. }
  1669. // 小套餐
  1670. if len(rowChartPermissionIdArr) > 0 {
  1671. // 说明有小套餐
  1672. tempItem := serviceTempNameMap["FICC小套餐"]
  1673. if tempItem == nil {
  1674. resp.Fail("小套餐名称不匹配, 请按模板导入", c)
  1675. return
  1676. }
  1677. rowChartPermissionIds := strings.Join(rowChartPermissionIdArr, ",")
  1678. cs := &fms.ContractService{
  1679. ProductId: crm.CompanyProductFicc,
  1680. ServiceTemplateId: tempItem.ServiceTemplateId,
  1681. Title: tempItem.Title,
  1682. Value: tempItem.Value,
  1683. TableValue: tempItem.TableValue,
  1684. ChartPermissionId: tempItem.ChartPermissionId,
  1685. ChartPermissionIds: rowChartPermissionIds,
  1686. }
  1687. cs.Set()
  1688. rowServices = append(rowServices, cs)
  1689. }
  1690. // 新增登记、套餐、开票到款信息
  1691. newId, e := fms.CreateImportContractRegister(rowRegister, rowServices, rowInvoices)
  1692. if e != nil {
  1693. resp.FailData("导入失败", "新增导入登记失败, Err: "+e.Error(), c)
  1694. return
  1695. }
  1696. newIds = append(newIds, newId)
  1697. }
  1698. }
  1699. }
  1700. // 更新登记状态
  1701. go func() {
  1702. for i := range newIds {
  1703. fmsService.CheckContractRegisterAmount(newIds[i])
  1704. }
  1705. }()
  1706. resp.Ok("操作成功", c)
  1707. }