/*
 * @description: 账号权限
 * @Author: CP
 * @Date: 2021-01-26 15:09:10
 * @FilePath: \construction_management\dao\permission_account_dao.go
 */

package dao

import (
	"encoding/json"
	"errors"

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

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

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

// 获得标段下账号权限
func (d *PermissionAccountDao) GetBidsectionId(bidsectionId int) []models.CmPermissionAccount {
	datalist := make([]models.CmPermissionAccount, 0)
	_ = d.engine.
		Where(" bidsection_id=?  ", bidsectionId).
		Find(&datalist)
	return datalist
}

// 获得标段下账号权限
func (d *PermissionAccountDao) GetBidsectionIdAccountId(bidsectionId int, accountId int) *models.CmPermissionAccount {
	data := &models.CmPermissionAccount{}
	_, err := d.engine.
		Where(" bidsection_id= ? and account_id = ? ", bidsectionId, accountId).
		Get(data)
	if err == nil {
		return data
	} else {
		data.Id = 0
		return data
	}
}

// 获得项目下账号权限
func (d *PermissionAccountDao) GetProjectIdAccountId(projectId int, accountId int) []models.CmPermissionAccount {
	datalist := make([]models.CmPermissionAccount, 0)
	_ = d.engine.
		Where(" project_id=? and account_id= ?  ", projectId, accountId).
		Find(&datalist)
	return datalist
}

//更新
func (d *PermissionAccountDao) Update(data *models.CmPermissionAccount, columns []string) error {
	//_, err := d.engine.Id(data.Id).MustCols(columns...).Update(data)
	_, err := d.engine.Where("project_id = ? and bidsection_id = ? and account_id= ? ", data.ProjectId, data.BidsectionId, data.AccountId).MustCols(columns...).Update(data)
	if err != nil {
		return errors.New("更新权限-账号失败")
	}
	return nil
}

// 获得当前账号下的标段权限
func (d *PermissionAccountDao) GetPermissionWithAccountId(accountId int, bidsectionId int, isAdmin bool) (viewmodels.PermissionView, error) {
	data := viewmodels.PermissionView{}
	permission := viewmodels.Permission{}
	if isAdmin == true {
		permission.ContractAccess = 1
		permission.ContractAdd = 1
		permission.ContractDelete = 1
		permission.QualityAccess = 1
		permission.QualityAdd = 1
		permission.QualityDelete = 1
		permission.SafeAccess = 1
		permission.SafeAdd = 1
		permission.SafeDelete = 1
	}
	has, err := d.engine.
		Sql("select * from cm_permission_account where bidsection_id= ? and account_id = ?", bidsectionId, accountId).
		Get(&data)
	if has == false {
		contractPermission := map[string]int{
			"add":    permission.ContractAccess,
			"delete": permission.ContractDelete,
			"access": permission.ContractAccess,
		}
		contractPermissionByte, err := json.Marshal(contractPermission)
		if err != nil {
			return data, errors.New("合同权限解析失败")
		}
		contractPermissionStr := string(contractPermissionByte)

		qualityPermission := map[string]int{
			"add":    permission.QualityAdd,
			"delete": permission.QualityDelete,
			"access": permission.QualityAccess,
		}
		qualityPermissionByte, err := json.Marshal(qualityPermission)
		if err != nil {
			return data, errors.New("合同权限解析失败")
		}
		qualityPermissionStr := string(qualityPermissionByte)

		safePermission := map[string]int{
			"add":    permission.SafeAdd,
			"delete": permission.SafeDelete,
			"access": permission.SafeAccess,
		}
		safePermissionByte, err := json.Marshal(safePermission)
		if err != nil {
			return data, errors.New("合同权限解析失败")
		}
		safePermissionStr := string(safePermissionByte)
		data.ContractPermission = contractPermissionStr
		data.QualityPermission = qualityPermissionStr
		data.SafePermission = safePermissionStr
	}
	return data, err
}
