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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbaselimitchemical.model.MeterBaseLimitChemical;
import com.steerinfo.baseinfo.meterbaselimitchemical.mapper.MeterBaseLimitChemicalMapper;
import com.steerinfo.baseinfo.meterbaselimitchemical.service.IMeterBaseLimitChemicalService;
import com.steerinfo.util.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.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseLimitChemicalMapper meterBaseLimitChemicalMapper;

    @Override
    protected IBaseMapper<MeterBaseLimitChemical, String> getMapper() {
        return meterBaseLimitChemicalMapper;
    }

    @Override
    public String checkandadd(MeterBaseLimitChemical model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("matterNo", model.getMatterNo());
            obj.put("limitTypeNo", model.getLimitTypeNo());
            List<MeterBaseLimitChemical> list=meterBaseLimitChemicalMapper.selectByParameters(obj);
            if( ! list.isEmpty())
            {
                return model.getMatterName()+"的"+model.getLimitTypeName()+"已配置！请修改";
            }
            String no=meterBaseLimitChemicalMapper.GetNewID();
            model.setLimitChemicalNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
            meterBaseLimitChemicalMapper.insert(model);
            return "";
    }

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

    @Override
    public String checkandupdate(String id,MeterBaseLimitChemical model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("matterNo", model.getMatterNo());
        obj.put("limitTypeNo", model.getLimitTypeNo());
        obj.put("limitChemicalNo", id);
        List<MeterBaseLimitChemical> list=meterBaseLimitChemicalMapper.selectForUpdate(obj);
        if( ! list.isEmpty())
        {
            return model.getMatterName()+"的"+model.getLimitTypeName()+"已配置！请修改";
        }
        meterBaseLimitChemicalMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check(MeterBaseLimitChemical model)
    {
        if( model == null){
            return "参数为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterNo())){
            return "物料编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMatterName())){
            return "物料名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLimitTypeNo())){
            return "限制类型编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLimitTypeName())){
            return "限制类型名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLimitClassNo())){
            return "限载种类编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getLimitClassName())){
            return "限载种类名称不能为空！！";
        }
        if(StringUtils.isEmpty(model.getLimitChemicalNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
