package repository

import (
	"database/sql"
)

//func LotNumberTheLastOne(tx *sql.Tx, hospitalId int64, commodityId int, commodityBatch string) (model.LotNumberList, error) {
//
//	// 正序
//	QuerySQL := "select  id , lot_number,convert(quantity / 1000, decimal(11,3)) ,convert(costmoney / 1000, decimal(11,3))  from g_new_stock  where  delflag = 0 and sys_hospital_id = ? and con_commodity_id =?  and commodity_batch = ? order by lot_number desc limit 1  "
//
//	ln := new(model.LotNumberList)
//	err := tx.QueryRow(QuerySQL, hospitalId, commodityId, commodityBatch).Scan(&ln.ID, &ln.LotNumber, &ln.Quantity, &ln.CostMoney)
//	if err != nil {
//		if err == sql.ErrNoRows {
//			err = nil
//			return model.LotNumberList{}, err
//		}
//		return *ln, errors.New("查询出错")
//	}
//	return *ln, err
//}

func UpdateCheckStockAddTo(tx *sql.Tx, quantity float64, hospitalId, conCommodityID int64, lotNumber string) error {

	updateStockAddSQL := "update g_new_stock set  quantity = quantity + ( ? * 1000) where  sys_hospital_id = ? and  con_commodity_id = ? and lot_number = ? "

	updateStockAddStmt, err := tx.Prepare(updateStockAddSQL)
	if err != nil {
		return err
	}

	_, err = updateStockAddStmt.Exec(quantity, hospitalId, conCommodityID, lotNumber)
	if err != nil {
		return err
	}
	return nil
}

func UpdateCheckStockSubTo(tx *sql.Tx, quantity float64, hospitalId, conCommodityID int64, lotNumber string) error {

	updateStockAddSQL := "update g_new_stock set  quantity = quantity - ( ? * 1000) where  sys_hospital_id = ? and  con_commodity_id = ? and lot_number = ? "

	updateStockAddStmt, err := tx.Prepare(updateStockAddSQL)
	if err != nil {
		return err
	}

	_, err = updateStockAddStmt.Exec(quantity, hospitalId, conCommodityID, lotNumber)
	if err != nil {
		return err
	}
	return nil
}

func UpdateStockAddToByID(tx *sql.Tx, quantity float64, id int) error {

	// 更新库存
	updateStockSQL := "update g_new_stock set  quantity = quantity + ( ? * 1000) where id = ? "

	updateStockStmt, err := tx.Prepare(updateStockSQL)
	if err != nil {
		return err
	}
	_, err = updateStockStmt.Exec(quantity, id)
	if err != nil {
		return err
	}

	return err

}

// 修改库存中间记录表信息
func InsertStockCount(tx *sql.Tx, userId, hospitalId, conCommodityID int64, now_Year, now_Month, now_Day int, now string,
	tempQuantity, tQuantity, aquantity float64, lotNumber, commodityBatch string) (int64, error) {

	// 新增商品单日库存流记录
	insertStockCountSQL := `insert g_new_stock_count set creater = ?, sys_hospital_id = ?, con_commodity_id = ?,
_year = ?, _month = ?,_day = ? ,eventtime = ? , change_quantity = ? * 1000, after_change_quantity = ? * 1000, before_change_quantity = ? * 1000, _type = 2 ,lot_number = ? ,commodity_batch = ?`

	insertStockCountStmt, err := tx.Prepare(insertStockCountSQL)
	if err != nil {
		return 0, nil
	}

	exec, err := insertStockCountStmt.Exec(userId, hospitalId, conCommodityID, now_Year, now_Month, now_Day,
		now, tempQuantity, tQuantity, aquantity, lotNumber, commodityBatch)
	if err != nil {
		return 0, err
	}
	lastInsertId, _ := exec.LastInsertId()

	return lastInsertId, err
}

