| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- package oilsupplier
- import (
- "dashoo.cn/backend/api/business/oilsupplier/oilcatalog"
- "dashoo.cn/backend/api/business/organize"
- . "dashoo.cn/backend/api/controllers"
- "dashoo.cn/business2/permission"
- "dashoo.cn/utils"
- "encoding/json"
- "fmt"
- . "github.com/linxGnu/goseaweedfs"
- "github.com/tealeg/xlsx"
- "log"
- "os"
- "strconv"
- "strings"
- "time"
- )
- type OilCatalogController struct {
- BaseController
- }
- // @Title 获取列表
- // @Description get user by token
- // @Success 200 {object} []suppliercert.OilSupplierCert
- // @router /list [get]
- func (this *OilCatalogController) GetEntityList() {
- //获取分页信息
- page := this.GetPageInfoForm()
- where := " 1=1 "
- orderBy := "Id"
- asc := false
- Order := this.GetString("Order")
- Prop := this.GetString("Prop")
- if Order != "" && Prop != "" {
- orderBy = Prop
- if Order == "asc" {
- asc = true
- }
- }
- SubId := this.GetString("SubId")
- CompanyName := this.GetString("CompanyName")
- Business := this.GetString("Business")
- catalogType := this.GetString("CatalogType")
- Status := this.GetString("Status")
- CreateOn := this.GetString("CreateOn")
- Year := this.GetString("Year")
- if SubId != "" {
- where = where + " and SubId=" + SubId
- }
- if catalogType != "" {
- where = where + " and CatalogType=" + catalogType
- }
- if CompanyName != "" {
- where = where + " and CompanyName like '%" + CompanyName + "%' "
- }
- if Business != "" {
- where = where + " and Business like '%" + Business + "%' "
- }
- if Status != "" {
- where = where + " and Status = '" + Status + "'"
- }
- if Year != "" {
- where = where + " and CreateOn >= '" + Year + "-01-01 00:00:00' and CreateOn <='" + Year + "-12-31 00:00:00'"
- }
- if CreateOn != "" {
- dates := strings.Split(CreateOn, ",")
- if len(dates) == 2 {
- minDate := dates[0]
- maxDate := dates[1]
- where = where + " and ValidityTo>='" + minDate + "' and ValidityTo<='" + maxDate + "'"
- }
- }
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- var list []oilcatalog.OilCatalog
- total := svc.GetPagingEntitiesWithOrderBytbl("", page.CurrentPage, page.Size, orderBy, asc, &list, where)
- var datainfo DataInfo
- datainfo.Items = list
- datainfo.CurrentItemCount = total
- datainfo.PageIndex = page.CurrentPage
- datainfo.ItemsPerPage = page.Size
- this.Data["json"] = &datainfo
- this.ServeJSON()
- }
- // @Title 添加
- // @Description 新增
- // @Success 200 {object} controllers.Request
- // @router /add [post]
- func (this *OilCatalogController) AddEntity() {
- var model oilcatalog.OilCatalog
- var jsonBlob = this.Ctx.Input.RequestBody
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- json.Unmarshal(jsonBlob, &model)
- // model.Status = "0" // 由前端分不同页面(企管法规处2和普通用户2)传值创建
- model.CreateOn = time.Now()
- model.CreateBy = this.User.Realname
- model.CreateUserId, _ = utils.StrTo(this.User.Id).Int()
- _, err := svc.InsertEntityBytbl(OilCatalogName, &model)
- var errinfo ErrorDataInfo
- if err == nil {
- //新增
- errinfo.Message = "添加成功!"
- errinfo.Code = 0
- errinfo.Item = model.Id
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "添加失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title 删除单条信息
- // @Description
- // @Success 200 {object} ErrorInfo
- // @Failure 403 :id 为空
- // @router /delete/:Id [delete]
- func (this *OilCatalogController) DeleteEntity() {
- Id := this.Ctx.Input.Param(":Id")
- var errinfo ErrorInfo
- if Id == "" {
- errinfo.Message = "操作失败!请求信息不完整"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- var model oilcatalog.OilCatalog
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- err := svc.DeleteEntityById(Id, &model)
- if err == nil {
- errinfo.Message = "删除成功"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "删除失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title 修改实体
- // @Description 修改实体
- // @Success 200 {object} controllers.Request
- // @router /update/:id [post]
- func (this *OilCatalogController) UpdateEntity() {
- id := this.Ctx.Input.Param(":id")
- var errinfo ErrorInfo
- if id == "" {
- errinfo.Message = "操作失败!请求信息不完整"
- errinfo.Code = -2
- this.Data["json"] = &errinfo
- this.ServeJSON()
- return
- }
- var model oilcatalog.OilCatalog
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- var jsonBlob = this.Ctx.Input.RequestBody
- json.Unmarshal(jsonBlob, &model)
- model.ModifiedOn = time.Now()
- model.ModifiedBy = this.User.Realname
- model.ModifiedUserId, _ = utils.StrTo(this.User.Id).Int()
- cols := []string{
- "RegCapital",
- "USCCode",
- "Address",
- "LegalPerson",
- "RecordScope",
- "IDCode",
- "DutyDept",
- "Dept",
- "CatalogType",
- "OrderNo",
- "CompanyName",
- "Business",
- "ValidityFrom",
- "ValidityTo",
- "Remark",
- }
- err := svc.UpdateEntityBytbl(OilCatalogName, id, &model, cols)
- if err == nil {
- errinfo.Message = "修改成功!"
- errinfo.Code = 0
- this.Data["json"] = &errinfo
- this.ServeJSON()
- } else {
- errinfo.Message = "修改失败!" + utils.AlertProcess(err.Error())
- errinfo.Code = -1
- this.Data["json"] = &errinfo
- this.ServeJSON()
- }
- }
- // @Title get 导出ex
- // @Description get SampleType by token
- // @Success 200 {object} sampletype.SampleType
- // @router /exportexcelall [get]
- func (this *OilCatalogController) ExportExcelAll() {
- CardTitle := this.GetString("CardTitle")
- CatalogType := this.GetString("CatalogType")
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- var list []oilcatalog.OilCatalog
- where := "CatalogType=" + CatalogType
- svc.GetEntities(&list, where)
- fileTitle := "大港油田" + CardTitle + "目录"
- showColumnArr := "序号,企业名称,业务范围,有效期起,有效期止,备注"
- f := xlsx.NewFile()
- sheet, _ := f.AddSheet(fileTitle)
- cellName := strings.Split(showColumnArr, ",")
- row := sheet.AddRow()
- row.WriteSlice(&cellName, -1)
- for idx, item := range list {
- dataString := strconv.Itoa(idx+1) + "," + item.CompanyName + "," + svc.TrimSpaceForString(item.Business) + "," + utils.ToStr(item.ValidityFrom.Format("2006-01-02")) + "," +
- utils.ToStr(item.ValidityTo.Format("2006-01-02")) + "," + item.Remark
- cellName := strings.Split(dataString, ",")
- row := sheet.AddRow()
- row.WriteSlice(&cellName, -1)
- }
- for c, cl := 0, len(sheet.Cols); c < cl; c++ {
- sheet.Cols[c].Width = 20
- }
- dir := "static/file/excel/report/" + this.GetAccode()
- SaveDirectory(dir)
- path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + fileTitle + ".xlsx"
- f.Save(path)
- var sw *Seaweed
- var filer []string
- if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
- filer = []string{_filer}
- }
- sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
- _, _, fID, _ := sw.UploadFile(path, "", "")
- retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
- os.Remove(path)
- fmt.Println("==retDocWatermarkUrl==", retDocUrl)
- this.Data["json"] = retDocUrl
- this.ServeJSON()
- }
- // @Title get 导出ex --收入业务
- // @Description get SampleType by token
- // @Success 200 {object} sampletype.SampleType
- // @router /exportexcelincome [get]
- func (this *OilCatalogController) ExportExcelIncome() {
- //CardTitle := this.GetString("CardTitle")
- CatalogType := this.GetString("CatalogType")
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- var list []oilcatalog.OilCatalog
- where := "CatalogType=" + CatalogType
- svc.GetEntities(&list, where)
- filetitle := "大港油田收入业务目录"
- showcolumnarr := "企业名称,法人姓名,法人身份证号,企业注册地址,全国统一信用代码,注册资本,申请备案范围,备注"
- f := xlsx.NewFile()
- sheet, _ := f.AddSheet(filetitle)
- cellname := strings.Split(showcolumnarr, ",")
- row := sheet.AddRow()
- row.WriteSlice(&cellname, -1)
- for _, item := range list {
- datastring := item.CompanyName + "," + item.LegalPerson + "," + item.IDCode + "," + item.Address + "," + item.USCCode +
- "," + strconv.FormatFloat(item.RegCapital, 'f', 2, 64) + "," + item.RecordScope + "," + item.Remark
- cellname := strings.Split(datastring, ",")
- row := sheet.AddRow()
- row.WriteSlice(&cellname, -1)
- }
- for c, cl := 0, len(sheet.Cols); c < cl; c++ {
- sheet.Cols[c].Width = 20
- }
- dir := "static/file/excel/report/" + this.GetAccode()
- SaveDirectory(dir)
- path := dir + "/" + utils.TimeFormat(time.Now(), "20060102") + filetitle + ".xlsx"
- f.Save(path)
- var sw *Seaweed
- var filer []string
- if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
- filer = []string{_filer}
- }
- sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
- _, _, fID, _ := sw.UploadFile(path, "", "")
- retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
- os.Remove(path)
- fmt.Println("==retDocWatermarkUrl==", retDocUrl)
- this.Data["json"] = retDocUrl
- this.ServeJSON()
- }
- // @Title
- // @Description
- // @Success 200 {object} controllers.Request
- // @router /isaccess [get]
- func (this *OilCatalogController) IsAccess() {
- roleId := this.GetString("RoleId")
- svcPerm := permission.GetPermissionService(utils.DBE)
- svc := organize.GetOrganizeService(utils.DBE)
- res := false
- if !svcPerm.IsAdmin(this.User.Id) {
- res = svc.UserInRoleById(this.User.Id, roleId)
- } else {
- res = true
- }
- this.Data["json"] = res
- this.ServeJSON()
- }
- // @Title get 导入excel
- // @Description get SampleType by token
- // @Success 200 {object} sampletype.SampleType
- // @router /importexcel [get]
- func (this *OilCatalogController) ImportExcel() {
- t := time.Now()
- url := this.GetString("ExcelUrl")
- CatalogType := this.GetString("CatalogType")
- var errorinfo ErrorInfo
- if url == "" {
- errorinfo.Code = -2
- errorinfo.Message = "导入失败,文件未上传成功"
- this.Data["json"] = &errorinfo
- this.ServeJSON()
- }
- svc := oilcatalog.GetOilCatalogService(utils.DBE)
- log.Printf("url:==" + url) // http://60.30.245.229//upfile/dc1/2,063156edd288
- extranetIP := utils.Cfg.MustValue("server", "extranetIP")
- localIP := utils.Cfg.MustValue("server", "localIP")
- if strings.Index(url, extranetIP) >= 0 {
- url = strings.Replace(url, extranetIP, localIP, 1)
- }
- _dir := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx"
- filename := strconv.Itoa(int(time.Now().Unix())) + ".xlsx"
- utils.DownloadFile(url, filename, _dir)
- filePath := utils.Cfg.MustValue("file", "tmplateDir") + "xlsx/" + filename
- xlFile, err := xlsx.OpenFile(filePath)
- //excelFileName := "F:/物资类项目与资质对照表-2017.xlsx"
- if err != nil {
- fmt.Printf("open failed: %s\n", err)
- }
- var sheet = xlFile.Sheets[0]
- errLineNo := ""
- for i := 1; i < len(sheet.Rows); i++ {
- cells := sheet.Rows[i].Cells
- if len(cells) != 0 {
- if cells[1].Value != "" {
- ValidityFrom, err1 := time.Parse("2006-01-02", cells[3].Value)
- ValidityTo, err2 := time.Parse("2006-01-02", cells[4].Value)
- CompanyName := cells[1].Value
- Business := cells[2].Value
- Remark := cells[5].Value
- if err1 != nil || err2 != nil {
- errLineNo = errLineNo + "," + strconv.Itoa(i)
- continue
- }
- var model oilcatalog.OilCatalog
- model.CatalogType, _ = strconv.Atoi(CatalogType)
- model.CompanyName = CompanyName
- model.Business = Business
- model.ValidityFrom = ValidityFrom
- model.ValidityTo = ValidityTo
- model.Remark = Remark
- where := "CatalogType=" + CatalogType + " and CompanyName='" + CompanyName + "'"
- has := svc.GetEntity(&model, where)
- if !has {
- svc.InsertEntity(&model)
- }
- }
- }
- }
- os.Remove(filePath)
- elapsed := time.Since(t)
- fmt.Println(elapsed)
- if len(errLineNo) > 0 {
- errorinfo.Code = -1
- errorinfo.Message = "导入完成!错误行号:" + errLineNo
- this.Data["json"] = &errorinfo
- this.ServeJSON()
- } else {
- errorinfo.Code = 0
- errorinfo.Message = "导入完成!"
- this.Data["json"] = &errorinfo
- this.ServeJSON()
- }
- }
|