xingzai 1 年間 前
コミット
11ed59e34a
1 ファイル変更275 行追加8 行削除
  1. 275 8
      controllers/cygx/contract_allocation.go

+ 275 - 8
controllers/cygx/contract_allocation.go

@@ -4,6 +4,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"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/company"
@@ -12,7 +13,8 @@ import (
 	"hongze/hz_crm_api/models/system"
 	cygxService "hongze/hz_crm_api/services/cygx"
 	"hongze/hz_crm_api/utils"
-	"math"
+	"os"
+	"path/filepath"
 	"strconv"
 	"strings"
 	"time"
@@ -275,6 +277,7 @@ func (this *ContractAllocationController) CompanyContractList() {
 					br.ErrMsg = "获取合同列表失败,Err:" + err.Error()
 					return
 				}
+
 				mapContractCode := make(map[string]int)
 				for _, v := range listContract {
 					mapContractCode[v.ContractCode] = v.ContractId
@@ -302,10 +305,9 @@ func (this *ContractAllocationController) CompanyContractList() {
 		Paging: page,
 		List:   list,
 	}
-
 	//导出excel
 	if isExport {
-		//IncrementalCompanyListExport(this, dataType, resp, br)
+		CompanyContractListExport(this, resp, br)
 		return
 	}
 	br.Ret = 200
@@ -314,6 +316,137 @@ func (this *ContractAllocationController) CompanyContractList() {
 	br.Data = resp
 }
 
+// CompanyContractListExport 导出Excel
+func CompanyContractListExport(this *ContractAllocationController, resp cygx.CompanyContractListResp, br *models.BaseResponse) {
+	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
+
+	sheel, err := xlsxFile.AddSheet("派点导出数据")
+	if err != nil {
+		br.Msg = "新增Sheet失败"
+		br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
+		return
+	}
+	sheel.SetColWidth(0, 0, 30)
+	sheel.SetColWidth(1, 1, 15)
+	sheel.SetColWidth(2, 2, 15)
+	sheel.SetColWidth(3, 3, 18)
+
+	titleRow := sheel.AddRow()
+
+	cellA := titleRow.AddCell()
+	cellA.SetStyle(style)
+	cellA.SetValue("合同编号")
+
+	cellB := titleRow.AddCell()
+	cellB.SetStyle(style)
+	cellB.SetValue("转正类型")
+
+	cellC := titleRow.AddCell()
+	cellC.SetStyle(style)
+	cellC.SetValue("合同类型")
+
+	cellD := titleRow.AddCell()
+	cellD.SetStyle(style)
+	cellD.SetValue("公司名称")
+
+	cellE := titleRow.AddCell()
+	cellE.SetStyle(style)
+	cellE.SetValue("所属销售")
+
+	cellF := titleRow.AddCell()
+	cellF.SetStyle(style)
+	cellF.SetValue("合同金额")
+
+	cellG := titleRow.AddCell()
+	cellG.SetStyle(style)
+	cellG.SetValue("合同期限")
+
+	cellH := titleRow.AddCell()
+	cellH.SetStyle(style)
+	cellH.SetValue("签约套餐")
+
+	cellI := titleRow.AddCell()
+	cellI.SetStyle(style)
+	cellI.SetValue("状态")
+
+	for _, v := range resp.List {
+		dataRow := sheel.AddRow()
+		dataRow.SetHeight(20)
+
+		cellA := dataRow.AddCell()
+		cellA.SetStyle(style)
+		cellA.SetValue(v.ContractCode)
+
+		cellB := dataRow.AddCell()
+		cellB.SetStyle(style)
+		cellB.SetValue(v.FormalType)
+
+		cellC := dataRow.AddCell()
+		cellC.SetStyle(style)
+		cellC.SetValue(v.ContractType)
+
+		cellD := dataRow.AddCell()
+		cellD.SetStyle(style)
+		cellD.SetValue(v.CompanyName)
+
+		cellE := dataRow.AddCell()
+		cellE.SetStyle(style)
+		cellE.SetValue(v.SellerName)
+
+		cellF := dataRow.AddCell()
+		cellF.SetStyle(style)
+		cellF.SetValue(v.Money)
+
+		cellG := dataRow.AddCell()
+		cellG.SetStyle(style)
+		cellG.SetValue(fmt.Sprint(v.StartDate, " ~  ", v.EndDate))
+
+		cellH := dataRow.AddCell()
+		cellH.SetStyle(style)
+		cellH.SetValue(v.PermissionName)
+
+		cellI := dataRow.AddCell()
+		cellI.SetStyle(style)
+		if v.IsAllocation == 1 {
+			cellI.SetValue("已派点")
+		} else {
+			cellI.SetValue("未派点")
+		}
+	}
+	err = xlsxFile.Save(downLoadnFilePath)
+	if err != nil {
+		br.Msg = "保存文件失败"
+		br.ErrMsg = "保存文件失败"
+		return
+	}
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	downloadFileName := "派点导出数据_" + randStr + ".xlsx"
+	this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
+	defer func() {
+		os.Remove(downLoadnFilePath)
+	}()
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "导出成功"
+}
+
 // @Title  详情
 // @Description 获取详情接口
 // @Param   CompanyContractId   query   int  true       "ID"
@@ -785,7 +918,7 @@ func (this *ContractAllocationController) CompanyContractStatistics() {
 		item := new(cygx.AllocationRealNameStatisticsListResp)
 		item.RealName = "合计"
 		item.TotalRelatedContract = mapPermissionAllocation[k]
-		item.TotalDispatchPoint = fmt.Sprint(mapPermissionMoney[k], "/", mapPermissionMoneyAvg[k])
+		item.TotalDispatchPoint = fmt.Sprint(utils.SubFloatToString(mapPermissionMoney[k], 2), "/", mapPermissionMoneyAvg[k])
 		item.GroupProportion = "100%"
 
 		// 部门占比
@@ -796,11 +929,9 @@ func (this *ContractAllocationController) CompanyContractStatistics() {
 		item.DepartmentProportion += "%"
 
 		mapPermissionUser[k] = append(mapPermissionUser[k], item)
-		var ratio float64       // 使用一个变量作为精度范围, 比如 2 位小数时,精度范围应该为 100
-		ratio = math.Pow(10, 2) // 计算精度范围,2 位小数 = 100
 		item = new(cygx.AllocationRealNameStatisticsListResp)
 		item.RealName = "平均"
-		item.TotalRelatedContract = math.Round(mapPermissionAllocation[k]/float64(lenUser)) * ratio / ratio
+		item.TotalRelatedContract = utils.SubFloatToFloat(mapPermissionAllocation[k]/float64(lenUser), 2)
 		item.TotalDispatchPoint = utils.SubFloatToString(mapPermissionMoney[k]/float64(lenUser), 2)
 
 		//组内占比
@@ -839,7 +970,7 @@ func (this *ContractAllocationController) CompanyContractStatistics() {
 	resp.TotalMoney = totalMoney
 	//导出excel
 	if isExport {
-		//IncrementalCompanyListExport(this, dataType, resp, br)
+		CompanyContractStatisticsExport(this, resp, br)
 		return
 	}
 	br.Ret = 200
@@ -847,3 +978,139 @@ func (this *ContractAllocationController) CompanyContractStatistics() {
 	br.Msg = "获取成功"
 	br.Data = resp
 }
+
+// CompanyContractStatisticsExport 导出Excel
+func CompanyContractStatisticsExport(this *ContractAllocationController, resp cygx.CygxAllocationCompanyContractDetailStatisticsResp, br *models.BaseResponse) {
+	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
+
+	sheel, err := xlsxFile.AddSheet("研究员派点统计")
+	if err != nil {
+		br.Msg = "新增Sheet失败"
+		br.ErrMsg = "新增Sheet失败,Err:" + err.Error()
+		return
+	}
+	sheel.SetColWidth(0, 0, 30)
+	sheel.SetColWidth(1, 1, 15)
+	sheel.SetColWidth(2, 2, 15)
+	sheel.SetColWidth(3, 3, 18)
+
+	titleRow := sheel.AddRow()
+
+	cellA := titleRow.AddCell()
+	cellA.SetStyle(style)
+	cellA.SetValue("组别")
+
+	cellB := titleRow.AddCell()
+	cellB.SetStyle(style)
+	cellB.SetValue("研究员")
+
+	cellC := titleRow.AddCell()
+	cellC.SetStyle(style)
+	cellC.SetValue("关联合同")
+
+	cellD := titleRow.AddCell()
+	cellD.SetStyle(style)
+	cellD.SetValue("总派点")
+
+	cellE := titleRow.AddCell()
+	cellE.SetStyle(style)
+	cellE.SetValue("组内占比")
+
+	cellF := titleRow.AddCell()
+	cellF.SetStyle(style)
+	cellF.SetValue("部门占比")
+
+	for _, v := range resp.List {
+		for k2, v2 := range v.List {
+
+			dataRow := sheel.AddRow()
+			dataRow.SetHeight(20)
+
+			cellA := dataRow.AddCell()
+			cellA.SetStyle(style)
+			cellA.SetValue(v.ChartPermissionName)
+			if k2 < len(v.List)-1 {
+				cellA.VMerge = 1
+			}
+
+			cellB := dataRow.AddCell()
+			cellB.SetStyle(style)
+			cellB.SetValue(v2.RealName)
+
+			cellC := dataRow.AddCell()
+			cellC.SetStyle(style)
+			cellC.SetValue(v2.TotalRelatedContract)
+
+			cellD := dataRow.AddCell()
+			cellD.SetStyle(style)
+			cellD.SetValue(v2.TotalDispatchPoint)
+
+			cellE := dataRow.AddCell()
+			cellE.SetStyle(style)
+			cellE.SetValue(v2.GroupProportion)
+
+			cellF := dataRow.AddCell()
+			cellF.SetStyle(style)
+			cellF.SetValue(v2.DepartmentProportion)
+		}
+	}
+
+	titleRow = sheel.AddRow()
+	cellA = titleRow.AddCell()
+	cellA.HMerge = 1
+	cellA.SetStyle(style)
+	cellA.SetValue("部门合计")
+
+	cellB = titleRow.AddCell()
+	cellB.SetStyle(style)
+	cellB.SetValue("")
+
+	cellC = titleRow.AddCell()
+	cellC.SetStyle(style)
+	cellC.SetValue(resp.TotalContract)
+
+	cellD = titleRow.AddCell()
+	cellD.SetStyle(style)
+	cellD.SetValue(resp.TotalMoney)
+
+	cellE = titleRow.AddCell()
+	cellE.SetStyle(style)
+	cellE.SetValue("-")
+
+	cellF = titleRow.AddCell()
+	cellF.SetStyle(style)
+	cellF.SetValue("100%")
+
+	err = xlsxFile.Save(downLoadnFilePath)
+	if err != nil {
+		br.Msg = "保存文件失败"
+		br.ErrMsg = "保存文件失败"
+		return
+	}
+	randStr := time.Now().Format(utils.FormatDateTimeUnSpace)
+	downloadFileName := "研究员派点统计_" + randStr + ".xlsx"
+	this.Ctx.Output.Download(downLoadnFilePath, downloadFileName)
+	defer func() {
+		os.Remove(downLoadnFilePath)
+	}()
+	br.Ret = 200
+	br.Success = true
+	br.Msg = "导出成功"
+}