package cn.bunny.services.service.financial.impl;

import cn.bunny.dao.dto.financial.budgetCategory.BudgetCategoryAddDto;
import cn.bunny.dao.dto.financial.budgetCategory.BudgetCategoryDto;
import cn.bunny.dao.dto.financial.budgetCategory.BudgetCategoryUpdateDto;
import cn.bunny.dao.entity.financial.BudgetCategory;
import cn.bunny.dao.pojo.result.PageResult;
import cn.bunny.dao.vo.financial.admin.BudgetCategoryParentVo;
import cn.bunny.dao.vo.financial.admin.BudgetCategoryVo;
import cn.bunny.services.mapper.financial.BudgetCategoryMapper;
import cn.bunny.services.service.financial.BudgetCategoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 预算分类表 服务实现类
 * </p>
 *
 * @author Bunny
 * @since 2024-11-11 15:36:57
 */
@Service
public class BudgetCategoryServiceImpl extends ServiceImpl<BudgetCategoryMapper, BudgetCategory> implements BudgetCategoryService {

    /**
     * * 预算分类表 服务实现类
     *
     * @param pageParams 预算分类表分页查询page对象
     * @param dto        预算分类表分页查询对象
     * @return 查询分页预算分类表返回对象
     */
    @Override
    public PageResult<BudgetCategoryVo> getBudgetCategoryList(Page<BudgetCategory> pageParams, BudgetCategoryDto dto) {
        IPage<BudgetCategoryVo> page = baseMapper.selectListByPage(pageParams, dto);

        return PageResult.<BudgetCategoryVo>builder()
                .list(page.getRecords())
                .pageNo(page.getCurrent())
                .pageSize(page.getSize())
                .total(page.getTotal())
                .build();
    }

    /**
     * 添加预算分类表
     *
     * @param dto 预算分类表添加
     */
    @Override
    public void addBudgetCategory(@Valid BudgetCategoryAddDto dto) {
        // 保存数据
        BudgetCategory budgetCategory = new BudgetCategory();
        BeanUtils.copyProperties(dto, budgetCategory);
        save(budgetCategory);
    }

    /**
     * 更新预算分类表
     *
     * @param dto 预算分类表更新
     */
    @Override
    public void updateBudgetCategory(@Valid BudgetCategoryUpdateDto dto) {
        // 更新内容
        BudgetCategory budgetCategory = new BudgetCategory();
        BeanUtils.copyProperties(dto, budgetCategory);
        updateById(budgetCategory);
    }

    /**
     * 删除|批量删除预算分类表
     *
     * @param ids 删除id列表
     */
    @Override
    public void deleteBudgetCategory(List<Long> ids) {
        baseMapper.deleteBatchIdsWithPhysics(ids);
    }

    /**
     * 返回所有父级对象
     *
     * @return 父级列表
     */
    @Override
    public List<BudgetCategoryParentVo> getAllParentList() {
        return list().stream().map(budgetCategory -> {
            BudgetCategoryParentVo budgetCategoryParentVo = new BudgetCategoryParentVo();
            BeanUtils.copyProperties(budgetCategory, budgetCategoryParentVo);
            return budgetCategoryParentVo;
        }).toList();
    }
}
