package controllers

import (
	"Xcx_New/enums"
	"Xcx_New/models"
	"Xcx_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")
	out_stock := c.GetString("out_stock")
	outStockInt, _ := strconv.ParseInt(out_stock, 10, 64)
	stock_attribute := c.GetString("stock_attribute")
	stockAttribute, _ := strconv.ParseInt(stock_attribute, 10, 64)
	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,
		OutStock:       outStockInt,
		StockAttribute: stockAttribute,
	}
	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")
	out_stock := c.GetString("out_stock")
	outStockInt, _ := strconv.ParseInt(out_stock, 10, 64)
	adminUserInfo := c.GetAdminUserInfo()
	stock_attribute := c.GetString("stock_attribute")
	stockAttribute, _ := strconv.ParseInt(stock_attribute, 10, 64)
	//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,
		OutStock:       outStockInt,
		StockAttribute: stockAttribute,
	}
	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)
	autoDetail, _ := service.FindStockOutDetailGoodInfoByGoodId(id)

	if len(goodInfo) > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteGoodTypeFail)
		return
	}

	if len(autoDetail) > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteGoodInfoFail)
		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) 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)
	}
}
