contract.go 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075
  1. package contract
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/rdlucklib/rdluck_tools/paging"
  6. "github.com/shopspring/decimal"
  7. "github.com/tealeg/xlsx"
  8. "hongze/hz_crm_api/controllers"
  9. "hongze/hz_crm_api/models"
  10. "hongze/hz_crm_api/models/company"
  11. "hongze/hz_crm_api/models/contract"
  12. "hongze/hz_crm_api/models/contract/request"
  13. "hongze/hz_crm_api/models/contract/response"
  14. "hongze/hz_crm_api/services"
  15. contractService "hongze/hz_crm_api/services/contract"
  16. "hongze/hz_crm_api/services/seal"
  17. "hongze/hz_crm_api/utils"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "time"
  24. )
  25. // 合同管理
  26. type ContractController struct {
  27. controllers.BaseAuthController
  28. }
  29. // GetServiceTemplateList
  30. // @Title 获取服务套餐列表
  31. // @Description 获取服务套餐列表
  32. // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  33. // @Param ContractType query string false "合同类型:枚举值:'新签合同','续约合同','补充协议'"
  34. // @Success 200 {object} []contract.ContractServiceTemplateMapItems
  35. // @router /service/template/list [get]
  36. func (this *ContractController) GetServiceTemplateList() {
  37. br := new(models.BaseResponse).Init()
  38. defer func() {
  39. this.Data["json"] = br
  40. this.ServeJSON()
  41. }()
  42. productId, _ := this.GetInt("ProductId")
  43. contractType := this.GetString("ContractType")
  44. //没有传参时,默认是从账号中获取
  45. if productId == 0 {
  46. productId = services.GetProductId(this.SysUser.RoleTypeCode)
  47. }
  48. if productId == 0 {
  49. br.Msg = "参数异常"
  50. br.ErrMsg = "获取参数异常,获取合同类型异常,当前RoleTypeCode:" + this.SysUser.RoleTypeCode
  51. return
  52. }
  53. list, err := contract.GetContractServiceTemplateMapByProductId(productId)
  54. if err != nil {
  55. br.Msg = "获取失败"
  56. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  57. return
  58. }
  59. for i := 0; i < len(list); i++ {
  60. item := list[i]
  61. tmpList, err := contract.GetContractServiceTemplateMapByParentId(item.ServiceTemplateId)
  62. if err != nil {
  63. br.Msg = "获取失败"
  64. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  65. return
  66. }
  67. secondLen := len(tmpList)
  68. for j := 0; j < secondLen; j++ {
  69. detail, err := contract.GetContractServiceDetailByTemplateId(tmpList[j].ServiceTemplateId)
  70. if err != nil {
  71. br.Msg = "获取失败"
  72. br.ErrMsg = "获取详情模板失败,Err:" + err.Error()
  73. return
  74. }
  75. if productId == 1 {
  76. // 特殊处理Ficc周报、商品双周报、数据点评的套餐显示
  77. // 把detail转成go struct
  78. // 查询默认的公有权限
  79. publicPermissionList, tmpE := models.GetFiccPermissionSecondPublic()
  80. if tmpE != nil {
  81. br.Msg = "获取失败"
  82. br.ErrMsg = "获取默认的公有权限失败,Err:" + tmpE.Error()
  83. return
  84. }
  85. publicPermissionIds := make([]int, 0)
  86. publicPermissionNames := make([]string, 0)
  87. for _, v := range publicPermissionList {
  88. publicPermissionIds = append(publicPermissionIds, v.ChartPermissionId)
  89. publicPermissionNames = append(publicPermissionNames, v.PermissionName)
  90. }
  91. for k, v := range detail {
  92. if v.Id == 4 || v.Id == 5 || v.Id == 6 {
  93. detailStruct := new(contract.ServiceTemplateDetailCol2)
  94. e := json.Unmarshal([]byte(v.Col2), detailStruct)
  95. if e != nil {
  96. br.Msg = "获取失败"
  97. br.ErrMsg = "获取套餐信息失败, json.Unmarshal Err: " + e.Error()
  98. return
  99. }
  100. //替换默认品种
  101. detailStruct.ValueId = publicPermissionIds
  102. detailStruct.Value = strings.Join(publicPermissionNames, ",")
  103. newCol2, e := json.Marshal(detailStruct)
  104. if e != nil {
  105. br.Msg = "获取失败"
  106. br.ErrMsg = "获取套餐信息失败, json.Marshal Err: " + e.Error()
  107. return
  108. }
  109. detail[k].Col2 = string(newCol2)
  110. }
  111. }
  112. }
  113. tmpList[j].Detail = detail
  114. // 权益存在第三级主客观套餐-CRM8.8
  115. thirdList, e := contract.GetContractServiceTemplateMapByParentId(tmpList[j].ServiceTemplateId)
  116. if e != nil {
  117. br.Msg = "获取失败"
  118. br.ErrMsg = "获取三级套餐失败, Err: " + e.Error()
  119. return
  120. }
  121. tmpList[j].Children = thirdList
  122. //if len(thirdList) > 0 {
  123. // if utils.InArrayByInt([]int{19, 20, 21, 22}, thirdList[0].ChartPermissionId) {
  124. // }
  125. //}
  126. }
  127. // 权益获取升级套餐-CRM13.2
  128. if productId == 2 {
  129. newTempList := make([]*contract.ContractServiceTemplateMapItems, 0)
  130. upgradeMap := make(map[int]*contract.ContractServiceTemplateMapItems)
  131. upgradeList, err := contract.GetContractServiceTemplateMapByChartPermissionId()
  132. if err != nil {
  133. br.Msg = "获取失败"
  134. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  135. return
  136. }
  137. for _, v := range upgradeList {
  138. upgradeMap[v.ChartPermissionId-100000] = v
  139. fmt.Println("v.ChartPermissionId-10000:", v.ChartPermissionId-100000)
  140. }
  141. for _, v := range tmpList {
  142. newTempList = append(newTempList, v)
  143. if len(v.Children) > 0 {
  144. if utils.InArrayByInt([]int{19, 20, 21, 22}, v.Children[0].ChartPermissionId) {
  145. newTempList = append(newTempList, upgradeMap[v.Children[0].ChartPermissionId])
  146. }
  147. }
  148. }
  149. tmpList = newTempList
  150. }
  151. detail, err := contract.GetContractServiceDetailByTemplateId(list[i].ServiceTemplateId)
  152. if err != nil {
  153. br.Msg = "获取失败"
  154. br.ErrMsg = "获取详情模板失败,Err:" + err.Error()
  155. return
  156. }
  157. if productId == 1 && contractType == "补充协议" {
  158. for _, v := range detail {
  159. //ficc 的合同模板的 补充协议需要将默认的宏观经济给移除掉
  160. if utils.InArrayByInt([]int{4, 5, 6}, v.Id) {
  161. var tableCel request.AddContractServiceDetailReq
  162. tmpErr := json.Unmarshal([]byte(v.Col2), &tableCel)
  163. if tmpErr != nil {
  164. br.Msg = "获取失败"
  165. br.ErrMsg = "配置解析失败,Err:" + tmpErr.Error()
  166. return
  167. }
  168. tableCel.ValueId = make([]int, 0)
  169. tableCel.Value = ``
  170. //contract.
  171. tmpStr, tmpErr := json.Marshal(tableCel)
  172. if tmpErr != nil {
  173. br.Msg = "获取失败"
  174. br.ErrMsg = "修改json配置后,转字符串失败,Err:" + tmpErr.Error()
  175. return
  176. }
  177. v.Col2 = string(tmpStr)
  178. }
  179. //v.Col1
  180. }
  181. }
  182. list[i].Detail = detail
  183. list[i].Children = tmpList
  184. }
  185. br.Ret = 200
  186. br.Success = true
  187. br.Msg = "获取成功"
  188. br.Data = list
  189. }
  190. // GetContractTemplateList
  191. // @Title 获取合同模板列表
  192. // @Description 获取合同模板列表
  193. // @Success 200 {object} []contract.ContractTemplate
  194. // @router /template/list [get]
  195. func (this *ContractController) GetContractTemplateList() {
  196. br := new(models.BaseResponse).Init()
  197. defer func() {
  198. this.Data["json"] = br
  199. this.ServeJSON()
  200. }()
  201. productId := services.GetProductId(this.SysUser.RoleTypeCode)
  202. list, err := contract.GetContractTemplateMapByProductId(productId)
  203. if err != nil {
  204. br.Msg = "获取失败"
  205. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  206. return
  207. }
  208. br.Ret = 200
  209. br.Success = true
  210. br.Msg = "获取成功"
  211. br.Data = list
  212. }
  213. // GetPayChannel
  214. // @Title 获取合同模板列表
  215. // @Description 获取合同模板列表
  216. // @Param Keyword query string true "关键字"
  217. // @Success 200 {object} []contract.PayChannel
  218. // @router /pay_channel/list [get]
  219. func (this *ContractController) GetPayChannel() {
  220. br := new(models.BaseResponse).Init()
  221. defer func() {
  222. this.Data["json"] = br
  223. this.ServeJSON()
  224. }()
  225. keyword := this.GetString("Keyword", "")
  226. list, err := contract.GetPayChannelList(keyword)
  227. if err != nil {
  228. br.Msg = "获取失败"
  229. br.ErrMsg = "获取数据失败,Err:" + err.Error()
  230. return
  231. }
  232. br.Ret = 200
  233. br.Success = true
  234. br.Msg = "获取成功"
  235. br.Data = list
  236. }
  237. // AddContract
  238. // @Title 新增合同
  239. // @Description 新增合同接口
  240. // @Param request body request.AddContractReq true "type json string"
  241. // @Success 200 {object} response.AddContractResp
  242. // @router /add [post]
  243. func (this *ContractController) AddContract() {
  244. br := new(models.BaseResponse).Init()
  245. defer func() {
  246. this.Data["json"] = br
  247. this.ServeJSON()
  248. }()
  249. sysUser := this.SysUser
  250. if sysUser == nil {
  251. br.Msg = "请登录"
  252. br.ErrMsg = "请登录,SysUser Is Empty"
  253. br.Ret = 408
  254. return
  255. }
  256. productId := services.GetProductId(sysUser.RoleTypeCode)
  257. if productId == 0 {
  258. br.Msg = "账户异常"
  259. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  260. br.Ret = 408
  261. return
  262. }
  263. var req request.AddContractReq
  264. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  265. if err != nil {
  266. br.Msg = "参数解析异常!"
  267. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  268. return
  269. }
  270. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  271. maxPrice := float64(1000000000000)
  272. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  273. br.Msg = "合同金额过大, 请核验金额"
  274. return
  275. }
  276. contractInfo, err, errMsg := contractService.AddContract(req, sysUser.AdminId, productId, sysUser.RealName)
  277. if err != nil {
  278. br.Msg = "保存合同失败!"
  279. if errMsg != "" {
  280. br.Msg = errMsg
  281. }
  282. br.ErrMsg = "保存合同失败,Err:" + err.Error()
  283. return
  284. }
  285. msg := "添加成功"
  286. //如果是直接发起审批,那么
  287. if req.IsAudit {
  288. tmpErr, errMsg := contractService.Apply(contractInfo.ContractId)
  289. if tmpErr != nil {
  290. br.Msg = "提交审批失败!"
  291. if errMsg != "" {
  292. br.Msg = errMsg
  293. }
  294. br.ErrMsg = "提交审批失败,Err:" + tmpErr.Error()
  295. return
  296. }
  297. msg = "提交审批成功"
  298. }
  299. br.Ret = 200
  300. br.Success = true
  301. br.Msg = msg
  302. br.Data = response.AddContractResp{
  303. ContractId: contractInfo.ContractId,
  304. }
  305. }
  306. // List
  307. // @Title 合同列表
  308. // @Description 合同列表接口
  309. // @Param ContractBusinessType query string false "合同业务类型,枚举值:'业务合同','代付合同'"
  310. // @Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'"
  311. // @Param ContractStatus query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"
  312. // @Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  313. // @Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  314. // @Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  315. // @Param SellerId query string false "选择的销售id"
  316. // @Param KeywordType query string false "搜索关键字类型,枚举值:'使用方','代付方'"
  317. // @Param Keyword query string false "搜索关键字"
  318. // @Param IsExport query bool false "是否导出excel,默认是false"
  319. // @Success 200 {object} response.ContractListResp
  320. // @router /list [get]
  321. func (this *ContractController) List() {
  322. br := new(models.BaseResponse).Init()
  323. defer func() {
  324. this.Data["json"] = br
  325. this.ServeJSON()
  326. }()
  327. sysUser := this.SysUser
  328. if sysUser == nil {
  329. br.Msg = "请登录"
  330. br.ErrMsg = "请登录,SysUser Is Empty"
  331. br.Ret = 408
  332. return
  333. }
  334. //合同类型、产品类型、合同状态、更新时间、所选销售
  335. //关键字:合同编号、客户名称,社会信用码
  336. contractType := this.GetString("ContractType")
  337. contractBusinessType := this.GetString("ContractBusinessType")
  338. contractStatus := this.GetString("ContractStatus")
  339. productId, _ := this.GetInt("ProductId")
  340. modifyStartTime := this.GetString("ModifyStartTime")
  341. modifyEndTime := this.GetString("ModifyEndTime")
  342. sellerIds := this.GetString("SellerId")
  343. keyword := this.GetString("Keyword")
  344. keywordType := this.GetString("KeywordType")
  345. condition := ""
  346. pars := make([]interface{}, 0)
  347. joinStr := "" //表连接
  348. productSlice := make([]int, 0) //需要下载的 产品 切片
  349. //如果不是超管或者合规,那么只能查看自己的合同
  350. if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  351. condition += ` AND a.product_id = 1 AND a.status not in ("待提交","已撤回") `
  352. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  353. condition += ` AND a.product_id = 2 AND a.status not in ("待提交","已撤回") `
  354. } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FINANCE { //超管可查看所有状态的合同(除过已撤回、待提交状态合同),可下载所有状态的合同
  355. condition += ` AND a.status not in ("待提交","已撤回") `
  356. } else {
  357. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_FINANCE {
  358. condition += ` AND a.seller_id = ? `
  359. pars = append(pars, sysUser.AdminId)
  360. }
  361. }
  362. //合同类型、、更新时间、所选销售
  363. //关键字:合同编号、客户名称,社会信用码
  364. if contractBusinessType != "" { //合同业务类型
  365. condition += ` AND a.contract_business_type = ? `
  366. pars = append(pars, contractBusinessType)
  367. }
  368. if contractType != "" {
  369. condition += ` AND a.contract_type = ? `
  370. pars = append(pars, contractType)
  371. }
  372. //合同状态
  373. if contractStatus != "" {
  374. condition += ` AND a.status = ? `
  375. pars = append(pars, contractStatus)
  376. }
  377. //产品类型
  378. if productId > 0 {
  379. condition += ` AND a.product_id = ? `
  380. pars = append(pars, productId)
  381. productSlice = append(productSlice, productId)
  382. } else {
  383. nowProductId := services.GetProductId(sysUser.RoleTypeCode)
  384. if nowProductId > 0 {
  385. productSlice = append(productSlice, nowProductId)
  386. } else {
  387. productSlice = append(productSlice, 1)
  388. productSlice = append(productSlice, 2)
  389. }
  390. }
  391. //所选销售
  392. if sellerIds != "" {
  393. condition += ` AND a.seller_id IN (` + sellerIds + `) `
  394. }
  395. //更新开始时间
  396. if modifyStartTime != "" {
  397. condition += ` AND a.modify_time >= ? `
  398. pars = append(pars, modifyStartTime)
  399. }
  400. //更新结束时间
  401. if modifyEndTime != "" {
  402. condition += ` AND a.modify_time <= ? `
  403. pars = append(pars, modifyEndTime)
  404. }
  405. //关键字
  406. //if keyword != "" {
  407. // condition += ` AND (contract_code LIKE '%` + keyword + `%' OR company_name LIKE '%` + keyword + `%' OR credit_code LIKE '%` + keyword + `%' ) `
  408. //}
  409. //搜索关键字和搜索类型同时不为空
  410. if keywordType != "" && keyword != "" {
  411. switch contractBusinessType {
  412. case "业务合同":
  413. if keywordType == "使用方" {
  414. condition += ` AND (a.contract_code LIKE '%` + keyword + `%' OR a.company_name LIKE '%` + keyword + `%' OR a.credit_code LIKE '%` + keyword + `%' ) `
  415. } else {
  416. joinStr = " join contract_relation b on a.contract_id=b.contract_id join contract c on b.payment_on_behalf_contract_id=c.contract_id "
  417. condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  418. //关联合同的查询条件
  419. condition += ` AND c.status in ("已签回","已审批") AND c.is_delete = 0 `
  420. }
  421. case "代付合同":
  422. if keywordType == "代付方" {
  423. condition += ` AND (a.contract_code LIKE '%` + keyword + `%' OR a.company_name LIKE '%` + keyword + `%' OR a.credit_code LIKE '%` + keyword + `%' ) `
  424. } else {
  425. joinStr = " join contract_relation b on a.contract_id=b.payment_on_behalf_contract_id join contract c on b.contract_id=c.contract_id "
  426. condition += ` AND (c.contract_code LIKE '%` + keyword + `%' OR c.company_name LIKE '%` + keyword + `%' OR c.credit_code LIKE '%` + keyword + `%' ) `
  427. //关联合同的查询条件
  428. condition += ` AND c.status in ("已签回","已审批") AND c.is_delete = 0 `
  429. }
  430. }
  431. }
  432. pageSize, _ := this.GetInt("PageSize")
  433. currentIndex, _ := this.GetInt("CurrentIndex")
  434. var startSize int
  435. if pageSize <= 0 {
  436. pageSize = utils.PageSize20
  437. }
  438. if currentIndex <= 0 {
  439. currentIndex = 1
  440. }
  441. startSize = paging.StartIndex(currentIndex, pageSize)
  442. //是否导出报表
  443. isExport, _ := this.GetBool("IsExport")
  444. if isExport {
  445. pageSize = 10000
  446. currentIndex = 1
  447. }
  448. total, err := contract.GetContractListCount(condition, joinStr, pars)
  449. if err != nil {
  450. br.Msg = "获取失败"
  451. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  452. return
  453. }
  454. list, err := contract.GetContractList(condition, joinStr, pars, startSize, pageSize)
  455. if err != nil {
  456. br.Msg = "获取合同列表失败!"
  457. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  458. return
  459. }
  460. //获取合同套餐服务列表
  461. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  462. if err != nil {
  463. br.Msg = "获取合同套餐服务列表失败!"
  464. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  465. return
  466. }
  467. templateTitleMap := make(map[int]string)
  468. for _, serviceTemplate := range serviceTemplateList {
  469. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  470. }
  471. //业务合同的关联合同id集合
  472. businessContractIdList := make([]string, 0)
  473. //代付合同的关联合同id集合
  474. paymentOnBehalfContractIdList := make([]string, 0)
  475. //所有合同id集合(用于查询服务内容)
  476. allServiceContractIdList := make([]string, 0)
  477. //基础合同的关联合同数据
  478. contractRelationMap := make(map[int][]*contract.RelationContractList)
  479. // 权益合同map
  480. equityContractMap := make(map[int]bool)
  481. for _, v := range list {
  482. //合同id
  483. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.ContractId))
  484. switch v.ContractBusinessType {
  485. case "业务合同":
  486. businessContractIdList = append(businessContractIdList, fmt.Sprint(v.ContractId))
  487. case "代付合同":
  488. paymentOnBehalfContractIdList = append(paymentOnBehalfContractIdList, fmt.Sprint(v.ContractId))
  489. }
  490. // 权益合同
  491. if v.ProductId == 2 {
  492. equityContractMap[v.ContractId] = true
  493. }
  494. }
  495. //业务合同的关联合同
  496. if len(businessContractIdList) > 0 {
  497. contractIdStr := strings.Join(businessContractIdList, ",")
  498. businessContractList, err := contract.GetContractRelationListByRelationContractIds(contractIdStr)
  499. if err != nil {
  500. br.Msg = "获取失败"
  501. br.ErrMsg = "获取业务关联合同失败,Err:" + err.Error()
  502. return
  503. }
  504. for _, v := range businessContractList {
  505. //代付合同id
  506. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.PaymentOnBehalfContractId))
  507. if _, ok := contractRelationMap[v.ContractId]; ok {
  508. contractRelationMap[v.ContractId] = append(contractRelationMap[v.ContractId], v)
  509. } else {
  510. tmpList := make([]*contract.RelationContractList, 0)
  511. contractRelationMap[v.ContractId] = append(tmpList, v)
  512. }
  513. }
  514. }
  515. //代付合同的关联合同
  516. if len(paymentOnBehalfContractIdList) > 0 {
  517. paymentOnBehalfContractIdStr := strings.Join(paymentOnBehalfContractIdList, ",")
  518. paymentOnBehalfContractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(paymentOnBehalfContractIdStr)
  519. if err != nil {
  520. br.Msg = "获取失败"
  521. br.ErrMsg = "获取代付关联合同失败,Err:" + err.Error()
  522. return
  523. }
  524. for _, v := range paymentOnBehalfContractList {
  525. //业务合同id
  526. allServiceContractIdList = append(allServiceContractIdList, fmt.Sprint(v.ContractId))
  527. if _, ok := contractRelationMap[v.PaymentOnBehalfContractId]; ok {
  528. contractRelationMap[v.PaymentOnBehalfContractId] = append(contractRelationMap[v.PaymentOnBehalfContractId], v)
  529. } else {
  530. tmpList := make([]*contract.RelationContractList, 0)
  531. contractRelationMap[v.PaymentOnBehalfContractId] = append(tmpList, v)
  532. }
  533. }
  534. }
  535. //合同套餐服务列表
  536. serviceListMap := make(map[int][]*contract.ContractServiceAndDetail, 0)
  537. // 权益合同
  538. if len(allServiceContractIdList) > 0 {
  539. allRelationIdStr := strings.Join(allServiceContractIdList, ",")
  540. tmpServiceList, err := contract.GetContractServiceAndDetailListByIds(allRelationIdStr)
  541. if err != nil {
  542. br.Msg = "获取失败"
  543. br.ErrMsg = "获取关联合同服务详情失败,Err:" + err.Error()
  544. return
  545. }
  546. allContractServiceIdList := make([]string, 0)
  547. serviceDetailListMap := make(map[int][]*contract.ContractServiceDetail, 0)
  548. for _, service := range tmpServiceList {
  549. if service.HasDetail == "是" {
  550. allContractServiceIdList = append(allContractServiceIdList, strconv.Itoa(service.ContractServiceId))
  551. }
  552. }
  553. if len(allContractServiceIdList) > 0 {
  554. allContractServiceIdStr := strings.Join(allContractServiceIdList, ",")
  555. list, detailErr := contract.GetContractServiceDetailListByServiceIds(allContractServiceIdStr)
  556. if detailErr != nil {
  557. br.Msg = "获取失败"
  558. br.ErrMsg = "查找合同服务详情异常,Err:" + detailErr.Error()
  559. return
  560. }
  561. for _, v := range list {
  562. if _, ok := serviceDetailListMap[v.ContractServiceId]; ok {
  563. serviceDetailListMap[v.ContractServiceId] = append(serviceDetailListMap[v.ContractServiceId], v)
  564. } else {
  565. tmpList := make([]*contract.ContractServiceDetail, 0)
  566. serviceDetailListMap[v.ContractServiceId] = append(tmpList, v)
  567. }
  568. }
  569. }
  570. for _, service := range tmpServiceList {
  571. if service.HasDetail == "是" {
  572. if serviceDetailList, ok := serviceDetailListMap[service.ContractServiceId]; ok {
  573. service.DetailList = serviceDetailList
  574. }
  575. //套餐名称
  576. if title, ok := templateTitleMap[service.ServiceTemplateId]; ok {
  577. service.Title = title
  578. }
  579. }
  580. //服务列表
  581. if _, ok := serviceListMap[service.ContractId]; ok {
  582. serviceListMap[service.ContractId] = append(serviceListMap[service.ContractId], service)
  583. } else {
  584. tmpList := make([]*contract.ContractServiceAndDetail, 0)
  585. serviceListMap[service.ContractId] = append(tmpList, service)
  586. }
  587. }
  588. // CRM8.8-权益合同主客观合并
  589. for k, v := range serviceListMap {
  590. if equityContractMap[k] {
  591. mergeList := contractService.EquityMergeSubjectAndObject(v)
  592. serviceListMap[k] = mergeList
  593. }
  594. }
  595. }
  596. //关联合同
  597. for _, contractRelationList := range contractRelationMap {
  598. for _, contractRelation := range contractRelationList {
  599. //服务权限列表
  600. // 关联合同同样合并
  601. if serviceList, ok := serviceListMap[contractRelation.ContractId]; ok {
  602. relationMergeList := contractService.EquityMergeSubjectAndObject(serviceList)
  603. contractRelation.Service = relationMergeList
  604. }
  605. }
  606. }
  607. for i := 0; i < len(list); i++ {
  608. //合同操作权限
  609. var opButton contract.ContractOpButton
  610. item := list[i]
  611. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  612. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  613. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  614. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  615. list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime)
  616. list[i].InvalidTimeStr = item.InvalidTime.Format(utils.FormatDateTime)
  617. list[i].CheckBackFileTimeStr = item.CheckBackFileTime.Format(utils.FormatDateTime)
  618. list[i].RescindTimeStr = item.RescindTime.Format(utils.FormatDateTime)
  619. //服务列表
  620. if serviceList, ok := serviceListMap[item.ContractId]; ok {
  621. item.Service = serviceList
  622. }
  623. //serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  624. //if err != nil {
  625. // br.Msg = "获取失败"
  626. // br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  627. // return
  628. //}
  629. //for i := 0; len(serviceList) > i; i++ {
  630. // if serviceList[i].HasDetail == "是" {
  631. // list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  632. // if detailErr != nil {
  633. // br.Msg = "获取失败"
  634. // br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  635. // return
  636. // }
  637. //
  638. // //套餐名称
  639. // serviceList[i].DetailList = list
  640. // if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  641. // serviceList[i].Title = title
  642. // }
  643. // }
  644. //}
  645. //list[i].Service = serviceList
  646. if item.Status == "待审批" {
  647. approvalInfo, tmpErr := contract.GetLastContractApprovalByContractId(item.ContractId, "contract")
  648. if tmpErr != nil {
  649. //err= tmpErr
  650. //return
  651. continue
  652. }
  653. //如果合同归属是当前账号,且处于第一级审批
  654. if item.SellerId == sysUser.AdminId && approvalInfo.CurrNodeId == approvalInfo.StartNodeId {
  655. opButton.Cancel = true
  656. }
  657. }
  658. //更新附件权限
  659. if item.SellerId == sysUser.AdminId && item.Status == "已审批" {
  660. opButton.UpdateFile = true
  661. }
  662. list[i].OpButton = opButton
  663. //关联合同
  664. if relationContractList, ok := contractRelationMap[item.ContractId]; ok {
  665. item.RelationContractList = relationContractList
  666. //list[i].RelationContractList = relationContractList
  667. }
  668. }
  669. page := paging.GetPaging(currentIndex, pageSize, total)
  670. //导出excel
  671. if isExport {
  672. contractListExport(this, list, productSlice, contractBusinessType, br)
  673. return
  674. }
  675. br.Ret = 200
  676. br.Success = true
  677. br.Msg = "获取成功"
  678. br.Data = response.ContractListResp{
  679. List: list,
  680. Paging: page,
  681. }
  682. }
  683. // contractListExport 合同列表数据导出
  684. func contractListExport(this *ContractController, list []*contract.ContractList, productSlice []int, contractBusinessType string, br *models.BaseResponse) {
  685. //产品类型
  686. //获取合同套餐服务列表
  687. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  688. if err != nil {
  689. br.Msg = "获取合同套餐服务列表失败!"
  690. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  691. return
  692. }
  693. templateTitleMap := make(map[int]string)
  694. for _, serviceTemplate := range serviceTemplateList {
  695. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  696. }
  697. //生成excel文件
  698. dir, err := os.Executable()
  699. exPath := filepath.Dir(dir)
  700. downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  701. xlsxFile := xlsx.NewFile()
  702. if err != nil {
  703. br.Msg = "生成文件失败"
  704. br.ErrMsg = "生成文件失败"
  705. return
  706. }
  707. //设置默认字体和文字大小
  708. xlsx.SetDefaultFont(12, "宋体")
  709. //定义边框样式
  710. border := xlsx.NewBorder("thin", "thin", "thin", "thin")
  711. //定义文字排版样式
  712. alignment := xlsx.Alignment{
  713. Horizontal: "center",
  714. Vertical: "center",
  715. WrapText: true,
  716. }
  717. //普通样式
  718. style := xlsx.NewStyle()
  719. style.Alignment = alignment
  720. style.ApplyAlignment = true
  721. style.Border = *border
  722. //标红文字,便于查看哪些产品被勾选
  723. redStyle := xlsx.NewStyle()
  724. redStyle.Alignment = alignment
  725. redStyle.ApplyAlignment = true
  726. redStyle.Font.Color = "ff0000"
  727. //定义底色需要标黄的 单元格颜色
  728. redFill := xlsx.Fill{"solid", "ffff00", "ffff00"}
  729. redStyle.Fill = redFill
  730. redStyle.Border = *border
  731. //标题样式
  732. titleStyle := xlsx.NewStyle()
  733. titleFont := xlsx.NewFont(14, "宋体")
  734. titleFont.Bold = true
  735. titleStyle.Font = *titleFont
  736. titleStyle.Alignment = alignment
  737. titleStyle.Border = *border
  738. //titleStyle.ApplyAlignment = true
  739. //表头
  740. headerStyle := xlsx.NewStyle()
  741. headerFont := xlsx.NewFont(12, "宋体")
  742. headerFont.Bold = true
  743. headerStyle.Font = *headerFont
  744. headerStyle.Alignment = alignment
  745. headerStyle.ApplyAlignment = true
  746. headerStyle.Border = *border
  747. defer func() {
  748. os.Remove(downLoadnFilePath)
  749. }()
  750. //遍历所拥有的的产品权限,然后插入到对应的sheet表单中
  751. for pidIndex := 0; pidIndex < len(productSlice); pidIndex++ {
  752. productId := productSlice[pidIndex]
  753. //表单名称
  754. sheetName := "合同数据"
  755. switch productId {
  756. case 1:
  757. sheetName = "ficc合同信息"
  758. case 2:
  759. sheetName = "权益合同信息"
  760. }
  761. //新增一个sheet表单
  762. sheel, err := xlsxFile.AddSheet(sheetName)
  763. if err != nil {
  764. br.Msg = "新增Sheet失败"
  765. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  766. return
  767. }
  768. //设置列宽
  769. sheel.SetColWidth(0, 0, 28)
  770. sheel.SetColWidth(1, 1, 30)
  771. sheel.SetColWidth(2, 2, 18)
  772. sheel.SetColWidth(3, 3, 18)
  773. sheel.SetColWidth(4, 4, 35)
  774. sheel.SetColWidth(5, 6, 20)
  775. sheel.SetColWidth(7, 7, 40)
  776. //合同信息列
  777. endContractInfoColIndex := 8
  778. sheel.SetColWidth(8, 8, 40)
  779. //权限列
  780. sheel.SetColWidth(endContractInfoColIndex, endContractInfoColIndex+18, 30)
  781. //标题行
  782. titleRow := sheel.AddRow()
  783. titleRow.SetHeight(30)
  784. //合同信息列
  785. titleCell := titleRow.AddCell()
  786. titleCell.HMerge = endContractInfoColIndex //向右合并列数,不包括自身列
  787. //遍历去添加需要合并的列
  788. for i := 0; i < endContractInfoColIndex; i++ {
  789. titleRow.AddCell()
  790. }
  791. //报表标题名称
  792. titleCell.SetValue("合同信息")
  793. titleCell.SetStyle(titleStyle)
  794. startI := endContractInfoColIndex + 1
  795. customStartCellI := endContractInfoColIndex + 1
  796. serviceSlice := make([]string, 0)
  797. serviceCellMap := make(map[string]int)
  798. //获取所有套餐列表
  799. {
  800. serviceList, err := getServiceTemplateList(productId)
  801. if err != nil {
  802. br.Msg = "查询套餐类型失败"
  803. br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  804. return
  805. }
  806. for i := 0; i < len(serviceList); i++ {
  807. //需要向右合并添加的列数
  808. addCellNum := 0
  809. item := serviceList[i]
  810. //套餐服务列
  811. servicesCell := titleRow.AddCell()
  812. //套餐名称
  813. servicesCell.SetValue(item.Title)
  814. servicesCell.SetStyle(titleStyle)
  815. //ficc小套餐
  816. if item.ServiceTemplateId == 2 {
  817. productPermission := make(map[int]string)
  818. productPermission[1] = "每日宏观商品复盘"
  819. productPermission[2] = "能化专栏《化里化外》"
  820. productPermission[3] = "股债日评"
  821. productPermission[4] = "黑色专栏《知白守黑》"
  822. productPermission[5] = "有色专栏《有声有色》"
  823. for id, name := range productPermission {
  824. key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", id)
  825. serviceCellMap[key] = startI
  826. serviceSlice = append(serviceSlice, name)
  827. startI++
  828. addCellNum++
  829. }
  830. permissionList := make([]*company.PermissionSetItem, 0)
  831. for _, v := range utils.PermissionFiccClassifyArr {
  832. items, err := company.GetPermissionSetItems(1, v)
  833. if err != nil {
  834. br.Msg = "获取失败"
  835. br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  836. return
  837. }
  838. permissionList = append(permissionList, items...)
  839. }
  840. //权限列表
  841. for j := 0; j < len(permissionList); j++ {
  842. key := fmt.Sprint(item.ServiceTemplateId, "_permission_", permissionList[j].ChartPermissionId)
  843. serviceCellMap[key] = startI
  844. serviceSlice = append(serviceSlice, permissionList[j].PermissionName)
  845. startI++
  846. addCellNum++
  847. }
  848. //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  849. addCellNum = addCellNum - 1
  850. if addCellNum > 0 {
  851. for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  852. titleRow.AddCell()
  853. }
  854. servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  855. }
  856. continue
  857. } else {
  858. if item.Children != nil && len(item.Children) > 0 {
  859. for j := 0; j < len(item.Children); j++ {
  860. key := fmt.Sprint("service_template_", item.Children[j].ServiceTemplateId)
  861. serviceCellMap[key] = startI
  862. serviceSlice = append(serviceSlice, item.Children[j].Title)
  863. startI++
  864. addCellNum++
  865. }
  866. //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  867. addCellNum = addCellNum - 1
  868. if addCellNum > 0 {
  869. for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  870. titleRow.AddCell()
  871. }
  872. servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  873. }
  874. } else {
  875. key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  876. serviceCellMap[key] = startI
  877. serviceSlice = append(serviceSlice, item.Title)
  878. startI++
  879. }
  880. }
  881. }
  882. }
  883. //表头
  884. headerRow := sheel.AddRow()
  885. headerRow.SetHeight(24)
  886. //合同基础信息
  887. {
  888. codeHeaderCell := headerRow.AddCell()
  889. codeHeaderCell.SetValue("合同编号")
  890. codeHeaderCell.SetStyle(headerStyle)
  891. companyHeaderCell := headerRow.AddCell()
  892. companyHeaderStr := "客户名称"
  893. if contractBusinessType == "代付合同" {
  894. companyHeaderStr = "代付方"
  895. }
  896. companyHeaderCell.SetValue(companyHeaderStr)
  897. companyHeaderCell.SetStyle(headerStyle)
  898. typeHeaderCell := headerRow.AddCell()
  899. typeHeaderCell.SetValue("合同类型")
  900. typeHeaderCell.SetStyle(headerStyle)
  901. priceHeaderCell := headerRow.AddCell()
  902. priceHeaderCell.SetValue("合同金额")
  903. priceHeaderCell.SetStyle(headerStyle)
  904. dateHeaderCell := headerRow.AddCell()
  905. dateHeaderCell.SetValue("合同有效期")
  906. dateHeaderCell.SetStyle(headerStyle)
  907. sellerHeaderCell := headerRow.AddCell()
  908. sellerHeaderCell.SetValue("销售")
  909. sellerHeaderCell.SetStyle(headerStyle)
  910. statusHeaderCell := headerRow.AddCell()
  911. statusHeaderCell.SetValue("合同状态")
  912. statusHeaderCell.SetStyle(headerStyle)
  913. //关联合同信息
  914. payHeaderCell := headerRow.AddCell()
  915. payHeaderStr := "付款方"
  916. if contractBusinessType == "代付合同" {
  917. payHeaderStr = "客户名称"
  918. }
  919. payHeaderCell.SetValue(payHeaderStr)
  920. payHeaderCell.SetStyle(headerStyle)
  921. remarkHeaderCell := headerRow.AddCell()
  922. remarkHeaderCell.SetValue("备注")
  923. remarkHeaderCell.SetStyle(headerStyle)
  924. }
  925. //添加服务套餐名称
  926. for _, service := range serviceSlice {
  927. cell := headerRow.AddCell()
  928. cell.SetValue(service)
  929. cell.SetStyle(headerStyle)
  930. }
  931. for _, v := range list {
  932. //如果当前合同不是该产品
  933. if v.ProductId != productId {
  934. continue
  935. }
  936. dataRow := sheel.AddRow()
  937. dataRow.SetHeight(24)
  938. //合同基础信息
  939. {
  940. codeCell := dataRow.AddCell()
  941. codeCell.SetValue(v.ContractCode)
  942. codeCell.SetStyle(style)
  943. companyCell := dataRow.AddCell()
  944. companyCell.SetValue(v.CompanyName)
  945. companyCell.SetStyle(style)
  946. typeCell := dataRow.AddCell()
  947. typeCell.SetValue(v.ContractType)
  948. typeCell.SetStyle(style)
  949. priceCell := dataRow.AddCell()
  950. priceCell.SetValue(v.Price)
  951. priceCell.SetStyle(style)
  952. dateCell := dataRow.AddCell()
  953. dateCell.SetValue(fmt.Sprint(v.StartDate.Format(utils.FormatDate), " 至 ", v.EndDate.Format(utils.FormatDate)))
  954. dateCell.SetStyle(style)
  955. sellerCell := dataRow.AddCell()
  956. sellerCell.SetValue(v.SellerName)
  957. sellerCell.SetStyle(style)
  958. //合同状态
  959. statusCell := dataRow.AddCell()
  960. statusCell.SetValue(v.Status)
  961. statusCell.SetStyle(style)
  962. //关联合同客户信息
  963. relationCompanyName := ``
  964. if v.RelationContractList != nil && len(v.RelationContractList) > 0 {
  965. payCompanyNameList := make([]string, 0)
  966. for _, relationContract := range v.RelationContractList {
  967. payCompanyNameList = append(payCompanyNameList, relationContract.CompanyName)
  968. }
  969. relationCompanyName = strings.Join(payCompanyNameList, ",")
  970. } else {
  971. relationCompanyName = v.PayChannel
  972. }
  973. payCell := dataRow.AddCell()
  974. payCell.SetValue(relationCompanyName)
  975. payCell.SetStyle(style)
  976. //备注:该字段由三部分拼接:服务内容中的补充内容+赠送时间+优惠金额
  977. //补充内容直接取该字段内容;
  978. //赠送时间以年为单位,超过整年的月份作为赠送月份,如14个月即为赠送2个月;
  979. //优惠金额等于原价-优惠后价格=优惠价格
  980. //补充内容
  981. var remark string
  982. if v.Remark != "" {
  983. remark += "补充内容:" + v.Remark
  984. }
  985. //赠送时间
  986. //合同结束日期与合同开始日期的时间差(小时差)
  987. newDecimal := decimal.NewFromFloat(v.EndDate.Sub(v.StartDate).Hours())
  988. //分母为365天 * 24 小时
  989. newDecimal2 := decimal.NewFromInt(24 * 365)
  990. //计算出来相差多少年,保留一位小数(四舍五入)
  991. numYearDecimal := newDecimal.Div(newDecimal2).Round(1).Mul(decimal.NewFromInt(10))
  992. //如果大于1年,那么才去判断是否赠送
  993. numYear, _ := numYearDecimal.Float64()
  994. if numYear > 10 {
  995. //获取多出来的整数年数据
  996. numYearDecimal = numYearDecimal.Mod(decimal.NewFromInt(10)).Div(decimal.NewFromInt(10))
  997. //计算赠送出来的月份
  998. numMonthDecimal := numYearDecimal.Mul(decimal.NewFromInt(12)).Round(0)
  999. numMonth, _ := numMonthDecimal.Float64()
  1000. if numMonth > 0 {
  1001. if remark != "" {
  1002. remark += ";"
  1003. }
  1004. remark += "赠送时间为:" + numMonthDecimal.String() + "个月"
  1005. }
  1006. }
  1007. //优惠金额
  1008. if v.OriginalPrice > v.Price {
  1009. differencePrice := v.OriginalPrice - v.Price
  1010. differencePriceDecimal := decimal.NewFromFloat(differencePrice)
  1011. if remark != "" {
  1012. remark += ";"
  1013. }
  1014. remark += "优惠金额:" + differencePriceDecimal.String() + "元。"
  1015. }
  1016. remarkCell := dataRow.AddCell()
  1017. remarkCell.SetValue(remark)
  1018. remarkCell.SetStyle(style)
  1019. }
  1020. //服务套餐
  1021. {
  1022. tmpServiceList := v.Service
  1023. if contractBusinessType == "代付合同" {
  1024. if v.RelationContractList != nil && len(v.RelationContractList) > 0 {
  1025. tmpServiceList = v.RelationContractList[0].Service
  1026. }
  1027. }
  1028. cellServiceMap := make(map[int]string)
  1029. for _, cellI := range serviceCellMap {
  1030. cellServiceMap[cellI] = "否"
  1031. }
  1032. if tmpServiceList != nil && len(tmpServiceList) > 0 {
  1033. permissionValues := []string{
  1034. "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评", "深度月报:包括宏观经济月报和下游草根调研月报+电话会讨论会<br/>(由弘则的研究员主持线上讨论)",
  1035. }
  1036. for _, item := range tmpServiceList {
  1037. //ficc小套餐
  1038. if item.ServiceTemplateId == 2 {
  1039. for _, detail := range item.DetailList {
  1040. var tableCel request.AddContractServiceDetailReq
  1041. tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  1042. if tmpErr != nil {
  1043. err = tmpErr
  1044. return
  1045. }
  1046. if tableCel.Value == "商品复盘" {
  1047. var tableCel2 request.AddContractServiceDetailReq
  1048. //获取品种列数据
  1049. tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  1050. if tmpErr != nil {
  1051. err = tmpErr
  1052. return
  1053. }
  1054. //tableCel2.Tag
  1055. for _, serviceId := range tableCel2.ValueId {
  1056. key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", serviceId)
  1057. if cellI, ok := serviceCellMap[key]; ok {
  1058. cellServiceMap[cellI] = "是"
  1059. }
  1060. }
  1061. } else if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  1062. //获取品种列数据
  1063. var tableCel2 request.AddContractServiceDetailReq
  1064. tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  1065. if tmpErr != nil {
  1066. err = tmpErr
  1067. return
  1068. }
  1069. for _, serviceId := range tableCel2.ValueId {
  1070. key := fmt.Sprint(item.ServiceTemplateId, "_permission_", serviceId)
  1071. if cellI, ok := serviceCellMap[key]; ok {
  1072. cellServiceMap[cellI] = "是"
  1073. }
  1074. }
  1075. }
  1076. }
  1077. } else {
  1078. key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  1079. if cellI, ok := serviceCellMap[key]; ok {
  1080. cellServiceMap[cellI] = "是"
  1081. }
  1082. }
  1083. }
  1084. }
  1085. //遍历所有套餐
  1086. for tmpStartCellI := customStartCellI; tmpStartCellI < startI; tmpStartCellI++ {
  1087. servicesCell := dataRow.AddCell()
  1088. cellValue, ok := cellServiceMap[tmpStartCellI]
  1089. if ok == false {
  1090. cellValue = "否"
  1091. }
  1092. servicesCell.SetValue(cellValue)
  1093. if cellValue == "是" { //勾选了话,标红处理
  1094. servicesCell.SetStyle(redStyle)
  1095. } else {
  1096. servicesCell.SetStyle(style)
  1097. }
  1098. }
  1099. }
  1100. }
  1101. }
  1102. err = xlsxFile.Save(downLoadnFilePath)
  1103. if err != nil {
  1104. br.Msg = "保存文件失败"
  1105. br.ErrMsg = "保存文件失败"
  1106. return
  1107. }
  1108. randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  1109. downloadFileName := "合同列表" + randStr + ".xlsx"
  1110. this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  1111. br.Ret = 200
  1112. br.Success = true
  1113. br.Msg = "导出成功"
  1114. }
  1115. // OperationList
  1116. // @Title 获取合同操作记录
  1117. // @Description 合同操作记录接口
  1118. // @Param ContractId query int true "合同id编号"
  1119. // @Success 200 {object} response.ContractOperationListResp
  1120. // @router /operation/list [get]
  1121. func (this *ContractController) OperationList() {
  1122. br := new(models.BaseResponse).Init()
  1123. defer func() {
  1124. this.Data["json"] = br
  1125. this.ServeJSON()
  1126. }()
  1127. sysUser := this.SysUser
  1128. if sysUser == nil {
  1129. br.Msg = "请登录"
  1130. br.ErrMsg = "请登录,SysUser Is Empty"
  1131. br.Ret = 408
  1132. return
  1133. }
  1134. //合同类型、产品类型、合同状态、更新时间、所选销售
  1135. //关键字:合同编号、客户名称,社会信用码
  1136. contractId, err := this.GetInt("ContractId")
  1137. if err != nil {
  1138. br.Msg = "获取合同编号失败"
  1139. br.ErrMsg = "获取合同编号失败,err:" + err.Error()
  1140. return
  1141. }
  1142. if contractId <= 0 {
  1143. br.Msg = "请传入合同编号"
  1144. br.ErrMsg = "请传入合同编号"
  1145. return
  1146. }
  1147. list, err := contract.GetContractOperationRListByContractId(contractId)
  1148. if err != nil {
  1149. br.Msg = "获取合同操作记录列表失败!"
  1150. br.ErrMsg = "获取合同操作记录列表失败,Err:" + err.Error()
  1151. return
  1152. }
  1153. contractOperationRecordList := make([]response.ContractOperationRecordListResp, 0)
  1154. for i := 0; i < len(list); i++ {
  1155. contractOperationRecord := response.ContractOperationRecordListResp{
  1156. Id: list[i].Id,
  1157. ContractId: list[i].ContractId,
  1158. Operation: list[i].Operation,
  1159. OpUserId: list[i].OpUserId,
  1160. OpUserName: list[i].OpUserName,
  1161. Remark: list[i].Remark,
  1162. ApprovalRecordId: list[i].ApprovalRecordId,
  1163. CreateTime: list[i].CreateTime,
  1164. CreateTimeStr: list[i].CreateTime.Format(utils.FormatDateTime),
  1165. }
  1166. contractOperationRecordList = append(contractOperationRecordList, contractOperationRecord)
  1167. }
  1168. br.Ret = 200
  1169. br.Success = true
  1170. br.Msg = "获取成功"
  1171. br.Data = response.ContractOperationListResp{
  1172. List: contractOperationRecordList,
  1173. }
  1174. }
  1175. // EditContract
  1176. // @Title 编辑合同
  1177. // @Description 编辑合同接口
  1178. // @Param request body request.EditContractReq true "type json string"
  1179. // @Success 200 {object} response.AddContractResp
  1180. // @router /edit [post]
  1181. func (this *ContractController) EditContract() {
  1182. br := new(models.BaseResponse).Init()
  1183. defer func() {
  1184. this.Data["json"] = br
  1185. this.ServeJSON()
  1186. }()
  1187. sysUser := this.SysUser
  1188. if sysUser == nil {
  1189. br.Msg = "请登录"
  1190. br.ErrMsg = "请登录,SysUser Is Empty"
  1191. br.Ret = 408
  1192. return
  1193. }
  1194. productId := services.GetProductId(sysUser.RoleTypeCode)
  1195. if productId == 0 {
  1196. br.Msg = "账户异常"
  1197. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  1198. br.Ret = 408
  1199. return
  1200. }
  1201. var req request.EditContractReq
  1202. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1203. if err != nil {
  1204. br.Msg = "参数解析异常!"
  1205. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1206. return
  1207. }
  1208. if req.ContractId <= 0 {
  1209. br.Msg = "合同编号必传!"
  1210. br.ErrMsg = "合同编号必传!"
  1211. return
  1212. }
  1213. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  1214. maxPrice := float64(1000000000000)
  1215. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  1216. br.Msg = "合同金额过大, 请核验金额"
  1217. return
  1218. }
  1219. contractInfo, err, errMsg := contractService.EditContract(req, sysUser)
  1220. if err != nil {
  1221. br.Msg = "修改合同失败!"
  1222. if errMsg != "" {
  1223. br.Msg = errMsg
  1224. }
  1225. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1226. return
  1227. }
  1228. msg := "修改成功"
  1229. //如果发起重新审核,那么
  1230. if req.ReAudit {
  1231. tmpErr, errMsg := contractService.Apply(contractInfo.ContractId)
  1232. if tmpErr != nil {
  1233. br.Msg = "发起重申失败!"
  1234. if errMsg != "" {
  1235. br.Msg = errMsg
  1236. }
  1237. br.ErrMsg = "发起重申失败,Err:" + tmpErr.Error()
  1238. return
  1239. }
  1240. msg = "发起重申成功"
  1241. }
  1242. br.Ret = 200
  1243. br.Success = true
  1244. br.Msg = msg
  1245. br.Data = response.AddContractResp{
  1246. ContractId: contractInfo.ContractId,
  1247. }
  1248. }
  1249. // Delete
  1250. // @Title 删除合同
  1251. // @Description 删除合同接口
  1252. // @Param ContractId query int true "合同id"
  1253. // @Success Ret=200 删除成功
  1254. // @router /delete [get]
  1255. func (this *ContractController) Delete() {
  1256. br := new(models.BaseResponse).Init()
  1257. defer func() {
  1258. this.Data["json"] = br
  1259. this.ServeJSON()
  1260. }()
  1261. sysUser := this.SysUser
  1262. if sysUser == nil {
  1263. br.Msg = "请登录"
  1264. br.ErrMsg = "请登录,SysUser Is Empty"
  1265. br.Ret = 408
  1266. return
  1267. }
  1268. contractId, _ := this.GetInt("ContractId")
  1269. //合同id
  1270. if contractId <= 0 {
  1271. br.Msg = "合同id必传!"
  1272. br.ErrMsg = "合同id必传"
  1273. return
  1274. }
  1275. err := contractService.DeleteContract(contractId, sysUser)
  1276. if err != nil {
  1277. br.Msg = "修改合同失败!"
  1278. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1279. return
  1280. }
  1281. br.Ret = 200
  1282. br.Success = true
  1283. br.Msg = "删除成功"
  1284. return
  1285. }
  1286. // Invalid
  1287. // @Title 作废合同
  1288. // @Description 作废合同接口
  1289. // @Param ContractId query int true "合同id"
  1290. // @Success Ret=200 作废成功
  1291. // @router /invalid [get]
  1292. func (this *ContractController) Invalid() {
  1293. br := new(models.BaseResponse).Init()
  1294. defer func() {
  1295. this.Data["json"] = br
  1296. this.ServeJSON()
  1297. }()
  1298. sysUser := this.SysUser
  1299. if sysUser == nil {
  1300. br.Msg = "请登录"
  1301. br.ErrMsg = "请登录,SysUser Is Empty"
  1302. br.Ret = 408
  1303. return
  1304. }
  1305. contractId, _ := this.GetInt("ContractId")
  1306. //合同id
  1307. if contractId <= 0 {
  1308. br.Msg = "合同id必传!"
  1309. br.ErrMsg = "合同id必传"
  1310. return
  1311. }
  1312. err := contractService.InvalidContract(contractId, sysUser)
  1313. if err != nil {
  1314. br.Msg = "修改合同失败!"
  1315. br.ErrMsg = "修改合同失败,Err:" + err.Error()
  1316. return
  1317. }
  1318. br.Ret = 200
  1319. br.Success = true
  1320. br.Msg = "作废成功"
  1321. return
  1322. }
  1323. // UploadCheckBackFile
  1324. // @Title 上传签回附件
  1325. // @Description 上传签回附件接口
  1326. // @Param request body request.UploadCheckBackFileReq true "type json string"
  1327. // @Success Ret=200 上传成功
  1328. // @router /upload_check_back_file [post]
  1329. func (this *ContractController) UploadCheckBackFile() {
  1330. br := new(models.BaseResponse).Init()
  1331. defer func() {
  1332. this.Data["json"] = br
  1333. this.ServeJSON()
  1334. }()
  1335. sysUser := this.SysUser
  1336. if sysUser == nil {
  1337. br.Msg = "请登录"
  1338. br.ErrMsg = "请登录,SysUser Is Empty"
  1339. br.Ret = 408
  1340. return
  1341. }
  1342. //var req request.UploadCheckBackFileReq
  1343. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1344. //if err != nil {
  1345. // br.Msg = "参数解析异常!"
  1346. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1347. // return
  1348. //}
  1349. ////合同编号
  1350. //if req.ContractId <= 0 {
  1351. // br.Msg = "请传入合同编号!"
  1352. // br.ErrMsg = "请传入合同编号"
  1353. // return
  1354. //}
  1355. //
  1356. //if req.FileUrl == "" {
  1357. // br.Msg = "请先上传附件!"
  1358. // br.ErrMsg = "请先上传附件"
  1359. // return
  1360. //}
  1361. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  1362. if contractIdStr == "" {
  1363. br.Msg = "合同ID必传!"
  1364. br.ErrMsg = "合同ID必传,Err"
  1365. return
  1366. }
  1367. contractId, err := strconv.Atoi(contractIdStr)
  1368. if err != nil {
  1369. br.Msg = "合同ID异常!"
  1370. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  1371. return
  1372. }
  1373. fileMulti, h, err := this.GetFile("file")
  1374. if err != nil {
  1375. br.Msg = "获取资源信息失败"
  1376. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1377. return
  1378. }
  1379. //合同编号
  1380. if contractId <= 0 {
  1381. br.Msg = "请传入合同编号!"
  1382. br.ErrMsg = "请传入合同编号"
  1383. return
  1384. }
  1385. ext := path.Ext(h.Filename)
  1386. resourceUrl, err := contractService.UploadCheckBackFile(contractId, ext, fileMulti, this.SysUser)
  1387. if err != nil {
  1388. br.Msg = "更新失败!"
  1389. br.ErrMsg = "更新失败" + err.Error()
  1390. return
  1391. }
  1392. //更新用印中的签回合同
  1393. _, _ = seal.UploadCheckBackFile(contractId, resourceUrl, this.SysUser)
  1394. br.Ret = 200
  1395. br.Success = true
  1396. br.Msg = "上传成功"
  1397. }
  1398. // UploadRescindFile
  1399. // @Title 上传解约附件
  1400. // @Description 上传解约附件接口
  1401. // @Param request body request.UploadRescindFileReq true "type json string"
  1402. // @Success Ret=200 上传成功
  1403. // @router /upload_rescind_file [post]
  1404. func (this *ContractController) UploadRescindFile() {
  1405. br := new(models.BaseResponse).Init()
  1406. defer func() {
  1407. this.Data["json"] = br
  1408. this.ServeJSON()
  1409. }()
  1410. sysUser := this.SysUser
  1411. if sysUser == nil {
  1412. br.Msg = "请登录"
  1413. br.ErrMsg = "请登录,SysUser Is Empty"
  1414. br.Ret = 408
  1415. return
  1416. }
  1417. //var req request.UploadRescindFileReq
  1418. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  1419. //if err != nil {
  1420. // br.Msg = "参数解析异常!"
  1421. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  1422. // return
  1423. //}
  1424. ////合同编号
  1425. //if req.ContractId <= 0 {
  1426. // br.Msg = "请传入合同编号!"
  1427. // br.ErrMsg = "请传入合同编号"
  1428. // return
  1429. //}
  1430. //
  1431. //if req.FileUrl == "" {
  1432. // br.Msg = "请先上传附件!"
  1433. // br.ErrMsg = "请先上传附件"
  1434. // return
  1435. //}
  1436. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  1437. if contractIdStr == "" {
  1438. br.Msg = "合同ID必传!"
  1439. br.ErrMsg = "合同ID必传,Err"
  1440. return
  1441. }
  1442. contractId, err := strconv.Atoi(contractIdStr)
  1443. if err != nil {
  1444. br.Msg = "合同ID异常!"
  1445. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  1446. return
  1447. }
  1448. fileMulti, h, err := this.GetFile("file")
  1449. if err != nil {
  1450. br.Msg = "获取资源信息失败"
  1451. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  1452. return
  1453. }
  1454. //合同编号
  1455. if contractId <= 0 {
  1456. br.Msg = "请传入合同编号!"
  1457. br.ErrMsg = "请传入合同编号"
  1458. return
  1459. }
  1460. ext := path.Ext(h.Filename)
  1461. err = contractService.UploadRescindFile(contractId, ext, fileMulti, this.SysUser)
  1462. br.Ret = 200
  1463. br.Success = true
  1464. br.Msg = "上传成功"
  1465. }
  1466. // Detail
  1467. // @Title 获取合同详情
  1468. // @Description 获取合同详情接口
  1469. // @Param ContractId query int true "合同id"
  1470. // @Success 200 {object} contract.ContractDetail
  1471. // @router /detail [get]
  1472. func (this *ContractController) Detail() {
  1473. br := new(models.BaseResponse).Init()
  1474. defer func() {
  1475. this.Data["json"] = br
  1476. this.ServeJSON()
  1477. }()
  1478. sysUser := this.SysUser
  1479. if sysUser == nil {
  1480. br.Msg = "请登录"
  1481. br.ErrMsg = "请登录,SysUser Is Empty"
  1482. br.Ret = 408
  1483. return
  1484. }
  1485. //合同类型、产品类型、合同状态、更新时间、所选销售
  1486. //关键字:合同编号、客户名称,社会信用码
  1487. contractId, _ := this.GetInt("ContractId")
  1488. //合同id
  1489. if contractId <= 0 {
  1490. br.Msg = "合同id必传!"
  1491. br.ErrMsg = "合同id必传"
  1492. return
  1493. }
  1494. contractInfo, err := contractService.GetContractDetail(contractId)
  1495. if err != nil {
  1496. br.Msg = "获取合同详情失败!"
  1497. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  1498. return
  1499. }
  1500. // CRM8.8-权益主客观合并
  1501. if contractInfo.ProductId == 2 {
  1502. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  1503. contractInfo.Service = mergeList
  1504. // 关联合同同样合并
  1505. for i, v := range contractInfo.RelationContractDetailList {
  1506. relationMergeList := contractService.EquityMergeSubjectAndObject(v.Service)
  1507. contractInfo.RelationContractDetailList[i].Service = relationMergeList
  1508. }
  1509. }
  1510. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  1511. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  1512. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  1513. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  1514. //合同中包含的产品权限
  1515. isNeedPermission := false //是否需要列出来套餐中的详细权限
  1516. for _, service := range contractInfo.Service {
  1517. if service.ServiceTemplateId == 2 { //小套餐才需要列出来详细的权限
  1518. isNeedPermission = true
  1519. }
  1520. }
  1521. var permissionLookListResp = make([]*contract.PermissionLookList, 0)
  1522. if isNeedPermission {
  1523. checkPermissionMap, tErr := contractService.GetServicePermissionMap(contractInfo.Service)
  1524. if tErr == nil {
  1525. permissionLookListMap := make(map[int][]*contract.PermissionLookItem)
  1526. var permissionIds []string
  1527. for _, v := range checkPermissionMap {
  1528. permissionIds = append(permissionIds, strconv.Itoa(v))
  1529. }
  1530. permissionIdStr := strings.Join(permissionIds, ",")
  1531. charPermissions, tErr1 := company.GetPermissionLookItemsSandO(permissionIdStr)
  1532. if tErr1 == nil {
  1533. for _, v := range charPermissions {
  1534. tmp := new(contract.PermissionLookItem)
  1535. tmp.ClassifyName = v.ClassifyName
  1536. tmp.PermissionName = v.PermissionName
  1537. tmp.ChartPermissionId = v.ChartPermissionId
  1538. permissionLookListMap[v.ParentId] = append(permissionLookListMap[v.ParentId], tmp)
  1539. }
  1540. classifyNameList, tErr2 := company.GetChartPermissionFirst()
  1541. if tErr2 == nil {
  1542. for _, v := range classifyNameList {
  1543. if perList, ok := permissionLookListMap[v.ChartPermissionId]; ok {
  1544. permissionLookListRespItem := &contract.PermissionLookList{
  1545. ClassifyName: v.ClassifyName,
  1546. Items: perList,
  1547. }
  1548. permissionLookListResp = append(permissionLookListResp, permissionLookListRespItem)
  1549. }
  1550. }
  1551. }
  1552. }
  1553. }
  1554. }
  1555. contractInfo.PermissionLookList = permissionLookListResp
  1556. br.Ret = 200
  1557. br.Success = true
  1558. br.Msg = "获取成功"
  1559. br.Data = contractInfo
  1560. }
  1561. //Export
  1562. //@Title 合同列表数据导出
  1563. //@Description 合同列表数据导出接口
  1564. //@Description 合同列表接口
  1565. //@Param ContractType query string false "合同类型,枚举值:'新签合同','续约合同','补充协议'"
  1566. //@Param ContractStatus query string false "合同状态,枚举值:'待提交','待审批','已撤回','已审批','已驳回','已作废'"
  1567. //@Param ProductId query int false "客户类型:传0或者不传为当前账号权限,1 代表是:ficc;2 代表是:权益"
  1568. //@Param ModifyStartTime query string false "服务更新时间的选择开始时间,格式:2021-05-23 00:00:00"
  1569. //@Param ModifyEndTime query string false "服务更新时间的选择结束时间,格式:2021-05-26 23:59:59"
  1570. //@Param SellerId query string false "选择的销售id"
  1571. //@Param Keyword query string false "搜索关键字"
  1572. //@Success 200 {object} response.ContractListResp
  1573. //@router /export [get]
  1574. //func (this *ContractController) Export() {
  1575. // br := new(models.BaseResponse).Init()
  1576. // defer func() {
  1577. // this.Data["json"] = br
  1578. // this.ServeJSON()
  1579. // }()
  1580. // sysUser := this.SysUser
  1581. // if sysUser == nil {
  1582. // br.Msg = "请登录"
  1583. // br.ErrMsg = "请登录,SysUser Is Empty"
  1584. // br.Ret = 408
  1585. // return
  1586. // }
  1587. //
  1588. // contractType := this.GetString("ContractType")
  1589. // contractStatus := this.GetString("ContractStatus")
  1590. // productId, _ := this.GetInt("ProductId")
  1591. // modifyStartTime := this.GetString("ModifyStartTime")
  1592. // modifyEndTime := this.GetString("ModifyEndTime")
  1593. // sellerIds := this.GetString("SellerId")
  1594. // keyword := this.GetString("Keyword")
  1595. //
  1596. // condition := ""
  1597. // pars := make([]interface{}, 0)
  1598. //
  1599. // //如果不是超管或者合规,那么只能查看自己的合同
  1600. // if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  1601. // condition += ` AND product_id = 1 AND status not in ("待提交","已撤回") `
  1602. // } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  1603. // condition += ` AND product_id = 2 AND status not in ("待提交","已撤回") `
  1604. // } else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_ADMIN { //超管可查看所有状态的合同(除过已撤回、待提交状态合同),可下载所有状态的合同
  1605. // condition += ` AND status not in ("待提交","已撤回") `
  1606. // } else {
  1607. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  1608. // condition += ` AND seller_id = ? `
  1609. // pars = append(pars, sysUser.AdminId)
  1610. // }
  1611. // }
  1612. //
  1613. // //需要下载的 产品 切片
  1614. // productSlice := make([]int, 0)
  1615. //
  1616. // //合同类型、、更新时间、所选销售
  1617. // //关键字:合同编号、客户名称,社会信用码
  1618. // if contractType != "" {
  1619. // condition += ` AND contract_type = ? `
  1620. // pars = append(pars, contractType)
  1621. // }
  1622. // //合同状态
  1623. // if contractStatus != "" {
  1624. // condition += ` AND status = ? `
  1625. // pars = append(pars, contractStatus)
  1626. // }
  1627. //
  1628. // //产品类型
  1629. // if productId > 0 {
  1630. // condition += ` AND product_id = ? `
  1631. // pars = append(pars, productId)
  1632. //
  1633. // productSlice = append(productSlice, productId)
  1634. // } else {
  1635. // nowProductId := services.GetProductId(sysUser.RoleTypeCode)
  1636. // if nowProductId > 0 {
  1637. // productSlice = append(productSlice, nowProductId)
  1638. // } else {
  1639. // productSlice = append(productSlice, 1)
  1640. // productSlice = append(productSlice, 2)
  1641. // }
  1642. // }
  1643. // //所选销售
  1644. // if sellerIds != "" {
  1645. // condition += ` AND seller_id IN (` + sellerIds + `) `
  1646. // }
  1647. // //更新开始时间
  1648. // if modifyStartTime != "" {
  1649. // condition += ` AND modify_time >= ? `
  1650. // pars = append(pars, modifyStartTime)
  1651. // }
  1652. // //更新结束时间
  1653. // if modifyEndTime != "" {
  1654. // condition += ` AND modify_time <= ? `
  1655. // pars = append(pars, modifyEndTime)
  1656. // }
  1657. // //关键字
  1658. // if keyword != "" {
  1659. // condition += ` AND (contract_code LIKE '%` + keyword + `%' OR company_name LIKE '%` + keyword + `%' OR credit_code LIKE '%` + keyword + `%' ) `
  1660. // }
  1661. //
  1662. // //取 一万 条合同数据
  1663. // list, err := contract.GetContractList(condition, pars, 0, 10000)
  1664. // if err != nil {
  1665. // br.Msg = "获取合同列表失败!"
  1666. // br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  1667. // return
  1668. // }
  1669. // //获取合同套餐服务列表
  1670. // serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  1671. // if err != nil {
  1672. // br.Msg = "获取合同套餐服务列表失败!"
  1673. // br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  1674. // return
  1675. // }
  1676. // templateTitleMap := make(map[int]string)
  1677. // for _, serviceTemplate := range serviceTemplateList {
  1678. // templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  1679. // }
  1680. //
  1681. // for i := 0; i < len(list); i++ {
  1682. // item := list[i]
  1683. // list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  1684. // list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  1685. //
  1686. // list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  1687. // list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  1688. //
  1689. // //服务列表
  1690. // serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  1691. // if err != nil {
  1692. // br.Msg = "获取失败"
  1693. // br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  1694. // return
  1695. // }
  1696. // for i := 0; len(serviceList) > i; i++ {
  1697. // if serviceList[i].HasDetail == "是" {
  1698. // list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  1699. // if detailErr != nil {
  1700. // br.Msg = "获取失败"
  1701. // br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  1702. // return
  1703. // }
  1704. //
  1705. // //套餐名称
  1706. // serviceList[i].DetailList = list
  1707. // if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  1708. // serviceList[i].Title = title
  1709. // }
  1710. // }
  1711. // }
  1712. // list[i].Service = serviceList
  1713. // }
  1714. //
  1715. // //生成excel文件
  1716. // dir, err := os.Executable()
  1717. // exPath := filepath.Dir(dir)
  1718. // downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
  1719. // xlsxFile := xlsx.NewFile()
  1720. // if err != nil {
  1721. // br.Msg = "生成文件失败"
  1722. // br.ErrMsg = "生成文件失败"
  1723. // return
  1724. // }
  1725. //
  1726. // //设置默认字体和文字大小
  1727. // xlsx.SetDefaultFont(12, "宋体")
  1728. //
  1729. // //定义边框样式
  1730. // border := xlsx.NewBorder("thin", "thin", "thin", "thin")
  1731. // //定义文字排版样式
  1732. // alignment := xlsx.Alignment{
  1733. // Horizontal: "center",
  1734. // Vertical: "center",
  1735. // WrapText: true,
  1736. // }
  1737. //
  1738. // //普通样式
  1739. // style := xlsx.NewStyle()
  1740. // style.Alignment = alignment
  1741. // style.ApplyAlignment = true
  1742. // style.Border = *border
  1743. //
  1744. // //标红文字,便于查看哪些产品被勾选
  1745. // redStyle := xlsx.NewStyle()
  1746. // redStyle.Alignment = alignment
  1747. // redStyle.ApplyAlignment = true
  1748. // redStyle.Font.Color = "ff0000"
  1749. // //定义底色需要标黄的 单元格颜色
  1750. // redFill := xlsx.Fill{"solid", "ffff00", "ffff00"}
  1751. // redStyle.Fill = redFill
  1752. // redStyle.Border = *border
  1753. //
  1754. // //标题样式
  1755. // titleStyle := xlsx.NewStyle()
  1756. // titleFont := xlsx.NewFont(14, "宋体")
  1757. // titleFont.Bold = true
  1758. // titleStyle.Font = *titleFont
  1759. // titleStyle.Alignment = alignment
  1760. // titleStyle.Border = *border
  1761. //
  1762. // //titleStyle.ApplyAlignment = true
  1763. //
  1764. // //表头
  1765. // headerStyle := xlsx.NewStyle()
  1766. // headerFont := xlsx.NewFont(12, "宋体")
  1767. // headerFont.Bold = true
  1768. // headerStyle.Font = *headerFont
  1769. // headerStyle.Alignment = alignment
  1770. // headerStyle.ApplyAlignment = true
  1771. // headerStyle.Border = *border
  1772. //
  1773. // defer func() {
  1774. // os.Remove(downLoadnFilePath)
  1775. // }()
  1776. //
  1777. // //遍历所拥有的的产品权限,然后插入到对应的sheet表单中
  1778. // for pidIndex := 0; pidIndex < len(productSlice); pidIndex++ {
  1779. // productId = productSlice[pidIndex]
  1780. // //表单名称
  1781. // sheetName := "合同数据"
  1782. // switch productId {
  1783. // case 1:
  1784. // sheetName = "ficc合同信息"
  1785. // case 2:
  1786. // sheetName = "权益合同信息"
  1787. // }
  1788. // //新增一个sheet表单
  1789. // sheel, err := xlsxFile.AddSheet(sheetName)
  1790. // if err != nil {
  1791. // br.Msg = "新增Sheet失败"
  1792. // br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1793. // return
  1794. // }
  1795. // //设置列宽
  1796. // sheel.SetColWidth(0, 0, 28)
  1797. // sheel.SetColWidth(1, 1, 30)
  1798. // sheel.SetColWidth(2, 2, 18)
  1799. // sheel.SetColWidth(3, 3, 18)
  1800. // sheel.SetColWidth(4, 4, 35)
  1801. // sheel.SetColWidth(5, 6, 20)
  1802. // sheel.SetColWidth(7, 7, 40)
  1803. // sheel.SetColWidth(7, 35, 30)
  1804. //
  1805. // //标题行
  1806. // titleRow := sheel.AddRow()
  1807. // titleRow.SetHeight(30)
  1808. //
  1809. // //合同信息列
  1810. // titleCell := titleRow.AddCell()
  1811. // titleCell.HMerge = 7 //向右合并列数,不包括自身列
  1812. // //遍历去添加需要合并的列
  1813. // for i := 0; i < 7; i++ {
  1814. // titleRow.AddCell()
  1815. // }
  1816. //
  1817. // //报表标题名称
  1818. // titleCell.SetValue("合同信息")
  1819. // titleCell.SetStyle(titleStyle)
  1820. //
  1821. // startI := 8
  1822. // customStartCellI := 8
  1823. //
  1824. // serviceSlice := make([]string, 0)
  1825. // serviceCellMap := make(map[string]int)
  1826. //
  1827. // //获取所有套餐列表
  1828. // {
  1829. // serviceList, err := getServiceTemplateList(productId)
  1830. // if err != nil {
  1831. // br.Msg = "查询套餐类型失败"
  1832. // br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
  1833. // return
  1834. // }
  1835. // for i := 0; i < len(serviceList); i++ {
  1836. // //需要向右合并添加的列数
  1837. // addCellNum := 0
  1838. //
  1839. // item := serviceList[i]
  1840. // //套餐服务列
  1841. // servicesCell := titleRow.AddCell()
  1842. // //套餐名称
  1843. // servicesCell.SetValue(item.Title)
  1844. // servicesCell.SetStyle(titleStyle)
  1845. //
  1846. // //ficc小套餐
  1847. // if item.ServiceTemplateId == 2 {
  1848. // productPermission := make(map[int]string)
  1849. // productPermission[1] = "每日宏观商品复盘"
  1850. // productPermission[2] = "能化专栏《化里化外》"
  1851. // productPermission[3] = "股债日评"
  1852. // productPermission[4] = "黑色专栏《知白守黑》"
  1853. // productPermission[5] = "有色专栏《有声有色》"
  1854. // for id, name := range productPermission {
  1855. // key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", id)
  1856. // serviceCellMap[key] = startI
  1857. // serviceSlice = append(serviceSlice, name)
  1858. // startI++
  1859. // addCellNum++
  1860. // }
  1861. //
  1862. // permissionList := make([]*company.PermissionSetItem, 0)
  1863. // for _, v := range utils.PermissionFiccClassifyArr {
  1864. // items, err := company.GetPermissionSetItems(1, v)
  1865. // if err != nil {
  1866. // br.Msg = "获取失败"
  1867. // br.ErrMsg = "获取权限信息失败,Err:" + err.Error()
  1868. // return
  1869. // }
  1870. // permissionList = append(permissionList, items...)
  1871. // }
  1872. //
  1873. // //权限列表
  1874. // for j := 0; j < len(permissionList); j++ {
  1875. // key := fmt.Sprint(item.ServiceTemplateId, "_permission_", permissionList[j].ChartPermissionId)
  1876. // serviceCellMap[key] = startI
  1877. // serviceSlice = append(serviceSlice, permissionList[j].PermissionName)
  1878. // startI++
  1879. // addCellNum++
  1880. // }
  1881. // //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  1882. // addCellNum = addCellNum - 1
  1883. // if addCellNum > 0 {
  1884. // for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  1885. // titleRow.AddCell()
  1886. // }
  1887. // servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  1888. // }
  1889. // continue
  1890. // } else {
  1891. // if item.Children != nil && len(item.Children) > 0 {
  1892. // for j := 0; j < len(item.Children); j++ {
  1893. // key := fmt.Sprint("service_template_", item.Children[j].ServiceTemplateId)
  1894. // serviceCellMap[key] = startI
  1895. // serviceSlice = append(serviceSlice, item.Children[j].Title)
  1896. // startI++
  1897. // addCellNum++
  1898. // }
  1899. // //因为自己本身有一列了,所以在最后增加列的时候,需要减去自身列
  1900. // addCellNum = addCellNum - 1
  1901. // if addCellNum > 0 {
  1902. // for tmpAddCellNum := 0; tmpAddCellNum < addCellNum; tmpAddCellNum++ {
  1903. // titleRow.AddCell()
  1904. // }
  1905. // servicesCell.HMerge = addCellNum //向右合并列数,不包括自身列
  1906. // }
  1907. // } else {
  1908. // key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  1909. // serviceCellMap[key] = startI
  1910. // serviceSlice = append(serviceSlice, item.Title)
  1911. // startI++
  1912. // }
  1913. // }
  1914. // }
  1915. // }
  1916. //
  1917. // //表头
  1918. // headerRow := sheel.AddRow()
  1919. // headerRow.SetHeight(24)
  1920. //
  1921. // //合同基础信息
  1922. // {
  1923. // codeHeaderCell := headerRow.AddCell()
  1924. // codeHeaderCell.SetValue("合同编号")
  1925. // codeHeaderCell.SetStyle(headerStyle)
  1926. //
  1927. // companyHeaderCell := headerRow.AddCell()
  1928. // companyHeaderCell.SetValue("客户名称")
  1929. // companyHeaderCell.SetStyle(headerStyle)
  1930. //
  1931. // typeHeaderCell := headerRow.AddCell()
  1932. // typeHeaderCell.SetValue("合同类型")
  1933. // typeHeaderCell.SetStyle(headerStyle)
  1934. //
  1935. // priceHeaderCell := headerRow.AddCell()
  1936. // priceHeaderCell.SetValue("合同金额")
  1937. // priceHeaderCell.SetStyle(headerStyle)
  1938. //
  1939. // dateHeaderCell := headerRow.AddCell()
  1940. // dateHeaderCell.SetValue("合同有效期")
  1941. // dateHeaderCell.SetStyle(headerStyle)
  1942. //
  1943. // sellerHeaderCell := headerRow.AddCell()
  1944. // sellerHeaderCell.SetValue("销售")
  1945. // sellerHeaderCell.SetStyle(headerStyle)
  1946. //
  1947. // statusHeaderCell := headerRow.AddCell()
  1948. // statusHeaderCell.SetValue("合同状态")
  1949. // statusHeaderCell.SetStyle(headerStyle)
  1950. //
  1951. // remarkHeaderCell := headerRow.AddCell()
  1952. // remarkHeaderCell.SetValue("备注")
  1953. // remarkHeaderCell.SetStyle(headerStyle)
  1954. // }
  1955. //
  1956. // //添加服务套餐名称
  1957. // for _, service := range serviceSlice {
  1958. // cell := headerRow.AddCell()
  1959. // cell.SetValue(service)
  1960. // cell.SetStyle(headerStyle)
  1961. // }
  1962. //
  1963. // for _, v := range list {
  1964. // //如果当前合同不是该产品
  1965. // if v.ProductId != productId {
  1966. // continue
  1967. // }
  1968. // dataRow := sheel.AddRow()
  1969. // dataRow.SetHeight(24)
  1970. //
  1971. // //合同基础信息
  1972. // {
  1973. // codeCell := dataRow.AddCell()
  1974. // codeCell.SetValue(v.ContractCode)
  1975. // codeCell.SetStyle(style)
  1976. //
  1977. // companyCell := dataRow.AddCell()
  1978. // companyCell.SetValue(v.CompanyName)
  1979. // companyCell.SetStyle(style)
  1980. //
  1981. // typeCell := dataRow.AddCell()
  1982. // typeCell.SetValue(v.ContractType)
  1983. // typeCell.SetStyle(style)
  1984. //
  1985. // priceCell := dataRow.AddCell()
  1986. // priceCell.SetValue(v.Price)
  1987. // priceCell.SetStyle(style)
  1988. //
  1989. // dateCell := dataRow.AddCell()
  1990. // dateCell.SetValue(fmt.Sprint(v.StartDate.Format(utils.FormatDate), " 至 ", v.EndDate.Format(utils.FormatDate)))
  1991. // dateCell.SetStyle(style)
  1992. //
  1993. // sellerCell := dataRow.AddCell()
  1994. // sellerCell.SetValue(v.SellerName)
  1995. // sellerCell.SetStyle(style)
  1996. //
  1997. // statusCell := dataRow.AddCell()
  1998. // statusCell.SetValue(v.Status)
  1999. // statusCell.SetStyle(style)
  2000. //
  2001. // //备注:该字段由三部分拼接:服务内容中的补充内容+赠送时间+优惠金额
  2002. // //补充内容直接取该字段内容;
  2003. // //赠送时间以年为单位,超过整年的月份作为赠送月份,如14个月即为赠送2个月;
  2004. // //优惠金额等于原价-优惠后价格=优惠价格
  2005. //
  2006. // //补充内容
  2007. // var remark string
  2008. // if v.Remark != "" {
  2009. // remark += "补充内容:" + v.Remark
  2010. // }
  2011. //
  2012. // //赠送时间
  2013. // //合同结束日期与合同开始日期的时间差(小时差)
  2014. // newDecimal := decimal.NewFromFloat(v.EndDate.Sub(v.StartDate).Hours())
  2015. // //分母为365天 * 24 小时
  2016. // newDecimal2 := decimal.NewFromInt(24 * 365)
  2017. // //计算出来相差多少年,保留一位小数(四舍五入)
  2018. // numYearDecimal := newDecimal.Div(newDecimal2).Round(1).Mul(decimal.NewFromInt(10))
  2019. // //如果大于1年,那么才去判断是否赠送
  2020. // numYear, _ := numYearDecimal.Float64()
  2021. // if numYear > 10 {
  2022. // //获取多出来的整数年数据
  2023. // numYearDecimal = numYearDecimal.Mod(decimal.NewFromInt(10)).Div(decimal.NewFromInt(10))
  2024. // //计算赠送出来的月份
  2025. // numMonthDecimal := numYearDecimal.Mul(decimal.NewFromInt(12)).Round(0)
  2026. // numMonth, _ := numMonthDecimal.Float64()
  2027. // if numMonth > 0 {
  2028. // if remark != "" {
  2029. // remark += ";"
  2030. // }
  2031. // remark += "赠送时间为:" + numMonthDecimal.String() + "个月"
  2032. // }
  2033. // }
  2034. // //优惠金额
  2035. // if v.OriginalPrice > v.Price {
  2036. // differencePrice := v.OriginalPrice - v.Price
  2037. // differencePriceDecimal := decimal.NewFromFloat(differencePrice)
  2038. //
  2039. // if remark != "" {
  2040. // remark += ";"
  2041. // }
  2042. // remark += "优惠金额:" + differencePriceDecimal.String() + "元。"
  2043. // }
  2044. // remarkCell := dataRow.AddCell()
  2045. // remarkCell.SetValue(remark)
  2046. // remarkCell.SetStyle(style)
  2047. // }
  2048. //
  2049. // //服务套餐
  2050. // {
  2051. // cellServiceMap := make(map[int]string)
  2052. // for _, cellI := range serviceCellMap {
  2053. // cellServiceMap[cellI] = "否"
  2054. // }
  2055. //
  2056. // if v.Service != nil && len(v.Service) > 0 {
  2057. // permissionValues := []string{
  2058. // "FICC周报", "商品双周报+线上电话会讨论会<br/>(由弘则的研究员主持线上讨论)", "数据点评", "深度月报:包括宏观经济月报和下游草根调研月报+电话会讨论会<br/>(由弘则的研究员主持线上讨论)",
  2059. // }
  2060. // for _, item := range v.Service {
  2061. // //ficc小套餐
  2062. // if item.ServiceTemplateId == 2 {
  2063. // for _, detail := range item.DetailList {
  2064. // var tableCel request.AddContractServiceDetailReq
  2065. // tmpErr := json.Unmarshal([]byte(detail.Col1), &tableCel)
  2066. // if tmpErr != nil {
  2067. // err = tmpErr
  2068. // return
  2069. // }
  2070. // if tableCel.Value == "商品复盘" {
  2071. // var tableCel2 request.AddContractServiceDetailReq
  2072. // //获取品种列数据
  2073. // tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  2074. // if tmpErr != nil {
  2075. // err = tmpErr
  2076. // return
  2077. // }
  2078. // //tableCel2.Tag
  2079. // for _, serviceId := range tableCel2.ValueId {
  2080. // key := fmt.Sprint(item.ServiceTemplateId, "_product_permission_", serviceId)
  2081. // if cellI, ok := serviceCellMap[key]; ok {
  2082. // cellServiceMap[cellI] = "是"
  2083. // }
  2084. // }
  2085. //
  2086. // } else if strings.Contains(strings.Join(permissionValues, ","), tableCel.Value) {
  2087. // //获取品种列数据
  2088. // var tableCel2 request.AddContractServiceDetailReq
  2089. // tmpErr := contractService.GetProductCelData(*detail, &tableCel2)
  2090. // if tmpErr != nil {
  2091. // err = tmpErr
  2092. // return
  2093. // }
  2094. // for _, serviceId := range tableCel2.ValueId {
  2095. // key := fmt.Sprint(item.ServiceTemplateId, "_permission_", serviceId)
  2096. // if cellI, ok := serviceCellMap[key]; ok {
  2097. // cellServiceMap[cellI] = "是"
  2098. // }
  2099. // }
  2100. // }
  2101. // }
  2102. // } else {
  2103. // key := fmt.Sprint("service_template_", item.ServiceTemplateId)
  2104. // if cellI, ok := serviceCellMap[key]; ok {
  2105. // cellServiceMap[cellI] = "是"
  2106. // }
  2107. // }
  2108. // }
  2109. // }
  2110. //
  2111. // //遍历所有套餐
  2112. // for tmpStartCellI := customStartCellI; tmpStartCellI < startI; tmpStartCellI++ {
  2113. // servicesCell := dataRow.AddCell()
  2114. // cellValue, ok := cellServiceMap[tmpStartCellI]
  2115. // if ok == false {
  2116. // cellValue = "否"
  2117. // }
  2118. // servicesCell.SetValue(cellValue)
  2119. // if cellValue == "是" { //勾选了话,标红处理
  2120. // servicesCell.SetStyle(redStyle)
  2121. // } else {
  2122. // servicesCell.SetStyle(style)
  2123. // }
  2124. // }
  2125. // }
  2126. //
  2127. // }
  2128. // }
  2129. // err = xlsxFile.Save(downLoadnFilePath)
  2130. // if err != nil {
  2131. // br.Msg = "保存文件失败"
  2132. // br.ErrMsg = "保存文件失败"
  2133. // return
  2134. // }
  2135. // randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
  2136. // downloadFileName := "合同列表" + randStr + ".xlsx"
  2137. // this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
  2138. // br.Ret = 200
  2139. // br.Success = true
  2140. // br.Msg = "导出成功"
  2141. //}
  2142. // Preview
  2143. // @Title 预览合同
  2144. // @Description 预览合同接口v
  2145. // @Param request body request.AddContractReq true "type json string"
  2146. // @Success 200 {object} response.ContractPreviewResp
  2147. // @router /preview [post]
  2148. func (this *ContractController) Preview() {
  2149. br := new(models.BaseResponse).Init()
  2150. defer func() {
  2151. this.Data["json"] = br
  2152. this.ServeJSON()
  2153. }()
  2154. sysUser := this.SysUser
  2155. if sysUser == nil {
  2156. br.Msg = "请登录"
  2157. br.ErrMsg = "请登录,SysUser Is Empty"
  2158. br.Ret = 408
  2159. return
  2160. }
  2161. productId := services.GetProductId(sysUser.RoleTypeCode)
  2162. if productId == 0 {
  2163. br.Msg = "账户异常"
  2164. br.ErrMsg = "账户异常,当前账户类型为:" + sysUser.RoleTypeCode
  2165. br.Ret = 408
  2166. return
  2167. }
  2168. var req request.AddContractReq
  2169. err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2170. if err != nil {
  2171. br.Msg = "参数解析异常!"
  2172. br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2173. return
  2174. }
  2175. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2176. maxPrice := float64(1000000000000)
  2177. if req.OriginalPrice >= maxPrice || req.Price >= maxPrice {
  2178. br.Msg = "合同金额过大, 请核验金额"
  2179. return
  2180. }
  2181. contractInfo, err := contractService.GetContractDetailByAddContractReq(req, sysUser.AdminId, productId, sysUser.RealName)
  2182. if err != nil {
  2183. br.Msg = "获取合同数据失败!"
  2184. br.ErrMsg = "获取合同数据失败,Err:" + err.Error()
  2185. return
  2186. }
  2187. // CMR8.8-合并权益主客观
  2188. if contractInfo.ProductId == 2 {
  2189. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2190. contractInfo.Service = mergeList
  2191. }
  2192. contractHtml, err := services.GetHtmlByContractDetail(contractInfo, "html")
  2193. if err != nil {
  2194. br.Msg = "生成预览合同失败!"
  2195. br.ErrMsg = "生成预览合同失败,Err:" + err.Error()
  2196. return
  2197. }
  2198. br.Ret = 200
  2199. br.Success = true
  2200. br.Msg = "获取成功"
  2201. br.Data = response.ContractPreviewResp{
  2202. Html: contractHtml,
  2203. }
  2204. }
  2205. // PreviewByContractId
  2206. // @Title 预览合同
  2207. // @Description 预览合同接口
  2208. // @Param ContractId query int true "合同id"
  2209. // @Success 200 {object} response.ContractPreviewResp
  2210. // @router /previewByContractId [get]
  2211. func (this *ContractController) PreviewByContractId() {
  2212. br := new(models.BaseResponse).Init()
  2213. defer func() {
  2214. this.Data["json"] = br
  2215. this.ServeJSON()
  2216. }()
  2217. sysUser := this.SysUser
  2218. if sysUser == nil {
  2219. br.Msg = "请登录"
  2220. br.ErrMsg = "请登录,SysUser Is Empty"
  2221. br.Ret = 408
  2222. return
  2223. }
  2224. contractId, _ := this.GetInt("ContractId")
  2225. //合同id
  2226. if contractId <= 0 {
  2227. br.Msg = "合同id必传!"
  2228. br.ErrMsg = "合同id必传"
  2229. return
  2230. }
  2231. contractInfo, err := contractService.GetContractDetail(contractId)
  2232. if err != nil {
  2233. br.Msg = "获取合同详情失败!"
  2234. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2235. return
  2236. }
  2237. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2238. maxPrice := float64(1000000000000)
  2239. if contractInfo.OriginalPrice >= maxPrice || contractInfo.Price >= maxPrice {
  2240. br.Msg = "合同金额过大, 请核验金额"
  2241. return
  2242. }
  2243. // CMR8.8-合并权益主客观
  2244. if contractInfo.ProductId == 2 {
  2245. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2246. contractInfo.Service = mergeList
  2247. }
  2248. contractHtml, err := services.GetHtmlByContractDetail(contractInfo, "html")
  2249. if err != nil {
  2250. br.Msg = "生成预览合同失败!"
  2251. br.ErrMsg = "生成预览合同失败,Err:" + err.Error()
  2252. return
  2253. }
  2254. br.Ret = 200
  2255. br.Success = true
  2256. br.Msg = "获取成功"
  2257. br.Data = response.ContractPreviewResp{
  2258. Html: contractHtml,
  2259. }
  2260. }
  2261. // DownLoadTmpContract
  2262. // @Title 下载临时合同
  2263. // @Description 下载临时合同接口
  2264. // @Param ContractId query int true "合同id"
  2265. // @router /downLoad/tmpContract [get]
  2266. func (this *ContractController) DownLoadTmpContract() {
  2267. br := new(models.BaseResponse).Init()
  2268. defer func() {
  2269. this.Data["json"] = br
  2270. this.ServeJSON()
  2271. }()
  2272. sysUser := this.SysUser
  2273. if sysUser == nil {
  2274. br.Msg = "请登录"
  2275. br.ErrMsg = "请登录,SysUser Is Empty"
  2276. br.Ret = 408
  2277. return
  2278. }
  2279. //合同类型、产品类型、合同状态、更新时间、所选销售
  2280. //关键字:合同编号、客户名称,社会信用码
  2281. contractId, _ := this.GetInt("ContractId")
  2282. //合同id
  2283. if contractId <= 0 {
  2284. br.Msg = "合同id必传!"
  2285. br.ErrMsg = "合同id必传"
  2286. return
  2287. }
  2288. contractInfo, err := contractService.GetContractDetail(contractId)
  2289. if err != nil {
  2290. br.Msg = "获取合同详情失败!"
  2291. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2292. return
  2293. }
  2294. // 限制一下合同金额, 目前只支持人民币转大写只支持到千亿, 虽然生产使用中不可能到这么大, 但还是加上吧 =_=!
  2295. maxPrice := float64(1000000000000)
  2296. if contractInfo.OriginalPrice >= maxPrice || contractInfo.Price >= maxPrice {
  2297. br.Msg = "合同金额过大, 请核验金额"
  2298. return
  2299. }
  2300. // CMR8.8-合并权益主客观
  2301. if contractInfo.ProductId == 2 {
  2302. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2303. contractInfo.Service = mergeList
  2304. }
  2305. //合同下载权限校验
  2306. if (sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_SELLER || sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_SELLER) && contractInfo.SellerId != sysUser.AdminId {
  2307. br.Msg = "合同异常!"
  2308. br.ErrMsg = "合同异常"
  2309. return
  2310. }
  2311. //contractHtml, tmpErr := services.GetHtmlByContractDetail(contractInfo, "pdf")
  2312. //if tmpErr != nil {
  2313. // err = tmpErr
  2314. // return
  2315. //}
  2316. //生成pdf
  2317. //pdfPath := fmt.Sprint("./static/word/系统生成合同_", contractId, ".pdf")
  2318. //tmpErr = services.Html2Pdf(contractHtml, pdfPath)
  2319. //if tmpErr != nil {
  2320. // err = tmpErr
  2321. // return
  2322. //}
  2323. //defer func() {
  2324. // os.Remove(pdfPath)
  2325. //}()
  2326. //downloadFileName := "合同草稿_" + contractInfo.ContractCode + ".pdf"
  2327. wordPath := fmt.Sprint("./static/word/系统生成合同_", contractId, ".docx")
  2328. tmpErr := services.GenerateWordV2(contractInfo, wordPath)
  2329. if tmpErr != nil {
  2330. err = tmpErr
  2331. return
  2332. }
  2333. defer func() {
  2334. os.Remove(wordPath)
  2335. }()
  2336. downloadFileName := contractInfo.CompanyName + "_" + contractInfo.ContractCode + "(草稿).docx"
  2337. this.Ctx.Output.Download(wordPath, downloadFileName)
  2338. br.Ret = 200
  2339. br.Success = true
  2340. br.Msg = "下载成功"
  2341. }
  2342. // getServiceTemplateList 获取系统服务列表
  2343. func getServiceTemplateList(productId int) (list []*contract.ContractServiceTemplateMapItems, err error) {
  2344. list, err = contract.GetContractServiceTemplateMapByProductId(productId)
  2345. if err != nil {
  2346. return
  2347. }
  2348. for i := 0; i < len(list); i++ {
  2349. item := list[i]
  2350. tmpList, tmpErr := contract.GetContractServiceTemplateMapByParentId(item.ServiceTemplateId)
  2351. if tmpErr != nil {
  2352. err = tmpErr
  2353. return
  2354. }
  2355. for j := 0; j < len(tmpList); j++ {
  2356. detail, tmpErr := contract.GetContractServiceDetailByTemplateId(tmpList[j].ServiceTemplateId)
  2357. if tmpErr != nil {
  2358. err = tmpErr
  2359. return
  2360. }
  2361. tmpList[j].Detail = detail
  2362. }
  2363. detail, tmpErr := contract.GetContractServiceDetailByTemplateId(list[i].ServiceTemplateId)
  2364. if tmpErr != nil {
  2365. err = tmpErr
  2366. return
  2367. }
  2368. list[i].Detail = detail
  2369. list[i].Children = tmpList
  2370. }
  2371. return
  2372. }
  2373. // BusinessContractCompanyList
  2374. // @Title 业务合同客户名称列表
  2375. // @Description 业务合同客户名称列表接口
  2376. // @Param Keyword query string true "搜索关键字"
  2377. // @Success 200 {object} []string
  2378. // @router /business_contract/company_name/list/ [get]
  2379. func (this *ContractController) BusinessContractCompanyList() {
  2380. br := new(models.BaseResponse).Init()
  2381. defer func() {
  2382. this.Data["json"] = br
  2383. this.ServeJSON()
  2384. }()
  2385. sysUser := this.SysUser
  2386. if sysUser == nil {
  2387. br.Msg = "请登录"
  2388. br.ErrMsg = "请登录,SysUser Is Empty"
  2389. br.Ret = 408
  2390. return
  2391. }
  2392. condition := ""
  2393. pars := make([]interface{}, 0)
  2394. condition += ` AND a.contract_business_type ="业务合同" AND a.status ="已签回" `
  2395. //如果不是超管或者合规,那么只能查看自己的合同
  2396. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  2397. // condition += ` AND a.product_id = 1 `
  2398. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  2399. // condition += ` AND product_id = 2 `
  2400. //} else {
  2401. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  2402. // condition += ` AND a.seller_id = ? `
  2403. // pars = append(pars, sysUser.AdminId)
  2404. // }
  2405. //}
  2406. condition += ` AND a.seller_id = ? `
  2407. pars = append(pars, sysUser.AdminId)
  2408. //关键字
  2409. keyword := this.GetString("Keyword")
  2410. keyword = strings.TrimSpace(keyword)
  2411. if keyword == "" {
  2412. br.Msg = "请输入客户名称"
  2413. br.ErrMsg = "请输入客户名称"
  2414. return
  2415. }
  2416. kw := fmt.Sprint("%", keyword, "%")
  2417. condition += ` AND (contract_code LIKE ? OR company_name LIKE ? OR credit_code LIKE ? ) `
  2418. pars = append(pars, kw, kw, kw)
  2419. list, err := contract.GetContractCompanyNameList(condition, pars)
  2420. if err != nil {
  2421. br.Msg = "获取合同列表失败!"
  2422. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  2423. return
  2424. }
  2425. companyNameList := make([]string, 0)
  2426. for _, v := range list {
  2427. companyNameList = append(companyNameList, v.CompanyName)
  2428. }
  2429. // 13.6正式共享客户的处理
  2430. {
  2431. // 通过关键词获取分配给当前销售的正式共享客户
  2432. cond := ` AND share_seller_id = ? AND is_share = 1 AND company_name LIKE ?`
  2433. pars := make([]interface{}, 0)
  2434. pars = append(pars, sysUser.AdminId, kw)
  2435. shares, e := company.GetCompanyListByCondition(cond, pars, []string{"company_id", "company_name"}, "")
  2436. if e != nil {
  2437. br.Msg = "获取失败"
  2438. br.ErrMsg = "获取共享客户信息失败, Err: " + e.Error()
  2439. return
  2440. }
  2441. for _, v := range shares {
  2442. if !utils.InArrayByStr(companyNameList, v.CompanyName) {
  2443. companyNameList = append(companyNameList, v.CompanyName)
  2444. }
  2445. }
  2446. }
  2447. br.Ret = 200
  2448. br.Success = true
  2449. br.Msg = "获取成功"
  2450. br.Data = companyNameList
  2451. }
  2452. // BusinessContractListByCompanyName
  2453. // @Title 根据客户名称获取所有已签回的业务合同列表
  2454. // @Description 根据客户名称获取所有已签回的业务合同列表接口
  2455. // @Param CompanyName query string false "客户名称"
  2456. // @Success 200 {object} response.ContractListResp
  2457. // @router /business_contract/list/ [get]
  2458. func (this *ContractController) BusinessContractListByCompanyName() {
  2459. br := new(models.BaseResponse).Init()
  2460. defer func() {
  2461. this.Data["json"] = br
  2462. this.ServeJSON()
  2463. }()
  2464. sysUser := this.SysUser
  2465. if sysUser == nil {
  2466. br.Msg = "请登录"
  2467. br.ErrMsg = "请登录,SysUser Is Empty"
  2468. br.Ret = 408
  2469. return
  2470. }
  2471. condition := ""
  2472. pars := make([]interface{}, 0)
  2473. condition += ` AND a.contract_business_type ="业务合同" AND a.status ="已签回" and a.price>a.paid_price `
  2474. //如果不是超管或者合规,那么只能查看自己的合同
  2475. //if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_FICC_ADMIN {
  2476. // condition += ` AND a.product_id = 1 `
  2477. //} else if sysUser.RoleTypeCode == utils.ROLE_TYPE_CODE_RAI_ADMIN {
  2478. // condition += ` AND product_id = 2 `
  2479. //} else {
  2480. // if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE && sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_ADMIN {
  2481. // condition += ` AND a.seller_id = ? `
  2482. // pars = append(pars, sysUser.AdminId)
  2483. // }
  2484. //}
  2485. //condition += ` AND a.seller_id = ? `
  2486. //pars = append(pars, sysUser.AdminId)
  2487. // 13.6正式共享客户-可以选择分配给自己的共享客户下的非自己创建的合同
  2488. joinStr := ` LEFT JOIN company AS b ON a.company_name = b.company_name `
  2489. condition += ` AND (a.seller_id = ? OR (b.is_share = 1 AND b.share_seller_id = ?))`
  2490. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  2491. //关键字
  2492. companyName := this.GetString("CompanyName")
  2493. if companyName == "" {
  2494. br.Msg = "请输入客户名称"
  2495. br.ErrMsg = "请输入客户名称"
  2496. return
  2497. }
  2498. condition += ` AND a.company_name = ? `
  2499. pars = append(pars, companyName)
  2500. pageSize, _ := this.GetInt("PageSize")
  2501. currentIndex, _ := this.GetInt("CurrentIndex")
  2502. var startSize int
  2503. if pageSize <= 0 {
  2504. pageSize = utils.PageSize20
  2505. }
  2506. if currentIndex <= 0 {
  2507. currentIndex = 1
  2508. }
  2509. startSize = paging.StartIndex(currentIndex, pageSize)
  2510. total, err := contract.GetContractListCount(condition, joinStr, pars)
  2511. if err != nil {
  2512. br.Msg = "获取失败"
  2513. br.ErrMsg = "获取数据总数失败,Err:" + err.Error()
  2514. return
  2515. }
  2516. list, err := contract.GetContractList(condition, joinStr, pars, startSize, pageSize)
  2517. if err != nil {
  2518. br.Msg = "获取合同列表失败!"
  2519. br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
  2520. return
  2521. }
  2522. //获取合同套餐服务列表
  2523. serviceTemplateList, err := contract.GetAllContractServiceTemplateList()
  2524. if err != nil {
  2525. br.Msg = "获取合同套餐服务列表失败!"
  2526. br.ErrMsg = "获取合同套餐服务列表失败,Err:" + err.Error()
  2527. return
  2528. }
  2529. templateTitleMap := make(map[int]string)
  2530. for _, serviceTemplate := range serviceTemplateList {
  2531. templateTitleMap[serviceTemplate.ServiceTemplateId] = serviceTemplate.Title
  2532. }
  2533. //业务合同的关联合同id集合
  2534. businessContractIdList := make([]string, 0)
  2535. //代付合同的关联合同id集合
  2536. paymentOnBehalfContractIdList := make([]string, 0)
  2537. //基础合同的关联合同数据
  2538. contractRelationMap := make(map[int][]*contract.RelationContractList)
  2539. for _, v := range list {
  2540. switch v.ContractBusinessType {
  2541. case "业务合同":
  2542. businessContractIdList = append(businessContractIdList, fmt.Sprint(v.ContractId))
  2543. case "代付合同":
  2544. paymentOnBehalfContractIdList = append(paymentOnBehalfContractIdList, fmt.Sprint(v.ContractId))
  2545. }
  2546. }
  2547. //业务合同的关联合同
  2548. if len(businessContractIdList) > 0 {
  2549. contractIdStr := strings.Join(businessContractIdList, ",")
  2550. businessContractList, err := contract.GetContractRelationListByRelationContractIds(contractIdStr)
  2551. if err != nil {
  2552. br.Msg = "获取失败"
  2553. br.ErrMsg = "获取业务关联合同失败,Err:" + err.Error()
  2554. return
  2555. }
  2556. for _, v := range businessContractList {
  2557. if _, ok := contractRelationMap[v.ContractId]; ok {
  2558. contractRelationMap[v.ContractId] = append(contractRelationMap[v.ContractId], v)
  2559. } else {
  2560. tmpList := make([]*contract.RelationContractList, 0)
  2561. contractRelationMap[v.ContractId] = append(tmpList, v)
  2562. }
  2563. }
  2564. }
  2565. //代付合同的关联合同
  2566. if len(paymentOnBehalfContractIdList) > 0 {
  2567. paymentOnBehalfContractIdStr := strings.Join(paymentOnBehalfContractIdList, ",")
  2568. paymentOnBehalfContractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(paymentOnBehalfContractIdStr)
  2569. if err != nil {
  2570. br.Msg = "获取失败"
  2571. br.ErrMsg = "获取代付关联合同失败,Err:" + err.Error()
  2572. return
  2573. }
  2574. for _, v := range paymentOnBehalfContractList {
  2575. if _, ok := contractRelationMap[v.PaymentOnBehalfContractId]; ok {
  2576. contractRelationMap[v.PaymentOnBehalfContractId] = append(contractRelationMap[v.PaymentOnBehalfContractId], v)
  2577. } else {
  2578. tmpList := make([]*contract.RelationContractList, 0)
  2579. contractRelationMap[v.PaymentOnBehalfContractId] = append(tmpList, v)
  2580. }
  2581. }
  2582. }
  2583. for i := 0; i < len(list); i++ {
  2584. //合同操作权限
  2585. var opButton contract.ContractOpButton
  2586. item := list[i]
  2587. list[i].StartDateStr = item.StartDate.Format(utils.FormatDate)
  2588. list[i].EndDateStr = item.EndDate.Format(utils.FormatDate)
  2589. list[i].CreateTimeStr = item.CreateTime.Format(utils.FormatDateTime)
  2590. list[i].ModifyTimeStr = item.ModifyTime.Format(utils.FormatDateTime)
  2591. list[i].ApproveTimeStr = item.ApproveTime.Format(utils.FormatDateTime)
  2592. list[i].InvalidTimeStr = item.InvalidTime.Format(utils.FormatDateTime)
  2593. list[i].CheckBackFileTimeStr = item.CheckBackFileTime.Format(utils.FormatDateTime)
  2594. list[i].RescindTimeStr = item.RescindTime.Format(utils.FormatDateTime)
  2595. //服务列表
  2596. serviceList, err := contract.GetContractServiceAndDetailList(item.ContractId)
  2597. if err != nil {
  2598. br.Msg = "获取失败"
  2599. br.ErrMsg = "查找合同服务异常,Err:" + err.Error()
  2600. return
  2601. }
  2602. for i := 0; len(serviceList) > i; i++ {
  2603. if serviceList[i].HasDetail == "是" {
  2604. list, detailErr := contract.GetContractServiceDetailListByServiceId(serviceList[i].ContractServiceId)
  2605. if detailErr != nil {
  2606. br.Msg = "获取失败"
  2607. br.ErrMsg = "查找合同服务详情异常,Err:" + err.Error()
  2608. return
  2609. }
  2610. //套餐名称
  2611. serviceList[i].DetailList = list
  2612. if title, ok := templateTitleMap[serviceList[i].ServiceTemplateId]; ok {
  2613. serviceList[i].Title = title
  2614. }
  2615. }
  2616. }
  2617. list[i].Service = serviceList
  2618. if item.Status == "待审批" {
  2619. approvalInfo, tmpErr := contract.GetLastContractApprovalByContractId(item.ContractId, "contract")
  2620. if tmpErr != nil {
  2621. //err= tmpErr
  2622. //return
  2623. continue
  2624. }
  2625. //如果合同归属是当前账号,且处于第一级审批
  2626. if item.SellerId == sysUser.AdminId && approvalInfo.CurrNodeId == approvalInfo.StartNodeId {
  2627. opButton.Cancel = true
  2628. }
  2629. }
  2630. list[i].OpButton = opButton
  2631. if relationContractList, ok := contractRelationMap[item.ContractId]; ok {
  2632. list[i].RelationContractList = relationContractList
  2633. }
  2634. }
  2635. page := paging.GetPaging(currentIndex, pageSize, total)
  2636. br.Ret = 200
  2637. br.Success = true
  2638. br.Msg = "获取成功"
  2639. br.Data = response.ContractListResp{
  2640. List: list,
  2641. Paging: page,
  2642. }
  2643. return
  2644. }
  2645. // UpdateFile
  2646. // @Title 更新合同附件(用于非标)
  2647. // @Description 更新合同附件接口
  2648. // @Param request body request.UploadFileReq true "type json string"
  2649. // @Success Ret=200 上传成功
  2650. // @router /update_file [post]
  2651. func (this *ContractController) UpdateFile() {
  2652. br := new(models.BaseResponse).Init()
  2653. defer func() {
  2654. this.Data["json"] = br
  2655. this.ServeJSON()
  2656. }()
  2657. sysUser := this.SysUser
  2658. if sysUser == nil {
  2659. br.Msg = "请登录"
  2660. br.ErrMsg = "请登录,SysUser Is Empty"
  2661. br.Ret = 408
  2662. return
  2663. }
  2664. //var req request.UploadFileReq
  2665. //err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
  2666. //if err != nil {
  2667. // br.Msg = "参数解析异常!"
  2668. // br.ErrMsg = "参数解析失败,Err:" + err.Error()
  2669. // return
  2670. //}
  2671. contractIdStr := this.Ctx.Request.Form.Get("ContractId")
  2672. if contractIdStr == "" {
  2673. br.Msg = "合同ID必传!"
  2674. br.ErrMsg = "合同ID必传,Err"
  2675. return
  2676. }
  2677. contractId, err := strconv.Atoi(contractIdStr)
  2678. if err != nil {
  2679. br.Msg = "合同ID异常!"
  2680. br.ErrMsg = "合同ID异常,Err:" + err.Error()
  2681. return
  2682. }
  2683. fileMulti, h, err := this.GetFile("file")
  2684. if err != nil {
  2685. br.Msg = "获取资源信息失败"
  2686. br.ErrMsg = "获取资源信息失败,Err:" + err.Error()
  2687. return
  2688. }
  2689. //合同编号
  2690. if contractId <= 0 {
  2691. br.Msg = "请传入合同编号!"
  2692. br.ErrMsg = "请传入合同编号"
  2693. return
  2694. }
  2695. ext := path.Ext(h.Filename)
  2696. err = contractService.UploadFile(contractId, ext, fileMulti, this.SysUser)
  2697. br.Ret = 200
  2698. br.Success = true
  2699. br.Msg = "上传成功"
  2700. }
  2701. // GetListBySeal
  2702. // @Title 获取用印所需合同列表
  2703. // @Description 获取用印所需合同列表
  2704. // @Param Keyword query string false "搜索关键字"
  2705. // @Success 200 {object} response.ContractListBySealResp
  2706. // @router /getListBySeal [get]
  2707. func (this *ContractController) GetListBySeal() {
  2708. br := new(models.BaseResponse).Init()
  2709. defer func() {
  2710. this.Data["json"] = br
  2711. this.ServeJSON()
  2712. }()
  2713. sysUser := this.SysUser
  2714. if sysUser == nil {
  2715. br.Msg = "请登录"
  2716. br.ErrMsg = "请登录,SysUser Is Empty"
  2717. br.Ret = 408
  2718. return
  2719. }
  2720. keyword := this.GetString("Keyword")
  2721. condition := ` AND a.status = "已审批"`
  2722. pars := make([]interface{}, 0)
  2723. if keyword != "" {
  2724. kw := fmt.Sprint("%", keyword, "%")
  2725. condition += ` AND (a.company_name LIKE ?) `
  2726. pars = append(pars, kw)
  2727. }
  2728. // 13.6正式共享客户
  2729. joinStr := ` LEFT JOIN company AS b ON a.company_name = b.company_name `
  2730. // 非合规角色可查看自己的合同或是自己的共享客户下的非自己创建的合同
  2731. if sysUser.RoleTypeCode != utils.ROLE_TYPE_CODE_COMPLIANCE {
  2732. condition += ` AND (a.seller_id = ? OR (b.is_share = 1 AND b.share_seller_id = ?))`
  2733. pars = append(pars, sysUser.AdminId, sysUser.AdminId)
  2734. }
  2735. list, e := contract.GetContractList(condition, joinStr, pars, 0, 100)
  2736. if e != nil {
  2737. br.Msg = "获取失败"
  2738. br.ErrMsg = "获取合同列表失败, Err:" + e.Error()
  2739. return
  2740. }
  2741. //list, err := contract.GetSearchListBySeal(condition, pars)
  2742. //if err != nil {
  2743. // br.Msg = "获取失败"
  2744. // br.ErrMsg = "获取数据失败,Err:" + err.Error()
  2745. // return
  2746. //}
  2747. newList := make([]response.ContractListBySealResp, 0)
  2748. if len(list) > 0 {
  2749. // 代付合同查询代付用户名称
  2750. contractIdSlice := make([]string, 0)
  2751. for i := 0; i < len(list); i++ {
  2752. if list[i].ContractBusinessType == "代付合同" {
  2753. contractIdSlice = append(contractIdSlice, strconv.Itoa(list[i].ContractId))
  2754. }
  2755. }
  2756. userCompanyNameMap := make(map[int]string, 0)
  2757. if len(contractIdSlice) > 0 {
  2758. contractIdStr := strings.Join(contractIdSlice, ",")
  2759. contractList, err := contract.GetContractRelationListByPaymentOnBehalfContractIds(contractIdStr)
  2760. if err != nil {
  2761. br.Msg = "获取失败"
  2762. br.ErrMsg = "获取代付合同对应的合同数据失败,Err:" + err.Error()
  2763. return
  2764. }
  2765. for i := 0; i < len(contractList); i++ {
  2766. userCompanyNameMap[contractList[i].PaymentOnBehalfContractId] = contractList[i].CompanyName
  2767. }
  2768. }
  2769. for _, item := range list {
  2770. var tempItem response.ContractListBySealResp
  2771. tempItem.ContractId = item.ContractId
  2772. tempItem.ContractCode = item.ContractCode
  2773. tempItem.CompanyName = item.CompanyName
  2774. tempItem.FileUrl = item.FileUrl
  2775. tempItem.Price = item.Price
  2776. tempItem.CreditCode = item.CreditCode
  2777. tempItem.ContractBusinessType = item.ContractBusinessType
  2778. if item.ContractBusinessType == "代付合同" {
  2779. tempItem.ContractType = "代付合同"
  2780. tempItem.UserCompanyName = userCompanyNameMap[item.ContractId]
  2781. } else {
  2782. tempItem.ContractType = item.ContractType
  2783. }
  2784. newList = append(newList, tempItem)
  2785. }
  2786. }
  2787. br.Ret = 200
  2788. br.Success = true
  2789. br.Msg = "获取成功"
  2790. br.Data = newList
  2791. }
  2792. // DetailMerge
  2793. // @Title 获取合同详情-有套餐合并,用于审批单回显等
  2794. // @Description 获取合同详情接口
  2795. // @Param ContractId query int true "合同id"
  2796. // @Success 200 {object} contract.ContractDetail
  2797. // @router /detail/merge [get]
  2798. func (this *ContractController) DetailMerge() {
  2799. br := new(models.BaseResponse).Init()
  2800. defer func() {
  2801. this.Data["json"] = br
  2802. this.ServeJSON()
  2803. }()
  2804. sysUser := this.SysUser
  2805. if sysUser == nil {
  2806. br.Msg = "请登录"
  2807. br.ErrMsg = "请登录,SysUser Is Empty"
  2808. br.Ret = 408
  2809. return
  2810. }
  2811. //合同类型、产品类型、合同状态、更新时间、所选销售
  2812. //关键字:合同编号、客户名称,社会信用码
  2813. contractId, _ := this.GetInt("ContractId")
  2814. //合同id
  2815. if contractId <= 0 {
  2816. br.Msg = "合同id必传!"
  2817. br.ErrMsg = "合同id必传"
  2818. return
  2819. }
  2820. contractInfo, err := contractService.GetContractDetail(contractId)
  2821. if err != nil {
  2822. br.Msg = "获取合同详情失败!"
  2823. br.ErrMsg = "获取合同详情失败,ERR:" + err.Error()
  2824. return
  2825. }
  2826. // CRM8.8-权益主客观合并
  2827. if contractInfo.ProductId == 2 {
  2828. mergeList := contractService.EquityMergeSubjectAndObject(contractInfo.Service)
  2829. contractInfo.Service = mergeList
  2830. // 关联合同同样合并
  2831. for i, v := range contractInfo.RelationContractDetailList {
  2832. relationMergeList := contractService.EquityMergeSubjectAndObject(v.Service)
  2833. contractInfo.RelationContractDetailList[i].Service = relationMergeList
  2834. }
  2835. }
  2836. contractInfo.StartDateStr = contractInfo.StartDate.Format(utils.FormatDate)
  2837. contractInfo.EndDateStr = contractInfo.EndDate.Format(utils.FormatDate)
  2838. contractInfo.ModifyTimeStr = contractInfo.ModifyTime.Format(utils.FormatDateTime)
  2839. contractInfo.CreateTimeStr = contractInfo.CreateTime.Format(utils.FormatDateTime)
  2840. //合同中包含的产品权限
  2841. isNeedPermission := false //是否需要列出来套餐中的详细权限
  2842. for _, service := range contractInfo.Service {
  2843. if service.ServiceTemplateId == 2 { //小套餐才需要列出来详细的权限
  2844. isNeedPermission = true
  2845. }
  2846. }
  2847. var permissionLookListResp = make([]*contract.PermissionLookList, 0)
  2848. if isNeedPermission {
  2849. checkPermissionMap, tErr := contractService.GetServicePermissionMap(contractInfo.Service)
  2850. if tErr == nil {
  2851. permissionLookListMap := make(map[int][]*contract.PermissionLookItem)
  2852. var permissionIds []string
  2853. for _, v := range checkPermissionMap {
  2854. permissionIds = append(permissionIds, strconv.Itoa(v))
  2855. }
  2856. permissionIdStr := strings.Join(permissionIds, ",")
  2857. charPermissions, tErr1 := company.GetPermissionLookItemsSandO(permissionIdStr)
  2858. if tErr1 == nil {
  2859. for _, v := range charPermissions {
  2860. tmp := new(contract.PermissionLookItem)
  2861. tmp.ClassifyName = v.ClassifyName
  2862. tmp.PermissionName = v.PermissionName
  2863. tmp.ChartPermissionId = v.ChartPermissionId
  2864. permissionLookListMap[v.ParentId] = append(permissionLookListMap[v.ParentId], tmp)
  2865. }
  2866. classifyNameList, tErr2 := company.GetChartPermissionFirst()
  2867. if tErr2 == nil {
  2868. for _, v := range classifyNameList {
  2869. if perList, ok := permissionLookListMap[v.ChartPermissionId]; ok {
  2870. permissionLookListRespItem := &contract.PermissionLookList{
  2871. ClassifyName: v.ClassifyName,
  2872. Items: perList,
  2873. }
  2874. permissionLookListResp = append(permissionLookListResp, permissionLookListRespItem)
  2875. }
  2876. }
  2877. }
  2878. }
  2879. }
  2880. }
  2881. contractInfo.PermissionLookList = permissionLookListResp
  2882. br.Ret = 200
  2883. br.Success = true
  2884. br.Msg = "获取成功"
  2885. br.Data = contractInfo
  2886. }