123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- package cygx
- import (
- "encoding/json"
- "github.com/rdlucklib/rdluck_tools/paging"
- "github.com/tealeg/xlsx"
- "hongze/hz_crm_api/controllers"
- "hongze/hz_crm_api/models"
- "hongze/hz_crm_api/models/cygx"
- "hongze/hz_crm_api/models/system"
- cygxService "hongze/hz_crm_api/services/cygx"
- "hongze/hz_crm_api/utils"
- "os"
- "path/filepath"
- "strconv"
- "strings"
- "time"
- )
- type ProductInteriorController struct {
- controllers.BaseAuthController
- }
- func (this *ProductInteriorController) PreserveAndPublish() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- var req cygx.AddProductInteriorReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- publishTime := utils.StrDateToDate(req.PublishTime)
- productInteriorId := req.ProductInteriorId
- columnName := req.ColumnName
- title := req.Title
- abstract := req.Abstract
- department := req.Department
- body := req.Body
- industrialManagementIds := req.IndustrialManagementIds
- industrialSubjectIds := req.IndustrialSubjectIds
- matchTypeId := req.MatchTypeId
- chartPermissionId := req.ChartPermissionId
-
- if industrialManagementIds != "" {
- industrialManagementIdList := strings.Split(industrialManagementIds, ",")
- for _, v := range industrialManagementIdList {
- _, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "产业ID不规范,Err:" + err.Error() + industrialManagementIds
- return
- }
- }
- }
- if industrialSubjectIds != "" {
- industrialSubjectIdList := strings.Split(industrialSubjectIds, ",")
- for _, v := range industrialSubjectIdList {
- _, err := strconv.Atoi(v)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "标的ID不规范,Err:" + err.Error() + industrialSubjectIds
- return
- }
- }
- }
- charInfo, errCategory := cygx.GetCategoryInfoById(chartPermissionId)
- if errCategory != nil {
- br.Msg = "获取品种信息失败"
- br.ErrMsg = "获取品种信息失败,Err:" + errCategory.Error()
- return
- }
- item := new(cygx.CygxProductInterior)
- item.Status = req.DoType
- item.ColumnName = columnName
- item.Title = title
- item.PublishTime = publishTime
- item.CreateTime = time.Now()
- item.ModifyTime = time.Now()
- item.Body = body
- item.Abstract = abstract
- item.Department = department
- item.AdminId = sysUser.AdminId
- item.MatchTypeId = matchTypeId
- item.ChartPermissionId = chartPermissionId
- item.ChartPermissionName = charInfo.PermissionName
- if req.DoType == 1 {
- item.IsCancel = 0
- }
- if productInteriorId == 0 {
-
- err = cygx.AddProductInterior(item, industrialManagementIds, industrialSubjectIds)
- } else {
-
- detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
- if err != nil {
- br.Msg = "详情不存在"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- if req.DoType == 1 {
- item.Status = 1
- item.IsCancel = 0
- } else {
- item.IsCancel = detail.IsCancel
- item.Status = detail.Status
- }
- item.ProductInteriorId = productInteriorId
- item.VisibleRange = detail.VisibleRange
- err = cygx.UpdateProductInterior(item, industrialManagementIds, industrialSubjectIds)
- }
- if err != nil {
- br.Msg = "保存失败"
- br.ErrMsg = "保存失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "操作成功"
- }
- func (this *ProductInteriorController) List() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- sysUser := this.SysUser
- if sysUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,SysUser Is Empty"
- br.Ret = 408
- return
- }
- resp := new(cygx.GetCygxProductInteriorResp)
- pageSize, _ := this.GetInt("PageSize")
- currentIndex, _ := this.GetInt("CurrentIndex")
- status, _ := this.GetInt("Status")
- startDate := this.GetString("StartDate")
- endDate := this.GetString("EndDate")
- var startSize int
- if pageSize <= 0 {
- pageSize = utils.PageSize20
- }
- if currentIndex <= 0 {
- currentIndex = 1
- }
- startSize = utils.StartIndex(currentIndex, pageSize)
- var condition string
- var pars []interface{}
- if status == 0 || status == 1 {
- condition += ` AND art.status = ? `
- pars = append(pars, status)
- }
- if startDate != "" && endDate != "" {
- condition += ` AND art.publish_time BETWEEN ? AND ? `
- pars = append(pars, startDate, endDate)
- }
- total, err := cygx.GetCygxProductInteriorCount(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- condition += " ORDER BY art.publish_time DESC , art.product_interior_id DESC "
- list, err := cygx.GetCygxProductInteriorList(condition, pars, startSize, pageSize)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- var productInteriorIds []int
- for _, v := range list {
- v.PublishTime = utils.TimeRemoveHms(v.PublishTime)
- if v.IsCancel == 1 {
- v.Status = 3
- }
- productInteriorIds = append(productInteriorIds, v.ProductInteriorId)
- }
-
- mapPv, mapUv := cygxService.GetCygxProductInteriorHistoryListMap(productInteriorIds)
-
- mapLabel := cygxService.GetCygxProductInteriorLabelListMap(productInteriorIds)
- mapMatchTypeName := cygxService.GetCygxReportMappingCygxListMap()
- for _, v := range list {
- v.Pv = mapPv[v.ProductInteriorId]
- v.Uv = mapUv[v.ProductInteriorId]
- v.Label = mapLabel[v.ProductInteriorId]
- v.MatchTypeName = mapMatchTypeName[v.MatchTypeId]
- }
- page := paging.GetPaging(currentIndex, pageSize, total)
- resp.List = list
- resp.Paging = page
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- func (this *ProductInteriorController) Detail() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- AdminUser := this.SysUser
- if AdminUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- resp := new(cygx.GetCygxProductInteriorDetailResp)
- productInteriorId, _ := this.GetInt("ProductInteriorId")
- if productInteriorId < 1 {
- br.Msg = "请输入详情ID"
- return
- }
- detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
- if err != nil {
- br.Msg = "详情不存在"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- detail.PublishTime = utils.TimeRemoveHms2(detail.PublishTime)
- industrialList, err := cygx.GetProductInteriorIndustrialGroupManagementList(productInteriorId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "GetProductInteriorIndustrialGroupManagementList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
- return
- }
- subjectList, err := cygx.GetProductInteriorIndustrialGroupSubjecttList(productInteriorId)
- if err != nil && err.Error() != utils.ErrNoRow() {
- br.Msg = "获取信息失败"
- br.ErrMsg = "GetProductInteriorIndustrialGroupSubjecttList,Err:" + err.Error() + "productInteriorId:" + strconv.Itoa(productInteriorId)
- return
- }
- if detail.MatchTypeId > 0 {
- matchDetail, err := cygx.GetCygxReportMappingCygxDetail(detail.MatchTypeId)
- if err != nil {
- br.Msg = "获取信息失败"
- br.ErrMsg = "GetCygxReportMappingCygxDetail,Err:" + err.Error()
- return
- }
- if matchDetail != nil {
- detail.MatchTypeName = matchDetail.MatchTypeName
- }
- }
- detail.ListIndustrial = industrialList
- detail.ListSubject = subjectList
- resp.Detail = detail
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- br.Data = resp
- }
- func (this *ProductInteriorController) Delete() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- AdminUser := this.SysUser
- if AdminUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- var req cygx.ProductInteriorIdReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- productInteriorId := req.ProductInteriorId
- if productInteriorId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,id不可为空"
- return
- }
- err = cygx.DeleteProductInterior(productInteriorId)
- if err != nil {
- br.Msg = "详情不存在"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.IsAddLog = true
- br.Msg = "删除成功"
- }
- func (this *ProductInteriorController) PublishReport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- var req cygx.ProductInteriorIdReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- productInteriorId := req.ProductInteriorId
- if productInteriorId == 0 {
- br.Msg = "参数错误"
- br.ErrMsg = "参数错误,id不可为空"
- return
- }
- detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
- if err != nil {
- br.Msg = "详情不存在"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- var status int
- var isCancel int
- if detail.Status == 0 {
- status = 1
- isCancel = 0
- } else {
- status = 0
- isCancel = 1
- go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
- }
- err = cygx.EditProductInteriorStatus(status, isCancel, productInteriorId)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- }
- func (this *ProductInteriorController) VisibleRange() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- AdminUser := this.SysUser
- if AdminUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- var req cygx.ProductInteriorIdReq
- err := json.Unmarshal(this.Ctx.Input.RequestBody, &req)
- if err != nil {
- br.Msg = "参数解析异常!"
- br.ErrMsg = "参数解析失败,Err:" + err.Error()
- return
- }
- productInteriorId := req.ProductInteriorId
- detail, err := cygx.GetCygxProductInteriorDetail(productInteriorId)
- if err != nil {
- br.Msg = "详情不存在"
- br.ErrMsg = "获取失败,Err:" + err.Error()
- return
- }
- var visibleRange int
- if detail.VisibleRange == 0 {
- visibleRange = 1
- go cygxService.UpdateResourceData(productInteriorId, "productinterior", "add", time.Now().Format(utils.FormatDateTime))
- go cygxService.SendWxMsgWithCygxProductInterior(productInteriorId)
- } else {
- visibleRange = 0
- go cygxService.UpdateResourceData(productInteriorId, "productinterior", "delete", time.Now().Format(utils.FormatDateTime))
- }
- err = cygx.ProductInteriorVisibleRange(visibleRange, productInteriorId)
- if err != nil {
- br.Msg = "操作失败"
- br.ErrMsg = "操作失败,Err:" + err.Error()
- return
- }
- br.Ret = 200
- br.Success = true
- br.Msg = "操作成功"
- br.IsAddLog = true
- }
- func (this *ProductInteriorController) PvExport() {
- br := new(models.BaseResponse).Init()
- defer func() {
- this.Data["json"] = br
- this.ServeJSON()
- }()
- AdminUser := this.SysUser
- if AdminUser == nil {
- br.Msg = "请登录"
- br.ErrMsg = "请登录,用户信息为空"
- br.Ret = 408
- return
- }
- productInteriorId, _ := this.GetInt("ProductInteriorId")
- if productInteriorId < 1 {
- br.Msg = "请输入详情ID"
- return
- }
- var condition string
- var pars []interface{}
- condition = ` AND product_interior_id = ? `
- pars = append(pars, productInteriorId)
- var respList []*cygx.CygxProductInteriorHistory
- list, err := cygx.GetCygxProductInteriorHistoryList(condition, pars)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取数据失败,Err:" + err.Error()
- return
- }
-
- resp := new(cygx.CanDownload)
- adminInfo, errAdmin := system.GetSysUserById(AdminUser.AdminId)
- if errAdmin != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,Err:" + errAdmin.Error()
- return
- }
- if adminInfo.Role == "admin" || adminInfo.Role == "researcher" {
- resp.IsCanDownload = true
- }
-
- if resp.IsCanDownload == false {
- mapMobile, err := cygxService.GetAdminLookUserMobile(adminInfo)
- if err != nil {
- br.Msg = "获取失败"
- br.ErrMsg = "获取失败,销售对应权限,Err:" + err.Error()
- return
- }
- for _, v := range list {
- if _, ok := mapMobile[v.Mobile]; ok {
- respList = append(respList, v)
- }
- }
- } else {
- respList = list
- }
-
- dir, err := os.Executable()
- exPath := filepath.Dir(dir)
- downLoadnFilePath := exPath + "/" + time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- xlsxFile := xlsx.NewFile()
- if err != nil {
- br.Msg = "生成文件失败"
- br.ErrMsg = "生成文件失败"
- return
- }
- style := xlsx.NewStyle()
- alignment := xlsx.Alignment{
- Horizontal: "center",
- Vertical: "center",
- WrapText: true,
- }
- style.Alignment = alignment
- style.ApplyAlignment = true
- sheet, err := xlsxFile.AddSheet("阅读明细")
- if err != nil {
- br.Msg = "新增Sheet失败"
- br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
- return
- }
- rowTitle := sheet.AddRow()
- cellA := rowTitle.AddCell()
- cellA.Value = "姓名"
- cellB := rowTitle.AddCell()
- cellB.Value = "手机号"
- cellC := rowTitle.AddCell()
- cellC.Value = "公司名称"
- cellD := rowTitle.AddCell()
- cellD.Value = "所属权益销售"
- cellE := rowTitle.AddCell()
- cellE.Value = "阅读时间"
- for _, item := range respList {
- row := sheet.AddRow()
- cellA := row.AddCell()
- cellA.Value = item.RealName
- cellB := row.AddCell()
- cellB.Value = item.Mobile
- cellC := row.AddCell()
- cellC.Value = item.CompanyName
- cellD := row.AddCell()
- cellD.Value = item.SellerName
- cellE := row.AddCell()
- cellE.Value = item.CreateTime.Format(utils.FormatDateTime)
- }
- err = xlsxFile.Save(downLoadnFilePath)
- if err != nil {
- br.Msg = "保存文件失败"
- br.ErrMsg = "保存文件失败"
- return
- }
- downloadFileName := time.Now().Format(utils.FormatDateTimeUnSpace) + ".xlsx"
- this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
- defer func() {
- os.Remove(downLoadnFilePath)
- }()
- br.Ret = 200
- br.Success = true
- br.Msg = "获取成功"
- }
|