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

import com.bonus.sgzb.base.domain.BmProjectInfo;
import com.bonus.sgzb.base.mapper.BmProjectInfoMapper;
import com.bonus.sgzb.base.service.BmProjectInfoService;
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BmProjectInfoServiceImpl implements BmProjectInfoService {

    @Autowired
    private BmProjectInfoMapper bmProjectInfoMapper;

    @Override
    public List<BmProjectInfo> getProjectInfoAll(BmProjectInfo bmProjectInfo) {

        return bmProjectInfoMapper.getProjectInfoAll(bmProjectInfo);
    }

    @Override
    public List<BmProjectInfo> getProjectInfo(BmProjectInfo bmProjectInfo) {
        return bmProjectInfoMapper.getProjectInfo(bmProjectInfo);
    }
    @Override
    public List<BmProjectInfo> getProjectLot() {
        return bmProjectInfoMapper.getProjectLot();
    }

    @Override
    public AjaxResult projectInfoAdd(BmProjectInfo bmProjectInfo) {
        BmProjectInfo info = bmProjectInfoMapper.selectByName(bmProjectInfo.getProName());
        if (StringUtils.isNotNull(info)) {
            return AjaxResult.error("新增工程项目名称重复，请重新提交！！！");
        }
        return AjaxResult.success(bmProjectInfoMapper.projectInfoAdd(bmProjectInfo));
    }

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

    @Override
    public AjaxResult updateBmProjectInfo(BmProjectInfo bmProjectInfo) {
        if (StringUtils.isNotEmpty(bmProjectInfo.getProName())) {
            BmProjectInfo info = bmProjectInfoMapper.selectByName(bmProjectInfo.getProName());
            if (StringUtils.isNotNull(info) && info.getProId() != bmProjectInfo.getProId()) {
                return AjaxResult.error("修改工程项目名称重复，请重新提交！！！");
            }
        }
        return AjaxResult.success(bmProjectInfoMapper.updateBmProjectInfo(bmProjectInfo));
    }

    @Override
    public int deleteProjectInfoById(Long proId) {
        return bmProjectInfoMapper.deleteProjectInfoById(proId);
    }

    @Override
    public String importBmProjectInfo(List<BmProjectInfo> bmProjectInfoList, boolean updateSupport) {
        if (StringUtils.isNull(bmProjectInfoList) || bmProjectInfoList.size() == 0)
        {
            throw new ServiceException("导入项目管理数据不能为空！");
        }
        //保存项目管理
        for (BmProjectInfo bmProjectInfo: bmProjectInfoList) {
            bmProjectInfoMapper.projectInfoAdd(bmProjectInfo);
        }
        return "导入成功";
    }
}
