package com.jy.business.corporation.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jy.business.capital.dao.CapitalAccountDao;
import com.jy.business.corporation.dao.CorporationDao;
import com.jy.business.corporation.model.dto.CorporationDto;
import com.jy.business.corporation.model.dto.CorporationSelectDto;
import com.jy.business.corporation.model.vo.CorporationVo;
import com.jy.business.corporation.service.CorporationService;
import com.jy.framework.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 公司信息 服务实现类
 * </p>
 *
 * @author
 * @since 2024-10-16
 */
@Service
public class CorporationServiceImpl implements CorporationService {

    @Resource
    private CorporationDao corporationDao;

    @Resource
    private CapitalAccountDao capitalAccountDao;

    @Override
    public Page<CorporationVo> getPage(CorporationSelectDto dto) {
        return corporationDao.getPage(dto);
    }

    @Override
    public CorporationVo getDetail(Long id) {
        CorporationVo vo = corporationDao.getDetail(id);
        AssertUtil.notNull(vo, "未知数据");
        return vo;
    }

    @Override
    public void add(CorporationDto dto) {
        corporationDao.save(dto);
    }

    @Override
    public void edit(CorporationDto dto) {
        corporationDao.updateById(dto);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<Long> idList) {
        // 公司是否存在资金账户
        corporationDao.listByIds(idList).forEach(item -> {
            boolean corporationExists = capitalAccountDao.corporationExists(item.getId());
            AssertUtil.eqFalse(corporationExists, "【{}】存在资金账户，无法删除", item.getName());
        });

        // 删除公司
        corporationDao.removeBatchByIds(idList);
    }

}
