package com.hotwj.platform.operationManagement.fundAccount.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.hotwj.platform.operationManagement.fundAccount.domain.bo.HotFundAccountBo;
import com.hotwj.platform.operationManagement.fundAccount.domain.vo.HotFundAccountVo;
import com.hotwj.platform.operationManagement.fundAccount.domain.HotFundAccount;
import com.hotwj.platform.operationManagement.fundAccount.mapper.HotFundAccountMapper;
import com.hotwj.platform.operationManagement.fundAccount.service.IHotFundAccountService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 资金账户Service业务层处理
 *
 * @author shihongwei
 * @date 2026-04-28
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HotFundAccountServiceImpl implements IHotFundAccountService {

    private final HotFundAccountMapper baseMapper;

    /**
     * 查询资金账户
     *
     * @param id 主键
     * @return 资金账户
     */
    @Override
    public HotFundAccountVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询资金账户列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 资金账户分页列表
     */
    @Override
    public TableDataInfo<HotFundAccountVo> queryPageList(HotFundAccountBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotFundAccount> lqw = buildQueryWrapper(bo);
        Page<HotFundAccountVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的资金账户列表
     *
     * @param bo 查询条件
     * @return 资金账户列表
     */
    @Override
    public List<HotFundAccountVo> queryList(HotFundAccountBo bo) {
        LambdaQueryWrapper<HotFundAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotFundAccount> buildQueryWrapper(HotFundAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotFundAccount> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(HotFundAccount::getId);
        lqw.eq(bo.getCompanyId() != null, HotFundAccount::getCompanyId, bo.getCompanyId());
        lqw.eq(bo.getUserId() != null, HotFundAccount::getUserId, bo.getUserId());
        lqw.eq(StringUtils.isNotBlank(bo.getAccountNo()), HotFundAccount::getAccountNo, bo.getAccountNo());
        lqw.eq(bo.getAccountType() != null, HotFundAccount::getAccountType, bo.getAccountType());
        lqw.eq(bo.getBalanceAmount() != null, HotFundAccount::getBalanceAmount, bo.getBalanceAmount());
        lqw.eq(bo.getStatus() != null, HotFundAccount::getStatus, bo.getStatus());
        lqw.like(StringUtils.isNotBlank(bo.getCreateByName()), HotFundAccount::getCreateByName, bo.getCreateByName());
        lqw.like(StringUtils.isNotBlank(bo.getUpdateByName()), HotFundAccount::getUpdateByName, bo.getUpdateByName());
        lqw.eq(bo.getIsDeleted() != null, HotFundAccount::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    /**
     * 新增资金账户
     *
     * @param bo 资金账户
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HotFundAccountBo bo) {
        HotFundAccount add = MapstructUtils.convert(bo, HotFundAccount.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改资金账户
     *
     * @param bo 资金账户
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HotFundAccountBo bo) {
        HotFundAccount update = MapstructUtils.convert(bo, HotFundAccount.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotFundAccount entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除资金账户信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
