package erp

import (
	"context"
	"git.kumo.work/shama/service/client"
	factory2 "git.kumo.work/shama/service/erp/factory"
	"git.kumo.work/shama/service/lib/bean"
	"time"
)

type factory struct {
	factory2.Factory
}
type ArgsFactoryList struct {
	Page   bean.Page
	Search FactorySearch
}
type FactorySearch struct {
	Name            string     // 工厂名称
	CreatedStaffIds []int64    // 录入人
	CreatedAtStart  *time.Time // 录入开始时间
	CreatedAtEnd    *time.Time // 录入结束时间
	Origin          string     // 工厂来源
	City            string     // 所在城市
	BanFlag         int64      // 禁用标记 1=禁用 2=正常
}
type ReplyFactoryList struct {
	List  []FactoryItem `json:"list"`
	Total int64         `json:"total"`
}

type FactoryItem struct {
	Id              int64      `json:"id"`
	Serial          string     `json:"serial"`
	Name            string     `json:"name"`
	ShortName       string     `json:"shortName"`
	EngName         string     `json:"engName"`
	City            string     `json:"city"`
	Address         string     `json:"address"`
	EngAddress      string     `json:"engAddress"`
	ZipCode         string     `json:"zipCode"`
	DomesticSupply  string     `json:"domesticSupply"`
	Origin          string     `json:"origin"`
	TradeScope      string     `json:"tradeScope"`
	CreditRank      string     `json:"creditRank"`
	TeamRank        string     `json:"teamRank"`
	PayWay          string     `json:"payWay"`
	Bank            string     `json:"bank"`
	BankAccount     string     `json:"bankAccount"`
	BankAddress     string     `json:"bankAddress"`
	TaxNumber       string     `json:"taxNumber"`
	Info            string     `json:"info"`
	LegalPerson     string     `json:"legalPerson"`
	RegCapital      string     `json:"regCapital"`
	RegAddress      string     `json:"regAddress"`
	RegOffice       string     `json:"regOffice"`
	YearSale        string     `json:"yearSale"`
	Contact         string     `json:"contact"`
	OpStartAt       *time.Time `json:"opStartAt"`
	OpEndAt         *time.Time `json:"opEndAt"`
	LicenseFilePath string     `json:"licenseFilePath"`
	LicenseStartAt  *time.Time `json:"licenseStartAt"`
	LicenseEndAt    *time.Time `json:"licenseEndAt"`
	ReportFilePath  string     `json:"reportFilePath"`
	ReportStartAt   *time.Time `json:"reportStartAt"`
	ReportEndAt     *time.Time `json:"reportEndAt"`
	ImgFilePaths    []string   `json:"imgFilePaths"`
	BanFlag         int64      `json:"banFlag"`
	MaterialQuality string     `json:"materialQuality"`
	InvoiceType     string     `json:"invoiceType"`
	Status          *int64     `json:"status"`
	CreatedStaffId  int64      `json:"createdStaffId"`
	CreatedAt       *time.Time `json:"createdAt"`
	UpdatedAt       *time.Time `json:"updatedAt"`
}

// List @TITLE 工厂列表
func (f *factory) List(ctx context.Context, args ArgsFactoryList) (reply ReplyFactoryList, err error) {
	xClient, err := client.GetClient(f)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "List", args, &reply)
	return
}

type ArgsFactoryAdd struct {
	StaffId int64
	FactoryAdd
}

