package com.mvc.service.sthjb;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mvc.dao.UnitWaterDataMapper;
import com.mvc.entity.UnitWaterData;
import com.mvc.entity.UnitWaterDataExample;
import com.mvc.model.ResPageModel;
import com.mvc.model.sthjb.UnitWaterDataModel;
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 UnitWaterDataService {
    @Resource
    private UnitWaterDataMapper unitWaterDataMapper;

    /**
     * 根据条件分页查询列表
     * @param queryModel
     * @return
     */
    public ResPageModel findAllUnitWaterData(UnitWaterDataModel queryModel) {
        UnitWaterDataExample example = new UnitWaterDataExample();
        UnitWaterDataExample.Criteria criteria = example.createCriteria();
        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<UnitWaterDataModel> pipelineLeakageModels = selectByExample(example);

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

    /**
     * 根据Example查询列表
     * @param example
     * @return
     */
    private List<UnitWaterDataModel> selectByExample(UnitWaterDataExample example) {
        List<UnitWaterData> unitWaterDatas = unitWaterDataMapper.selectByExample(example);
        List<UnitWaterDataModel> resList = new ArrayList<>();
        for (UnitWaterData UnitWaterData : unitWaterDatas) {
            UnitWaterDataModel model = new UnitWaterDataModel();
            BeanUtils.copyProperties(UnitWaterData, model);
            resList.add(model);
        }
        return resList;
    }

    /**
     * 新增记录
     * @param model
     * @return
     */
    public UnitWaterDataModel saveUnitWaterData(UnitWaterDataModel model) {
        model.setCreateTime(new Date());
        UnitWaterData unitWaterData = new UnitWaterData();
        BeanUtils.copyProperties(model, unitWaterData);
        int insert = unitWaterDataMapper.insert(unitWaterData);
        model.setId(unitWaterData.getId());
        return model;
    }

    /**
     * 更新记录
     * @param model
     * @return
     */
    public UnitWaterDataModel updateUnitWaterData(UnitWaterDataModel model) {
        UnitWaterData unitWaterData = unitWaterDataMapper.selectByPrimaryKey(model.getId());
        BeanUtils.copyProperties(model, unitWaterData);
        int i = unitWaterDataMapper.updateByPrimaryKey(unitWaterData);
        return model;
    }

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


    /**
     * 检查数据合法性
     * @param model
     * @return
     */
    public String checkDataLegitimacy(UnitWaterDataModel model) {
        UnitWaterDataExample example = new UnitWaterDataExample();
        example.createCriteria()
                .andUnitCodeEqualTo(model.getUnitCode())
                .andYearEqualTo(model.getYear());
        if(selectByExample(example).size()>0){
            return "该单位该年数据已存在";
        }

        return null;
    }

    /**
     * 根据单位编码和年查询当前数据
     * @param unitCode
     * @param year
     * @return
     */
    public UnitWaterDataModel findCurrentUnitWaterData(String unitCode, Integer year) {
    	UnitWaterDataExample example = new UnitWaterDataExample();
    	UnitWaterDataExample.Criteria criteria = example.createCriteria();
    	criteria.andUnitCodeEqualTo(unitCode);
        criteria.andYearEqualTo(year);
    	List<UnitWaterDataModel> unitWaterDataModels = selectByExample(example);
    	return unitWaterDataModels.size() > 0 ? unitWaterDataModels.get(0) : null;
    }
}