package com.steerinfo.meterwork.meterworkgrabactual.service.impl;

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkcaractualfirst.model.MeterWorkCarActualFirst;
import com.steerinfo.meterwork.meterworkgrabactual.model.MeterWorkGrabActual;
import com.steerinfo.meterwork.meterworkgrabactual.mapper.MeterWorkGrabActualMapper;
import com.steerinfo.meterwork.meterworkgrabactual.service.IMeterWorkGrabActualService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;

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

    @Autowired
    private MeterWorkGrabActualMapper meterWorkGrabActualMapper;

    @Override
    protected IBaseMapper<MeterWorkGrabActual, String> getMapper() {
        return meterWorkGrabActualMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkandadd(MeterWorkGrabActual model) throws MarkerMetException {
        try {
            ckeck(model);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            String no = meterWorkGrabActualMapper.getNewID(afl);
            model.setActualNo(sdf.format(new Date()) + no);
            model.setValidFlag("1");
            model.setCreateTime(new Date());
            int num = meterWorkGrabActualMapper.insertSelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int checkandupdate(MeterWorkGrabActual model) throws MarkerMetException {
        try {
            if (model.getValidFlag().equals(DbConstants.INVALID)) {
                throw new MarkerMetException(500, "已经作废的数据无法修改！！");
            }
            ckeck(model);
            int num = meterWorkGrabActualMapper.updateByPrimaryKeySelective(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void ckeck(MeterWorkGrabActual model) throws MarkerMetException {
        if (model.getDataHch() == null || model.getDataHch().equals("")) {
            throw new MarkerMetException(500, "行车号不能为空");
        }
        if (model.getDataType() == null || model.getDataType().equals("")) {
            throw new MarkerMetException(500, "数据类型不能为空");
        }
        if (model.getDataArea() == null || model.getDataArea().equals("")) {
            throw new MarkerMetException(500, "计量区域不能为空");
        }
        if (model.getMeterOperator() == null || model.getMeterOperator().equals("")) {
            throw new MarkerMetException(500, "计量操作人员不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (StringUtils.isEmpty(model.getActualNo())) {
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
            model.setValidFlag(DbConstants.VALID);
            model.setCreateTime(new Date());
        } else {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
    }
}
