package gaf3.core.services.role.service

import gaf3.core.data.PagedData
import gaf3.core.exception.BusinessError
import gaf3.core.jpa.extension.findAll
import gaf3.core.services.role.dao.GafRoleItemDAO
import gaf3.core.services.role.domain.RoleForm
import gaf3.core.services.role.entity.GafRoleItem
import gaf3.core.util.DataBeanHelper
import gaf3.core.util.StringUtil
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.data.repository.findByIdOrNull
import org.springframework.stereotype.Service
import org.springframework.util.Assert.hasText
import org.springframework.validation.annotation.Validated
import java.util.*

@Service
@Validated
class RoleService (@Autowired private val itemDao: GafRoleItemDAO) {
    @JvmOverloads
    fun find(filter: GafRoleItem, skip: Int = 0, limit: Int = 100): PagedData<GafRoleItem> {
        val page:Int = skip/limit
        val paged = itemDao.findAll(filter, PageRequest.of(page, limit, Sort.Direction.ASC, "ranking"))

        return PagedData(paged.content, paged.totalElements.toInt())
    }

    fun create(type: String, form: RoleForm): GafRoleItem {
        // TODO: 检查约束条件
        hasText(type, "type不能为空")
        hasText(form.name, "name不能为空")
        hasText(form.code, "code不能为空")
        if(itemDao.existsByName(form.name!!)) throw BusinessError(BusinessError.ERR_DATA_EXISTED, "角色已存在")
        if(itemDao.existsByCode(form.code!!)) throw BusinessError(BusinessError.ERR_DATA_EXISTED, "角色编码重复")

        // TODO: 准备数据
        var data = GafRoleItem()
        DataBeanHelper.Bean2Obj(form, data)
        data.id = UUID.randomUUID().toString()
        data.preset = 0

        // TODO: 保存数据
        data = itemDao.save(data)
        return data
    }

    fun update(id: String, data: RoleForm): GafRoleItem {
        // 检查约束条件
        hasText(id, "id不能为空")

        // 检查数据存在性
        var entity = itemDao.findByIdOrNull(id) ?: throw BusinessError(BusinessError.ERR_DATA_NOTEXIST)
        if(!StringUtil.isNullOrEmpty(data.name) && data.name != entity.name && itemDao.existsByName(data.name!!)) {
            throw BusinessError(BusinessError.ERR_DATA_EXISTED, "角色名称已经存在")
        }
        if(!StringUtil.isNullOrEmpty(data.code) && data.code != entity.code && itemDao.existsByName(data.code!!)) {
            throw BusinessError(BusinessError.ERR_DATA_EXISTED, "角色编码已经存在")
        }
        // 准备数据，禁止修改type、code、preset
        DataBeanHelper.Bean2Obj(data, entity, "type", "code", "preset")

        // 保存数据
        entity = itemDao.save(entity)
        return entity
    }

    fun delete(id: String) {
        // TODO: 验证信息
        hasText(id, "ID不能为空")
        // 检查数据存在性
        var entity = itemDao.findByIdOrNull(id) ?: throw BusinessError(BusinessError.ERR_DATA_NOTEXIST)
        if(entity.preset == 1) {
            throw BusinessError(BusinessError.ERR_SERVICE_FAULT, "系统预置数据不能删除")
        }
        // TODO: 删除数据
        itemDao.deleteById(id)
    }

    fun fetch(id: String): GafRoleItem? {
        // TODO: 验证信息
        hasText(id, "ID不能为空")
        return itemDao.findByIdOrNull(id)
    }

    fun fetchByName(name: String): GafRoleItem? {
        // TODO: 验证信息
        hasText(name, "name不能为空")
        return itemDao.findOneByName(name)
    }
//
//    fun listTypes(): List<GafRoleItem> {
//        return itemDao.listTypes()
//    }
//
//    fun listItems(type: String?): List<GafRoleItem> {
//        return itemDao.listItems(type)
//    }
}