Browse Source

数据导出excel提交

wangkaiyue 5 years ago
parent
commit
2aced7867d

File diff suppressed because it is too large
+ 42 - 112
src/jfw/modules/subscribepay/src/dataexport.json


+ 184 - 138
src/jfw/modules/subscribepay/src/entity/dataexport.go

@@ -4,6 +4,7 @@ import (
 	"bytes"
 	"config"
 	"encoding/json"
+	"errors"
 	"fmt"
 	"jfw/public"
 	"log"
@@ -29,32 +30,36 @@ func init() {
 
 //价格配置文件
 type DataexportConfig struct {
-	UnitPrice_normal                 float64           `json:"unitPrice_normal"`
-	UnitPrice_senior                 float64           `json:"unitPrice_senior"`
-	Discount                         float64           `json:"discount"`
-	OrderMinPrice                    float64           `json:"orderMinPrice"`
-	MsgMaxCount                      int               `json:"msgMaxCount"`
-	Standard_Fields                  *Dataexport_Field `json:"standard"`
-	Senior_Fields                    *Dataexport_Field `json:"senior"`
-	Mail_attach_content              string            `json:"mail_attach_content"`
-	Mail_attach_content_key          string            `json:"mail_attach_content_key"`
-	Mail_notice_content              string            `json:"mail_notice_content"`
-	Mail_notice_title                string            `json:"mail_notice_title"`
-	Mail_retry                       int               `json:"mail_retry"`
-	AuditPersons                     []string          `json:"auditPersons"`
-	Font                             Font              `json:"font"`
-	Mail_invoice_finance_content     string            `json:"mail_invoice_finance_content"`     //to北京财务订单内容
-	Mail_vip_invoice_finance_content string            `json:"mail_vip_invoice_finance_content"` //vip订单 to北京财务申请发票内容
-	Mail_ent_invoice_finance_content string            `json:"mail_ent_invoice_finance_content"` //企业商机管理 to北京财务申请发票内容
-	Mail_order_finance_content       string            `json:"mail_order_finance_content"`       //to北京财务申请发票内容
-	Finance_emails                   []string          `json:"finance_emails"`
-	Qmxdomain                        string            `json:"qmxdomain"`
-	DataReportContent                string            `json:"dataReportContent"`
+	UnitPrice_normal                 float64      `json:"unitPrice_normal"`
+	UnitPrice_senior                 float64      `json:"unitPrice_senior"`
+	Discount                         float64      `json:"discount"`
+	OrderMinPrice                    float64      `json:"orderMinPrice"`
+	MsgMaxCount                      int          `json:"msgMaxCount"`
+	Standard_Fields                  []*ExeclCell `json:"standard"`
+	Senior_Fields                    []*ExeclCell `json:"senior"`
+	Mail_attach_content              string       `json:"mail_attach_content"`
+	Mail_attach_content_key          string       `json:"mail_attach_content_key"`
+	Mail_notice_content              string       `json:"mail_notice_content"`
+	Mail_notice_title                string       `json:"mail_notice_title"`
+	Mail_retry                       int          `json:"mail_retry"`
+	AuditPersons                     []string     `json:"auditPersons"`
+	Font                             Font         `json:"font"`
+	Mail_invoice_finance_content     string       `json:"mail_invoice_finance_content"`     //to北京财务订单内容
+	Mail_vip_invoice_finance_content string       `json:"mail_vip_invoice_finance_content"` //vip订单 to北京财务申请发票内容
+	Mail_ent_invoice_finance_content string       `json:"mail_ent_invoice_finance_content"` //企业商机管理 to北京财务申请发票内容
+	Mail_order_finance_content       string       `json:"mail_order_finance_content"`       //to北京财务申请发票内容
+	Finance_emails                   []string     `json:"finance_emails"`
+	Qmxdomain                        string       `json:"qmxdomain"`
+	DataReportContent                string       `json:"dataReportContent"`
 }
-type Dataexport_Field struct {
-	Names  []string  `json:"names"`
-	Fields []string  `json:"fields"`
-	Widths []float64 `json:"widths"`
+
+type ExeclCell struct {
+	Name   string  `json:"name"`   //名称
+	Filed  string  `json:"filed"`  //对应字段
+	Width  float64 `json:"width"`  //宽度
+	Color  string  `json:"color"`  //北京颜色
+	VMerge int     `json:"vMerge"` //垂直合并单元格
+	HMerge int     `json:"hMerge"` //水平合并单元格
 }
 
 type Font struct {
@@ -118,25 +123,157 @@ func (d *dataExportStruct) PayCallBack(param *CallBackParam) bool {
 	return update
 }
 
+var (
+	defaultExcelBorder = xlsx.Border{ //边框样式
+		Left:        "thin",
+		LeftColor:   "00A6A6A6",
+		Right:       "thin",
+		RightColor:  "00A6A6A6",
+		Top:         "thin",
+		TopColor:    "00A6A6A6",
+		Bottom:      "thin",
+		BottomColor: "00A6A6A6",
+	}
+	defaultExcelAlignment = xlsx.Alignment{ //对其方式
+		Horizontal: "center",
+		Vertical:   "center",
+		WrapText:   true,
+	}
+	defaultCellStlye = &xlsx.Style{ //默认样式
+		Border:    defaultExcelBorder,
+		Alignment: defaultExcelAlignment,
+	}
+)
+
+//生成数据导出excel
+func CreateDataExortExcleFile(filter_id string, data_count int, isSenior bool, download_url, filename string) error {
+	//查询数据
+	list, _ := public.GetDataExportSearchResultUseId(filter_id, qutil.If(isSenior, "2", "1").(string), data_count)
+	if list == nil || len(*list) == 0 {
+		return errors.New("未查询到数据")
+	}
+	//创建excel文件
+	file := xlsx.NewFile()
+	sheet, err := file.AddSheet("Sheet1")
+	if err != nil {
+		return errors.New("创建Sheet1失败")
+	}
+	var row *xlsx.Row
+	var cell *xlsx.Cell
+	var excelHead []*ExeclCell
+	//添加头部标题
+	if !isSenior {
+		row = sheet.AddRow()
+		row.SetHeight(20)
+		excelHead = ExConf.Standard_Fields
+		for index, excel := range excelHead {
+			cell = row.AddCell()
+			cell.SetString(excel.Name)
+			cell.SetStyle(&xlsx.Style{
+				Fill:      *xlsx.NewFill("solid", excel.Color, excel.Color),
+				Border:    defaultExcelBorder,
+				Alignment: defaultExcelAlignment,
+			})
+			sheet.Col(index).Width = excel.Width
+		}
+	} else {
+		excelHead = ExConf.Senior_Fields
+		//铺设第一层title
+		row = sheet.AddRow()
+		row.SetHeight(12)
+		lastF := false //合并行时标识
+		for _, excel := range excelHead {
+			if excel.VMerge != 1 && excel.HMerge == 0 {
+				if lastF { //不可读取内容
+					lastF = false
+					continue
+				}
+				cell = row.AddCell()
+			} else {
+				lastF = true
+				cell = row.AddCell()
+				cell.SetString(excel.Name)
+				cell.VMerge = excel.VMerge
+				cell.HMerge = excel.HMerge
+			}
+			if excel.HMerge != 0 {
+				lastF = true
+			}
+			cell.SetStyle(&xlsx.Style{
+				Fill:      *xlsx.NewFill("solid", excel.Color, excel.Color),
+				Border:    defaultExcelBorder,
+				Alignment: defaultExcelAlignment,
+			})
+		}
+		//铺设第二层title并设置宽度
+		row = sheet.AddRow()
+		row.SetHeight(12)
+		cNum := 0
+		for _, excel := range excelHead {
+			if excel.HMerge != 0 {
+				continue
+			}
+			cell = row.AddCell()
+			if excel.VMerge != 1 {
+				cell.SetString(excel.Name)
+			}
+			sheet.Col(cNum).Width = excel.Width
+			cNum++
+			cell.SetStyle(&xlsx.Style{
+				Fill:      *xlsx.NewFill("solid", excel.Color, excel.Color),
+				Border:    defaultExcelBorder,
+				Alignment: defaultExcelAlignment,
+			})
+		}
+	}
+	//填充数据
+	for k, data := range *list {
+		row = sheet.AddRow()
+		row.SetHeight(50)
+		data["index"] = k + 1
+		for _, v := range excelHead {
+			if v.Filed == "" {
+				continue
+			}
+			cell = row.AddCell()
+			cell.SetValue(data[v.Filed])
+			cell.SetStyle(&xlsx.Style{
+				Border:    defaultExcelBorder,
+				Alignment: defaultExcelAlignment,
+			})
+		}
+	}
+	dir := "./web/staticres/res/dataexport"
+	if strings.Contains(download_url, "jyapp") {
+		dir = "./web/staticres/jyapp/res/dataexport"
+	}
+	_, path_error := os.Stat(dir)
+	if path_error != nil && os.IsNotExist(path_error) {
+		// 创建文件夹
+		os.MkdirAll(dir, os.ModePerm)
+	}
+	return file.Save(dir + "/" + filename)
+}
+
 //发送邮箱验证码
 func SendMailIdentCode(to, code string, auth []*mail.GmailAuth) bool {
 	html := fmt.Sprintf(`<div>
-		<div>
-			%s,您好!
-		</div>
-		<div style="padding: 20px 70px 10px 70px;">
-			<p>您正在进行绑定邮箱地址验证,请在邮件验证码输入框输入下方验证码:</p>
-			<span style="font-weight: bold;font-size: x-large;">%s</span>
-			<p>请勿向任何人泄露您收到的验证码。</p>
-			<p>如果您没有使用剑鱼标讯,请忽略此邮件。</p>
-			<p>此为系统邮件,请勿回复。</p>
-			<p>如有疑问,请联系客服 400-108-6670。</p>
-		</div>
-		<div>
-			<p>此致</p>
-			<p>剑鱼标讯</p>
-		</div>	
-	</div>`, to, code)
+            <div>
+                  %s,您好!
+            </div>
+            <div style="padding: 20px 70px 10px 70px;">
+                  <p>您正在进行绑定邮箱地址验证,请在邮件验证码输入框输入下方验证码:</p>
+                  <span style="font-weight: bold;font-size: x-large;">%s</span>
+                  <p>请勿向任何人泄露您收到的验证码。</p>
+                  <p>如果您没有使用剑鱼标讯,请忽略此邮件。</p>
+                  <p>此为系统邮件,请勿回复。</p>
+                  <p>如有疑问,请联系客服 400-108-6670。</p>
+            </div>
+            <div>
+                  <p>此致</p>
+                  <p>剑鱼标讯</p>
+            </div>      
+      </div>`, to, code)
 
 	for k, v := range auth {
 		if mail.GSendMail("剑鱼标讯", to, "", "", "剑鱼标讯邮箱校验", html, "", "", v) {
@@ -258,7 +395,7 @@ func SendMailToBJFinance(order *map[string]interface{}, pay_time, transaction_id
 					publishtime = fmt.Sprintf("%s-%s", qutil.FormatDateByInt64(&startTime, qutil.Date_yyyyMMdd_Point), create_time[:10])
 				}
 			}
-			//超级搜索页面 筛选区域为 area 		没有region    移动端数据导出改
+			//超级搜索页面 筛选区域为 area           没有region    移动端数据导出改
 			if sc.Region == nil {
 				region = strings.Join(sc.Area, " ")
 			} else {
@@ -598,107 +735,16 @@ func SendMailToPayUser(order *map[string]interface{}, order_money float64, pay_t
 		create_time = strings.Replace(create_time, "-", ".", -1)
 		create_time = regexp.MustCompile(":[^:]+$").ReplaceAllString(create_time, "")
 	}
-	//
-	file := xlsx.NewFile()
-	sheet, _ := file.AddSheet("Sheet1")
-	var row *xlsx.Row
-	var cell *xlsx.Cell
-	//边框
-	border := xlsx.Border{
-		Left:        "thin",
-		LeftColor:   "00A6A6A6",
-		Right:       "thin",
-		RightColor:  "00A6A6A6",
-		Top:         "thin",
-		TopColor:    "00A6A6A6",
-		Bottom:      "thin",
-		BottomColor: "00A6A6A6",
+	if err := CreateDataExortExcleFile(filter_id, data_count, data_spec == "高级字段包", download_url, filename); err != nil {
+		log.Println(user_mail, "数据导出生成excel失败!", err)
 	}
-	//对齐方式
-	alignment := xlsx.Alignment{
-		Horizontal: "center",
-		Vertical:   "center",
-		WrapText:   true,
-	}
-	//增加标题
-	row = sheet.AddRow()
-	row.SetHeight(30)
-	var names []string
-	var fields []string
-	var widths []float64
-	dataType := "1"
-	if data_spec == "标准字段包" {
-		names = ExConf.Standard_Fields.Names
-		fields = ExConf.Standard_Fields.Fields
-		widths = ExConf.Standard_Fields.Widths
-	} else if data_spec == "高级字段包" {
-		dataType = "2"
-		names = ExConf.Senior_Fields.Names
-		fields = ExConf.Senior_Fields.Fields
-		widths = ExConf.Senior_Fields.Widths
-	}
-	style1 := &xlsx.Style{
-		Fill:      *xlsx.NewFill("solid", "00DDD9C4", "00DDD9C4"),
-		Border:    border,
-		Alignment: alignment,
-	}
-	for k, name := range names {
-		cell = row.AddCell()
-		cell.SetString(name)
-		cell.SetStyle(style1)
-		if k < len(widths) {
-			sheet.Col(k).Width = widths[k]
-		}
-	}
-	//增加其他
-	style2 := &xlsx.Style{
-		Border:    border,
-		Alignment: alignment,
-	}
-	list, _ := public.GetDataExportSearchResultUseId(filter_id, dataType, data_count)
-	if list != nil {
-		for _, v := range *list {
-			row = sheet.AddRow()
-			row.SetHeight(50)
-			for _, field := range fields {
-				cell = row.AddCell()
-				cell.SetValue(v[field])
-				cell.SetStyle(style2)
-			}
-		}
-	} else {
-		log.Println(user_mail, "数据导出邮件没有获取到数据!")
-	}
-	dir := "./web/staticres/res/dataexport"
-	if strings.Contains(download_url, "jyapp") {
-		dir = "./web/staticres/jyapp/res/dataexport"
-	}
-	_, path_error := os.Stat(dir)
-	if path_error != nil && os.IsNotExist(path_error) {
-		// 创建文件夹
-		os.MkdirAll(dir, os.ModePerm)
-	}
-	err := file.Save(dir + "/" + filename)
-	if err != nil {
-		log.Println(user_mail, filename, "生成附件文件错误:", err)
-	}
-	//	mw := &myWrite{
-	//		Byte: &bytes.Buffer{},
-	//	}
-	//	err = file.Write(mw)
-	//	if err != nil {
-	//		log.Println(user_mail, "数据导出生成excel失败!", err)
-	//	}
-	//	bt := mw.Byte.Bytes()
 	now := time.Now()
-	//fname := fmt.Sprintf("%s.xlsx", order_code)
-	//
 	mailcontent := ""
 	buyer, winner, subtype := "", "", ""
 	publishtime, region, industry, keys := "", "", "", ""
 	sc_money := ""
 	sc := new(SieveCondition)
-	err = json.Unmarshal([]byte(filter), &sc)
+	err := json.Unmarshal([]byte(filter), &sc)
 	selectType := "title"
 	if err == nil && sc != nil {
 		selectType = qutil.ObjToString(sc.SelectType)
@@ -724,7 +770,7 @@ func SendMailToPayUser(order *map[string]interface{}, order_money float64, pay_t
 				publishtime = fmt.Sprintf("%s-%s", qutil.FormatDateByInt64(&startTime, qutil.Date_yyyyMMdd_Point), create_time[:10])
 			}
 		}
-		//超级搜索页面 筛选区域为 area 		没有region    移动端数据导出改
+		//超级搜索页面 筛选区域为 area           没有region    移动端数据导出改
 		if sc.Region == nil {
 			region = strings.Join(sc.Area, " ")
 		} else {
@@ -805,7 +851,7 @@ func SendRetryMail(retry int, user_mail, subject, content, fname string, bt []by
 }
 
 /*
-	获取-筛选条件-金额
+   获取-筛选条件-金额
 */
 func GetPriceDes_SieveCondition(minPrice, maxPrice string) string {
 	des := ""

Some files were not shown because too many files changed in this diff