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

import com.steerinfo.baseinfo.meterbasecalibrastandard.mapper.MeterBaseCalibraStandardMapper;
import com.steerinfo.baseinfo.meterbasecalibrastandard.model.MeterBaseCalibraStandard;
import com.steerinfo.baseinfo.meterbasecalibrastandard.service.IMeterBaseCalibraStandardService;


import com.steerinfo.baseinfo.meterbasespotinfo.model.MeterBaseSpotInfo;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.base.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

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

    @Autowired
    private MeterBaseCalibraStandardMapper meterBaseCalibraStandardMapper;

    @Override
    protected IBaseMapper<MeterBaseCalibraStandard, String> getMapper() {
        return meterBaseCalibraStandardMapper;
    }

    @Override
//    查询全部
    public List<MeterBaseCalibraStandard> findAll() {

        List<MeterBaseCalibraStandard> list=meterBaseCalibraStandardMapper.findAll();
        //system.out.println("meterBaseCalibraStandardMapper.findAll()"+ meterBaseCalibraStandardMapper.findAll());
        return list;
    }

    @Override
    public String checkandadd(MeterBaseCalibraStandard model)
    {
        if (model == null ) {
            return "请传入参数!";
        }
        if (StringUtils.isEmpty(model.getCalibrationTypeNo())) {
            return "校秤编码";
        }
        if (StringUtils.isEmpty(model.getCalibrationTypeName())) {
            return "校秤类型名称";
        }
        String no=meterBaseCalibraStandardMapper.GetNewID();
//        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);
        model.setStandardNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) +no);

//        UserPayload payload = UserPayload.getCurrUser();
//        String userId = payload.getUserCode();
//        String userName = payload.getUserName();
//        model.setCreateManNo(userId);
//        model.setCreateManName(userName);
        model.setCreateTime(new Date());
        model.setValueFlag("0");
        meterBaseCalibraStandardMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseCalibraStandard model) {
        if (model == null ) {
            return "请传入参数!";
        }
        if (StringUtils.isEmpty(model.getCalibrationTypeNo())) {
            return "校秤编码不能为空";
        }
        if (StringUtils.isEmpty(model.getCalibrationTypeName())) {
            return "校秤类型名称不能为空";
        }
        model.setUpdateTime(new Date());
        //设置修改人、修改时间
//        UserPayload payload = UserPayload.getCurrUser();
//        String userId = payload.getUserCode();
//        String userName = payload.getUserName();
//        model.setUpdateTime(new Date());
//        model.setUpdateManNo(userId);
//        model.setUpdateManName(userName);
        meterBaseCalibraStandardMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String recovery(List<MeterBaseCalibraStandard> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCalibraStandard model:models) {
                model.setStandardNo(model.getStandardNo());
                model.setValueFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCalibraStandardMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(List<MeterBaseCalibraStandard> models) {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCalibraStandard model:models) {
                model.setStandardNo(model.getStandardNo());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseCalibraStandardMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }


}
