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

import com.steerinfo.baseinfo.combaseinfo.mapper.ComBaseInfoMapper;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasehistarecountd.model.MeterBaseHisTareCountD;
import com.steerinfo.baseinfo.meterbasehistarecountd.mapper.MeterBaseHisTareCountDMapper;
import com.steerinfo.baseinfo.meterbasehistarecountd.service.IMeterBaseHisTareCountDService;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractual.model.MeterWorkCarActual;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    @Autowired
    private MeterBaseHisTareCountDMapper meterBaseHisTareCountDMapper;

    @Autowired
    private ComBaseInfoMapper comBaseInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseHisTareCountD, String> getMapper() {
        return meterBaseHisTareCountDMapper;
    }

    /**
     * 新增数据
     *
     * @param model
     * @return
     */
    @Override
    public MeterBaseHisTareCountD insert(MeterBaseHisTareCountD model) {
        UserPayload payload = UserPayload.getCurrUser();
        String userCode = payload.getUserCode();
        String userName = payload.getUserName();
        String maxID = comBaseInfoMapper.getConfigID("METER_BASE_HIS_TARE_COUNT_D", "COUNT_NO");

        model.setCountNo(maxID);
        model.setCreateTime(new Date());
        model.setCreateManNo(userCode);
        model.setCreateManName(userName);

        meterBaseHisTareCountDMapper.insert(model);
        return model;
    }

    /**
     * 批量修改接口
     *
     * @param models
     * @return
     */
    @Override
    public int update(List<MeterBaseHisTareCountD> models) {
        int num = 0;
        try {
            UserPayload payload = UserPayload.getCurrUser();
            String userCode = payload.getUserCode();
            String userName = payload.getUserName();
            for (MeterBaseHisTareCountD model : models) {
                model.setUpdateTime(new Date());
                model.setUpdateManNo(userCode);
                model.setUpdateManName(userName);
            }
            meterBaseHisTareCountDMapper.batchUpdate(models);
        } catch (MarkerMetException e) {
            throw e;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return num;
    }

}
