package controllers

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

type StockApiController struct {
	BaseAuthAPIController
}

func StockApiRegistRouters() {
	beego.Router("/api/stock/dealer/create", &StockApiController{}, "post:CreateDealer")
	beego.Router("/api/stock/dealer/modify", &StockApiController{}, "post:ModifyDealer")
	beego.Router("/api/stock/dealer/list", &StockApiController{}, "get:GetDealersList")
	beego.Router("/api/stock/dealer/delete", &StockApiController{}, "post:DeleteDealer")
	beego.Router("/api/stock/dealer/get", &StockApiController{}, "get:GetDealer")

	beego.Router("/api/stock/manufacturer/create", &StockApiController{}, "post:CreateManufacturer")
	beego.Router("/api/stock/manufacturer/modify", &StockApiController{}, "post:ModifyManufacturer")
	beego.Router("/api/stock/manufacturer/list", &StockApiController{}, "get:GetManufacturerList")
	beego.Router("/api/stock/manufacturer/delete", &StockApiController{}, "post:DeleteManufacturer")
	beego.Router("/api/stock/manufacturer/get", &StockApiController{}, "get:GetManufacturer")

	beego.Router("/api/stock/dealer/all", &StockApiController{}, "get:GetAllDealer")
	beego.Router("/api/stock/manufacturer/all", &StockApiController{}, "get:GetAllManufacturer")

}

func (c *StockApiController) CreateDealer() {
	dealer_name := c.GetString("dealer_name")
	contact := c.GetString("contact")
	contact_phone := c.GetString("contact_phone")
	platform_number := c.GetString("platform_number")
	email := c.GetString("email")
	contact_address := c.GetString("contact_address")
	remark := c.GetString("remark")

	if len(dealer_name) <= 0 {
		utils.ErrorLog("len(dealer_name) == 0")

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetAdminUserInfo()

	total := service.FindAllDealerTotal(adminUserInfo.CurrentOrgId)
	totalStr := strconv.FormatInt(total+1, 10)
	code := "1000" + totalStr
	dealer := models.Dealer{
		DealerName:     dealer_name,
		Contact:        contact,
		ContactPhone:   contact_phone,
		ContactAddress: contact_address,
		DealerCode:     code,
		Ctime:          time.Now().Unix(),
		Mtime:          time.Now().Unix(),
		Remark:         remark,
		Creater:        adminUserInfo.AdminUser.Id,
		Email:          email,
		PlatformNumber: platform_number,
		OrgId:          adminUserInfo.CurrentOrgId,
		Status:         1,
	}

	err, dealers := service.AddSigleDealer(&dealer)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"dealer": dealers,
		})

	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)

	}

}
func (c *StockApiController) ModifyDealer() {
	id, _ := c.GetInt64("id", 0)
	dealer_name := c.GetString("dealer_name")
	contact := c.GetString("contact")
	contact_phone := c.GetString("contact_phone")
	platform_number := c.GetString("platform_number")
	dealer_code := c.GetString("dealer_code")
	email := c.GetString("email")
	contact_address := c.GetString("contact_address")
	remark := c.GetString("remark")
	if id <= 0 {
		utils.ErrorLog("id == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if len(dealer_name) <= 0 {
		utils.ErrorLog("len(dealer_name) == 0")

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetAdminUserInfo()
	dealer := models.Dealer{
		ID:             id,
		DealerName:     dealer_name,
		Contact:        contact,
		ContactPhone:   contact_phone,
		ContactAddress: contact_address,
		Mtime:          time.Now().Unix(),
		DealerCode:     dealer_code,
		Remark:         remark,
		Creater:        adminUserInfo.AdminUser.Id,
		Email:          email,
		PlatformNumber: platform_number,
		Modifier:       adminUserInfo.AdminUser.Id,
		OrgId:          adminUserInfo.CurrentOrgId,
		Status:         1,
	}

	err := service.ModifyDealer(&dealer)

	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"dealer": dealer,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *StockApiController) GetDealersList() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 7)
	adminUserInfo := c.GetAdminUserInfo()
	deales, total, err := service.FindAllDealerList(adminUserInfo.CurrentOrgId, page, limit)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"dealer": deales,
			"total":  total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *StockApiController) DeleteDealer() {
	id, _ := c.GetInt64("id", 0)
	total, _ := service.FindStockInByDealerId(id)
	if total > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteDealerWrong)
		return
	}
	err := service.DeleteDealerById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"msg": "删除成功",
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockApiController) GetDealer() {
	id, _ := c.GetInt64("id", 0)
	dealer, err := service.FindDealerById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"dealer": dealer,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockApiController) CreateManufacturer() {
	manufacturer_name := c.GetString("manufacturer_name")
	contact := c.GetString("contact")
	contact_phone := c.GetString("contact_phone")
	platform_number := c.GetString("platform_number")
	email := c.GetString("email")
	contact_address := c.GetString("contact_address")
	remark := c.GetString("remark")

	if len(manufacturer_name) <= 0 {
		utils.ErrorLog("len(manufacturer_name) == 0")

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetAdminUserInfo()
	total := service.FindManufacturerTotal(adminUserInfo.CurrentOrgId)
	totalStr := strconv.FormatInt(total+1, 10)
	code := "2000" + totalStr
	manufacturer := models.Manufacturer{
		ManufacturerName: manufacturer_name,
		Contact:          contact,
		ContactPhone:     contact_phone,
		ContactAddress:   contact_address,
		Ctime:            time.Now().Unix(),
		Mtime:            time.Now().Unix(),
		Remark:           remark,
		Creater:          adminUserInfo.AdminUser.Id,
		Email:            email,
		PlatformNumber:   platform_number,
		OrgId:            adminUserInfo.CurrentOrgId,
		Status:           1,
		ManufacturerCode: code,
	}

	err, manufacturers := service.AddSigleManufacturer(&manufacturer)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"manufacturer": manufacturers,
		})

	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}
