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

import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.mapper.MeterBaseRailwayAiweightMapper;
import com.steerinfo.baseinfo.meterbaserailwayaiweight.model.MeterBaseRailwayAiweight;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.metermonitor.metermonitornote.mapper.MeterMonitorNoteMapper;
import com.steerinfo.metermonitor.metermonitornote.model.MeterMonitorNote;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import com.steerinfo.meterwork.meterworkrailwayreceived.model.MeterWorkRailwayReceived;
import com.steerinfo.meterwork.meterworkrailwayreceived.mapper.MeterWorkRailwayReceivedMapper;
import com.steerinfo.meterwork.meterworkrailwayreceived.service.IMeterWorkRailwayReceivedService;
import com.steerinfo.meterwork.meterworkrailwayrecmain.mapper.MeterWorkRailwayRecMainMapper;
import com.steerinfo.meterwork.meterworkrailwayrecmain.model.MeterWorkRailwayRecMain;
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.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;

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

    @Resource
    private MeterWorkRailwayReceivedMapper meterWorkRailwayReceivedMapper;

    @Resource
    private MeterWorkRailwayRecMainMapper meterWorkRailwayRecMainMapper;

    @Resource
    private MeterBaseRailwayAiweightMapper meterBaseRailwayAiweightMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Override
    protected IBaseMapper<MeterWorkRailwayReceived, String> getMapper() {
        return meterWorkRailwayReceivedMapper;
    }

    @Override
    public RESTfulResult checkandadd(MeterWorkRailwayReceived model) throws Exception {
        try{
            RESTfulResult rm = new RESTfulResult();
            rm.setCode("200");
            rm.setData(model.getActualFirstNo());
            if(model.getRailwayNo() == null && StringUtils.isEmpty(model.getRailwayNo())) {
                model.setRailwayNo("车号异常");
            }
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//            String afl = sdf.format(new Date());
//            String no = meterWorkRailwayReceivedMapper.getNewID(afl);
//            model.setActualFirstNo(sdf.format(new Date()) + no);
            meterWorkRailwayReceivedMapper.insertSelective(model);
            rm.setMessage("成功");
            return rm;
        }
        catch (Exception e){
            throw e;
        }
    }

    @Override
    public List<MeterWorkRailwayReceived> noPage(HashMap<String, Object> parmas) {
        List<MeterWorkRailwayReceived> rows = meterWorkRailwayReceivedMapper.noPage(parmas);
        return rows;
    }

    @Override
    public RESTfulResult updateList(List<MeterWorkRailwayReceived> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            int count = 0;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            for (MeterWorkRailwayReceived model: models) {
                if(model.getMemo() != null && StringUtils.isNotEmpty(model.getMemo())) {
                    MeterBaseOperationLog note = new MeterBaseOperationLog();
                    String no = PublicMethod.IdCreate(3);
                    note.setId(no);
                    note.setCreateManNo(model.getCreateManNo());
                    note.setCreateManName(model.getCreateManName());
                    note.setCreateTime(new Date());
                    note.setOperationPage("终端-轨道衡从表数据维护");
                    note.setOperationFunction("修改");
                    note.setOperationCarnNo(model.getRailwayNo());
                    meterBaseOperationLogMapper.insertSelective(note);
                    model.setMemo("");
                }

                if(model.getAiWeight() != null && StringUtils.isNotEmpty(model.getAiWeight())) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put("railwayNo", model.getRailwayNo());
                    map.put("valueFlag", "0");
                    List<MeterBaseRailwayAiweight> AiModels = meterBaseRailwayAiweightMapper.selectByParameters(map);
                    MeterBaseRailwayAiweight AiModel = new MeterBaseRailwayAiweight();
                    if(AiModels.size() >= 1) {
                        AiModel = AiModels.get(0);
                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
                        AiModel.setRailwayType(model.getRailwayModelName());
                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
                        AiModel.setUpdateManNo("systemAdd");
                        AiModel.setUpdateManName("systemAdd");
                        AiModel.setUpdateTime(new Date());
                        meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(AiModel);
                    }else {
                        AiModel.setActualFirstNo(model.getActualFirstNo());
                        AiModel.setRailwayNo(model.getRailwayNo());
                        AiModel.setMeterWeight(new BigDecimal(model.getAiWeight()));
                        AiModel.setRailwayType(model.getRailwayModelName());
                        AiModel.setRailwayTypeNo("001019002");
                        AiModel.setRailwayTypeNo("动态轨道衡");
                        AiModel.setValueFlag("0");
                        AiModel.setCreateManNo("systemAdd");
                        AiModel.setCreateManName("systemAdd");
                        AiModel.setCreateTime(new Date());
                        AiModel.setRailwaySpeed(model.getRailwaySpeed());
                        meterBaseRailwayAiweightMapper.insertSelective(AiModel);
                    }

                }
//                meterWorkRailwayReceivedMapper.deleteByPrimaryKey(model.getActualFirstNo());
//                rm.setData(model.getActualFirstNo());
//                if(model.getRailwayNo() == null && StringUtils.isEmpty(model.getRailwayNo())) {
//                    model.setRailwayNo("车号异常");
//                }
////            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
////            String afl = sdf.format(new Date());
////            String no = meterWorkRailwayReceivedMapper.getNewID(afl);
////            model.setActualFirstNo(sdf.format(new Date()) + no);
//                meterWorkRailwayReceivedMapper.insertSelective(model);
                meterWorkRailwayReceivedMapper.updateByPrimaryKeySelective(model);
                count ++;
            }
            //修改主表是否已处理完
            if(models.size() >= 1) {
                MeterWorkRailwayReceived receivedModel = meterWorkRailwayReceivedMapper.selectByPrimaryKey(models.get(0).getActualFirstNo());
                if(receivedModel != null){
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("railwayWorkNo", receivedModel.getRailwayWorkNo());
                    HashMap<String, Object> countMap = meterWorkRailwayReceivedMapper.getCount(obj);
                    int countNum = Integer.parseInt(countMap.get("COUNTNUM").toString());
                    if(countNum < 1) {
                        MeterWorkRailwayRecMain mainModel = meterWorkRailwayRecMainMapper.selectByPrimaryKey(receivedModel.getRailwayWorkNo());
                        if(mainModel != null) {
                            mainModel.setIsDispose("1");
                            meterWorkRailwayRecMainMapper.updateByPrimaryKeySelective(mainModel);
                        }
                    }
                }
            }
            
            rm.setSucceed();
            rm.setMessage("操作成功，共有" + count + "条数据更新成功");
            rm.setCode("200");
            rm.setData(count);
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常");
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return rm;
    }
}
