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

import com.jisco.dynamicweight.dynamicbasecamera.factories.ConverterFactories;
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.baseinfo.meterbaserailwayaiweight.service.IMeterBaseRailwayAiweightService;
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.util.PublicMethod;
import com.steerinfo.util.StringUtils;
import io.micrometer.core.instrument.Meter;
import org.apache.shiro.crypto.hash.Hash;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Resource
    private MeterBaseRailwayAiweightMapper meterBaseRailwayAiweightMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    private static Logger logger	= LoggerFactory.getLogger( ConverterFactories.class );


    @Override
    protected IBaseMapper<MeterBaseRailwayAiweight, String> getMapper() {
        return meterBaseRailwayAiweightMapper;
    }

    @Override
    public RESTfulResult checkAdd(List<MeterBaseRailwayAiweight> models) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(new Date());
            for(MeterBaseRailwayAiweight model: models) {
                HashMap<String, Object> map = new HashMap<>();
                map.put("railwayNo", model.getRailwayNo());
                List<MeterBaseRailwayAiweight> AiModels =  meterBaseRailwayAiweightMapper.selectByParameters(map);
                if(AiModels.size() >= 1) {
                    model.setActualFirstNo(AiModels.get(0).getActualFirstNo());
                    model.setValueFlag("0");
                    model.setRailwayTypeNo("001019002");
                    model.setRailwayTypeName("动态轨道衡");
                    model.setUpdateManNo("admin");
                    model.setUpdateManName("admins");
                    model.setUpdateTime(new Date());
                    meterBaseRailwayAiweightMapper.updateByPrimaryKeySelective(model);
                }else {
                    String no = meterBaseRailwayAiweightMapper.getNewID(afl);
                    model.setActualFirstNo(sdf.format(new Date()) + no);
                    model.setValueFlag("0");
                    model.setRailwayTypeNo("001019002");
                    model.setRailwayTypeName("动态轨道衡");
                    model.setCreateManNo("admin");
                    model.setCreateManName("admins");
                    if(model.getCreateTime() == null) {
                        model.setCreateTime(new Date());
                    }
                    meterBaseRailwayAiweightMapper.insertSelective(model);
                }
            }

            rm.setSucceed();
            rm.setCode("200");
            rm.setData(models);
            rm.setMessage("操作成功！！");
        }catch(Exception e) {
            e.printStackTrace();
            rm.setFailed();
            rm.setMessage("操作失败，请确认！！>>>" + e.getMessage());
        }
        return rm;
    }

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

    @Override
    public PageList<MeterBaseRailwayAiweight> likeByDesc(HashMap<String, Object> params, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseRailwayAiweight> rows = meterBaseRailwayAiweightMapper.likeByDesc(params);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }
}
