package com.zt.plat.module.contractorder.service.amountdismantle;

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantlePageReqVO;
import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantleRespVO;
import com.zt.plat.module.contractorder.api.vo.contract.AmountDismantleSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.amountdismantle.AmountDismantleDO;
import com.zt.plat.module.contractorder.dal.mysql.amountdismantle.AmountDismantleMapper;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;


import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.zt.plat.framework.common.util.collection.CollectionUtils.convertList;
import static com.zt.plat.module.contractorder.enums.ErrorCodeConstants.AMOUNT_DISMANTLE_NOT_EXISTS;


/**
 * 结算金额拆分 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class AmountDismantleServiceImpl implements AmountDismantleService {

    @Resource
    private AmountDismantleMapper amountDismantleMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AmountDismantleRespVO createAmountDismantle(AmountDismantleSaveReqVO createReqVO) {
        // 插入
        AmountDismantleDO amountDismantle = BeanUtils.toBean(createReqVO, AmountDismantleDO.class);
        amountDismantleMapper.insert(amountDismantle);
        // 返回
        return BeanUtils.toBean(amountDismantle, AmountDismantleRespVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAmountDismantle(AmountDismantleSaveReqVO updateReqVO) {
        // 校验存在
        validateAmountDismantleExists(updateReqVO.getId());
        // 更新
        AmountDismantleDO updateObj = BeanUtils.toBean(updateReqVO, AmountDismantleDO.class);
        amountDismantleMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAmountDismantle(Long id) {
        // 校验存在
        validateAmountDismantleExists(id);
        // 删除
        amountDismantleMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAmountDismantleListByIds(List<Long> ids) {
        // 校验存在
        validateAmountDismantleExists(ids);
        // 删除
        amountDismantleMapper.deleteByIds(ids);
    }

    private void validateAmountDismantleExists(List<Long> ids) {
        List<AmountDismantleDO> list = amountDismantleMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(AMOUNT_DISMANTLE_NOT_EXISTS);
        }
    }

    private void validateAmountDismantleExists(Long id) {
        if (amountDismantleMapper.selectById(id) == null) {
            throw exception(AMOUNT_DISMANTLE_NOT_EXISTS);
        }
    }

    @Override
    public AmountDismantleDO getAmountDismantle(Long id) {
        return amountDismantleMapper.selectById(id);
    }

    @Override
    public PageResult<AmountDismantleDO> getAmountDismantlePage(AmountDismantlePageReqVO pageReqVO) {
        return amountDismantleMapper.selectPage(pageReqVO);
    }

    @Override
    public List<AmountDismantleDO> getAmountDismantleListByContractId(Long contractId) {
        return amountDismantleMapper.selectList(AmountDismantleDO::getContractId, contractId);
    }

}