package com.mvc.service.forwarding;

import com.mvc.dao.DicForwarderMapper;
import com.mvc.entity.DicForwarder;
import com.mvc.model.forwarding.DicForwarderModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;

@Service
@Transactional
public class DicForwarderService {
    @Resource
    private DicForwarderMapper dicForwarderMapper;

    public List<DicForwarderModel> findAllDicForwarder() {
        List<DicForwarder> DicForwarders = dicForwarderMapper.selectByExample(null);
        List<DicForwarderModel> resList = new ArrayList<>();
        for (DicForwarder DicForwarder : DicForwarders) {
            DicForwarderModel model = new DicForwarderModel();
            BeanUtils.copyProperties(DicForwarder, model);
            resList.add(model);
        }

        return resList;
    }

    public DicForwarderModel saveDicForwarder(DicForwarderModel model) {
        DicForwarder DicForwarder = new DicForwarder();
        BeanUtils.copyProperties(model, DicForwarder);
        int insert = dicForwarderMapper.insert(DicForwarder);
        model.setId(DicForwarder.getId());
        return model;
    }

    public DicForwarderModel updateDicForwarder(DicForwarderModel model) {
        DicForwarder DicForwarder = dicForwarderMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, DicForwarder);
        int i = dicForwarderMapper.updateByPrimaryKey(DicForwarder);
        return model;
    }

    public int deleteDicForwarder(int id) {
        int i = dicForwarderMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 根据ID获取DicForwarder
     * @param id
     * @return
     */
    public DicForwarderModel findDicForwarderById(int id) {
        DicForwarder dicForwarder = dicForwarderMapper.selectByPrimaryKey(id);
        if (dicForwarder != null) {
            DicForwarderModel model=new DicForwarderModel();
            BeanUtils.copyProperties(dicForwarder,model);
            return model;
        }
        return null;
    }
}
