/*
 * @description: 安全巡检数据库操作相关
 * @Author: LanJianRong
 * @Date: 2020-11-20
 * @FilePath: \construction_management\dao\safe_dao.go
 */

package dao

import (
	"fmt"
	"time"

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

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

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

// 添加审批记录
func (d *SafeAuditDao) AddAuditRecord(safeId int, bId int, auditId int, times int, status int, progress int, opinion string, rectifiedInfo string) error {
	auditor := &models.CmSafeAudit{SafeId: safeId, BidsectionId: bId, AuditId: auditId, Times: times, CreateTime: time.Now(), Status: status, Progress: progress, Opinion: opinion, Rectifiedinfo: rectifiedInfo}
	_, err := d.engine.InsertOne(auditor)
	return err
}

// 获取最新审批顺序
func (d *SafeAuditDao) GetNewOrder(safeId int, times int) int {
	var max_order int
	_, err := d.engine.Sql("select Max(`audit_order`) as max_order from cm_safe_audit where safe_id = ? and times = ?", safeId, times).Get(&max_order)
	fmt.Println(err)
	if max_order == 0 {
		return 1
	} else {
		return max_order + 1
	}
}

// times从1开始循环，往history里面push
func (d *SafeAuditDao) GetAuditHistory(id int, times int) map[int][]viewmodels.HistorySafeAudit {
	auditorHistory := make(map[int][]viewmodels.HistorySafeAudit, 0)
	for i := 1; i <= times; i++ {
		auditors := make([]viewmodels.HistorySafeAudit, 0)
		d.engine.Sql("select ca.`id`, pa.`name`, pa.`position`, ca.`create_time`, ca.progress, ca.opinion, ca.status from `cm_project_account` as pa, `cm_safe_audit` as ca where ca.`safe_id` = ? and ca.`audit_id` = pa.`id` and times = ? order by id desc", id, i).Find(&auditors)
		auditorsArr := make([]viewmodels.HistorySafeAudit, 0)
		for _, auditor := range auditors {
			auditorVM := viewmodels.HistorySafeAudit{}
			id, _ := comm.AesEncrypt(auditor.Id, conf.SignSecret)
			auditorVM.Id = id
			auditorVM.CreateTime = auditor.CreateTime
			auditorVM.Name = auditor.Name
			auditorVM.Position = auditor.Position
			auditorVM.Status = auditor.Status
			auditorVM.Opinion = auditor.Opinion
			auditorVM.Progress = auditor.Progress
			auditorsArr = append(auditorsArr, auditorVM)
		}
		auditorHistory[i] = auditorsArr
	}
	return auditorHistory
}

// 根据id获取记录
func (d *SafeAuditDao) FindById(id int) (*models.CmSafeAudit, error) {
	data := &models.CmSafeAudit{Id: id}
	_, err := d.engine.Get(data)
	return data, err
}

// 查找最新的整改单列表
func (d *SafeAuditDao) GetRectifications(bid int, pid int, dataId int, dataType int) ([]viewmodels.CheckOrderVM, error) {
	data := make([]viewmodels.CheckOrderVM, 0)
	err := d.engine.Sql("select cm.`content` as opinion, cm.`rectified_time` as `create_time`, pa.`name` from `cm_rectification` as cm left join `cm_project_account` as pa on pa.`id` = cm.`account_id` where cm.`bidsection_id` = ? and cm.`project_id` = ? and cm.`data_id` = ? and cm.`data_type` = ? ", bid, pid, dataId, dataType).Find(&data)
	return data, err
}

// 插入审批记录
func (d *SafeAuditDao) InsertData(data models.CmSafeAudit) error {
	_, err := d.engine.Insert(&data)
	return err
}

// 改变下一条审批记录为待审核状态
func (d *SafeAuditDao) ChangeNextRecord(times int, audit_order int) error {
	data := &models.CmSafeAudit{Status: 1, CreateTime: time.Now()}
	_, err := d.engine.Where("times = ? and audit_order = ?", times, audit_order).Update(data)
	return err
}

// 获取最后一个审批人
func (d *SafeAuditDao) GetLastAuditor(times int, safeId int) (*models.CmSafeAudit, error) {
	data := &models.CmSafeAudit{}
	_, err := d.engine.Where("times = ? and safe_id = ?", times, safeId).Desc("audit_order").Limit(1).Get(data)
	return data, err
}
