package cases

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

	"github.com/go-xorm/xorm"
)

// CaseDAO 当前数据库操作对象
type CaseDAO struct {
	ctx *utils.Context
	db  *xorm.Session
}

// NewCaseDAO New Inst
func NewCaseDAO(ctx *utils.Context) *CaseDAO {
	return &CaseDAO{
		ctx: ctx,
		db:  ctx.DB,
	}
}

// CaseInfo 案场
type CaseInfo struct {
	model.SysCase  `xorm:"extends"`
	CreateUserName string
}

// GetCaseList 获取案场列表
func (m *CaseDAO) GetCaseList(caseids, casename string, page, pageSize int) ([]CaseInfo, error) {
	var cases []CaseInfo
	sql := `select a.*,b.user_name as create_user_name from sys_case a left join sys_user b on a.create_user = b.user_id
	 where a.status>` + strconv.Itoa(models.STATUS_DEL) + ` and a.case_id in ('` + strings.Replace(caseids, ",", "','", -1) + `')`
	if casename != "" {
		sql = sql + ` and  a.case_name like '%` + casename + `%'`
	}

	sql = sql + " order by create_date desc limit " + strconv.Itoa((page-1)*pageSize) + ", " + strconv.Itoa(page*pageSize)

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

// GetCaseCount 获取案场数量
func (m *CaseDAO) GetCaseCount(caseids, casename string) (int, error) {
	var cases []model.SysCase
	dao := m.db.Where("status>" + strconv.Itoa(models.STATUS_DEL)).And("case_id in ('" + strings.Replace(caseids, ",", "','", -1) + "')")
	if casename != "" {
		dao = dao.And("case_name like '%" + casename + "%'")
	}
	err := dao.Find(&cases)
	return len(cases), err
}

// GetCaseByID 根据ID获取案场信息
func (m *CaseDAO) GetCaseByID(caseid string) (*model.SysCase, error) {
	var cases []model.SysCase
	err := m.db.Where("status>"+strconv.Itoa(models.STATUS_DEL)).And("case_id=?", caseid).Find(&cases)
	if err != nil {
		return nil, err
	}
	if len(cases) > 0 {
		return &cases[0], nil
	}
	return nil, nil
}

// AddCase 新增案场信息
func (m *CaseDAO) AddCase(caseinfo model.SysCase) (*model.SysCase, error) {
	caseinfo.CaseId = utils.GetGUID()
	caseinfo.CreateDate = time.Now()
	caseinfo.Status = models.STATUS_NORMAL
	user := m.ctx.Get("user").(model.SysUser)
	caseinfo.CreateUser = user.UserId
	_, err := m.db.Insert(caseinfo)
	return &caseinfo, err
}

// UpdateCase 修改案场信息
func (m *CaseDAO) UpdateCase(caseinfo model.SysCase) error {
	var cols = []string{
		"case_name",
		"case_desc",
		"case_icon",
		"case_address",
		"case_tel",
		"coordinate",
	}
	_, err := m.db.Cols(cols...).Where("case_id=?", caseinfo.CaseId).Update(caseinfo)
	return err
}

// DelCase 删除案场信息
func (m *CaseDAO) DelCase(caseid string) error {
	var caseinfo = model.SysCaseUser{
		CaseId: caseid,
		Status: models.STATUS_DEL,
	}
	var cols = []string{
		"status",
	}
	_, err := m.db.Cols(cols...).Where("case_id=?", caseid).Update(caseinfo)
	return err
}

// GetCaseByOrg 获取案场列表
func (m *CaseDAO) GetCaseByOrg(org string) ([]model.SysCase, error) {
	var cs []model.SysCase

	if err := m.db.Where("org_id=?", org).
		And("status=?", models.STATUS_NORMAL).
		Asc("create_date").
		Find(&cs); err != nil {
		return nil, err
	}

	return cs, nil
}

type UserCase struct {
	CaseId   string
	CaseName string
	UserId   string
	UserName string
}

// GetCaseUserByType 获取案场人员
func (m *CaseDAO) GetCaseUserByType(csIDs []string, typeID string) ([]UserCase, error) {
	query := `
		SELECT n.user_id, n.real_name as user_name, s.case_id, '' as case_name
		FROM
			td_user_type s
		JOIN sys_user_type m ON s.type_id = m.type_id
		JOIN sys_user n ON m.user_id = n.user_id
		AND n.status = ?
		WHERE
			s.case_id IN ('` + strings.Join(csIDs, "','") + `')
		AND s.type_id = ?
	`

	var users []UserCase
	if err := m.db.SQL(query, models.STATUS_NORMAL, typeID).Find(&users); err != nil {
		return nil, err
	}

	return users, nil
}
