/*
 * Copyright (c) 2023 - present Dromara, All rights reserved.
 *
 *   https://visor.dromara.org
 *   https://visor.dromara.org.cn
 *   https://visor.orionsec.cn
 *
 * Members:
 *   Jiahang Li - ljh1553488six@139.com - author
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ${currentPackage};

import cn.orionsec.kit.lang.utils.collect.Lists;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.dromara.visor.common.constant.ErrorMessage;
import org.dromara.visor.common.utils.Valid;
#foreach($pkg in ${customModuleFilePackages})
import ${pkg}.*;
#end
#foreach($pkg in ${customProviderFilePackages})
import ${pkg}.*;
#end
import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * $!{table.comment} 对外服务实现类
 *
 * @author ${author}
 * @version ${version}
 * @since ${date}
 */
@Slf4j
@Service
public class ${type}ApiImpl implements ${type}Api {

    @Resource
    private ${type}Service ${typeLower}Service;

    @Resource
    private ${type}DAO ${typeLower}DAO;

    @Override
    public Long create${type}(${type}CreateDTO dto) {
        log.info("${type}Api.create${type} dto: {}", JSON.toJSONString(dto));
        Valid.valid(dto);
        // 转换
        ${type}CreateRequest request = ${type}ProviderConvert.MAPPER.toRequest(dto);
        // 创建
        return ${typeLower}Service.create${type}(request);
    }

    @Override
    public Integer update${type}ById(${type}UpdateDTO dto) {
        log.info("${type}Api.update${type}ById dto: {}", JSON.toJSONString(dto));
        Valid.valid(dto);
        // 转换
        ${type}UpdateRequest request = ${type}ProviderConvert.MAPPER.toRequest(dto);
        // 修改
        return ${typeLower}Service.update${type}ById(request);
    }

    @Override
    public Integer update${type}(${type}QueryDTO query, ${type}UpdateDTO update) {
        log.info("${type}Api.update${type} query: {}, update: {}", JSON.toJSONString(query), JSON.toJSONString(update));
        Valid.valid(query);
        Valid.valid(update);
        // 更新
        int effect = ${typeLower}Service.update${type}(${type}ProviderConvert.MAPPER.toRequest(query),
                ${type}ProviderConvert.MAPPER.toRequest(update));
        log.info("${type}Api.update${type} effect: {}", effect);
        return effect;
    }

    @Override
    public ${type}DTO get${type}ById(Long id) {
        log.info("${type}Api.get${type}ById id: {}", id);
        Valid.notNull(id, ErrorMessage.ID_MISSING);
        // 修改
        ${type}DO record = ${typeLower}DAO.selectById(id);
        if (record == null) {
            return null;
        }
        // 转换
        return ${type}ProviderConvert.MAPPER.to(record);
    }

    @Override
    public List<${type}DTO> get${type}ByIdList(List<Long> idList) {
        log.info("${type}Api.get${type}ByIdList idList: {}", idList);
        if (Lists.isEmpty(idList)) {
            return Lists.empty();
        }
        // 查询
        List<${type}DO> rows = ${typeLower}DAO.selectBatchIds(idList);
        // 转换
        return ${type}ProviderConvert.MAPPER.toList(rows);
    }

    @Override
    public List<${type}DTO> get${type}List(${type}QueryDTO dto) {
        log.info("${type}Api.get${type}List dto: {}", JSON.toJSONString(dto));
        Valid.valid(dto);
        // 条件
        LambdaQueryWrapper<${type}DO> wrapper = this.buildQueryWrapper(dto);
        // 查询
        return ${typeLower}DAO.of(wrapper).list(${type}ProviderConvert.MAPPER::to);
    }

    #if($meta.enableCache)
    @Override
    public List<${type}DTO> get${type}ListByCache() {
        return ${typeLower}Service.get${type}ListByCache()
                .stream()
                .map(${type}ProviderConvert.MAPPER::to)
                .sorted(Comparator.comparing(${type}DTO::getId))
                .collect(Collectors.toList());
    }

    #end
    @Override
    public Long get${type}Count(${type}QueryDTO dto) {
        log.info("${type}Api.get${type}Count dto: {}", JSON.toJSONString(dto));
        Valid.valid(dto);
        // 条件
        LambdaQueryWrapper<${type}DO> wrapper = this.buildQueryWrapper(dto);
        // 查询
        return ${typeLower}DAO.selectCount(wrapper);
    }

    @Override
    public Integer delete${type}ById(Long id) {
        log.info("${type}Api.delete${type}ById id: {}", id);
        Valid.notNull(id, ErrorMessage.ID_MISSING);
        // 删除
        Integer effect = ${typeLower}Service.delete${type}ById(id);
        log.info("${type}Api.delete${type}ById id: {}, effect: {}", id, effect);
        return effect;
    }

    @Override
    public Integer delete${type}ByIdList(List<Long> idList) {
        log.info("${type}Api.delete${type}ByIdList idList: {}", idList);
        Valid.notEmpty(idList, ErrorMessage.ID_MISSING);
        // 删除
        Integer effect = ${typeLower}Service.delete${type}ByIdList(idList);
        log.info("${type}Api.delete${type}ByIdList effect: {}", effect);
        return effect;
    }

    @Override
    public Integer delete${type}(${type}QueryDTO dto) {
        log.info("${type}Api.delete${type} dto: {}", JSON.toJSONString(dto));
        Valid.valid(dto);
        // 删除
        Integer effect = ${typeLower}Service.delete${type}(${type}ProviderConvert.MAPPER.toRequest(dto));
        log.info("${type}Api.delete${type} effect: {}", effect);
        return effect;
    }

    /**
     * 构建查询 wrapper
     *
     * @param dto dto
     * @return wrapper
     */
    private LambdaQueryWrapper<${type}DO> buildQueryWrapper(${type}QueryDTO dto) {
        return ${typeLower}DAO.wrapper()
        #foreach($field in ${table.fields})
                .eq(${type}DO::get${field.capitalName}, dto.get${field.capitalName}())#if(!$foreach.hasNext);#end
        #end
    }

}