type FactoryAdd struct {
	Serial          string                // 工厂编号
	ImgFilePaths    []string              // 图片地址
	Name            string                // 工厂名称
	ShortName       string                // 工厂简称
	EngName         string                // 英文名称
	Origin          string                // 工厂来源
	MaterialQuality string                // 材质
	InvoiceType     string                // 发票
	BanFlag         int64                 // 禁用标记 1=禁用 2=正常
	City            string                // 所在城市
	Address         string                // 工厂地址
	EngAddress      string                // 英文地址
	ZipCode         string                // 邮编
	DomesticSupply  string                // 境内货源地
	TradeScope      string                // 工厂业务范围
	CreditRank      string                // 信用等级
	TeamRank        string                // 合作等级
	PayWay          string                // 结算方式
	Bank            string                // 开户银行
	BankAccount     string                // 银行账号
	BankAddress     string                // 银行地址
	TaxNumber       string                // 工厂税号
	Info            string                // 备注说明
	LegalPerson     string                // 法人
	RegCapital      string                // 注册资金
	RegAddress      string                // 注册地址
	RegOffice       string                // 注册机关
	YearSale        string                // 年销售额
	Contact         string                // 联系方式
	LicenseFilePath string                // 营业执照图片
	ReportFilePath  string                // 验厂报告图片
	OpStartAt       *time.Time            // 营业期限 起
	OpEndAt         *time.Time            // 营业期限 止
	LicenseStartAt  *time.Time            // 营业执照 起
	LicenseEndAt    *time.Time            // 营业执照 止
	ReportStartAt   *time.Time            // 验厂报告 起
	ReportEndAt     *time.Time            // 验厂报告 止
	Contacts        []factory2.ContactAdd // 联系人
}

// Add @TITLE 添加工厂
func (f *factory) Add(ctx context.Context, args ArgsFactoryAdd) (factoryId int64, err error) {
	xClient, err := client.GetClient(f)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Add", args, &factoryId)
	return
}

type ReplyFactoryInfo struct {
	Id              int64      `json:"id"`
	Serial          string     `json:"serial"`
	Name            string     `json:"name"`
	ShortName       string     `json:"shortName"`
	EngName         string     `json:"engName"`
	City            string     `json:"city"`
	Address         string     `json:"address"`
	EngAddress      string     `json:"engAddress"`
	ZipCode         string     `json:"zipCode"`
	DomesticSupply  string     `json:"domesticSupply"`
	Origin          string     `json:"origin"`
	TradeScope      string     `json:"tradeScope"`
	CreditRank      string     `json:"creditRank"`
	TeamRank        string     `json:"teamRank"`
	PayWay          string     `json:"payWay"`
	Bank            string     `json:"bank"`
	BankAccount     string     `json:"bankAccount"`
	BankAddress     string     `json:"bankAddress"`
	TaxNumber       string     `json:"taxNumber"`
	Info            string     `json:"info"`
	LegalPerson     string     `json:"legalPerson"`
	RegCapital      string     `json:"regCapital"`
	RegAddress      string     `json:"regAddress"`
	RegOffice       string     `json:"regOffice"`
	YearSale        string     `json:"yearSale"`
	Contact         string     `json:"contact"`
	OpStartAt       *time.Time `json:"opStartAt"`
	OpEndAt         *time.Time `json:"opEndAt"`
	LicenseFilePath string     `json:"licenseFilePath"`
	LicenseStartAt  *time.Time `json:"licenseStartAt"`
	LicenseEndAt    *time.Time `json:"licenseEndAt"`
	ReportFilePath  string     `json:"reportFilePath"`
	ReportStartAt   *time.Time `json:"reportStartAt"`
	ReportEndAt     *time.Time `json:"reportEndAt"`
	ImgFilePaths    []string   `json:"imgFilePaths"`
	BanFlag         int64      `json:"banFlag"`
	MaterialQuality string     `json:"materialQuality"`
	InvoiceType     string     `json:"invoiceType"`
	Status          *int64     `json:"status"`
	CreatedStaffId  int64      `json:"createdStaffId"`
	CreatedAt       *time.Time `json:"createdAt"`
	UpdatedAt       *time.Time `json:"updatedAt"`
}

// Info @TITLE 工厂详情
func (f *factory) Info(ctx context.Context, factoryId int64) (reply ReplyFactoryInfo, err error) {
	xClient, err := client.GetClient(f)
	if err != nil {
		return
	}
	err = xClient.Call(ctx, "Info", factoryId, &reply)
	return
}

type ArgsFactoryEdit struct {
	FactoryId int64
	FactoryAdd
}

// Edit @TITLE 编辑工厂
func (f *factory) Edit(ctx context.Context, args ArgsFactoryEdit) (err error) {
	xClient, err := client.GetClient(f)
	if err != nil {
		return
	}
	reply := 0
	return xClient.Call(ctx, "Edit", args, &reply)
}