package com.ruoyi.oa.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.oa.domain.SysOaReceiveAccount;
import com.ruoyi.oa.domain.bo.SysOaReceiveAccountBo;
import com.ruoyi.oa.domain.vo.SysOaReceiveAccountVo;
import com.ruoyi.oa.mapper.SysOaReceiveAccountMapper;
import com.ruoyi.oa.service.ISysOaReceiveAccountService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;


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

/**
 * 付款账户管理Service业务层处理
 *
 * @author hdka
 * @date 2024-11-02
 */
@RequiredArgsConstructor
@Service
public class SysOaReceiveAccountServiceImpl implements ISysOaReceiveAccountService {

    private final SysOaReceiveAccountMapper baseMapper;

    /**
     * 查询付款账户管理
     */
    @Override
    public SysOaReceiveAccountVo queryById(Long receiveAccountId){
        return baseMapper.selectVoById(receiveAccountId);
    }

    /**
     * 查询付款账户管理列表
     */
    @Override
    public List<SysOaReceiveAccountVo> queryPageList(SysOaReceiveAccountBo bo) {

        LambdaQueryWrapper<SysOaReceiveAccount> sysOaReceiveAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysOaReceiveAccountLambdaQueryWrapper.eq(SysOaReceiveAccount::getReceiveAccountId,-1L);
        SysOaReceiveAccountVo sysOaReceiveAccountVo = baseMapper.selectVoOne(sysOaReceiveAccountLambdaQueryWrapper);
        LambdaQueryWrapper<SysOaReceiveAccount> sysOaReceiveAccountLambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        List<SysOaReceiveAccountVo> sysOaReceiveAccountVos = baseMapper.selectVoList(sysOaReceiveAccountLambdaQueryWrapper1.eq(SysOaReceiveAccount::getParentId, -1L));
        for (SysOaReceiveAccountVo oaReceiveAccountVo : sysOaReceiveAccountVos) {
            oaReceiveAccountVo.setLabel(oaReceiveAccountVo.getReceiveAccountName());
        }
        sysOaReceiveAccountVo.setLabel(sysOaReceiveAccountVo.getReceiveAccountName());
        sysOaReceiveAccountVo.setChildren(sysOaReceiveAccountVos);
        ArrayList<SysOaReceiveAccountVo> list = new ArrayList<>();
        list.add(sysOaReceiveAccountVo);
        return list;
    }

    /**
     * 查询付款账户管理列表
     */
    @Override
    public List<SysOaReceiveAccountVo> queryList(SysOaReceiveAccountBo bo) {
        LambdaQueryWrapper<SysOaReceiveAccount> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysOaReceiveAccount> buildQueryWrapper(SysOaReceiveAccountBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysOaReceiveAccount> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getReceiveAccountName()), SysOaReceiveAccount::getReceiveAccountName, bo.getReceiveAccountName());
        return lqw;
    }

    /**
     * 新增付款账户管理
     */
    @Override
    public SysOaReceiveAccountVo insertByBo(SysOaReceiveAccountBo bo) {
        SysOaReceiveAccount add = BeanUtil.toBean(bo, SysOaReceiveAccount.class);
        validEntityBeforeSave(add);
        Long receiveAccountId = System.currentTimeMillis()/1000%1000000000;
        add.setReceiveAccountId(receiveAccountId);
        baseMapper.insert(add);
        return BeanUtil.copyProperties(add, SysOaReceiveAccountVo.class);
    }

    /**
     * 修改付款账户管理
     */
    @Override
    public Boolean updateByBo(SysOaReceiveAccountBo bo) {
        SysOaReceiveAccount update = BeanUtil.toBean(bo, SysOaReceiveAccount.class);
        validEntityBeforeSave(update);
        System.out.println(bo);
        return baseMapper.updateById(update) > 0;
    }

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

    /**
     * 批量删除付款账户管理
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