func (c *StockApiController) ModifyManufacturer() {

	id, _ := c.GetInt64("id", 0)
	manufacturer_name := c.GetString("manufacturer_name")
	contact := c.GetString("contact")
	contact_phone := c.GetString("contact_phone")
	platform_number := c.GetString("platform_number")
	email := c.GetString("email")
	contact_address := c.GetString("contact_address")
	remark := c.GetString("remark")
	manufacturer_code := c.GetString("manufacturer_code")

	if id <= 0 {
		utils.ErrorLog("id == 0")
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	if len(manufacturer_name) <= 0 {
		utils.ErrorLog("len(manufacturer_name) == 0")

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeParamWrong)
		return
	}

	adminUserInfo := c.GetAdminUserInfo()
	manufacturer := models.Manufacturer{
		ID:               id,
		ManufacturerName: manufacturer_name,
		Contact:          contact,
		ContactPhone:     contact_phone,
		ContactAddress:   contact_address,
		Mtime:            time.Now().Unix(),
		Remark:           remark,
		Creater:          adminUserInfo.AdminUser.Id,
		Email:            email,
		PlatformNumber:   platform_number,
		Modifier:         adminUserInfo.AdminUser.Id,
		OrgId:            adminUserInfo.CurrentOrgId,
		ManufacturerCode: manufacturer_code,
		Status:           1,
	}

	err := service.ModifyManufacturer(&manufacturer)

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

}
func (c *StockApiController) GetManufacturerList() {
	page, _ := c.GetInt64("page", 1)
	limit, _ := c.GetInt64("limit", 10)
	adminUserInfo := c.GetAdminUserInfo()
	manufacturer, total, err := service.FindAllManufacturerList(adminUserInfo.CurrentOrgId, page, limit)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"manufacturer": manufacturer,
			"total":        total,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *StockApiController) DeleteManufacturer() {
	id, _ := c.GetInt64("id", 0)
	total, _ := service.FindStockInByManufacturerId(id)
	if total > 0 {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeDeleteManufacturerWrong)
		return
	}

	err := service.DeleteManufacturerById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"msg": "删除成功",
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}
func (c *StockApiController) GetManufacturer() {
	id, _ := c.GetInt64("id", 0)
	manufacturer, err := service.FindManufacturerById(id)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"manufacturer": manufacturer,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}

func (c *StockApiController) GetAllDealer() {
	adminUserInfo := c.GetAdminUserInfo()
	dealer, err := service.FindAllDealer(adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"dealer": dealer,
		})
	} else {

		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}
}

func (c *StockApiController) GetAllManufacturer() {
	adminUserInfo := c.GetAdminUserInfo()
	manufacturer, err := service.FindAllManufacturer(adminUserInfo.CurrentOrgId)
	if err == nil {
		c.ServeSuccessJSON(map[string]interface{}{
			"manufacturer": manufacturer,
		})
	} else {
		c.ServeFailJSONWithSGJErrorCode(enums.ErrorCodeSystemError)
	}

}