// 修改库存中间记录表信息
func InsertStockCountToAdd(tx *sql.Tx, userId, hospitalId, conCommodityID int64, now_Year, now_Month, now_Day int, now string,
	tempQuantity, tQuantity, aquantity float64, lotNumber, commodityBatch string) (int64, error) {

	// 新增商品单日库存流记录
	insertStockCountSQL := `insert g_new_stock_count set creater = ?, sys_hospital_id = ?, con_commodity_id = ?,
_year = ?, _month = ?,_day = ? ,eventtime = ? , change_quantity = ? * 1000, after_change_quantity = ? * 1000, before_change_quantity = ? * 1000, _type = 5 ,lot_number = ? ,commodity_batch = ?`

	insertStockCountStmt, err := tx.Prepare(insertStockCountSQL)
	if err != nil {
		return 0, nil
	}

	exec, err := insertStockCountStmt.Exec(userId, hospitalId, conCommodityID, now_Year, now_Month, now_Day,
		now, tempQuantity, tQuantity, aquantity, lotNumber, commodityBatch)
	if err != nil {
		return 0, err
	}
	lastInsertId, _ := exec.LastInsertId()

	return lastInsertId, err
}

// 修改库存中间记录表信息
func InsertStockCountCheckSub(tx *sql.Tx, userId, hospitalId, conCommodityID int64, now_Year, now_Month, now_Day int, now string,
	tempQuantity, tQuantity, aquantity float64, lotNumber, commodityBatch string) (int64, error) {

	// 新增商品单日库存流记录
	insertStockCountSQL := `insert g_new_stock_count set creater = ?, sys_hospital_id = ?, con_commodity_id = ?,
_year = ?, _month = ?,_day = ? ,eventtime = ? , change_quantity = ? * 1000, after_change_quantity = ? * 1000, before_change_quantity = ? * 1000, _type = 6 ,lot_number = ? ,commodity_batch = ?`

	insertStockCountStmt, err := tx.Prepare(insertStockCountSQL)
	if err != nil {
		return 0, nil
	}

	exec, err := insertStockCountStmt.Exec(userId, hospitalId, conCommodityID, now_Year, now_Month, now_Day,
		now, tempQuantity, tQuantity, aquantity, lotNumber, commodityBatch)
	if err != nil {
		return 0, err
	}
	lastInsertId, _ := exec.LastInsertId()

	return lastInsertId, err
}

// 盘库增加
func CheckAddLotNumberDetail(tx *sql.Tx, userId, hospitalId, DetailId, conCommodityID, conCategoryID int64, conCategoryName string,
	topCategoryID int64, commodityCode, commodityEncode, commodityName, commodityNamepy, commodityBrand string,
	commodityIsuseAlias int, commodityAliasname, commodityStandardname, commodityStandardcode, commodityVender,
	commdityFormat, commodityUnit string, tempQuantity, constPrice float64, lotNumber, commodityBatch string, countID int64) error {

	checkAddLotNumber := `insert g_new_stock_check_lot_number_detail set
creater = ?, sys_hospital_id = ?, new_stock_check_detail_id = ?, con_commodity_id = ?, con_category_id = ?, con_category_name = ?, top_category_id = ?,
commodity_code = ?, commodity_encode = ?, commodity_name = ?, commodity_namepy = ?, commodity_brand = ?, commodity_isuse_alias = ?,
commodity_aliasname = ?, commodity_standardname = ?, commodity_standardcode = ?, commodity_vender = ?, commodity_format = ?, commodity_unit = ?,
quantity = ? * 1000, retreatprice = ? * 1000,lot_number = ?,commodity_batch =?, count_check_id = ?`

	checkAddLotNumberStmt, err := tx.Prepare(checkAddLotNumber)
	if err != nil {
		return err
	}

	_, err = checkAddLotNumberStmt.Exec(userId, hospitalId, DetailId, conCommodityID, conCategoryID, conCategoryName,
		topCategoryID, commodityCode, commodityEncode, commodityName, commodityNamepy, commodityBrand,
		commodityIsuseAlias, commodityAliasname, commodityStandardname, commodityStandardcode, commodityVender,
		commdityFormat, commodityUnit, tempQuantity, constPrice, lotNumber, commodityBatch, countID)

	if err != nil {
		return err
	}

	return nil
}
