package com.mvc.service.ysfx;

import com.mvc.dao.DicWaterTypeMapper;
import com.mvc.entity.DicWaterType;
import com.mvc.entity.DicWaterTypeExample;
import com.mvc.model.ysfx.DicWaterTypeModel;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class DicWaterTypeService {

    @Autowired(required = false)
    private DicWaterTypeMapper dicWaterTypeMapper;

    public  List<DicWaterTypeModel> findAllDicWaterType() {
        List<DicWaterType> DicWaterTypes = dicWaterTypeMapper.selectByExample(null);
        List<DicWaterTypeModel> resList=new ArrayList<>();
        for(DicWaterType DicWaterType:DicWaterTypes){
            DicWaterTypeModel model = new DicWaterTypeModel();
            BeanUtils.copyProperties(DicWaterType,model);
            resList.add(model);
        }
        return resList;
    }

    public DicWaterTypeModel saveDicWaterType(DicWaterTypeModel model) {
        DicWaterType DicWaterType = new DicWaterType();
        BeanUtils.copyProperties(model,DicWaterType);
        int insert = dicWaterTypeMapper.insert(DicWaterType);
        model.setId(DicWaterType.getId());
        return model;
    }

    public DicWaterTypeModel updateDicWaterType(DicWaterTypeModel model) {
        DicWaterType DicWaterType = dicWaterTypeMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,DicWaterType);
        int i = dicWaterTypeMapper.updateByPrimaryKey(DicWaterType);
        return model;
    }

    public int deleteDicWaterType(int id) {
        int i = dicWaterTypeMapper.deleteByPrimaryKey(id);
        return i;
    }

    public List<DicWaterTypeModel> findDicWaterTypeModelsByUnitCode(String unitCode) {
        List<DicWaterTypeModel> resList=new ArrayList<>();
        DicWaterTypeExample example=new DicWaterTypeExample();
        DicWaterTypeExample.Criteria criteria = example.createCriteria();
        criteria.andUnitCodeEqualTo(unitCode);
        List<DicWaterType> dicWaterTypes = dicWaterTypeMapper.selectByExample(example);
        if(dicWaterTypes.size()>0){
            for(DicWaterType type:dicWaterTypes){
                DicWaterTypeModel model=new DicWaterTypeModel();
                BeanUtils.copyProperties(type,model);
                resList.add(model);
            }
        }
        return resList;
    }
}
