check_stock.go 6.61 KB
Newer Older
haoyanbin's avatar
1  
haoyanbin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
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
}