/**
* @Author: zou.yingbin
* @Create  : 2021/1/7 17:58
* @Modify  : 2021/1/7 17:58
 */

package models

import (
	"fmt"
	"mtp2_if/db"
	"mtp2_if/logger"
	"mtp2_if/mtpcache"
)

// ErmcpHedgePlan 套保计划表结构
type ErmcpHedgePlan struct {
	Areauserid        int64   `json:"areauserid"  xorm:"'Areauserid'"`               // 用户ID
	Hedgeplanid       string  `json:"hedgeplanid"  xorm:"'Hedgeplanid'"`             // 套保计划ID(601+Unix秒时间戳(10位)+xxxxxx)
	Hedgeplanno       string  `json:"hedgeplanno"  xorm:"'Hedgeplanno'"`             // 套保计划编号(名称)
	Contracttype      int     `json:"contracttype"  xorm:"'Contracttype'"`           // 计划类型 - 1:采购 -1:销售
	Deliverygoodsid   int     `json:"deliverygoodsid"  xorm:"'Deliverygoodsid'"`     // 现货品种ID
	Deliverygoodsname string  `json:"deliverygoodsname"  xorm:"'Deliverygoodsname'"` // 现货品种名称
	Wrstandardid      int     `json:"wrstandardid"  xorm:"'Wrstandardid'"`           // 现货商品ID
	Wrstandardname    string  `json:"wrstandardname"  xorm:"'Wrstandardname'"`       // 现货商品名称
	Producttype       int     `json:"producttype"  xorm:"'Producttype'"`             // 产品类型 - 1:标准仓单 2:等标 3:非标
	Spotgoodsdesc     string  `json:"spotgoodsdesc"  xorm:"'Spotgoodsdesc'"`         // 商品型号
	Planqty           float64 `json:"planqty"  xorm:"'Planqty'"`                     // 计划数量
	Convertfactor     float64 `json:"convertfactor"  xorm:"'Convertfactor'"`         // 标仓系数
	Plantime          string  `json:"plantime"  xorm:"'Plantime'"`                   // 计划时间
	Hedgeplanstatus   int     `json:"hedgeplanstatus"  xorm:"'Hedgeplanstatus'"`     // 套保计划状态 -  0:未提交 1:待审核 2:执行中 3:正常完结 4:审核拒绝 5:异常完结 6:已撤回
	Remark            string  `json:"remark"  xorm:"'Remark'"`                       // 备注
	CREATETIME        string  `json:"createtime"  xorm:"'CREATETIME'"`               // 创建时间
	UPDATETIME        string  `json:"updatetime"  xorm:"'UPDATETIME'"`               // 更新时间
	AUDITTIME         string  `json:"audittime"  xorm:"'AUDITTIME'"`                 // 审核时间
	UNITID            int32   `json:"unitid"  xorm:"'UNITID'"`                       // 单位id
	ENUMDICNAME       string  `json:"enumdicname"`                                   // 单位名称
}

func (r *ErmcpHedgePlan) calc() {
	r.ENUMDICNAME = mtpcache.GetEnumDicitemName(r.UNITID)
}

func (r *ErmcpHedgePlan) buildSql(status string) string {
	str := "select to_char(t.Hedgeplanid) Hedgeplanid," +
		"       t.Hedgeplanno," +
		"       t.Contracttype," +
		"       t.Deliverygoodsid," +
		"       t.g.Deliverygoodsname," +
		"       t.Wrstandardid," +
		"       w.Wrstandardname," +
		"       w.unitid," +
		"       t.Producttype," +
		"       t.Spotgoodsdesc," +
		"       t.Planqty," +
		"       t.Convertfactor," +
		"       to_char(t.Plantime,'yyyy-mm-dd hh24:mi:ss') Plantime," +
		"       to_char(t.createtime,'yyyy-mm-dd hh24:mi:ss') createtime," +
		"       to_char(t.updatetime, 'yyyy-mm-dd hh24:mi:ss') updatetime," +
		"       to_char(t.audittime, 'yyyy-mm-dd hh24:mi:ss') audittime," +
		"       t.Hedgeplanstatus," +
		"       t.Remark" +
		"  from ermcp_hedgeplan t" +
		"  left join wrstandard w" +
		"    on t.wrstandardid = w.wrstandardid" +
		"  left join deliverygoods g" +
		"    on t.deliverygoodsid = g.deliverygoodsid" +
		" where t.hedgeplanstatus in (%v) and t.areauserid=%v"

	if status == "2" {
		// 执行中状态，按审核时间倒序
		str += " order by t.audittime desc"
	} else {
		str += " order by t.updatetime desc"
	}

	return fmt.Sprintf(str, status, r.Areauserid)
}

// GetData 从数据库中查询套保计划数据
func (r *ErmcpHedgePlan) GetData(status string) ([]ErmcpHedgePlan, error) {
	e := db.GetEngine()
	sData := make([]ErmcpHedgePlan, 0)
	if err := e.SQL(r.buildSql(status)).Find(&sData); err != nil {
		logger.GetLogger().Errorf("query hedgeplan:%v", err)
		return sData, err
	}
	for i := range sData {
		sData[i].calc()
	}

	return sData, nil
}
