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

import com.steerinfo.baseinfo.meterbasematterinfo.mapper.MeterBaseMatterInfoMapper;
import com.steerinfo.baseinfo.meterbasematterinfo.model.MeterBaseMatterInfo;
import com.steerinfo.baseinfo.meterbasemetertypeconf.mapper.MeterBaseMetertypeConfMapper;
import com.steerinfo.baseinfo.meterbasemetertypeconf.model.MeterBaseMetertypeConf;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasetermtarecar.model.MeterBaseTermTareCar;
import com.steerinfo.baseinfo.meterbasetermtarecar.mapper.MeterBaseTermTareCarMapper;
import com.steerinfo.baseinfo.meterbasetermtarecar.service.IMeterBaseTermTareCarService;
import com.steerinfo.pretrack.pretrackscale.model.PreTrackScale;
import com.steerinfo.util.StringUtils;
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;

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

    @Resource
    private MeterBaseTermTareCarMapper meterBaseTermTareCarMapper;

    @Override
    protected IBaseMapper<MeterBaseTermTareCar, String> getMapper() {
        return meterBaseTermTareCarMapper;
    }

    @Override
    public String checkandadd(MeterBaseTermTareCar model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        MeterBaseTermTareCar com = meterBaseTermTareCarMapper.selectByPrimaryKey(model.getCarNo());
        if (com != null) {
            return "该车号已存在，请直接修改";
        }
        model.setCreateTime(new Date());
        model.setValueFlag("0");
        meterBaseTermTareCarMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandaddlist(List<MeterBaseTermTareCar> models)
    {
        if (models == null || models.size() < 1) {
            return "请传入参数!";
        }
        for (MeterBaseTermTareCar model : models) {
            String msg = check(model);
            if (StringUtils.isNotEmpty(msg)) {
                return msg;
            }
            MeterBaseTermTareCar com = meterBaseTermTareCarMapper.selectByPrimaryKey(model.getCarNo());
            if (com != null) {
                return "存在车号重复，请直接修改";
            }
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
            meterBaseTermTareCarMapper.insert(model);
        }
        return "";
    }

    @Resource
    MeterBaseMetertypeConfMapper metertypeConfMapper;
    @Resource
    MeterBaseMatterInfoMapper matterInfoMapper;

    @Override
    public List<MeterBaseTermTareCar> doValidTareWf(PreTrackScale model) throws Exception {
        //业务类型、物资、车辆均配置能够使用期限皮重
        //业务类型可保存期限皮
        HashMap<String, Object> cf = new HashMap<>();
        cf.put("isAllowTermTare", "1");
        cf.put("valueFlag", "1");
        cf.put("meterTypeNo",model.getMeterTypeNo());
        List<MeterBaseMetertypeConf> confs=metertypeConfMapper.selectByParameters(cf);
        if(confs!=null&&confs.size()>0){
            //物资可保存期限皮
            cf.clear();
            cf.put("matterNo",model.getMatterNo());
            cf.put("valueFlag", "1");
            cf.put("allowedTareWeight","1");
            List<MeterBaseMatterInfo> matterInfos=matterInfoMapper.selectByParameters(cf);
            if(confs!=null&&confs.size()>0){
                //车辆可保存期限皮
                cf.clear();
                cf.put("carNo", model.getCarNo());
                cf.put("valueFlag", "0");
                return meterBaseTermTareCarMapper.selectByParameters(cf);
            }else{
                throw new Exception("当前物资不允许留期限皮");
            }
        }else{
            throw new Exception("当前计量类型不允许留期限皮");
        }
    }

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

    @Override
    public String checkandupdate(String id,MeterBaseTermTareCar model)
    {
        String msg = check(model);
        if (StringUtils.isNotEmpty(msg)) {
            return msg;
        }
        if( ! model.getCarNo().equals(id)){
            return "车号不允许修改";}
        model.setUpdateTime(new Date());
        meterBaseTermTareCarMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check (MeterBaseTermTareCar model) {
        if (model == null) {
            return "参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarNo())) {
            return "车号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarDriverName())) {
            return "驾驶员不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarOwnerName())) {
            return "车主不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarUnderUnit())) {
            return "隶属单位不能为空！！";
        }
        return "";
    }
}
