/**
* @Author: zou.yingbin
* @Create  : 2021/3/9 16:21
* @Modify  : 2021/3/9 16:21
 */

package models

import (
	"fmt"
	"mtp2_if/db"
	"mtp2_if/utils"
)

// ErmcpWareHouseInfo 仓库信息结构
type ErmcpWareHouseInfo struct {
	AUTOID          int64  `json:"autoid"  xorm:"'AUTOID'"`                   // 自增ID(仓库ID)
	WAREHOUSECODE   string `json:"warehousecode"  xorm:"'WAREHOUSECODE'"`     // 仓库代码
	WAREHOUSENAME   string `json:"warehousename"  xorm:"'WAREHOUSENAME'"`     // 仓库名称
	WAREHOUSETYPE   int32  `json:"warehousetype"  xorm:"'WAREHOUSETYPE'"`     // 仓库类型 - 1 厂库  2 自有库  3 合作库
	AREAUSERID      int64  `json:"areauserid"  xorm:"'AREAUSERID'"`           // 所属机构
	WAREHOUSESTATUS int32  `json:"warehousestatus"  xorm:"'WAREHOUSESTATUS'"` // 仓库状态 - 1:正常 2:注销 3:待审核 4:审核拒绝
	COUNTRYID       int32  `json:"countryid"  xorm:"'COUNTRYID'"`             // 国家
	PROVINCEID      int32  `json:"provinceid"  xorm:"'PROVINCEID'"`           // 省
	CITYID          int32  `json:"cityid"  xorm:"'CITYID'"`                   // 市
	DISTRICTID      int32  `json:"districtid"  xorm:"'DISTRICTID'"`           // 区
	ADDRESS         string `json:"address"  xorm:"'ADDRESS'"`                 // 详细地址
	REMARK          string `json:"remark"  xorm:"'REMARK'"`                   // 审核备注
	CREATETIME      string `json:"createtime"  xorm:"'CREATETIME'"`           // 创建时间
	HASVIDEO        int32  `json:"hasvideo"  xorm:"'HASVIDEO'"`               // 是否有视频 - 0:无 1:有
	VIDEOURL        string `json:"videourl"  xorm:"'VIDEOURL'"`               // 视频地址
	CONTACTNAME     string `json:"contactname"  xorm:"'CONTACTNAME'"`         // 联系人
	CONTACTNUM      string `json:"contactnum"  xorm:"'CONTACTNUM'"`           // 联系电话

	FilterStatus string `json:"-"` // 查询条件 状态
}

func (r *ErmcpWareHouseInfo) buildSql() string {
	var sqlId utils.SQLVal = "SELECT t.AUTOID," +
		"       t.WAREHOUSECODE," +
		"       t.WAREHOUSENAME," +
		"       t.WAREHOUSETYPE," +
		"       t.AREAUSERID," +
		"       t.WAREHOUSESTATUS," +
		"       t.COUNTRYID," +
		"       t.PROVINCEID," +
		"       t.CITYID," +
		"       t.DISTRICTID," +
		"       t.ADDRESS," +
		"       t.REMARK," +
		"       to_char(t.CREATETIME,'yyyy-mm-dd hh24:mi:ss') CREATETIME," +
		"       t.HASVIDEO," +
		"       t.VIDEOURL," +
		"       t.CONTACTNAME," +
		"       t.CONTACTNUM" +
		"  FROM WAREHOUSEINFO t" +
		" WHERE 1 = 1"
	if r.AREAUSERID > 0 {
		sqlId.And("t.AREAUSERID", r.AREAUSERID)
	}
	if len(r.FilterStatus) > 0 {
		sqlId.Join(fmt.Sprintf(" and t.WAREHOUSESTATUS in(%v)", r.FilterStatus))
	}
	return sqlId.String()
}

// GetDataEx 获取仓库信息
func (r *ErmcpWareHouseInfo) GetDataEx() (interface{}, error) {
	sData := make([]ErmcpWareHouseInfo, 0)
	err := db.GetEngine().SQL(r.buildSql()).Find(&sData)
	return sData, err
}
