register.go 65 KB

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