// Copyright (c) Shenyang Leading Edge Intelligent Technology Co., Ltd. All rights reserved.

package implments

import (
	dal "LAPP_LF_MOM_BACKEND/dao/log"
	"LAPP_LF_MOM_BACKEND/db"
	"LAPP_LF_MOM_BACKEND/grmi"
	meta "LAPP_LF_MOM_BACKEND/meta/log"
	bmodel "LAPP_LF_MOM_BACKEND/models/base"
	model "LAPP_LF_MOM_BACKEND/models/log"
	"LAPP_LF_MOM_BACKEND/utils"
	"LAPP_LF_MOM_BACKEND/web/models"
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize/v2"
)

/******************************************************************************
 *
 * @Description              : ArtDemandLstCache的默认查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-11 16:10:25
 *
 ******************************************************************************/
var DefaultConditionOfArtDemandLstCache = grmi.NewCondition(
	map[string]grmi.ConditionItem{
		meta.ArtDemandLstCache_DemandId.UrlParameterName:           grmi.NewConditionItem(meta.ArtDemandLstCache_DemandId, grmi.Equal, true),
		meta.ArtDemandLstCache_Pos.UrlParameterName:                grmi.NewConditionItem(meta.ArtDemandLstCache_Pos, grmi.Equal, true),
		meta.ArtDemandLstCache_ArtId.UrlParameterName:              grmi.NewConditionItem(meta.ArtDemandLstCache_ArtId, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandTime.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandYear.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandYear, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandMonth.UrlParameterName:        grmi.NewConditionItem(meta.ArtDemandLstCache_DemandMonth, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandWeek.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandWeek, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandDate.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandDate, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandHour.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandHour, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandQty.UrlParameterName:          grmi.NewConditionItem(meta.ArtDemandLstCache_DemandQty, grmi.Equal, false),
		meta.ArtDemandLstCache_QtyUomId.UrlParameterName:           grmi.NewConditionItem(meta.ArtDemandLstCache_QtyUomId, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandObjId.UrlParameterName:        grmi.NewConditionItem(meta.ArtDemandLstCache_DemandObjId, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandType.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_DemandType, grmi.Equal, false),
		meta.ArtDemandLstCache_Status.UrlParameterName:             grmi.NewConditionItem(meta.ArtDemandLstCache_Status, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandSendTime.UrlParameterName:     grmi.NewConditionItem(meta.ArtDemandLstCache_DemandSendTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandRecieveTime.UrlParameterName:  grmi.NewConditionItem(meta.ArtDemandLstCache_DemandRecieveTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandConfirmTime.UrlParameterName:  grmi.NewConditionItem(meta.ArtDemandLstCache_DemandConfirmTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandCloseTime.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCloseTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandDeliveredQty.UrlParameterName: grmi.NewConditionItem(meta.ArtDemandLstCache_DemandDeliveredQty, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandCtrlPara1.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlPara1, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandCtrlPara2.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlPara2, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandCtrlPara3.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlPara3, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandCtrlPara4.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlPara4, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandCtrlTime1.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlTime1, grmi.Approximate, false),
		meta.ArtDemandLstCache_DemandCtrlTime2.UrlParameterName:    grmi.NewConditionItem(meta.ArtDemandLstCache_DemandCtrlTime2, grmi.Approximate, false),
		meta.ArtDemandLstCache_OrderStatus.UrlParameterName:        grmi.NewConditionItem(meta.ArtDemandLstCache_OrderStatus, grmi.Equal, false),
		meta.ArtDemandLstCache_UnproducedQty.UrlParameterName:      grmi.NewConditionItem(meta.ArtDemandLstCache_UnproducedQty, grmi.Equal, false),
		meta.ArtDemandLstCache_OrderType.UrlParameterName:          grmi.NewConditionItem(meta.ArtDemandLstCache_OrderType, grmi.Equal, false),
		meta.ArtDemandLstCache_ProjectId.UrlParameterName:          grmi.NewConditionItem(meta.ArtDemandLstCache_ProjectId, grmi.Equal, false),
		meta.ArtDemandLstCache_DemandStatus.UrlParameterName:       grmi.NewConditionItem(meta.ArtDemandLstCache_DemandStatus, grmi.Equal, false),
		meta.ArtDemandLstCache_PlanResourceId.UrlParameterName:     grmi.NewConditionItem(meta.ArtDemandLstCache_PlanResourceId, grmi.Equal, false),
		meta.ArtDemandLstCache_PlanStartDate.UrlParameterName:      grmi.NewConditionItem(meta.ArtDemandLstCache_PlanStartDate, grmi.Approximate, false),
		meta.ArtDemandLstCache_PlanEndDate.UrlParameterName:        grmi.NewConditionItem(meta.ArtDemandLstCache_PlanEndDate, grmi.Approximate, false),
		meta.ArtDemandLstCache_PlanStartTime.UrlParameterName:      grmi.NewConditionItem(meta.ArtDemandLstCache_PlanStartTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_PlanEndTime.UrlParameterName:        grmi.NewConditionItem(meta.ArtDemandLstCache_PlanEndTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_ActStartTime.UrlParameterName:       grmi.NewConditionItem(meta.ArtDemandLstCache_ActStartTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_ActEndTime.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_ActEndTime, grmi.Approximate, false),
		meta.ArtDemandLstCache_LastModify.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_LastModify, grmi.Approximate, false),
		meta.ArtDemandLstCache_LastUser.UrlParameterName:           grmi.NewConditionItem(meta.ArtDemandLstCache_LastUser, grmi.Equal, false),
		meta.ArtDemandLstCache_CreateTime.UrlParameterName:         grmi.NewConditionItem(meta.ArtDemandLstCache_CreateTime, grmi.Approximate, false),
	},
	nil,
)

/******************************************************************************
 *
 * @Description              : ArtDemandLstCache的默认分页查询条件
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-09 14:00:44
 *
 ******************************************************************************/
var DefaultConditionOfArtDemandLstCacheAndPaging = grmi.NewCondition(
	DefaultConditionOfArtDemandLstCache.Items,
	grmi.NewPaging(),
)

/******************************************************************************
 *
 * @Struct Name              : ArtDemandLstCacheServiceImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : ArtDemandLstCache的服务实现
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-09 14:00:44
 *
 ******************************************************************************/
type ArtDemandLstCacheServiceImplement struct {
}

/******************************************************************************
 *
 * @Function Name            : NewArtDemandLstCacheDAOImplement
 *-----------------------------------------------------------------------------
 *
 * @Description              : 创建一个ArtDemandLstCacheDAOImplement实例
 *
 * @Return Value             : ArtDemandLstCacheDAOImplement实例
 *
 * @Author                   : 代码生成器创建
 *
 * @Date                     : 2021-06-09 14:00:44
 *
 ******************************************************************************/
func NewArtDemandLstCacheServiceImplement() *ArtDemandLstCacheServiceImplement {
	return &ArtDemandLstCacheServiceImplement{}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.InsertOne
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) InsertOne(user *models.Usertab, entity *model.ArtDemandLstCache) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "InsertOneArtDemandLstCache", "插入一个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.DemandId, entity.Pos)
	if err != nil {
		return err
	}
	if record != nil {
		return grmi.NewBusinessError("已经存在相同主键的记录!")
	}
	err = dao.InsertOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.DeleteOne
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) DeleteOne(user *models.Usertab, demandId string, pos int) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "DeleteOneArtDemandLstCache", "删除一个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(demandId, pos)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.DeleteOne(demandId, pos)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.SelectOne
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) SelectOne(user *models.Usertab, demandId string, pos int) (*model.ArtDemandLstCache, error) {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "SelectOneArtDemandLstCache", "查询一个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	result, err := dao.SelectOne(demandId, pos)
	if err != nil {
		return nil, err
	}
	return result, nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.UpdateOne
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) UpdateOne(user *models.Usertab, entity *model.ArtDemandLstCache) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "UpdateOneArtDemandLstCache", "修改一个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	record, err := dao.SelectOne(entity.DemandId, entity.Pos)
	if err != nil {
		return err
	}
	if record == nil {
		return grmi.NewBusinessError("不存在指定记录!")
	}
	err = dao.UpdateOne(entity)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.Insert
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) Insert(user *models.Usertab, entities *[]model.ArtDemandLstCache) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "InsertArtDemandLstCache", "插入多个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, entity.Pos)
		if err != nil {
			return err
		}
		if record != nil {
			return grmi.NewBusinessError("已经存在相同主键的记录!")
		}
	}

	err := dao.Insert(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.Delete
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) Delete(user *models.Usertab, entities *[]model.ArtDemandLstCache) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "DeleteArtDemandLstCache", "删除多个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, entity.Pos)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Delete(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.Select
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) Select(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "SelectArtDemandLstCache", "查询ArtDemandLstCache")

	condition := DefaultConditionOfArtDemandLstCacheAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	condition.OrderByFields = append(condition.OrderByFields, meta.ArtDemandLstCache_DemandCtrlPara1, meta.ArtDemandLstCache_DemandCtrlPara2)
	if condition.Fill(urlParameters) {
		result, err := dao.SelectAndPaging(condition.Paging, predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	} else {
		result, err := dao.Select(predicates, condition.OrderByFields)
		if err != nil {
			return nil, err
		}
		return result, nil
	}
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.Update
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) Update(user *models.Usertab, entities *[]model.ArtDemandLstCache) error {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "UpdateArtDemandLstCache", "修改多个ArtDemandLstCache")

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)

	for _, entity := range *entities {
		record, err := dao.SelectOne(entity.DemandId, entity.Pos)
		if err != nil {
			return err
		}
		if record == nil {
			return grmi.NewBusinessError("不存在指定记录!")
		}
	}

	err := dao.Update(entities)
	if err != nil {
		return err
	}
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstService.SyncHronousData
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) SyncHronousDataCache(plantNr int, filePath string) (err error) {
	//grmi.Log(user, "/services/om/implments/ArtDemandLstService.service.impl.go", "SyncHronousData", "同步Portal的WorkOrder订单")
	f, err := excelize.OpenFile(filePath)
	if err != nil {
		return
	}
	// 获取 ArtDemandHead 上所有单元格
	rows, err := f.GetRows("ArtDemandHeadCache")
	if err != nil {
		return err
	}
	heads := make([]model.SyncArtDemandHeadCache, 0)
	for k, row := range rows {
		if k == 0 {
			continue
		}
		head := model.SyncArtDemandHeadCache{}
		head.PlantNr = utils.ValueToInt(row[0], 0)
		head.ArtId = utils.ValueToString(row[1], "")
		head.DemandId = utils.ValueToString(row[2], "")
		head.DemandType = utils.ValueToString(row[3], "")
		head.DemandYear = utils.ValueToInt(row[4], 0)
		head.DemandMonth = utils.ValueToInt(row[5], 0)
		head.DemandWeek = utils.ValueToInt(row[6], 0)
		DemandDateStr := utils.ValueToString(row[7], "")
		DemandDate, err := utils.TimeParseyyyyMMdd(DemandDateStr)
		if err != nil {
			return err
		}
		head.DemandDate = grmi.Date(DemandDate)
		head.DemandHour = utils.ValueToInt(row[8], 0)
		DemandTimeStr := utils.ValueToString(row[9], "")
		DemandTime, err := utils.TimeParse(DemandTimeStr)
		if err != nil {
			return err
		}
		head.DemandTime = grmi.DateTime(DemandTime)
		head.DemandQty = utils.ValueToFloat(row[10], 0.0)
		head.QtyUomId = utils.ValueToString(row[11], "")
		head.Status = utils.ValueToInt(row[12], 0)
		DemandSendTimeStr := utils.ValueToString(row[13], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandSendTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[14], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandRecieveTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[15], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandConfirmTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[16], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandCloseTime = grmi.DateTime(DemandSendTime)
		}
		head.DemandDeliveredQty = utils.ValueToFloat(row[17], 0.0)
		head.DemandCtrlPara1 = utils.ValueToInt(row[18], 0)
		head.DemandCtrlPara2 = utils.ValueToInt(row[19], 0)
		head.DemandCtrlPara3 = utils.ValueToString(row[20], "")
		head.DemandCtrlPara4 = utils.ValueToString(row[21], "")
		DemandSendTimeStr = utils.ValueToString(row[22], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandCtrlTime1 = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[23], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.DemandCtrlTime2 = grmi.DateTime(DemandSendTime)
		}
		head.OrderStatus = utils.ValueToInt(row[24], 0)
		head.UnproducedQty = utils.ValueToFloat(row[25], 0.0)
		head.OrderType = utils.ValueToString(row[26], "")
		head.WorkOrderId = utils.ValueToString(row[27], "")
		head.ProjectId = utils.ValueToString(row[28], "")
		head.DemandStatus = utils.ValueToInt(row[29], 0)
		head.DemandCtrlPara5 = utils.ValueToInt(row[30], 0)
		head.DemandCtrlPara6 = utils.ValueToInt(row[31], 0)
		head.ChangeStatus = utils.ValueToInt(row[32], 0)
		head.PlanResourceId = utils.ValueToString(row[33], "")
		DemandSendTimeStr = utils.ValueToString(row[34], "")
		if DemandSendTimeStr != "0001-01-01" {
			DemandSendTime, err := utils.TimeParseyyyyMMdd(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.PlanStartDate = grmi.Date(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[35], "")
		if DemandSendTimeStr != "0001-01-01" {
			DemandSendTime, err := utils.TimeParseyyyyMMdd(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.PlanEndDate = grmi.Date(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[36], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.PlanStartTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[37], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.PlanEndTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[38], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.ActStartTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[39], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.ActEndTime = grmi.DateTime(DemandSendTime)
		}
		head.CancelQty = utils.ValueToFloat(row[40], 0.0)
		LastModifyStr := utils.ValueToString(row[41], "")
		LastModify, err := utils.TimeParse(LastModifyStr)
		if err != nil {
			return err
		}
		head.LastModify = grmi.DateTime(LastModify)
		head.LastUser = utils.ValueToString(row[42], "")
		DemandSendTimeStr = utils.ValueToString(row[43], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			head.CreateTime = grmi.DateTime(DemandSendTime)
		}
		heads = append(heads, head)
	}
	// 获取 ArtDemandLst 上所有单元格
	rows, err = f.GetRows("ArtDemandLstCache")
	if err != nil {
		return err
	}
	lst := make([]model.SyncArtDemandLstCache, 0)
	for k, row := range rows {
		if k == 0 {
			continue
		}
		one := model.SyncArtDemandLstCache{}
		one.PlantNr = utils.ValueToInt(row[0], 0)
		one.DemandId = utils.ValueToString(row[1], "")
		one.Pos = utils.ValueToInt(row[2], 0)
		one.ArtId = utils.ValueToString(row[3], "")
		DemandTimeStr := utils.ValueToString(row[4], "")
		if DemandTimeStr != "0001-01-01 00:00:00" {
			DemandTime, _ := utils.TimeParse(DemandTimeStr)
			one.DemandTime = grmi.DateTime(DemandTime)
		}
		one.DemandYear = utils.ValueToInt(row[5], 0)
		one.DemandMonth = utils.ValueToInt(row[6], 0)
		one.DemandWeek = utils.ValueToInt(row[7], 0)
		DemandDateStr := utils.ValueToString(row[8], "")
		DemandDate, _ := utils.TimeParseyyyyMMdd(DemandDateStr)
		one.DemandDate = grmi.Date(DemandDate)
		one.DemandHour = utils.ValueToInt(row[9], 0)
		one.DemandQty = utils.ValueToFloat(row[10], 0.0)
		one.QtyUomId = utils.ValueToString(row[11], "")
		one.DemandObjId = utils.ValueToString(row[12], "")
		one.DemandType = utils.ValueToString(row[13], "")
		one.Status = utils.ValueToInt(row[14], 0)
		DemandSendTimeStr := utils.ValueToString(row[15], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandSendTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[16], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandRecieveTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[17], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandConfirmTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[18], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandCloseTime = grmi.DateTime(DemandSendTime)
		}
		one.DemandDeliveredQty = utils.ValueToFloat(row[19], 0.0)
		one.DemandCtrlPara1 = utils.ValueToInt(row[20], 0)
		one.DemandCtrlPara2 = utils.ValueToInt(row[21], 0)
		one.DemandCtrlPara3 = utils.ValueToString(row[22], "")
		one.DemandCtrlPara4 = utils.ValueToString(row[23], "")
		DemandSendTimeStr = utils.ValueToString(row[24], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandCtrlTime1 = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[25], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.DemandCtrlTime2 = grmi.DateTime(DemandSendTime)
		}
		one.OrderStatus = utils.ValueToInt(row[26], 0)
		one.UnproducedQty = utils.ValueToFloat(row[27], 0.0)
		one.OrderType = utils.ValueToString(row[28], "")
		one.ProjectId = utils.ValueToString(row[29], "")
		one.DemandStatus = utils.ValueToInt(row[30], 0)
		one.PlanResourceId = utils.ValueToString(row[31], "")
		DemandSendTimeStr = utils.ValueToString(row[32], "")
		if DemandSendTimeStr != "0001-01-01" {
			DemandSendTime, err := utils.TimeParseyyyyMMdd(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.PlanStartDate = grmi.Date(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[33], "")
		if DemandSendTimeStr != "0001-01-01" {
			DemandSendTime, err := utils.TimeParseyyyyMMdd(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.PlanEndDate = grmi.Date(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[34], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.PlanStartTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[35], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.PlanEndTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[36], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.ActStartTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[37], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, err := utils.TimeParse(DemandSendTimeStr)
			if err != nil {
				return err
			}
			one.ActEndTime = grmi.DateTime(DemandSendTime)
		}
		DemandSendTimeStr = utils.ValueToString(row[38], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.LastModify = grmi.DateTime(DemandSendTime)
		}
		one.LastUser = utils.ValueToString(row[39], "")
		DemandSendTimeStr = utils.ValueToString(row[40], "")
		if DemandSendTimeStr != "0001-01-01 00:00:00" {
			DemandSendTime, _ := utils.TimeParse(DemandSendTimeStr)
			one.CreateTime = grmi.DateTime(DemandSendTime)
		}
		lst = append(lst, one)
	}

	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	err = session.Begin()
	if err != nil {
		return
	}
	dao := dal.NewArtDemandHeadCacheDAO(session, plantNr, "syncPost")
	logdao := dal.NewArtDemandLstCacheDAO(session, plantNr, "syncPost")
	err = dao.DeleteWhere([]grmi.Predicate{meta.ArtDemandHeadCache_PlantNr.NewPredicate(grmi.Equal, bmodel.PlantNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	err = logdao.DeleteWhere([]grmi.Predicate{meta.ArtDemandLstCache_PlantNr.NewPredicate(grmi.Equal, bmodel.PlantNr)})
	if err != nil {
		session.Rollback()
		return err
	}
	for _, v := range heads {
		err = dao.SyncInsertOne(&v)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	for _, vv := range lst {
		err = logdao.SyncInsertOne(&vv)
		if err != nil {
			session.Rollback()
			return err
		}
	}
	session.Commit()
	return nil
}

/******************************************************************************
 *
 * @Reference LAPP_LF_MOM_BACKEND/services/log/ArtDemandLstCacheService.ExportExcel
 *
 ******************************************************************************/
func (impl *ArtDemandLstCacheServiceImplement) ExportExcel(user *models.Usertab, urlParameters map[string]string) (interface{}, error) {

	grmi.Log(user, "/services/log/implments/ArtDemandLstCache.service.impl.go", "ExportExcel", "导出供应商需求数据")

	condition := DefaultConditionOfArtDemandLstCacheAndPaging
	engine := db.Eloquent.Master()
	session := engine.NewSession()
	defer session.Close()
	predicates, err := condition.BuildPredicates(urlParameters)
	if err != nil {
		return nil, err
	}
	dao := dal.NewArtDemandLstCacheDAO(session, user.Pid, user.Userid)
	condition.OrderByFields = append(condition.OrderByFields, meta.ArtDemandLstCache_DemandCtrlPara1, meta.ArtDemandLstCache_DemandCtrlPara2)
	result, err := dao.Select(predicates, condition.OrderByFields)
	if err != nil {
		return nil, err
	}
	exportLi := make([]model.ArtDemandLstCacheExport, 0, len(result))
	for _, artDemandLstCache := range result {
		var data model.ArtDemandLstCacheExport

		orderStatusLi := map[int]string{
			bmodel.WO_STATUS_UNPLANNED:  "未计划",
			bmodel.WO_STATUS_PLANNED:    "已计划",
			bmodel.WO_STATUS_LOCKED:     "已锁定",
			bmodel.WO_STATUS_RELEASED:   "已下达",
			bmodel.WO_STATUS_INTERRUPT:  "中断中",
			bmodel.WO_STATUS_RUNNING:    "生产中",
			bmodel.WO_STATUS_FINISHED:   "已完成",
			bmodel.WO_STATUS_DISPATCHED: "已发运",
			bmodel.WO_STATUS_FROZEN:     "冻结",
			bmodel.WO_STATUS_CANCELED:   "取消",
		}
		var orderStatus string
		for statusInt, statusString := range orderStatusLi {
			if artDemandLstCache.OrderStatus == statusInt {
				orderStatus = statusString
				break
			}
		}
		
		var demandStatus string
		if artDemandLstCache.DemandStatus == model.DemandOrderStatusAccomplish {
			demandStatus = "完成"
		} else {
			demandStatus = "未完成"
		}


		data.PlantNr = artDemandLstCache.PlantNr
		data.ArtId = artDemandLstCache.ArtId
		data.DemandCtrlPara3 = artDemandLstCache.DemandCtrlPara3
		data.DemandTime = artDemandLstCache.DemandTime
		data.DemandId = artDemandLstCache.DemandId
		data.DemandYear = artDemandLstCache.DemandYear
		data.DemandMonth = artDemandLstCache.DemandMonth
		data.DemandWeek = artDemandLstCache.DemandWeek
		data.DemandDate = artDemandLstCache.DemandDate
		data.DemandHour = artDemandLstCache.DemandHour
		data.DemandQty = artDemandLstCache.DemandQty
		data.QtyUomId = artDemandLstCache.QtyUomId
		data.UnproducedQty = artDemandLstCache.UnproducedQty
		data.DemandType = artDemandLstCache.DemandType
		data.DemandCtrlPara2 = artDemandLstCache.DemandCtrlPara2
		data.DemandCtrlPara1 = artDemandLstCache.DemandCtrlPara1
		data.Pos = artDemandLstCache.Pos
		data.OrderStatus = orderStatus
		data.OrderType = artDemandLstCache.OrderType
		data.ProjectId = artDemandLstCache.ProjectId
		data.DemandStatus = demandStatus
		data.PlanResourceId = artDemandLstCache.PlanResourceId
		data.LastModify = artDemandLstCache.LastModify
		data.LastUser = artDemandLstCache.LastUser
		data.CreateTime = artDemandLstCache.CreateTime
		exportLi = append(exportLi, data)
	}
	temp := make([]interface{}, len(exportLi))
	for index, item := range exportLi {
		temp[index] = item
	}
	titleList := []string{
		"工厂ID",
		"物料ID",
		"物料描述",
		"需求时间",
		"需求ID",
		"需求年份",
		"需求月份",
		"需求周",
		"需求日期",
		"需求时",
		"需求数量",
		"数量单位",
		"未生产数量",
		"需求类型",
		"serial",
		"seq",
		"auto_id",
		"工单状态",
		"工单类型",
		"项目ID",
		"需求状态",
		"产线",
		"最后修改时间",
		"最后操作人",
		"创建时间",
	}

	filepath, err := grmi.SaveExcelFile(temp, titleList, "供应商临时需求明细", "供应商需求临时明细")
	if err != nil {
		return nil, grmi.NewBusinessError(fmt.Sprintf("导出报表失败，请重试, errors: %s", err.Error()))
	}
	return filepath, nil
}
