package com.steerinfo.appservice.appprerailwayscale.service.impl;

import com.steerinfo.appservice.appprerailwayscale.mapper.AppPreRailwayScaleMapper;
import com.steerinfo.appservice.appprerailwayscale.model.AppPreRailwayScale;
import com.steerinfo.appservice.appprerailwayscale.service.IAppPreRailwayScaleService;
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.meterwork.except.MarkerMetException;
import com.steerinfo.util.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * PreRailwayScale服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2020-10-21 09:55
 * 类描述
 * 修订历史：
 * 日期：2020-10-21
 * 作者：generator
 * 参考：
 * 描述：PreRailwayScale服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "appPreRailwayScaleService")
@Primary
public class AppPreRailwayScaleServiceImpl extends BaseServiceImpl<AppPreRailwayScale, String> implements IAppPreRailwayScaleService {

    @Resource
    private AppPreRailwayScaleMapper appPreRailwayScaleMapper;

    @Override
    protected IBaseMapper<AppPreRailwayScale, String> getMapper() {
        return appPreRailwayScaleMapper;
    }

    @Override
    public PageList<AppPreRailwayScale> queryForApp(HashMap parmas, Integer pageNum, Integer pageSize){
        PageHelper.startPage(pageNum, pageSize);
        List<AppPreRailwayScale> rows = appPreRailwayScaleMapper.selectForApp(parmas);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    @Transactional
    public String approval(AppPreRailwayScale model){
        if(model == null){
            throw  new MarkerMetException(500,"参数为空!!");
        }
        if (!model.getValueFlag().equals("0")) {
            throw  new MarkerMetException(500,"该状态不允许审核!!");
        }
        AppPreRailwayScale aprs = new AppPreRailwayScale();
        aprs.setPredictionNo(model.getPredictionNo());
        aprs.setValueFlag("1");//未使用
        aprs.setCreateTime(new Date());
        appPreRailwayScaleMapper.updateByPrimaryKeySelective(aprs);
        return aprs.getPredictionNo();
    }

    @Transactional
    @Override
    public String uncheck(AppPreRailwayScale model){
        if(model == null){
            throw  new MarkerMetException(500,"参数为空!!");
        }
        if (!model.getValueFlag().equals("1")) {
            throw  new MarkerMetException(500,"该状态不允许反审!!");
        }
        AppPreRailwayScale aprs = new AppPreRailwayScale();
        aprs.setPredictionNo(model.getPredictionNo());
        aprs.setValueFlag("0");//未确定
        aprs.setCreateTime(new Date());
        appPreRailwayScaleMapper.updateByPrimaryKeySelective(aprs);
        return aprs.getPredictionNo();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public String checkandadd(AppPreRailwayScale model)
    {
        if(model == null) {
            throw  new MarkerMetException(500,"参数为空!!");
        }
        /*
        String [] nos = model.getRailwayCarriageNo().split(",");
        int start = Integer.parseInt(nos[0]);
        int end = Integer.parseInt(nos[1]);
        //若开始大于结束，调整位置
        if (start > end){
           int a = end;
           end = start;
           start = a;
        }
        for (int i = start;i <= end; i++){
            model.setPredictionType("1");//单次预报
            model.setRailwayTypeNo("001019002");
            model.setRailwayTypeName("动态轨道衡");
            model.setValueFlag("0");//未配车，未确认
            model.setRailwayCarriageNo(Integer.toString(i));
            check(model);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String afl = sdf.format(model.getCreateTime());
            String no =appPreRailwayScaleMapper.getNewID(afl);
            model.setPredictionNo(afl+no);
            appPreRailwayScaleMapper.insertSelective(model);
        }
        */
        model.setPredictionType("1");//单次预报
        model.setRailwayTypeNo("001019002");
        model.setRailwayTypeName("动态轨道衡");
        model.setValueFlag("0");//未配车，未确认
        model.setRailwayCarriageNo(model.getRailwayCarriageNo());
        check(model);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String afl = sdf.format(model.getCreateTime());
        String no =appPreRailwayScaleMapper.getNewID(afl);
        model.setPredictionNo("T"+afl+no);
        appPreRailwayScaleMapper.insertSelective(model);
        return  model.getPredictionNo();
    }

    //检查数据是否符合要求
    private void check (AppPreRailwayScale model)throws MarkerMetException
    {
        if(model.getMeterTypeNo() == null || model.getMeterTypeNo().equals("")) {
            throw  new MarkerMetException(500,"业务类型编号为空!!");
        }
        if(model.getMeterTypeName() == null || model.getMeterTypeName().equals("")) {
            throw  new MarkerMetException(500,"业务类型名称为空!!");
        }
        if(model.getRailwayTypeNo() == null || model.getRailwayTypeNo().equals("")) {
            throw  new MarkerMetException(500,"轨道衡类型编号为空!!");
        }
        if(model.getRailwayTypeName() == null || model.getRailwayTypeName().equals("")) {
            throw  new MarkerMetException(500,"轨道衡类型名称为空!!");
        }
        if(StringUtils.isEmpty(model.getPredictionNo())){
            model.setValueFlag("0");//未配车，未确认
            model.setCreateTime(new Date());
        }
    }

}
