package com.bonus.material.supplier.service.impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.material.supplier.domain.MaSupplier;
import com.bonus.material.supplier.mapper.MaSupplierMapper;
import com.bonus.material.supplier.service.MaSupplierService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MaSupplierServiceImpl implements MaSupplierService {

    @Resource
    private MaSupplierMapper maSupplierMapper;

    @Override
    public MaSupplier getById(Long supplierId) {
        return maSupplierMapper.selectById(supplierId);
    }

    @Override
    public List<MaSupplier> list(MaSupplier query) {
        return maSupplierMapper.list(query);
    }

    @Override
    public AjaxResult add(MaSupplier supplier) {
        try {
            int num = maSupplierMapper.getNum(supplier);
            if (num > 0) {
                return AjaxResult.error("统一社会信用代码已存在");
            }
            int insert = maSupplierMapper.insert(supplier);
            return insert > 0 ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
        } catch (Exception e) {
            return AjaxResult.error("新增失败");
        }
    }

    @Override
    public AjaxResult edit(MaSupplier supplier) {
        try {
            int num = maSupplierMapper.getNum(supplier);
            if (num > 0) {
                return AjaxResult.error("统一社会信用代码已存在");
            }
            int update = maSupplierMapper.update(supplier);
            return update > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改失败");
        } catch (Exception e) {
            return AjaxResult.error("修改失败");
        }
    }

    @Override
    public int remove(Long supplierId) {
        return maSupplierMapper.deleteById(supplierId);
    }

    @Override
    public List<MaSupplier> listEnabled() {
        return maSupplierMapper.listEnabled();
    }
}


