package com.fuint.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fuint.common.dto.ext.CurrencyDto;
import com.fuint.common.param.PageParam;
import com.fuint.common.vo.CurrencyVo;
import com.fuint.framework.annoation.OperationServiceLog;
import com.fuint.framework.exception.BusinessCheckException;
import com.fuint.framework.pagination.PaginationRequest;
import com.fuint.framework.pagination.PaginationResponse;
import com.fuint.framework.web.PageResult;
import com.fuint.repository.model.MtCurrency;
import com.fuint.common.service.CurrencyService;
import com.fuint.common.enums.StatusEnum;
import com.fuint.repository.mapper.MtCurrencyMapper;
import com.github.pagehelper.PageHelper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * 货币表服务接口
 *
 * Created by pzg
 * CopyRight https://www.fuint.cn
 */
@Service
@AllArgsConstructor
public class CurrencyServiceImpl extends ServiceImpl<MtCurrencyMapper, MtCurrency> implements CurrencyService {

    private static final Logger logger = LoggerFactory.getLogger(CurrencyServiceImpl.class);


    @Override
    public boolean addCurrency(CurrencyDto currencyDto) {
        if (!isNameUnique(currencyDto.getName())) {
            return false; // 货币名称已存在
        }
        MtCurrency mtCurrency = new MtCurrency();
        mtCurrency.setId(currencyDto.getId());
        mtCurrency.setName(currencyDto.getName());
        mtCurrency.setDeleteFlag(0);
        return save(mtCurrency);
    }

    @Override
    public boolean updateCurrency(CurrencyDto currencyDto) {
        MtCurrency existing = getById(currencyDto.getId());
        if (existing == null) {
            return false; // 货币不存在
        }
        if (!existing.getName().equals(currencyDto.getName()) && !isNameUnique(currencyDto.getName())) {
            return false; // 货币名称已存在
        }
        MtCurrency mtCurrency = new MtCurrency();
        mtCurrency.setId(currencyDto.getId());
        mtCurrency.setName(currencyDto.getName());
        return updateById(mtCurrency);
    }

    @Override
    public PageResult<CurrencyVo> getCurrencyById(String name, PageParam pageParam) {
        IPage<MtCurrency> mtCurrencyIPage = this.baseMapper.selectPage(new Page<>(pageParam.getPage(), pageParam.getPageSize()), new LambdaQueryWrapper<MtCurrency>()
                .like(StringUtils.isNotBlank(name),MtCurrency::getName, name));
        List<CurrencyVo> currencyVos = mtCurrencyIPage.getRecords().stream().map(mtCurrency -> {
            CurrencyVo currencyVo = new CurrencyVo();
            BeanUtils.copyProperties(mtCurrency, currencyVo);
            return currencyVo;
        }).collect(Collectors.toList());
        return PageResult.<CurrencyVo>builder().total(Math.toIntExact(mtCurrencyIPage.getTotal())).data(currencyVos).build();
    }

    @Override
    public boolean deleteCurrency(Integer id) {
        return removeById(id);
    }

    @Override
    public List<CurrencyVo> allList(String name) {
        //获取货币列表，可根据名称模糊查询
        List<MtCurrency> mtCurrencies = this.list(new LambdaQueryWrapper<MtCurrency>()
                .like(StringUtils.isNotBlank(name), MtCurrency::getName, name));
        //将MtCurrency复制到CurrencyVo并返回
	    return mtCurrencies.stream().map(mtCurrency -> {
            CurrencyVo currencyVo = new CurrencyVo();
            BeanUtils.copyProperties(mtCurrencies, currencyVo);
            return currencyVo;
        }).collect(Collectors.toList());
    }


    public boolean isNameUnique(String name) {
        return this.baseMapper.countByNameAndProject(name) == 0;
    }
}
