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

import com.steerinfo.baseinfo.meterbaseoperationlog.mapper.MeterBaseOperationLogMapper;
import com.steerinfo.baseinfo.meterbaseoperationlog.model.MeterBaseOperationLog;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.model.MeterBaseRailwayHisTareData;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasehistaredata.model.MeterBaseHisTareData;
import com.steerinfo.baseinfo.meterbasehistaredata.mapper.MeterBaseHisTareDataMapper;
import com.steerinfo.baseinfo.meterbasehistaredata.service.IMeterBaseHisTareDataService;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.util.PublicMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    @Resource
    private MeterBaseHisTareDataMapper meterBaseHisTareDataMapper;

    @Resource
    private MeterBaseOperationLogMapper meterBaseOperationLogMapper;

    @Override
    protected IBaseMapper<MeterBaseHisTareData, String> getMapper() {
        return meterBaseHisTareDataMapper;
    }

    @Override
    public String Recovery(MeterBaseHisTareData model)
    {
        try {
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("valueFlag","0");
            obj.put("carNo",model.getCarNo());
            obj.put("dataNo",model.getDataNo());
            List<MeterBaseHisTareData> list=meterBaseHisTareDataMapper.selectByParameters(obj);
            if ( ! list.isEmpty()){
                return "该车号已存在有效数据，无法启用！";
            }
            model.setId(model.getId());
            model.setValueFlag("0");
            meterBaseHisTareDataMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(MeterBaseHisTareData model)
    {
        try {

            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();

            model.setId(model.getId());
            model.setValueFlag("1");
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
            model.setUpdateTime(new Date());
            model.setNote(model.getNote());
            meterBaseHisTareDataMapper.updateByPrimaryKeySelective(model);

            MeterBaseOperationLog logModel = new MeterBaseOperationLog();
            logModel.setId(PublicMethod.IdCreate(2));
            logModel.setCreateManNo(userId);
            logModel.setCreateManName(userName);
            logModel.setCreateTime(new Date());
            logModel.setOperationPage("web端-历史皮重数据维护");
            logModel.setOperationFunction("作废");
            logModel.setOperationId(model.getId());
            logModel.setOperationCarnNo(model.getCarNo());
            logModel.setOperationBeforeFix(model.getNote());
            meterBaseOperationLogMapper.insertSelective(logModel);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public PageList<MeterBaseHisTareData> getList(HashMap params) {
        PageHelper.startPage(Integer.parseInt(params.get("pageNum").toString()), Integer.parseInt(params.get("pageSize").toString()));
        List<MeterBaseHisTareData> list = meterBaseHisTareDataMapper.getList(params);
        PageList pageInfo = new PageList(list);
        return pageInfo;
    }
}
