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

import com.steerinfo.baseinfo.meterbaseloaduserloadinfo.mapper.MeterBaseLoadUserloadinfoMapper;
import com.steerinfo.baseinfo.meterbaseloaduserloadinfo.model.MeterBaseLoadUserloadinfo;
import com.steerinfo.baseinfo.meterbaseloaduserloadinfo.service.IMeterBaseLoadUserloadinfoService;
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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseLoadUserloadinfoMapper meterBaseLoadUserloadinfoMapper;

    @Override
    protected IBaseMapper<MeterBaseLoadUserloadinfo, String> getMapper() {
        return meterBaseLoadUserloadinfoMapper;
    }

    @Override
    public int relation(List<MeterBaseLoadUserloadinfo> models) {
        if (models == null || models.size() <= 0) {
            throw new MarkerMetException(500, "参数为空!!");
        }
        int num = 0;
        for (MeterBaseLoadUserloadinfo model : models) {
            check(model); //数据完整性校验
            checkRepeat(model); //数据重复性校验
            String id = new SimpleDateFormat("yyMMddHHmmssS").format(new Date()) + ((int) (Math.random() * 90) + 10);
            model.setId(id);
            model.setUserloadinfoNo(id);
            meterBaseLoadUserloadinfoMapper.insertSelective(model);
            num++;
        }
        return num;
    }

    /**
     * 数据校验；若创建人不为空则自动赋值创建时间；若更新人不为空则自动赋值更新时间；
     * @param model
     * @return
     */
    private MeterBaseLoadUserloadinfo check(MeterBaseLoadUserloadinfo model) {
        String msg = "";
        int count = 0;
        if (StringUtils.isEmpty(model.getLoadPointNo())) {
            msg += "卸货点编码不允许为空；";
            count++;
        }
        if (StringUtils.isEmpty(model.getLoadPointName())) {
            msg += "卸货点名称不允许为空；";
            count++;
        }
        if (StringUtils.isEmpty(model.getUserCode())) {
            msg += "用户名编码不允许为空；";
            count++;
        }
        if (StringUtils.isEmpty(model.getUserName())) {
            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;
    }

    /**
     * 数据重复性校验，判断是否重复新增了数据
     * @param model
     * @return
     */
    private MeterBaseLoadUserloadinfo checkRepeat(MeterBaseLoadUserloadinfo model) {
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("loadPointNo", model.getLoadPointNo());
        obj.put("userCode", model.getUserCode());
        List<MeterBaseLoadUserloadinfo> listRepeat = meterBaseLoadUserloadinfoMapper.selectByParameters(obj);
        if (listRepeat!=null && listRepeat.size()>0) {
            throw new MarkerMetException(500, "用户【"+model.getUserName()+"】与卸货点【"+ model.getLoadPointName()+"】已经关联！");
        }
        return model;
    }
}
