package com.mvc.service.sthjb;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.WaterMeterInventoryMapper;
import com.mvc.entity.WaterMeterInventory;
import com.mvc.entity.WaterMeterInventoryExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.sthjb.WaterMeterInventoryModel;
import com.mvc.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
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.Date;
import java.util.List;

/**
 * 水计量器具信息
 */
@Service
@Transactional
public class WaterMeterInventoryService {
    @Resource
    private WaterMeterInventoryMapper waterMeterInventoryMapper;

    /**
     * 根据条件分页查询列表
     *
     * @param queryModel
     * @return
     */
    public ResPageModel findAllWaterMeterInventory(WaterMeterInventoryModel queryModel) {
        WaterMeterInventoryExample example = new WaterMeterInventoryExample();
        WaterMeterInventoryExample.Criteria criteria = example.createCriteria();
        //器具编码
        if (StringUtils.isNotEmpty(queryModel.getMeterCode())) {
            criteria.andMeterCodeEqualTo(queryModel.getMeterCode());
        }
        //名称
        if (StringUtils.isNotEmpty(queryModel.getDisplayName())) {
            criteria.andDisplayNameLike(StringUtil.getLikeFormat(queryModel.getDisplayName()));
        }
        //等级
        if (queryModel.getLevel() != null) {
            criteria.andLevelEqualTo(queryModel.getLevel());
        }
        //型号
        if (StringUtils.isNotEmpty(queryModel.getModel())) {
            criteria.andModelLike(StringUtil.getLikeFormat(queryModel.getModel()));
        }
        //创建人
        if (StringUtils.isNotEmpty(queryModel.getCreateMan())) {
            criteria.andCreateManLike(StringUtil.getLikeFormat(queryModel.getCreateMan()));
        }
        //单位代码
        if (StringUtils.isNotEmpty(queryModel.getUnitCode())) {
            criteria.andUnitCodeEqualTo(queryModel.getUnitCode());
        }
        example.setOrderByClause(" CREATE_TIME desc ");

        Page<Object> pageObject = PageHelper.startPage(queryModel.getPageNum(), queryModel.getPageSize());
        List<WaterMeterInventoryModel> pipelineLeakageModels = selectByExample(example);

        ResPageModel resPageModel = new ResPageModel(pipelineLeakageModels, pageObject);
        return resPageModel;
    }

    /**
     * 根据Example查询列表
     *
     * @param example
     * @return
     */
    private List<WaterMeterInventoryModel> selectByExample(WaterMeterInventoryExample example) {
        List<WaterMeterInventory> waterMeterInventorys = waterMeterInventoryMapper.selectByExample(example);
        List<WaterMeterInventoryModel> resList = new ArrayList<>();
        for (WaterMeterInventory WaterMeterInventory : waterMeterInventorys) {
            WaterMeterInventoryModel model = new WaterMeterInventoryModel();
            BeanUtils.copyProperties(WaterMeterInventory, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 新增记录
     *
     * @param model
     * @return
     */
    public WaterMeterInventoryModel saveWaterMeterInventory(WaterMeterInventoryModel model) {
        model.setCreateTime(new Date());
        WaterMeterInventory waterMeterInventory = new WaterMeterInventory();
        BeanUtils.copyProperties(model, waterMeterInventory);
        int insert = waterMeterInventoryMapper.insert(waterMeterInventory);
        model.setId(waterMeterInventory.getId());
        return model;
    }

    /**
     * 更新记录
     *
     * @param model
     * @return
     */
    public WaterMeterInventoryModel updateWaterMeterInventory(WaterMeterInventoryModel model) {
        WaterMeterInventory waterMeterInventory = waterMeterInventoryMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, waterMeterInventory);
        int i = waterMeterInventoryMapper.updateByPrimaryKey(waterMeterInventory);
        return model;
    }

    /**
     * 根据ID删除记录
     *
     * @param id
     * @return
     */
    public int deleteWaterMeterInventory(int id) {
        int i = waterMeterInventoryMapper.deleteByPrimaryKey(id);
        return i;
    }

    /**
     * 检查数据合法性
     *
     * @param model
     * @return
     */
    public String checkDataLegitimacy(WaterMeterInventoryModel model) {

        return null;
    }
}