package com.mvc.service.taizhang;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.WmAccMapper;
import com.mvc.entity.WmAcc;
import com.mvc.model.WmAccModel;
import com.mvc.utils.StringUtil;
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.*;

//水表台账服务类
@Service
@Transactional
public class WmAccService {

    @Autowired
    private WmAccMapper wmAccMapper;

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

    public WmAccModel saveWmAcc(WmAccModel model) {
        model.setWmId(StringUtil.getUuid());
        model.setCreateDate(new Date());
        WmAcc WmAcc = new WmAcc();
        BeanUtils.copyProperties(model,WmAcc);
        int insert = wmAccMapper.insert(WmAcc);
        model.setId(WmAcc.getId());
        return model;
    }

    public WmAccModel updateWmAcc(WmAccModel model) {
        WmAcc WmAcc = wmAccMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model,WmAcc);
        int i = wmAccMapper.updateByPrimaryKey(WmAcc);
        return model;
    }

    public int deleteWmAcc(int id) {
        int i = wmAccMapper.deleteByPrimaryKey(id);
        return i;
    }

}
