package com.bonus.sgzb.base.service.impl;


import com.bonus.sgzb.base.domain.BmProjectLot;
import com.bonus.sgzb.base.domain.vo.BmProjectLotImport;
import com.bonus.sgzb.base.mapper.BmProjectLotMapper;
import com.bonus.sgzb.base.service.BmProjectLotService;
import com.bonus.sgzb.common.core.exception.ServiceException;
import com.bonus.sgzb.common.core.utils.StringUtils;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class BmProjectLotServiceImpl implements BmProjectLotService {

    @Autowired
    private BmProjectLotMapper bmProjectLotMapper;

    @Override
    public List<BmProjectLot> getProjectLotAll(BmProjectLot bmProjectLot) {

        return bmProjectLotMapper.getProjectLotAll(bmProjectLot);
    }

    @Override
    public List<BmProjectLot> getProjectLot(BmProjectLot bmProjectLot) {

        return bmProjectLotMapper.getProjectLot(bmProjectLot);
    }

    @Override
    public AjaxResult projectLotAdd(BmProjectLot bmProjectLot) {
        BmProjectLot info = bmProjectLotMapper.selectByName(bmProjectLot.getLotName());
        if (StringUtils.isNotNull(info)) {
            return AjaxResult.error("新增标段工程名称重复，请重新提交！！！");
        }
        return AjaxResult.success(bmProjectLotMapper.projectLotAdd(bmProjectLot));
    }

    @Override
    public void remove(Long[] proIds) {
        bmProjectLotMapper.remove(proIds);
    }

    @Override
    public AjaxResult updateBmProjectLot(BmProjectLot bmProjectLot) {
        if (StringUtils.isNotEmpty(bmProjectLot.getLotName())) {
            BmProjectLot info = bmProjectLotMapper.selectByName(bmProjectLot.getLotName());
            if (StringUtils.isNotNull(info) && info.getLotId() != bmProjectLot.getLotId()) {
                return AjaxResult.error("修改标段工程名称重复，请重新提交！！！");
            }
        }
        return AjaxResult.success(bmProjectLotMapper.updateBmProjectLot(bmProjectLot));
    }

    @Override
    public int deleteProjectLotById(Long lotId) {
        return bmProjectLotMapper.deleteProjectLotById(lotId);
    }

    @Override
    @Transactional
    public String importBmProjectLot(List<BmProjectLotImport> bmProjectLotImportList) {
        log.info("BmProjectLotServiceImpl   importBmProjectLot  导入开始------");
        if (bmProjectLotImportList.isEmpty())
        {
            throw new ServiceException("导入项目管理数据不能为空！");
        }
        //保存项目管理
        for (BmProjectLotImport bmProjectLotImport: bmProjectLotImportList) {
            bmProjectLotMapper.projectLotImport(bmProjectLotImport);
        }
        log.info("BmProjectLotServiceImpl   importBmProjectLot  导入结束------");
        return "导入成功";
    }
}
