zwxi 11 months ago
parent
commit
12301a9f33
3 changed files with 511 additions and 1 deletions
  1. 508 0
      controller/contract/pre_register.go
  2. 2 1
      models/fms/contract_invoice.go
  3. 1 0
      routers/contract.go

+ 508 - 0
controller/contract/pre_register.go

@@ -1,9 +1,11 @@
 package contract
 
 import (
+	"bytes"
 	"fmt"
 	"github.com/gin-gonic/gin"
 	"github.com/go-playground/validator/v10"
+	"github.com/tealeg/xlsx"
 	"hongze/fms_api/controller/resp"
 	"hongze/fms_api/global"
 	"hongze/fms_api/models/base"
@@ -13,6 +15,7 @@ import (
 	crmService "hongze/fms_api/services/crm"
 	fmsService "hongze/fms_api/services/fms"
 	"hongze/fms_api/utils"
+	"net/http"
 	"strconv"
 	"strings"
 	"time"
@@ -46,6 +49,7 @@ func (rg *PreRegisterController) List(c *gin.Context) {
 	if pageIndex <= 0 {
 		pageIndex = 1
 	}
+
 	cond := ` invoice_type IN (3, 4) `
 	pars := make([]interface{}, 0)
 	if req.Keyword != "" {
@@ -70,6 +74,10 @@ func (rg *PreRegisterController) List(c *gin.Context) {
 	page := new(base.Page)
 	page.SetPageSize(req.PageSize)
 	page.SetCurrent(req.Current)
+	if req.IsExport == 1 {
+		page.SetPageSize(10000)
+		page.SetCurrent(1)
+	}
 	sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
 	page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: sortTypeMap[0]})
 
@@ -120,6 +128,10 @@ func (rg *PreRegisterController) List(c *gin.Context) {
 		respList = append(respList, item)
 	}
 
+	if req.IsExport == 1 {
+		ExportPreRegister(c, list)
+		return
+	}
 	page.SetTotal(total)
 	baseData := new(base.BaseData)
 	baseData.SetPage(page)
@@ -1268,3 +1280,499 @@ func (rg *PreRegisterController) Save(c *gin.Context) {
 
 	resp.Ok("操作成功", c)
 }
+
+func ExportPreRegister(c *gin.Context,list []*fms.ContractPreRegisterItem) {
+	//var req fms.PreRegisterListReq
+	//if e := c.BindQuery(&req); e != nil {
+	//	err, ok := e.(validator.ValidationErrors)
+	//	if !ok {
+	//		resp.FailData("参数解析失败", "Err:"+e.Error(), c)
+	//		return
+	//	}
+	//	resp.FailData("参数解析失败", err.Translate(global.Trans), c)
+	//	return
+	//}
+	//pageSize := req.PageSize
+	//pageIndex := req.Current
+	//if pageSize <= 0 {
+	//	pageSize = utils.PageSize20
+	//}
+	//if pageIndex <= 0 {
+	//	pageIndex = 1
+	//}
+	//cond := ` invoice_type IN (3, 4) `
+	//pars := make([]interface{}, 0)
+	//if req.Keyword != "" {
+	//	kw := "%" + req.Keyword + "%"
+	//	cond += ` AND company_name LIKE ? `
+	//	pars = append(pars, kw)
+	//}
+	// 货币列表
+	currencyOB := new(fms.CurrencyUnit)
+	currencyCond := `enable = 1`
+	currencyPars := make([]interface{}, 0)
+	currencyList, e := currencyOB.List(currencyCond, currencyPars)
+	if e != nil {
+		resp.FailMsg("获取失败", "获取货币列表失败, Err: "+e.Error(), c)
+		return
+	}
+	unitMap := make(map[string]string)
+	for i := range currencyList {
+		unitMap[currencyList[i].Code] = currencyList[i].UnitName
+	}
+	//
+	//page := new(base.Page)
+	//page.SetPageSize(req.PageSize)
+	//page.SetCurrent(req.Current)
+	//sortTypeMap := map[int]bool{0: false, 1: true, 2: false}
+	//page.AddOrderItem(base.OrderItem{Column: "create_time", Asc: sortTypeMap[0]})
+	//
+	//_, list, e := fms.GetContractPreRegisterItemPageList(page, cond, pars)
+	//if e != nil {
+	//	resp.FailMsg("获取失败", "获取预登记列表失败, Err: "+e.Error(), c)
+	//	return
+	//}
+	//serviceMap := make(map[int]string, 0)
+	registerIds := make([]int, 0)
+	for i := range list {
+		list[i].UnitName = unitMap[list[i].CurrencyUnit]
+		registerIds = append(registerIds, list[i].ContractRegisterId)
+	}
+	// 获取小套餐品种
+	cpCond := `product_id = ? AND permission_name <> ? AND enabled = 1 AND parent_id> 0 `
+	cpPars := make([]interface{}, 0)
+	cpPars = append(cpPars, crm.CompanyProductFicc, crm.ChartPermissionStrategyName)
+	cp := new(crm.ChartPermission)
+	permissionList, e := cp.List(cpCond, cpPars)
+	if e != nil {
+		resp.FailData("获取小套餐品种失败", "Err:"+e.Error(), c)
+		return
+	}
+	permissionLen := len(permissionList)
+	permissionNameIdMap := make(map[string]int)
+	for i := range permissionList {
+		permissionNameIdMap[permissionList[i].PermissionName] = permissionList[i].ChartPermissionId
+	}
+
+	// 获取所有FICC套餐服务
+	serviceList, e := fms.GetContractServiceTemplateMapByProductId(crm.CompanyProductFicc)
+	if e != nil {
+		resp.FailData("获取套餐服务失败", "Err:"+e.Error(), c)
+		return
+	}
+	// 除大小套餐外的其他套餐(下面的导出需要以此动态处理)
+	otherService := make([]*fms.ContractServiceTemplateItem, 0)
+	for i := range serviceList {
+		if serviceList[i].Title != "FICC大套餐" && serviceList[i].Title != "FICC小套餐" {
+			otherService = append(otherService, serviceList[i])
+		}
+	}
+	otherServiceLen := len(otherService)
+
+	// 获取所有权益套餐服务
+	raiServiceList, e := fms.GetContractServiceTemplateAllByProductId(crm.CompanyProductRai)
+	if e != nil {
+		resp.FailData("获取权益套餐服务失败", "Err:"+e.Error(), c)
+		return
+	}
+	raiServiceIdMap := make(map[int]*fms.ContractServiceTemplateItem)
+	for i := range raiServiceList {
+		raiServiceIdMap[raiServiceList[i].ServiceTemplateId] = raiServiceList[i]
+	}
+
+	// 除大小套餐外的其他套餐(下面的导出需要以此动态处理)
+	raiOtherService := make([]*fms.ContractServiceTemplateItem, 0)
+	raiServiceNotShowMap := make(map[int]struct{})
+	for _, v := range raiServiceList {
+		if v.Pid > 0 && raiServiceIdMap[v.Pid].Pid > 0 {
+			raiServiceNotShowMap[v.Pid] = struct{}{}
+			raiServiceNotShowMap[raiServiceIdMap[v.Pid].Pid] = struct{}{}
+		} else if v.Pid > 0 {
+			raiServiceNotShowMap[v.Pid] = struct{}{}
+		}
+	}
+	for _, v := range raiServiceList {
+		if _, ok := raiServiceNotShowMap[v.ServiceTemplateId]; !ok {
+			if v.Pid > 0 {
+				v.ParentTitle = raiServiceIdMap[v.Pid].Title
+			}
+			raiOtherService = append(raiOtherService, v)
+		}
+	}
+	otherServiceLen += len(raiOtherService)
+
+	//套餐金额map
+	serviceAmountMap := make(map[int]map[int]*fms.ContractServiceAmount)
+
+	// 套餐/开票/到款列表
+	serviceMap := make(map[int][]*fms.ContractServiceWithParentTitle)
+	serviceChartPermissionsMap := make(map[int][]int)
+	invoiceMap := make(map[int][]*fms.ContractInvoice)
+	paymentMap := make(map[int][]*fms.ContractInvoice)
+	maxInvoice := 0
+	maxPayment := 0
+	if len(registerIds) > 0 {
+		// 获取套餐信息
+		csCond := `cv.contract_register_id IN ?`
+		csPars := make([]interface{}, 0)
+		csPars = append(csPars, registerIds)
+		cs := new(fms.ContractService)
+		serviceList, e := cs.ListWithWithParentTitle(csCond, csPars)
+		if e != nil {
+			resp.FailData("获取合同套餐列表失败", "Err:"+e.Error(), c)
+			return
+		}
+		for i := range serviceList {
+			cid := serviceList[i].ContractRegisterId
+			if serviceMap[cid] == nil {
+				serviceMap[cid] = make([]*fms.ContractServiceWithParentTitle, 0)
+			}
+			serviceMap[cid] = append(serviceMap[cid], serviceList[i])
+			// 小套餐权限
+			if serviceChartPermissionsMap[cid] == nil {
+				serviceChartPermissionsMap[cid] = make([]int, 0)
+			}
+			if serviceList[i].ChartPermissionIds != "" {
+				ids := utils.JoinStr2IntArr(serviceList[i].ChartPermissionIds, ",")
+				serviceChartPermissionsMap[cid] = append(serviceChartPermissionsMap[cid], ids...)
+			}
+		}
+
+		// 获取合同的套餐金额信息
+		serviceAmountList, e := fms.GetContractServiceAmountByContractRegisterIds(registerIds)
+		if e != nil {
+			resp.FailData("获取合同的套餐金额信息失败", "Err:"+e.Error(), c)
+			return
+		}
+		for _, v := range serviceAmountList {
+			if _, ok := serviceAmountMap[v.ContractRegisterId]; !ok {
+				serviceAmountMap[v.ContractRegisterId] = make(map[int]*fms.ContractServiceAmount)
+			}
+			serviceAmountMap[v.ContractRegisterId][v.ProductId] = v
+		}
+		// 获取开票/到款详情, 并取最大的开票/到款数(用于动态扩展第二列表头)
+		ci := new(fms.ContractInvoice)
+		csCond = `contract_register_id IN ?`
+		csPars = make([]interface{}, 0)
+		csPars = append(csPars, registerIds)
+		invoiceList, e := ci.List(csCond, csPars, "")
+		if e != nil {
+			resp.FailData("获取开票/到款列表失败", "Err:"+e.Error(), c)
+			return
+		}
+		for k := range invoiceList {
+			cid := invoiceList[k].ContractRegisterId
+			if invoiceMap[cid] == nil {
+				invoiceMap[cid] = make([]*fms.ContractInvoice, 0)
+			}
+			if paymentMap[cid] == nil {
+				paymentMap[cid] = make([]*fms.ContractInvoice, 0)
+			}
+			if invoiceList[k].InvoiceType == fms.ContractInvoiceTypeMake {
+				invoiceMap[cid] = append(invoiceMap[cid], invoiceList[k])
+				continue
+			}
+			if invoiceList[k].InvoiceType == fms.ContractInvoiceTypePay {
+				paymentMap[cid] = append(paymentMap[cid], invoiceList[k])
+				continue
+			}
+		}
+		// 取最大开票/到款数
+		for j := range invoiceMap {
+			if len(invoiceMap[j]) > maxInvoice {
+				maxInvoice = len(invoiceMap[j])
+			}
+		}
+		for p := range paymentMap {
+			if len(paymentMap[p]) > maxPayment {
+				maxPayment = len(paymentMap[p])
+			}
+		}
+	}
+
+	// 生成Excel文件
+	xlsxFile := xlsx.NewFile()
+	style := xlsx.NewStyle()
+	alignment := xlsx.Alignment{
+		Horizontal: "center",
+		Vertical:   "center",
+		WrapText:   true,
+	}
+	style.Alignment = alignment
+	style.ApplyAlignment = true
+
+
+	sheetName := "预登记列表"
+	sheet, err := xlsxFile.AddSheet(sheetName)
+	if err != nil {
+		resp.FailData("新增Sheet失败", "Err:"+err.Error(), c)
+		return
+	}
+
+	// 1行表头
+	hInt := 0
+	titleRow := sheet.AddRow()
+	titleRow.SetHeight(40)
+	// 1行1列-右合并两格, 下合并1行
+	cell1 := titleRow.AddCell()
+	cell1.HMerge = 10
+
+	cell1.VMerge = 1
+	cell1.SetString("")
+	cell1.SetStyle(style)
+
+	// 右增两列空白格用于第一列合并, 否则后续单元格合并会有问题
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	hInt += 13
+
+	titleRow.AddCell().SetString("")
+	hInt += 1
+	// 1行2列
+	cell2 := titleRow.AddCell()
+	cell2.VMerge = 1
+	cell2.SetString("FICC大套餐")
+	cell2.SetStyle(style)
+	hInt++
+
+	// 1行3列-右合并小套餐数
+
+	cell3 := titleRow.AddCell()
+	hInt++
+	cell3.HMerge = permissionLen - 1
+	cell3.VMerge = 1
+	cell3.SetString("FICC小套餐")
+	cell3.SetStyle(style)
+	// 同上右增单元格小套餐数-1的空白单元格用于合并
+	for i := 0; i < permissionLen-1; i++ {
+		titleRow.AddCell().SetString("")
+		hInt++
+	}
+
+	for i := range otherService {
+		cellOther := titleRow.AddCell()
+		cellOther.VMerge = 1
+		hInt++
+		cellOther.SetString(otherService[i].Title)
+		cellOther.SetStyle(style)
+	}
+
+	//权益大套餐
+	cell4 := titleRow.AddCell()
+	cell4.HMerge = 1
+	cell4.VMerge = 1
+	cell4.SetString("权益大套餐")
+	cell4.SetStyle(style)
+	titleRow.AddCell().SetString("")
+	hInt += 2
+
+	//权益分行业套餐
+	cell5 := titleRow.AddCell()
+	//cell5.HMerge = 8
+	cell5.SetString("权益分行业套餐")
+	cell5.SetStyle(style)
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+	titleRow.AddCell().SetString("")
+
+	//第二行,前面几个单元格用于第一行的合并
+	titleRow2 := sheet.AddRow()
+	titleRow2.SetHeight(30)
+	for i := 0; i < hInt; i++ {
+		titleRow2.AddCell().SetString("")
+	}
+
+	//权益分行业套餐
+	row2Cell1 := titleRow2.AddCell()
+	row2Cell1.HMerge = 1
+	row2Cell1.SetString("医药")
+	row2Cell1.SetStyle(style)
+	titleRow2.AddCell().SetString("")
+
+	row2Cell2 := titleRow2.AddCell()
+	row2Cell2.HMerge = 1
+	row2Cell2.SetString("消费")
+	row2Cell2.SetStyle(style)
+	titleRow2.AddCell().SetString("")
+
+	row2Cell3 := titleRow2.AddCell()
+	row2Cell3.HMerge = 1
+	row2Cell3.SetString("科技")
+	row2Cell3.SetStyle(style)
+	titleRow2.AddCell().SetString("")
+
+	row2Cell4 := titleRow2.AddCell()
+	row2Cell4.HMerge = 1
+	row2Cell4.SetString("智造")
+	row2Cell4.SetStyle(style)
+	titleRow2.AddCell().SetString("")
+
+	row2Cell5 := titleRow2.AddCell()
+	row2Cell5.SetString("策略")
+	row2Cell5.SetStyle(style)
+
+	// 第三行表头
+	titleRow3 := sheet.AddRow()
+	titleRow3.SetHeight(60)
+	row3Title := make([]string, 0)
+	row3TitleParent := make([]string, 0)
+	row3Title = append(row3Title, "客户名称", "约定起始日期", "约定结束日期", "合同类型", "开票日期", "开票金额", "开票换算金额", "开票销售",
+		"到款日期", "到款金额", "到款换算金额", "到款销售", "开票备注", "到款备注")
+
+	row3Title = append(row3Title, "FICC大套餐")
+	for i := range permissionList {
+		row3Title = append(row3Title, permissionList[i].PermissionName)
+	}
+	// 其他套餐
+	for i := range otherService {
+		row3Title = append(row3Title, otherService[i].Title)
+	}
+	// 定义二级套餐名称
+	for i := range row3Title {
+		row3TitleParent = append(row3TitleParent, row3Title[i])
+	}
+	// 权益三级套餐
+	for i := range raiOtherService {
+		row3Title = append(row3Title, raiOtherService[i].Title)
+		if raiOtherService[i].ParentTitle != "" {
+			row3TitleParent = append(row3TitleParent, raiOtherService[i].ParentTitle)
+		} else {
+			row3TitleParent = append(row3TitleParent, raiOtherService[i].Title)
+		}
+	}
+
+	// 设置表头
+	for i := range row3Title {
+		v := titleRow3.AddCell()
+		v.SetString(row3Title[i])
+		v.SetStyle(style)
+	}
+
+	// 此处取第二行标题NameKeyMap, 后面的动态匹配
+	row2NameKeyMap := make(map[string]int)
+	for i := range row3Title {
+		row2NameKeyMap[row3Title[i]] = i
+	}
+
+	for _, v := range list {
+		k := -1 // 套餐匹配用
+		dataRow := sheet.AddRow()
+		dataRow.SetHeight(20)
+		dataRow.AddCell().SetString(v.CompanyName)
+		dataRow.AddCell().SetString(v.StartDate)
+		dataRow.AddCell().SetString(v.EndDate)
+		contractTypeString := ``
+		if v.HasPayment == 1 {
+			//代付
+			if v.ContractType == 1 {
+				contractTypeString = "代付合同(新签)"
+			} else if v.ContractType == 2 {
+				contractTypeString = "代付合同(续约)"
+			} else {
+				contractTypeString = "代付合同(补充)"
+			}
+		} else {
+			if v.ContractType == 1 {
+				contractTypeString = "自付合同(新签)"
+			} else if v.ContractType == 2 {
+				contractTypeString = "自付合同(续约)"
+			} else {
+				contractTypeString = "自付合同(补充)"
+			}
+		}
+		dataRow.AddCell().SetString(contractTypeString)
+		dataRow.AddCell().SetString(v.InvoiceDate)
+		originAmount := fmt.Sprintf("%.2f", v.OriginAmount)
+		dataRow.AddCell().SetString(originAmount)
+		amount := fmt.Sprintf("%.2f", v.Amount)
+		dataRow.AddCell().SetString(amount)
+		dataRow.AddCell().SetString(v.SellerName)
+
+		dataRow.AddCell().SetString(v.ArriveDate)
+		arriveOriginAmount := fmt.Sprintf("%.2f", v.ArriveOriginAmount)
+		dataRow.AddCell().SetString(arriveOriginAmount)
+		arriveAmount := fmt.Sprintf("%.2f", v.ArriveAmount)
+		dataRow.AddCell().SetString(arriveAmount)
+		dataRow.AddCell().SetString(v.SellerName)
+		dataRow.AddCell().SetString("")
+		dataRow.AddCell().SetString("")
+		k += 15
+		svList := serviceMap[v.ContractRegisterId]
+
+		// Ficc大套餐
+		col4Name := row3Title[k]
+		col4 := ""
+		if svList != nil && len(svList) > 0 {
+			for isv := range svList {
+				if svList[isv].Title == col4Name {
+					col4 = "是"
+					break
+				}
+			}
+		}
+		dataRow.AddCell().SetString(col4)
+
+		// 小套餐
+		serviceChartPermissionIds := serviceChartPermissionsMap[v.ContractRegisterId]
+		for i := 0; i < permissionLen; i++ {
+			k += 1
+			colName := row3Title[k]
+			chartPermissionId := permissionNameIdMap[colName]
+			if utils.InArray(chartPermissionId, serviceChartPermissionIds) {
+				dataRow.AddCell().SetString("是")
+			} else {
+				dataRow.AddCell().SetString("")
+			}
+		}
+
+		// 除大小套餐外的其他套餐(处理方式其实跟上面的大套餐一样, 只是中间隔了小套餐按照顺序要这么处理=_=!)
+		for i := 0; i < otherServiceLen; i++ {
+			k += 1
+			otherColName := row3Title[k]
+			otherCol := ""
+			if svList != nil && len(svList) > 0 {
+				for isv := range svList {
+					if svList[isv].Title == otherColName {
+						// 判断权益套餐权限时,需要同时判断父级name
+						if otherColName == "主观" || otherColName == "客观" {
+							if row3TitleParent[k] == svList[isv].ParentTitle {
+								otherCol = "是"
+								break
+							}
+						} else {
+							otherCol = "是"
+							break
+						}
+					}
+				}
+			}
+			dataRow.AddCell().SetString(otherCol)
+		}
+	}
+
+	// 输出文件
+	var buffer bytes.Buffer
+	_ = xlsxFile.Write(&buffer)
+	content := bytes.NewReader(buffer.Bytes())
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	fileName := sheetName + randStr + ".xlsx"
+
+	c.Writer.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, fileName))
+	c.Writer.Header().Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
+	http.ServeContent(c.Writer, c.Request, fileName, time.Now(), content)
+}

+ 2 - 1
models/fms/contract_invoice.go

@@ -531,7 +531,8 @@ func GetInvoiceListCurrencySum(condition string, pars []interface{}, groupRule s
 
 // PreRegisterListReq 预登记列表请求体
 type PreRegisterListReq struct {
-	Keyword string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
+	Keyword  string `json:"keyword" form:"keyword" binding:"omitempty" description:"关键词"`
+	IsExport int    `json:"is_export" form:"is_export" description:"是否导出: 0-否; 1-是"`
 	base.PageReq
 }
 

+ 1 - 0
routers/contract.go

@@ -46,4 +46,5 @@ func InitContract(rg *gin.RouterGroup) {
 	preRegisterGroup.POST("del", preRegister.Del)
 	preRegisterGroup.POST("detail", preRegister.Detail)
 	preRegisterGroup.POST("save", preRegister.Save)
+	preRegisterGroup.GET("export", preRegister.ExportPreRegister)
 }