package com.steerinfo.baseinfo.meterbasematterinfomatch.service.impl;

import com.steerinfo.baseinfo.meterbasematterinfomatch.mapper.MeterBaseMatterInfoMatchMapper;
import com.steerinfo.baseinfo.meterbasematterinfomatch.model.MeterBaseMatterInfoMatch;
import com.steerinfo.baseinfo.meterbasematterinfomatch.service.IMeterBaseMatterInfoMatchService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * MeterBaseMatterInfoMatch服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-03-10 09:13
 * 类描述
 * 修订历史：
 * 日期：2021-03-10
 * 作者：generator
 * 参考：
 * 描述：MeterBaseMatterInfoMatch服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "meterBaseMatterInfoMatchService")
public class MeterBaseMatterInfoMatchServiceImpl extends BaseServiceImpl<MeterBaseMatterInfoMatch, String> implements IMeterBaseMatterInfoMatchService {

    @Autowired
    private MeterBaseMatterInfoMatchMapper meterBaseMatterInfoMatchMapper;

    @Override
    protected IBaseMapper<MeterBaseMatterInfoMatch, String> getMapper() {
        return meterBaseMatterInfoMatchMapper;
    }

    @Override
    public int relation(List<MeterBaseMatterInfoMatch> models) {
        if (models == null || models.size() <= 0) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        int num = 0;
        for (MeterBaseMatterInfoMatch model : models) {
            check(model); //数据完整性校验；不需要重复性校验，直接update
            meterBaseMatterInfoMatchMapper.updateByPrimaryKey(model);
            num++;
        }
        return num;
    }

    /**
     * 数据校验；若创建人不为空则自动赋值创建时间；若更新人不为空则自动赋值更新时间；
     * @param model
     * @return
     */
    private MeterBaseMatterInfoMatch check(MeterBaseMatterInfoMatch model) {
        String msg = "";
        int count = 0;
        if (StringUtils.isEmpty(model.getMatterName())) {
            msg += "新系统物资名称不允许为空；";
            count++;
        }
        if (StringUtils.isEmpty(model.getMatterNameOld())) {
            msg += "老系统物资名称不允许为空；";
            count++;
        }
        if (count > 0) {
            throw new MarkerMetException(500, msg);
        }
        if (StringUtils.isNotEmpty(model.getCreateManNo()) && model.getCreateTime() == null){
            model.setCreateTime(new Date());
        }
        if (StringUtils.isNotEmpty(model.getUpdateManNo())){
            model.setUpdateTime(new Date());
        }
        return model;
    }
}
