/*
 * @description:项目用户数据库操作
 * @Author: CP
 * @Date: 2020-08-31 15:57:04
 * @FilePath: \construction_management\dao\project_account_dao.go
 */
package dao

import (
	"errors"

	"github.com/go-xorm/xorm"
	"go.mod/models"
	"go.mod/web/viewmodels"
)

//数据库操作引擎
type ProjectAccountDao struct {
	engine *xorm.Engine
}

//获得一个DAO对象
func NewProjectAccountDao(engine *xorm.Engine) *ProjectAccountDao {
	return &ProjectAccountDao{
		engine: engine,
	}
}

//CmProjectAccount 设置的值获得数据-弃用
// func (d *ProjectAccountDao) GetOne(data *models.CmProjectAccount) *models.CmProjectAccount {
// 	//data := data
// 	//Get取到值后，会自动赋值到data中
// 	ok, err := d.engine.
// 		Where("account = ?", data.Account).
// 		Get(data)
// 	if ok && err == nil {
// 		return data
// 	} else {
// 		data.Id = 0
// 		return data
// 	}
// }

// 根据账号获得数据
func (d *ProjectAccountDao) GetAccount(account string) *models.CmProjectAccount {
	data := &models.CmProjectAccount{}
	ok, err := d.engine.
		Where("account = ?", account).
		Get(data)
	if ok && err == nil {
		return data
	} else {
		data.Id = 0
		return data
	}
}

//id获得数据
func (d *ProjectAccountDao) Get(id int, projectId int) *models.CmProjectAccount {
	data := &models.CmProjectAccount{Id: id, ProjectId: projectId}
	ok, err := d.engine.Get(data)
	if ok && err == nil {
		return data
	} else {
		data.Id = 0
		return data
	}
}

// 获得项目下的账号信息
func (d *ProjectAccountDao) GetAll(projectId int) []models.CmProjectAccount {
	datalist := make([]models.CmProjectAccount, 0)
	err := d.engine.
		Where("project_id = ?", projectId).
		Desc("id").
		Find(&datalist)
	if err != nil {
		return datalist
	} else {
		return datalist
	}
}

func (d *ProjectAccountDao) GetInId(ids string) []models.CmProjectAccount {
	datalist := make([]models.CmProjectAccount, 0)
	err := d.engine.
		Where("id in (?) ", ids).
		Desc("id").
		Find(&datalist)
	if err != nil {
		return datalist
	} else {
		return datalist
	}
}

// 检索 账号姓名单位手机
func (d *ProjectAccountDao) Search(name string, projectId int) []models.CmProjectAccount {
	datalist := make([]models.CmProjectAccount, 0)
	err := d.engine.
		Where("project_id = ? ", projectId).
		And("account like ? or name like ? or company like ? or mobile like ?", name+"%", name+"%", name+"%", name+"%").
		Desc("id").
		Find(&datalist)
	if err != nil {
		return datalist
	} else {
		return datalist
	}
}

// 新增
func (d *ProjectAccountDao) Add(data *models.CmProjectAccount) error {
	_, err := d.engine.Insert(data)
	return err
}

//更新
func (d *ProjectAccountDao) Update(data *models.CmProjectAccount, columns []string) error {
	//_, err := d.engine.Id(data.Id).MustCols(columns...).Update(data)
	is, err := d.engine.Where("id = ? and project_id = ? ", data.Id, data.ProjectId).MustCols(columns...).Update(data)
	if is == 0 {
		return errors.New("未找到账号")
	}
	return err
}

// 物理删除
func (d *ProjectAccountDao) Delete(data *models.CmProjectAccount) error {
	is, err := d.engine.Where("id = ? and project_id = ? ", data.Id, data.ProjectId).Delete(data)
	if is == 0 {
		return errors.New("未找到账号")
	}
	return err
}

// 查找
func (d *ProjectAccountDao) FindById(id int) (viewmodels.ProjectInfo, error) {
	projectInfo := viewmodels.ProjectInfo{}
	_, err := d.engine.Sql("select p.`name` as project_name, p.`code`, p.`create_time`, a.`mobile`, a.`name` from `cm_project` as p, `cm_project_account` as a where p.`user_id` = a.`id` and p.`id` = ?", id).Get(&projectInfo)
	return projectInfo, err
}
