package cases

import (
	"spaceofcheng/services/models"
	"spaceofcheng/services/models/model"
	"spaceofcheng/services/utils"
	"strconv"
	"strings"
	"time"
)

const (
	STATUS_UNLOCK = 0
)

// CaseKeyInfo 案场钥匙信息
type CaseKeyInfo struct {
	model.TaCaseKey `xorm:"extends"`
	CustomerId      string
	CustomerName    string
}

// GetKeysByCase 根据案场获取钥匙信息
func (m *CaseDAO) GetKeysByCase(caseid, num string, page, pageSize int) ([]CaseKeyInfo, error) {
	var keys []CaseKeyInfo
	query := `
		SELECT
			t.*, s.customer_id,
			s.customer_name
		FROM
			ta_case_key t
		LEFT JOIN ta_case_key_use s ON t.key_id = s.key_id AND s.status != ?
		WHERE
			t.case_id = ?
		AND t.status > ?
	`

	if num != "" {
		query += " AND t.lock_num like '%" + num + "%'"
	}

	offset := (page - 1) * pageSize
	query += " order by lock_no asc LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa(offset)

	err := m.db.SQL(query, models.STATUS_NORMAL, caseid, models.STATUS_DEL).Find(&keys)
	return keys, err
}

// GetKeysCountByCase 根据案场获取钥匙数量
func (m *CaseDAO) GetKeysCountByCase(caseid, num string) (int, error) {
	var keys []model.TaCaseKey
	dao := m.db.Where("case_id=?", caseid).And("status>" + strconv.Itoa(models.STATUS_DEL))
	if num != "" {
		dao.And("lock_num like '%" + num + "%'")
	}
	err := dao.Find(&keys)
	return len(keys), err
}

// GetCurrentKey 获取当前钥匙的占用人员
func (m *CaseDAO) GetCurrentKey(keyid string) (*model.TaCaseKeyUse, error) {
	var use []model.TaCaseKeyUse
	err := m.db.Where("key_id=?", keyid).And("status=?", models.STATUS_NORMAL).Find(&use)
	if err != nil {
		return nil, err
	}
	if len(use) > 0 {
		return &use[0], nil
	}
	return nil, nil
}

// GetKeyByID 根据ID获取钥匙信息
func (m *CaseDAO) GetKeyByID(keyid string) (*model.TaCaseKey, error) {
	var keys []model.TaCaseKey
	err := m.db.Where("key_id=?", keyid).Find(&keys)
	if err != nil {
		return nil, err
	}
	if len(keys) > 0 {
		return &keys[0], nil
	}
	return nil, nil
}

// AddKey 新增单个钥匙信息
func (m *CaseDAO) AddKey(key model.TaCaseKey) (*model.TaCaseKey, error) {
	key.KeyId = utils.GetGUID()
	key.Status = STATUS_UNLOCK
	key.CreateDate = time.Now()
	_, err := m.db.Insert(key)
	return &key, err
}

// DelKey 删除钥匙信息
func (m *CaseDAO) DelKey(keyid string) error {
	var keyinfo = model.TaCaseKey{
		KeyId:  keyid,
		Status: models.STATUS_DEL,
	}
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("key_id=?", keyid).Update(keyinfo)
	return err
}

// UnLockKey 解绑钥匙信息
func (m *CaseDAO) UnLockKey(keyid string) error {
	sql := `update ta_case_key_use set status=` + strconv.Itoa(STATUS_UNLOCK) + ` where key_id= '` + keyid + `'`
	_, err := m.db.Exec(sql)
	return err
}

// GetKeyByNumRange 根据区间获取钥匙
func (m *CaseDAO) GetKeyByNumRange(caseid, nums string) ([]model.TaCaseKey, error) {
	var keys []model.TaCaseKey
	sql := `select * from ta_case_key where status>` + strconv.Itoa(models.STATUS_DEL) + `
	and case_id='` + caseid + `' and lock_num in ('` + strings.Replace(nums, ",", "','", -1) + `')`

	err := m.db.Sql(sql).Find(&keys)
	return keys, err
}

// GetKeyByNum 获取钥匙
func (m *CaseDAO) GetKeyByNum(caseid string, beginnum, endnum int) ([]model.TaCaseKey, error) {
	var keys []model.TaCaseKey
	sql := `select * from ta_case_key where  status>` + strconv.Itoa(models.STATUS_DEL) + `
	and case_id='` + caseid + `' and lock_no BETWEEN ` + strconv.Itoa(beginnum) + ` and ` + strconv.Itoa(endnum)

	err := m.db.Sql(sql).Find(&keys)
	return keys, err
}
