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

import com.steerinfo.baseinfo.meterbasemattertimes.mapper.MeterBaseMatterTimesMapper;
import com.steerinfo.baseinfo.meterbasemattertimes.model.MeterBaseMatterTimes;
import com.steerinfo.baseinfo.meterbasemattertimes.service.IMeterBaseMatterTimesService;
import com.steerinfo.baseinfo.meterbasetimesinfo.mapper.MeterBaseTimesInfoMapper;
import com.steerinfo.baseinfo.meterbasetimesinfo.model.MeterBaseTimesInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterBaseMatterTimesMapper meterBaseMatterTimesMapper;

    @Resource
    private MeterBaseTimesInfoMapper meterBaseTimesInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseMatterTimes, String> getMapper() {
        return meterBaseMatterTimesMapper;
    }

    @Override
    @Transactional
    public String checkandadd(List<MeterBaseMatterTimes> models)
    {
        for (MeterBaseMatterTimes model:models) {
            MeterBaseTimesInfo mbti=meterBaseTimesInfoMapper.selectByPrimaryKey(model.getTimesNo());
            if(! mbti.getValidFlag().equals("1")) {
                return "该时间段已无效，请先启用！！";
            }
            HashMap<String, Object> obj2 = new HashMap<>();
            obj2.put("matterNo", model.getMatterNo());
            List<MeterBaseMatterTimes> mbtis = meterBaseMatterTimesMapper.selectByParameters(obj2);
            if(mbtis.size() > 0){
                return "该物料已经配置，请勿重复配置！！";
            }
            String no = meterBaseMatterTimesMapper.GetNewID();
            model.setMatterTimesNo(no);
            model.setCreateTime(new Date());
            meterBaseMatterTimesMapper.insertSelective(model);
        }
        return "";
    }
}
