package report

import (
	"LAPP_ACURA_MOM_BACKEND/grmi"
	svr "LAPP_ACURA_MOM_BACKEND/services/report"
	"LAPP_ACURA_MOM_BACKEND/web/middleware/jwts"
	"LAPP_ACURA_MOM_BACKEND/web/supports"
	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/core/router"
	"strings"
	"time"
)

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-09 16:52
 *
 ******************************************************************************/
func RegisterRouteMappingOfCustomizedQuery(party router.Party) {

	party = party.Party("/customizedquery")

	var service = svr.NewCustomizedQueryService()
	RegisterRouteOfProductFamilyInventoryQueryInCustomizedQuery(party, service)
	RegisterRouteOfProductFamilyInventoryExportExcelInCustomizedQuery(party, service)
	RegisterRouteMappingOfSeatQueryInCustomizedQuery(party, service)
	RegisterRouteMappingOfSeatExportExcelInCustomizedQuery(party, service)
	RegisterRouteMappingOfPackingRecordQueryInCustomizedQuery(party, service)
	RegisterRouteMappingOfPackingRecordExportExcelInCustomizedQuery(party, service)
	RegisterRouteMappingOfRepairQueryInCustomizedQuery(party, service)
	RegisterRouteMappingOfRepairAnalysis_CountByDateInCustomizedQuery(party, service)
	RegisterRouteMappingOfWorkLineDateReportInCustomizedQuery(party, service)
	RegisterRouteMappingOfWorkLineDateReportExportExcelInCustomizedQuery(party, service)
	RegisterRouteMappingOfAndonAnalysis_CountByDateInCustomizedQuery(party, service)
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteOfProductFamilyInventoryQueryInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中派生库存查询的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-09 16:52
 *
 ******************************************************************************/
func RegisterRouteOfProductFamilyInventoryQueryInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/productfamilyinventoryquery", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var productFamilyId string
		if productFamilyId, ok = urlParameters["productfamilyid"]; !ok {
			productFamilyId = ""
		}
		var projectId string
		if projectId, ok = urlParameters["projectid"]; !ok {
			projectId = ""
		}
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.ProductFamilyInventoryQuery(user, productFamilyId, projectId, serialOrderId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})

}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteOfProductFamilyInventoryExportExcelInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中导出派生库存查询结果的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-09 16:52
 *
 ******************************************************************************/
func RegisterRouteOfProductFamilyInventoryExportExcelInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/productfamilyinventoryquery_exportexcel", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var productFamilyId string
		if productFamilyId, ok = urlParameters["productfamilyid"]; !ok {
			productFamilyId = ""
		}
		var projectId string
		if projectId, ok = urlParameters["projectid"]; !ok {
			projectId = ""
		}
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.ProductFamilyInventoryExportExcel(user, productFamilyId, projectId, serialOrderId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})

}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfSeatQueryInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中座椅查询的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-09 16:52
 *
 ******************************************************************************/
func RegisterRouteMappingOfSeatQueryInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/seatquery", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var status string
		if status, ok = urlParameters["status"]; !ok {
			status = ""
		}
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.SeatQuery(user, serialOrderId, status)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfSeatExportExcelInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义导出座椅查询结果的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-09 16:52
 *
 ******************************************************************************/
func RegisterRouteMappingOfSeatExportExcelInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/seatquery_exportexcel", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var status string
		if status, ok = urlParameters["serialorderid"]; !ok {
			status = ""
		}
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.SeatExportExcel(user, serialOrderId, status)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfPackingRecordQueryInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中打包记录查询的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-10 17:38
 *
 ******************************************************************************/
func RegisterRouteMappingOfPackingRecordQueryInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/packingrecordquery", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.PackingRecordQuery(user, serialOrderId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfPackingRecordExportExcelInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中导出打包记录查询结果的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-10 17:38
 *
 ******************************************************************************/
func RegisterRouteMappingOfPackingRecordExportExcelInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/packingrecordquery_exportexcel", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var serialOrderId string
		if serialOrderId, ok = urlParameters["serialorderid"]; !ok {
			serialOrderId = ""
		}
		result, err := service.PackingRecordExportExcel(user, serialOrderId)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfRepairQueryInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中不良统计分析的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-10 10:31
 *
 ******************************************************************************/
func RegisterRouteMappingOfRepairQueryInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/repairquery", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		paging := grmi.NewPaging()
		paging.Fill(urlParameters)
		var workLineId string
		if workLineId, ok = urlParameters["worklineid"]; !ok {
			workLineId = ""
		}
		var artId string
		if artId, ok = urlParameters["artid"]; !ok {
			artId = ""
		}
		var repairReason string
		if repairReason, ok = urlParameters["repairreason"]; !ok {
			repairReason = ""
		}
		var beginTime *time.Time
		if urlParameter, ok := urlParameters["begintime"]; ok {
			origin, err := time.Parse(grmi.URLDateTimeFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			beginTime = &origin
		}
		var endTime *time.Time
		if urlParameter, ok := urlParameters["endtime"]; ok {
			origin, err := time.Parse(grmi.URLDateTimeFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			endTime = &origin
		}
		result, err := service.RepairQuery(user, paging, workLineId, artId, repairReason, beginTime, endTime)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfRepairAnalysis_CountByDateInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中产品缺陷率分析的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-10 10:31
 *
 ******************************************************************************/
func RegisterRouteMappingOfRepairAnalysis_CountByDateInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/repairanalysis-countbydate", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var beginDate *time.Time
		if urlParameter, ok := urlParameters["begindate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			beginDate = &origin
		}
		var endDate *time.Time
		if urlParameter, ok := urlParameters["enddate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			endDate = &origin
		}
		result, err := service.RepairAnalysis_CountByDate(user, beginDate, endDate)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfWorkLineDateReportInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中产线生产日报的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-16 15:44
 *
 ******************************************************************************/
func RegisterRouteMappingOfWorkLineDateReportInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/worklinedatereport", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var workLineId string
		if workLineId, ok = urlParameters["worklineid"]; !ok {
			workLineId = ""
		}
		var beginDate *time.Time
		if urlParameter, ok := urlParameters["begindate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			beginDate = &origin
		}
		var endDate *time.Time
		if urlParameter, ok := urlParameters["enddate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			endDate = &origin
		}
		result, err := service.WorkLineDateReport(user, workLineId, beginDate, endDate)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfWorkLineDateReportInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中导出产线生产日报的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-16 16:22
 *
 ******************************************************************************/
func RegisterRouteMappingOfWorkLineDateReportExportExcelInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/worklinedatereport_exportexcel", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		var workLineId string
		if workLineId, ok = urlParameters["worklineid"]; !ok {
			workLineId = ""
		}
		var beginDate *time.Time
		if urlParameter, ok := urlParameters["begindate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			beginDate = &origin
		}
		var endDate *time.Time
		if urlParameter, ok := urlParameters["enddate"]; ok {
			origin, err := time.Parse(grmi.URLDateFormat, urlParameter)
			if err != nil {
				supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
				return
			}
			endDate = &origin
		}
		result, err := service.WorkLineDateReportExportExcel(user, workLineId, beginDate, endDate)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : RegisterRouteMappingOfAndonAnalysis_CountByDateInCustomizedQuery
 *-----------------------------------------------------------------------------
 *
 * @Description              : 注册自定义查询中导出产线生产日报的路由
 *
 * @Function Parameters      : 路由分组
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-16 16:22
 *
 ******************************************************************************/
func RegisterRouteMappingOfAndonAnalysis_CountByDateInCustomizedQuery(party router.Party, service svr.CustomizedQueryService) {

	party.Get("/andonanalysis-countbydate", func(ctx iris.Context) {
		user, ok := jwts.ParseToken(ctx)
		if !ok {
			return
		}
		urlParameters := ctx.URLParams()
		workLineId := GetStringFormUrlParameter(urlParameters, "worklineid")
		workPlaceIds := GetStringsFormUrlParameter(urlParameters, "workplaceids")
		ruleDesc := GetStringFormUrlParameter(urlParameters, "ruledesc")
		beginDate, err := GetNullableDateFormUrlParameter(urlParameters, "begindate")
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		endDate, err := GetNullableDateFormUrlParameter(urlParameters, "enddate")
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		result, err := service.AndonAnalysis_CountByDate(user, workLineId, workPlaceIds, ruleDesc, beginDate, endDate)
		if err != nil {
			supports.Error(ctx, iris.StatusBadRequest, err.Error(), nil)
			return
		}
		supports.Ok(ctx, supports.OptionSuccess, result)
	})
}

/******************************************************************************
 *
 * @Function Name            : GetStringFormUrlParameter
 *-----------------------------------------------------------------------------
 *
 * @Description              : 从Url参数获取string的值
 *
 * @Function Parameters      : Url参数集合
 *
 * @Function Parameters      : 参数名字
 *
 * @Return Value             : 解析出的值
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-17 10:17
 *
 ******************************************************************************/
func GetStringFormUrlParameter(urlParameters map[string]string, parameterName string) string {
	if urlParameter, ok := urlParameters[parameterName]; ok {
		return urlParameter
	} else {
		return ""
	}
}

/******************************************************************************
 *
 * @Function Name            : GetStringsFormUrlParameter
 *-----------------------------------------------------------------------------
 *
 * @Description              : 从Url参数获取[]string的值
 *
 * @Function Parameters      : Url参数集合
 *
 * @Function Parameters      : 参数名字
 *
 * @Return Value             : 解析出的值
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-17 10:48
 *
 ******************************************************************************/
func GetStringsFormUrlParameter(urlParameters map[string]string, parameterName string) []string {
	if urlParameter, ok := urlParameters[parameterName]; ok {
		return strings.Split(urlParameter, ",")
	} else {
		return nil
	}
}

/******************************************************************************
 *
 * @Function Name            : GetNullableGoDateFormUrlParameter
 *-----------------------------------------------------------------------------
 *
 * @Description              : 从Url参数获取类型为*time.Time的日期值
 *
 * @Function Parameters      : Url参数集合
 *
 * @Function Parameters      : 参数名字
 *
 * @Return Value             : 解析出的值
 *
 * @Return Value             : 执行时发生的错误
 *
 * @Author                   : chenglei.wei@le-it.com.cn
 *
 * @Date                     : 2022-03-17 10:32
 *
 ******************************************************************************/
func GetNullableDateFormUrlParameter(urlParameters map[string]string, parameterName string) (*time.Time, error) {
	if urlParameter, ok := urlParameters[parameterName]; ok {
		value, err := time.Parse(grmi.URLDateFormat, urlParameter)
		if err != nil {
			return nil, err
		}
		return &value, nil
	}
	return nil, nil
}
