package controllers

import (
	"XT_New/enums"
	"XT_New/models"
	"XT_New/service"
	"fmt"
	"github.com/astaxie/beego"
	"strconv"
	"time"
)

type StockGoodApiController struct {
	BaseAuthAPIController
}

func StockGoodApiRegistRouters() {
	beego.Router("/api/stock/good/type/create", &StockGoodApiController{}, "post:CreateGoodType")
	beego.Router("/api/stock/good/type/modify", &StockGoodApiController{}, "post:ModifyGoodType")
	beego.Router("/api/stock/good/type/list", &StockGoodApiController{}, "get:GetGoodTypeList")
	beego.Router("/api/type/delete", &StockGoodApiController{}, "post:DeleteGoodType")
	beego.Router("/api/stock/good/type/get", &StockGoodApiController{}, "get:GetGoodType")

	beego.Router("/api/stock/good/info/create", &StockGoodApiController{}, "post:CreateGoodInfo")
	beego.Router("/api/stock/good/info/modify", &StockGoodApiController{}, "post:ModifyGoodInfo")
	beego.Router("/api/stock/good/info/list", &StockGoodApiController{}, "get:GetGoodInfoList")
	beego.Router("/api/info/delete", &StockGoodApiController{}, "post:DeleteGoodInfo")
	beego.Router("/api/stock/good/info/get", &StockGoodApiController{}, "get:GetGoodInfoByGoodId")
	beego.Router("/api/stock/good/info", &StockGoodApiController{}, "get:GetGoodInfoById")

	beego.Router("/api/stock/good/type/all", &StockGoodApiController{}, "get:GetAllGoodType")
	beego.Router("/api/stock/good/info/all", &StockGoodApiController{}, "get:GetAllGoodInfo")
	beego.Router("/api/good/get", &StockGoodApiController{}, "get:GetAllGood")

}

func (c *StockGoodApiController) CreateGoodType() {
	type_name := c.GetString("type_name")
	remark := c.GetString("remark")
	adminUserInfo := c.GetAdminUserInfo()
	totals := service.FindGoodTypeByName(type_name, adminUserInfo.CurrentOrgId)
	if totals > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeGoodTypeNameExistError)
		return
	}

	total := service.FindAllGoodTypeTotal(adminUserInfo.CurrentOrgId)
	code := strconv.FormatInt(total+1, 10)
	code = "34000000" + code
	goodType := models.GoodsType{
		TypeCode: code,
		TypeName: type_name,
		Remark:   remark,
		Ctime:    time.Now().Unix(),
		Mtime:    time.Now().Unix(),
		OrgId:    adminUserInfo.CurrentOrgId,
		Creater:  adminUserInfo.AdminUser.Id,
		Status:   1,
	}
	err, types := service.AddSigleGoodType(&goodType)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodTypes": types,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) ModifyGoodType() {
	id, _ := c.GetInt64("id", 0)
	type_name := c.GetString("type_name")
	remark := c.GetString("remark")
	good_type_code := c.GetString("type_code")
	adminUserInfo := c.GetAdminUserInfo()

	//totals := service.FindGoodTypeByName(type_name, adminUserInfo.CurrentOrgId)
	//if totals > 0 {
	//	c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeGoodTypeNameExistError)
	//	return
	//}

	goodType := models.GoodsType{
		ID:       id,
		TypeCode: good_type_code,
		TypeName: type_name,
		Remark:   remark,
		Ctime:    time.Now().Unix(),
		Mtime:    time.Now().Unix(),
		OrgId:    adminUserInfo.CurrentOrgId,
		Modifier: adminUserInfo.AdminUser.Id,
		Status:   1,
	}
	err, types := service.ModifyGoodType(&goodType)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodTypes": types,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}

func (c *StockGoodApiController) GetGoodTypeList() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 7)
	keyword := c.GetString("keyword")

	adminUserInfo := c.GetAdminUserInfo()
	goodTypes, total, err := service.FindAllGoodTypeList(adminUserInfo.CurrentOrgId, page, limit, keyword)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"list":  goodTypes,
			"total": total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) DeleteGoodType() {
	id, _ := c.GetInt64("id", 0)
	adminUserInfo := c.GetAdminUserInfo()
	goodInfo, _ := service.FindGoodInfoByGoodId(id)
	if len(goodInfo) > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteGoodTypeFail)
		return
	}
	err := service.DeleteGoodTypeById(id, adminUserInfo.AdminUser.Id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"msg": "删除成功",
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}
func (c *StockGoodApiController) GetGoodType() {
	id, _ := c.GetInt64("id", 0)
	goodType, err := service.FindGoodTypeById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodType": goodType,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}

func (c *StockGoodApiController) CreateGoodInfo() {
	good_id, _ := c.GetInt64("good_id", 0)
	specification_name := c.GetString("specification_name")
	good_unit, _ := c.GetInt64("good_unit", -1)
	buy_price, _ := c.GetFloat("buy_price", 0)
	sell_price, _ := c.GetFloat("sell_price", 0)
	remark := c.GetString("remark")
	manufacturer, _ := c.GetInt64("manufacturer", 0)
	dealer, _ := c.GetInt64("dealer", 0)
	expiry_date_warn_day_count, _ := c.GetInt64("expiry_date_warn_day_count", 0)
	stock_warn_count, _ := c.GetInt64("stock_warn_count", 0)
	is_reuse, _ := c.GetInt64("is_reuse", 0)

	adminUserInfo := c.GetAdminUserInfo()

	totals := service.FindGoodInfoByName(specification_name, adminUserInfo.CurrentOrgId)
	if totals > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeGoodInfoNameExistError)
		return
	}

	total := service.FindAllGoodInfoTotal(adminUserInfo.CurrentOrgId)
	code := strconv.FormatInt(total+1, 10)
	code = "24000000" + code

	goodInfo := models.GoodInfo{
		GoodCode:               code,
		SpecificationName:      specification_name,
		GoodTypeId:             good_id,
		GoodUnit:               good_unit,
		BuyPrice:               buy_price,
		SellPrice:              sell_price,
		Remark:                 remark,
		Ctime:                  time.Now().Unix(),
		Manufacturer:           manufacturer,
		Dealer:                 dealer,
		ExpiryDateWarnDayCount: expiry_date_warn_day_count,
		StockWarnCount:         stock_warn_count,
		IsReuse:                is_reuse,
		Status:                 1,
		OrgId:                  adminUserInfo.CurrentOrgId,
		Creater:                adminUserInfo.AdminUser.Id,
	}
	err, goodInfos := service.AddSigleGoodInfo(&goodInfo)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfos,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}

