oilcatalog.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. package oilsupplier
  2. import (
  3. "dashoo.cn/backend/api/business/oilsupplier/oilcatalog"
  4. "dashoo.cn/backend/api/business/organize"
  5. . "dashoo.cn/backend/api/controllers"
  6. "dashoo.cn/business2/permission"
  7. "dashoo.cn/utils"
  8. "encoding/json"
  9. "fmt"
  10. . "github.com/linxGnu/goseaweedfs"
  11. "github.com/tealeg/xlsx"
  12. "log"
  13. "os"
  14. "strconv"
  15. "strings"
  16. "time"
  17. )
  18. type OilCatalogController struct {
  19. BaseController
  20. }
  21. // @Title 获取列表
  22. // @Description get user by token
  23. // @Success 200 {object} []suppliercert.OilSupplierCert
  24. // @router /list [get]
  25. func (this *OilCatalogController) GetEntityList() {
  26. //获取分页信息
  27. page := this.GetPageInfoForm()
  28. where := " 1=1 "
  29. orderBy := "Id"
  30. asc := false
  31. Order := this.GetString("Order")
  32. Prop := this.GetString("Prop")
  33. if Order != "" && Prop != "" {
  34. orderBy = Prop
  35. if Order == "asc" {
  36. asc = true
  37. }
  38. }
  39. SubId := this.GetString("SubId")
  40. CompanyName := this.GetString("CompanyName")
  41. Business := this.GetString("Business")
  42. catalogType := this.GetString("CatalogType")
  43. Status := this.GetString("Status")
  44. CreateOn := this.GetString("CreateOn")
  45. Year := this.GetString("Year")
  46. if SubId != "" {
  47. where = where + " and SubId=" + SubId
  48. }
  49. if catalogType != "" {
  50. where = where + " and CatalogType=" + catalogType
  51. }
  52. if CompanyName != "" {
  53. where = where + " and CompanyName like '%" + CompanyName + "%' "
  54. }
  55. if Business != "" {
  56. where = where + " and Business like '%" + Business + "%' "
  57. }
  58. if Status != "" {
  59. where = where + " and Status = '" + Status + "'"
  60. }
  61. if Year != "" {
  62. where = where + " and CreateOn >= '" + Year + "-01-01 00:00:00' and CreateOn <='" + Year + "-12-31 00:00:00'"
  63. }
  64. if CreateOn != "" {
  65. dates := strings.Split(CreateOn, ",")
  66. if len(dates) == 2 {
  67. minDate := dates[0]
  68. maxDate := dates[1]
  69. where = where + " and ValidityTo>='" + minDate + "' and ValidityTo<='" + maxDate + "'"
  70. }
  71. }
  72. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  73. var list []oilcatalog.OilCatalog
  74. total := svc.GetPagingEntitiesWithOrderBytbl("", page.CurrentPage, page.Size, orderBy, asc, &list, where)
  75. var datainfo DataInfo
  76. datainfo.Items = list
  77. datainfo.CurrentItemCount = total
  78. datainfo.PageIndex = page.CurrentPage
  79. datainfo.ItemsPerPage = page.Size
  80. this.Data["json"] = &datainfo
  81. this.ServeJSON()
  82. }
  83. // @Title 添加
  84. // @Description 新增
  85. // @Success 200 {object} controllers.Request
  86. // @router /add [post]
  87. func (this *OilCatalogController) AddEntity() {
  88. var model oilcatalog.OilCatalog
  89. var jsonBlob = this.Ctx.Input.RequestBody
  90. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  91. json.Unmarshal(jsonBlob, &model)
  92. // model.Status = "0" // 由前端分不同页面(企管法规处2和普通用户2)传值创建
  93. model.CreateOn = time.Now()
  94. model.CreateBy = this.User.Realname
  95. model.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
  96. _, err := svc.InsertEntityBytbl(OilCatalogName, &model)
  97. var errinfo ErrorDataInfo
  98. if err == nil {
  99. //新增
  100. errinfo.Message = "添加成功!"
  101. errinfo.Code = 0
  102. errinfo.Item = model.Id
  103. this.Data["json"] = &errinfo
  104. this.ServeJSON()
  105. } else {
  106. errinfo.Message = "添加失败!" + utils.AlertProcess(err.Error())
  107. errinfo.Code = -1
  108. this.Data["json"] = &errinfo
  109. this.ServeJSON()
  110. }
  111. }
  112. // @Title 删除单条信息
  113. // @Description
  114. // @Success 200 {object} ErrorInfo
  115. // @Failure 403 :id 为空
  116. // @router /delete/:Id [delete]
  117. func (this *OilCatalogController) DeleteEntity() {
  118. Id := this.Ctx.Input.Param(":Id")
  119. var errinfo ErrorInfo
  120. if Id == "" {
  121. errinfo.Message = "操作失败!请求信息不完整"
  122. errinfo.Code = -2
  123. this.Data["json"] = &errinfo
  124. this.ServeJSON()
  125. return
  126. }
  127. var model oilcatalog.OilCatalog
  128. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  129. err := svc.DeleteEntityById(Id, &model)
  130. if err == nil {
  131. errinfo.Message = "删除成功"
  132. errinfo.Code = 0
  133. this.Data["json"] = &errinfo
  134. this.ServeJSON()
  135. } else {
  136. errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
  137. errinfo.Code = -1
  138. this.Data["json"] = &errinfo
  139. this.ServeJSON()
  140. }
  141. }
  142. // @Title 修改实体
  143. // @Description 修改实体
  144. // @Success 200 {object} controllers.Request
  145. // @router /update/:id [post]
  146. func (this *OilCatalogController) UpdateEntity() {
  147. id := this.Ctx.Input.Param(":id")
  148. var errinfo ErrorInfo
  149. if id == "" {
  150. errinfo.Message = "操作失败!请求信息不完整"
  151. errinfo.Code = -2
  152. this.Data["json"] = &errinfo
  153. this.ServeJSON()
  154. return
  155. }
  156. var model oilcatalog.OilCatalog
  157. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  158. var jsonBlob = this.Ctx.Input.RequestBody
  159. json.Unmarshal(jsonBlob, &model)
  160. model.ModifiedOn = time.Now()
  161. model.ModifiedBy = this.User.Realname
  162. model.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
  163. cols := []string{
  164. "RegCapital",
  165. "USCCode",
  166. "Address",
  167. "LegalPerson",
  168. "RecordScope",
  169. "IDCode",
  170. "DutyDept",
  171. "Dept",
  172. "CatalogType",
  173. "OrderNo",
  174. "CompanyName",
  175. "Business",
  176. "ValidityFrom",
  177. "ValidityTo",
  178. "Remark",
  179. }
  180. err := svc.UpdateEntityBytbl(OilCatalogName, id, &model, cols)
  181. if err == nil {
  182. errinfo.Message = "修改成功!"
  183. errinfo.Code = 0
  184. this.Data["json"] = &errinfo
  185. this.ServeJSON()
  186. } else {
  187. errinfo.Message = "修改失败!" + utils.AlertProcess(err.Error())
  188. errinfo.Code = -1
  189. this.Data["json"] = &errinfo
  190. this.ServeJSON()
  191. }
  192. }
  193. // @Title get 导出ex
  194. // @Description get SampleType by token
  195. // @Success 200 {object} sampletype.SampleType
  196. // @router /exportexcelall [get]
  197. func (this *OilCatalogController) ExportExcelAll() {
  198. CardTitle := this.GetString("CardTitle")
  199. CatalogType := this.GetString("CatalogType")
  200. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  201. var list []oilcatalog.OilCatalog
  202. where := "CatalogType=" + CatalogType
  203. svc.GetEntities(&list, where)
  204. fileTitle := "大港油田" + CardTitle + "目录"
  205. showColumnArr := "序号,企业名称,业务范围,有效期起,有效期止,备注"
  206. f := xlsx.NewFile()
  207. sheet, _ := f.AddSheet(fileTitle)
  208. cellName := strings.Split(showColumnArr, ",")
  209. row := sheet.AddRow()
  210. row.WriteSlice(&cellName, -1)
  211. for idx, item := range list {
  212. dataString := strconv.Itoa(idx+1) + "," + item.CompanyName + "," + svc.TrimSpaceForString(item.Business) + "," + utils.ToStr(item.ValidityFrom.Format("2006-01-02")) + "," +
  213. utils.ToStr(item.ValidityTo.Format("2006-01-02")) + "," + item.Remark
  214. cellName := strings.Split(dataString, ",")
  215. row := sheet.AddRow()
  216. row.WriteSlice(&cellName, -1)
  217. }
  218. for c, cl := 0, len(sheet.Cols); c < cl; c++ {
  219. sheet.Cols[c].Width = 20
  220. }
  221. dir := "static/file/excel/report/" + this.GetAccode()
  222. SaveDirectory(dir)
  223. path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + fileTitle + ".xlsx"
  224. f.Save(path)
  225. var sw *Seaweed
  226. var filer []string
  227. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  228. filer = []string{_filer}
  229. }
  230. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  231. _, _, fID, _ := sw.UploadFile(path, "", "")
  232. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  233. os.Remove(path)
  234. fmt.Println("==retDocWatermarkUrl==", retDocUrl)
  235. this.Data["json"] = retDocUrl
  236. this.ServeJSON()
  237. }
  238. // @Title get 导出ex --收入业务
  239. // @Description get SampleType by token
  240. // @Success 200 {object} sampletype.SampleType
  241. // @router /exportexcelincome [get]
  242. func (this *OilCatalogController) ExportExcelIncome() {
  243. //CardTitle := this.GetString("CardTitle")
  244. CatalogType := this.GetString("CatalogType")
  245. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  246. var list []oilcatalog.OilCatalog
  247. where := "CatalogType=" + CatalogType
  248. svc.GetEntities(&list, where)
  249. filetitle := "大港油田收入业务目录"
  250. showcolumnarr := "企业名称,法人姓名,法人身份证号,企业注册地址,全国统一信用代码,注册资本,申请备案范围,备注"
  251. f := xlsx.NewFile()
  252. sheet, _ := f.AddSheet(filetitle)
  253. cellname := strings.Split(showcolumnarr, ",")
  254. row := sheet.AddRow()
  255. row.WriteSlice(&cellname, -1)
  256. for _, item := range list {
  257. datastring := item.CompanyName + "," + item.LegalPerson + "," + item.IDCode + "," + item.Address + "," + item.USCCode +
  258. "," + strconv.FormatFloat(item.RegCapital, 'f', 2, 64) + "," + item.RecordScope + "," + item.Remark
  259. cellname := strings.Split(datastring, ",")
  260. row := sheet.AddRow()
  261. row.WriteSlice(&cellname, -1)
  262. }
  263. for c, cl := 0, len(sheet.Cols); c < cl; c++ {
  264. sheet.Cols[c].Width = 20
  265. }
  266. dir := "static/file/excel/report/" + this.GetAccode()
  267. SaveDirectory(dir)
  268. path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + filetitle + ".xlsx"
  269. f.Save(path)
  270. var sw *Seaweed
  271. var filer []string
  272. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  273. filer = []string{_filer}
  274. }
  275. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  276. _, _, fID, _ := sw.UploadFile(path, "", "")
  277. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  278. os.Remove(path)
  279. fmt.Println("==retDocWatermarkUrl==", retDocUrl)
  280. this.Data["json"] = retDocUrl
  281. this.ServeJSON()
  282. }
  283. // @Title
  284. // @Description
  285. // @Success 200 {object} controllers.Request
  286. // @router /isaccess [get]
  287. func (this *OilCatalogController) IsAccess() {
  288. roleId := this.GetString("RoleId")
  289. svcPerm := permission.GetPermissionService(utils.DBE)
  290. svc := organize.GetOrganizeService(utils.DBE)
  291. res := false
  292. if !svcPerm.IsAdmin(this.User.Id) {
  293. res = svc.UserInRoleById(this.User.Id, roleId)
  294. } else {
  295. res = true
  296. }
  297. this.Data["json"] = res
  298. this.ServeJSON()
  299. }
  300. // @Title get 导入excel
  301. // @Description get SampleType by token
  302. // @Success 200 {object} sampletype.SampleType
  303. // @router /importexcel [get]
  304. func (this *OilCatalogController) ImportExcel() {
  305. t := time.Now()
  306. url := this.GetString("ExcelUrl")
  307. CatalogType := this.GetString("CatalogType")
  308. var errorinfo ErrorInfo
  309. if url == "" {
  310. errorinfo.Code = -2
  311. errorinfo.Message = "导入失败,文件未上传成功"
  312. this.Data["json"] = &errorinfo
  313. this.ServeJSON()
  314. }
  315. svc := oilcatalog.GetOilCatalogService(utils.DBE)
  316. log.Printf("url:==" + url) // http://60.30.245.229//upfile/dc1/2,063156edd288
  317. extranetIP := utils.Cfg.MustValue("server", "extranetIP")
  318. localIP := utils.Cfg.MustValue("server", "localIP")
  319. if strings.Index(url, extranetIP) >= 0 {
  320. url = strings.Replace(url, extranetIP, localIP, 1)
  321. }
  322. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx"
  323. filename := strconv.Itoa(int(time.Now().Unix())) + ".xlsx"
  324. utils.DownloadFile(url, filename, _dir)
  325. filePath := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx/" + filename
  326. xlFile, err := xlsx.OpenFile(filePath)
  327. //excelFileName := "F:/物资类项目与资质对照表-2017.xlsx"
  328. if err != nil {
  329. fmt.Printf("open failed: %s\n", err)
  330. }
  331. var sheet = xlFile.Sheets[0]
  332. errLineNo := ""
  333. for i := 1; i < len(sheet.Rows); i++ {
  334. cells := sheet.Rows[i].Cells
  335. if len(cells) != 0 {
  336. if cells[1].Value != "" {
  337. ValidityFrom, err1 := time.Parse("2006-01-02", cells[3].Value)
  338. ValidityTo, err2 := time.Parse("2006-01-02", cells[4].Value)
  339. CompanyName := cells[1].Value
  340. Business := cells[2].Value
  341. Remark := cells[5].Value
  342. if err1 != nil || err2 != nil {
  343. errLineNo = errLineNo + "," + strconv.Itoa(i)
  344. continue
  345. }
  346. var model oilcatalog.OilCatalog
  347. model.CatalogType, _ = strconv.Atoi(CatalogType)
  348. model.CompanyName = CompanyName
  349. model.Business = Business
  350. model.ValidityFrom = ValidityFrom
  351. model.ValidityTo = ValidityTo
  352. model.Remark = Remark
  353. where := "CatalogType=" + CatalogType + " and CompanyName='" + CompanyName + "'"
  354. has := svc.GetEntity(&model, where)
  355. if !has {
  356. svc.InsertEntity(&model)
  357. }
  358. }
  359. }
  360. }
  361. os.Remove(filePath)
  362. elapsed := time.Since(t)
  363. fmt.Println(elapsed)
  364. if len(errLineNo) > 0 {
  365. errorinfo.Code = -1
  366. errorinfo.Message = "导入完成!错误行号:" + errLineNo
  367. this.Data["json"] = &errorinfo
  368. this.ServeJSON()
  369. } else {
  370. errorinfo.Code = 0
  371. errorinfo.Message = "导入完成!"
  372. this.Data["json"] = &errorinfo
  373. this.ServeJSON()
  374. }
  375. }