package com.mvc.service.dic;

import com.mvc.dao.DicProductTypeMapper;
import com.mvc.entity.DicProductType;
import com.mvc.model.dic.DicProductTypeModel;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DicProductTypeService {

    @Resource
    private DicProductTypeMapper dicProductTypeMapper;

    public List<DicProductTypeModel> findAllDicProductType() {
        List<DicProductType> DicProductTypes = dicProductTypeMapper.selectByExample(null);
        List<DicProductTypeModel> resList = new ArrayList<>();
        for (DicProductType DicProductType : DicProductTypes) {
            DicProductTypeModel model = new DicProductTypeModel();
            BeanUtils.copyProperties(DicProductType, model);
            resList.add(model);
        }
        return resList;
    }

    public DicProductTypeModel saveDicProductType(DicProductTypeModel model) {
        DicProductType DicProductType = new DicProductType();
        BeanUtils.copyProperties(model, DicProductType);
        int insert = dicProductTypeMapper.insert(DicProductType);
        model.setId(DicProductType.getId());
        return model;
    }

    public DicProductTypeModel updateDicProductType(DicProductTypeModel model) {
        DicProductType DicProductType = dicProductTypeMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, DicProductType);
        int i = dicProductTypeMapper.updateByPrimaryKey(DicProductType);
        return model;
    }

    public int deleteDicProductType(int id) {
        int i = dicProductTypeMapper.deleteByPrimaryKey(id);
        return i;
    }
}
