/*
 * @description: 安全巡检数据库操作相关
 * @Author: LanJianRong
 * @Date: 2020-11-20
 * @FilePath: \construction_management\dao\quality_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 QualityAuditDao struct {
	engine *xorm.Engine
}

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

// 添加审批记录
func (d *QualityAuditDao) AddAuditRecord(qualityId int, bId int, auditId int, times int, status int, progress int, opinion string, rectifiedInfo string) error {
	auditor := &models.CmQualityAudit{QualityId: qualityId, 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 *QualityAuditDao) GetNewOrder(qualityId int, times int) int {
	var max_order int
	_, err := d.engine.Sql("select Max(`audit_order`) as max_order from cm_quality_audit where quality_id = ? and times = ?", qualityId, times).Get(&max_order)
	fmt.Println(err)
	if max_order == 0 {
		return 1
	} else {
		return max_order + 1
	}
}

// times从1开始循环，往history里面push
func (d *QualityAuditDao) GetAuditHistory(id int, times int) map[int][]viewmodels.HistoryQualityAudit {
	auditorHistory := make(map[int][]viewmodels.HistoryQualityAudit, 0)
	for i := 1; i <= times; i++ {
		auditors := make([]viewmodels.HistoryQualityAudit, 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_quality_audit` as ca where ca.`quality_id` = ? and ca.`audit_id` = pa.`id` and times = ? order by id desc", id, i).Find(&auditors)
		auditorsArr := make([]viewmodels.HistoryQualityAudit, 0)
		for _, auditor := range auditors {
			auditorVM := viewmodels.HistoryQualityAudit{}
			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 *QualityAuditDao) FindById(id int) (*models.CmQualityAudit, error) {
	data := &models.CmQualityAudit{Id: id}
	_, err := d.engine.Get(data)
	return data, err
}

// 查找最新的整改单
func (d *QualityAuditDao) GetLastedOrder(QualityId int) ([]viewmodels.CheckOrderVM, error) {
	data := make([]viewmodels.CheckOrderVM, 0)
	err := d.engine.Sql("select cm.`rectifiedInfo` as opinion, cm.`create_time`, pa.`name` from `cm_quality_audit` as cm left join `cm_project_account` as pa on pa.`id` = cm.`audit_id` where cm.`quality_id` = ? and cm.`progress` = ?", QualityId, 2).Find(&data)
	return data, err
}

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

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

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