package com.mvc.service.forwarding;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.TForwarderRecordsMapper;
import com.mvc.entity.TForwarderRecords;
import com.mvc.model.forwarding.TForwarderRecordsModel;
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 TForwarderRecordsService {
    @Resource
    private TForwarderRecordsMapper tForwarderRecordsMapper;

    public Map<String, Object> findAllTForwarderRecords(Integer page, Integer rows) {
        Map<String, Object> resMap = new HashMap<>();
        Page<Object> pageObject = PageHelper.startPage(page, rows);
        List<TForwarderRecords> TForwarderRecordss = tForwarderRecordsMapper.selectByExample(null);
        List<TForwarderRecordsModel> resList = new ArrayList<>();
        for (TForwarderRecords TForwarderRecords : TForwarderRecordss) {
            TForwarderRecordsModel model = new TForwarderRecordsModel();
            BeanUtils.copyProperties(TForwarderRecords, model);
            resList.add(model);
        }
        resMap.put("total", pageObject.getTotal());
        resMap.put("list", resList);
        return resMap;
    }

    public TForwarderRecordsModel saveTForwarderRecords(TForwarderRecordsModel model) {
        try {
            TForwarderRecords forwarderRecords = new TForwarderRecords();
            BeanUtils.copyProperties(model, forwarderRecords);
            tForwarderRecordsMapper.insert(forwarderRecords);
            model.setId(forwarderRecords.getId());
        } catch (Exception ex) {
            throw ex;
        }
        return model;
    }

    public TForwarderRecordsModel updateTForwarderRecords(TForwarderRecordsModel model) {
        TForwarderRecords TForwarderRecords = tForwarderRecordsMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, TForwarderRecords);
        int i = tForwarderRecordsMapper.updateByPrimaryKey(TForwarderRecords);
        return model;
    }

    public int deleteTForwarderRecords(int id) {
        int i = tForwarderRecordsMapper.deleteByPrimaryKey(id);
        return i;
    }
}
