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

import cn.hutool.core.collection.CollUtil;
import com.zt.plat.framework.common.pojo.PageResult;
import com.zt.plat.framework.common.util.object.BeanUtils;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendPageReqVO;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendRespVO;
import com.zt.plat.module.contractorder.api.vo.contract.ContractReceiveSendSaveReqVO;
import com.zt.plat.module.contractorder.dal.dataobject.contractreceivesend.ContractReceiveSendDO;
import com.zt.plat.module.contractorder.dal.mysql.contractreceivesend.ContractReceiveSendMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;

import java.util.List;

import static com.zt.plat.module.contractorder.enums.ErrorCodeConstants.CONTRACT_RECEIVE_SEND_NOT_EXISTS;


/**
 * 收发货规则 Service 实现类
 *
 * @author 后台管理-1
 */
@Service
@Validated
public class ContractReceiveSendServiceImpl implements ContractReceiveSendService {

    @Resource
    private ContractReceiveSendMapper contractReceiveSendMapper;

    @Override
    public ContractReceiveSendRespVO createContractReceiveSend(ContractReceiveSendSaveReqVO createReqVO) {
        // 插入
        ContractReceiveSendDO contractReceiveSend = BeanUtils.toBean(createReqVO, ContractReceiveSendDO.class);
        contractReceiveSendMapper.insert(contractReceiveSend);
        // 返回
        return BeanUtils.toBean(contractReceiveSend, ContractReceiveSendRespVO.class);
    }

    @Override
    public void updateContractReceiveSend(ContractReceiveSendSaveReqVO updateReqVO) {
        // 校验存在
        validateContractReceiveSendExists(updateReqVO.getId());
        // 更新
        ContractReceiveSendDO updateObj = BeanUtils.toBean(updateReqVO, ContractReceiveSendDO.class);
        contractReceiveSendMapper.updateById(updateObj);
    }

    @Override
    public void deleteContractReceiveSend(Long id) {
        // 校验存在
        validateContractReceiveSendExists(id);
        // 删除
        contractReceiveSendMapper.deleteById(id);
    }

    @Override
        public void deleteContractReceiveSendListByIds(List<Long> ids) {
        // 校验存在
        validateContractReceiveSendExists(ids);
        // 删除
        contractReceiveSendMapper.deleteByIds(ids);
        }

    private void validateContractReceiveSendExists(List<Long> ids) {
        List<ContractReceiveSendDO> list = contractReceiveSendMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(CONTRACT_RECEIVE_SEND_NOT_EXISTS);
        }
    }

    private void validateContractReceiveSendExists(Long id) {
        if (contractReceiveSendMapper.selectById(id) == null) {
            throw exception(CONTRACT_RECEIVE_SEND_NOT_EXISTS);
        }
    }

    @Override
    public ContractReceiveSendDO getContractReceiveSend(Long id) {
        return contractReceiveSendMapper.selectById(id);
    }

    @Override
    public PageResult<ContractReceiveSendDO> getContractReceiveSendPage(ContractReceiveSendPageReqVO pageReqVO) {
        return contractReceiveSendMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ContractReceiveSendRespVO> getContractReceiveSendListByContractId(Long contractId) {
        return BeanUtils.toBean(contractReceiveSendMapper.selectList(ContractReceiveSendDO::getContractId, contractId), ContractReceiveSendRespVO.class);
    }

}