func (c *StockGoodApiController) ModifyGoodInfo() {
	id, _ := c.GetInt64("id", 0)
	good_id, _ := c.GetInt64("good_id", 0)
	specification_name := c.GetString("specification_name")
	good_unit, _ := c.GetInt64("good_unit", -1)
	buy_price, _ := c.GetFloat("buy_price", 0)
	sell_price, _ := c.GetFloat("sell_price", 0)
	remark := c.GetString("remark")
	manufacturer, _ := c.GetInt64("manufacturer", 0)
	dealer, _ := c.GetInt64("dealer", 0)
	expiry_date_warn_day_count, _ := c.GetInt64("expiry_date_warn_day_count", 0)
	stock_warn_count, _ := c.GetInt64("stock_warn_count", 0)
	is_reuse, _ := c.GetInt64("is_reuse", 0)
	code := c.GetString("good_code")

	adminUserInfo := c.GetAdminUserInfo()

	//totals := service.FindGoodInfoByName(specification_name, adminUserInfo.CurrentOrgId)
	//if totals > 0 {
	//	c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeGoodInfoNameExistError)
	//	return
	//}

	goodInfo := models.GoodInfo{
		ID:                     id,
		GoodCode:               code,
		SpecificationName:      specification_name,
		GoodTypeId:             good_id,
		GoodUnit:               good_unit,
		BuyPrice:               buy_price,
		SellPrice:              sell_price,
		Remark:                 remark,
		Mtime:                  time.Now().Unix(),
		Manufacturer:           manufacturer,
		Dealer:                 dealer,
		ExpiryDateWarnDayCount: expiry_date_warn_day_count,
		StockWarnCount:         stock_warn_count,
		IsReuse:                is_reuse,
		Status:                 1,
		OrgId:                  adminUserInfo.CurrentOrgId,
		Modifier:               adminUserInfo.AdminUser.Id,
	}
	err, goodInfos := service.ModifyGoodInfo(&goodInfo)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfos,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}
func (c *StockGoodApiController) GetGoodInfoList() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 7)
	keyword := c.GetString("keyword")

	adminUserInfo := c.GetAdminUserInfo()
	goodInfos, total, err := service.FindGoodInfoList(adminUserInfo.CurrentOrgId, page, limit, keyword)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"list":  goodInfos,
			"total": total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}
func (c *StockGoodApiController) DeleteGoodInfo() {
	id, _ := c.GetInt64("id", 0)
	adminUserInfo := c.GetAdminUserInfo()

	total, _ := service.FindWarehouseInfoTotalByGoodId(id)
	if total > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteGoodInfoFail)
		return
	}
	err := service.DeleteGoodInfoById(id, adminUserInfo.AdminUser.Id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"msg": "删除成功",
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}

func (c *StockGoodApiController) GetGoodInfoByGoodId() {
	id, _ := c.GetInt64("id", 0)
	goodInfo, err := service.FindGoodInfoByGoodId(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"list": goodInfo,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) GetGoodInfoById() {
	id, _ := c.GetInt64("id", 0)
	goodInfo, err := service.FindGoodInfoById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfo,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) GetAllGoodType() {
	adminUserInfo := c.GetAdminUserInfo()
	goodTypes, err := service.FindAllGoodType(adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodType": goodTypes,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) GetAllGoodInfo() {
	adminUserInfo := c.GetAdminUserInfo()
	goodInfo, err := service.FindAllGoodInfo(adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfo,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) GetAllGood() {
	adminUserInfo := c.GetAdminUserInfo()
	manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
	dealer_id, _ := c.GetInt64("dealer_id", 0)
	fmt.Println(manufacturer_id)
	fmt.Println(dealer_id)

	goodInfo, err := service.FindAllGoodByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfo,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockGoodApiController) GetWarehouseGoodInfo() {
	adminUserInfo := c.GetAdminUserInfo()
	manufacturer_id, _ := c.GetInt64("manufacturer_id", 0)
	dealer_id, _ := c.GetInt64("dealer_id", 0)

	goodInfo, err := service.FindAllGoodByManufactureId(manufacturer_id, dealer_id, adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"goodInfo": goodInfo,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
