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

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.meterwork.meterworkrailwayactfirst.mapper.MeterWorkRailwayActFirstMapper;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.meterwork.meterworkrailwayrecmain.model.MeterWorkRailwayRecMain;
import com.steerinfo.meterwork.meterworkrailwayrecmain.mapper.MeterWorkRailwayRecMainMapper;
import com.steerinfo.meterwork.meterworkrailwayrecmain.service.IMeterWorkRailwayRecMainService;
import com.steerinfo.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Resource
    private MeterWorkRailwayRecMainMapper meterWorkRailwayRecMainMapper;

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayActFirstMapper meterWorkRailwayActFirstMapper;

    @Override
    protected IBaseMapper<MeterWorkRailwayRecMain, String> getMapper() {
        return meterWorkRailwayRecMainMapper;
    }

    @Override
    public RESTfulResult checkandadd(MeterWorkRailwayRecMain model) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            rm.setData(model.getActualNo());
            meterWorkRailwayRecMainMapper.insertSelective(model);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RESTfulResult addList(MeterWorkRailwayRecMain mainModel, List<MeterWorkRailwayReceived> receivedList) {
        RESTfulResult rm = new RESTfulResult();
        try{
            rm.setCode("200");
            meterWorkRailwayRecMainMapper.deleteByPrimaryKey(mainModel.getActualNo());
//            rm.setData(mainModel.getActualNo());
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(mainModel.getCreateTime());
            String lcNo = meterWorkRailwayRecMainMapper.getLcNo(afl, mainModel.getBaseSpotNo());
            mainModel.setSoundName(mainModel.getBaseSpotName().replaceAll("#", "")
                    .replaceAll("轨道衡", "")
                    .replaceAll("动轨", "")
                    .replaceAll("动态", "") + "-" + afl + lcNo + "(" + receivedList.size() + ")");
            meterWorkRailwayRecMainMapper.insertSelective(mainModel);
            for (MeterWorkRailwayReceived model: receivedList) {
                meterWorkRailwayReceivedMapper.deleteByPrimaryKeyLike(model.getActualFirstNo());
            }
            for (MeterWorkRailwayReceived model: receivedList) {
//                if(model.getRailwayNo() == null || StringUtils.isEmpty(model.getRailwayNo())) {
//                    model.setRailwayNo("车号异常");
//                }
                SimpleDateFormat sdfD = new SimpleDateFormat("yyyyMMddHHmmss");
                String key = model.getBaseSpotNo() + sdfD.format(model.getCreateTime()) + PublicMethod.RandomCreate(3);
                model.setActualFirstNo(key);
                model.setRailwayWorkNo(mainModel.getSoundName());
                meterWorkRailwayReceivedMapper.insertSelective(model);
            }
            rm.setMessage("成功");
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            rm.setFailed();
            rm.setCode("500");
            rm.setMessage("失败");
        }
        return rm;
    }

    @Override
    public List<MeterWorkRailwayRecMain> listBynoPage(HashMap<String, Object> parmas) {
        return meterWorkRailwayRecMainMapper.listBynoPage(parmas);
    }
}
