package com.dsk.biz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsk.biz.domain.BusinessOpenTender;
import com.dsk.biz.domain.bo.BusinessOpenTenderDto;
import com.dsk.biz.domain.bo.BusinessSearchDto;
import com.dsk.biz.mapper.BusinessOpenTenderMapper;
import com.dsk.biz.service.IBusinessOpenTenderService;
import com.dsk.common.core.domain.PageQuery;
import com.dsk.common.core.page.TableDataInfo;
import jodd.bean.BeanException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.Arrays;
import java.util.List;

/**
 * @Author lcl
 * @Data 2023/10/23 16:29
 */
@Slf4j
@Service
public class BusinessOpenTenderServiceImpl implements IBusinessOpenTenderService {

    @Resource
    private BusinessOpenTenderMapper baseMapper;


    @Override
    public TableDataInfo<BusinessOpenTender> selectList(BusinessOpenTenderDto dto, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.selectPage(pageQuery.build(),
                Wrappers.<BusinessOpenTender>lambdaQuery().eq(BusinessOpenTender::getBusinessId, dto.getBusinessId())));
    }

    @Override
    public int add(BusinessOpenTender bo) {
        verifyBean(bo);
        return baseMapper.insert(bo);
    }

    @Override
    public int edit(BusinessOpenTender bo) {
        if(ObjectUtils.isArray(bo.getId())) throw new BeanException("id不能为空！");
        verifyBean(bo);
        return baseMapper.updateById(bo);
    }

    @Override
    public int remove(Long[] ids) {
        return baseMapper.deleteBatchIds(Arrays.asList(ids));
    }

    private void verifyBean(BusinessOpenTender bo){
        if(ObjectUtils.isArray(bo.getBusinessId())) throw new BeanException("项目id不能为空！");
        if(ObjectUtils.isArray(bo.getTenderer())) throw new BeanException("开标人不能为空！");
        if(ObjectUtils.isArray(bo.getTendererNature())) throw new BeanException("企业性质不能为空！");
        if(ObjectUtils.isArray(bo.getTenderAmount())) throw new BeanException("投标金额不能为空！");
    }
}
