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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecarrierunit.model.MeterBaseCarrierUnit;
import com.steerinfo.baseinfo.meterbasecarrierunit.mapper.MeterBaseCarrierUnitMapper;
import com.steerinfo.baseinfo.meterbasecarrierunit.service.IMeterBaseCarrierUnitService;
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;
import java.util.Map;

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

    @Autowired
    private MeterBaseCarrierUnitMapper meterBaseCarrierUnitMapper;

    @Override
    protected IBaseMapper<MeterBaseCarrierUnit, String> getMapper() {
        return meterBaseCarrierUnitMapper;
    }

    @Override
    public String checkandadd(MeterBaseCarrierUnit model){
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("carrierUnitLogin", model.getCarrierUnitLogin());
        List<MeterBaseCarrierUnit> com1 = meterBaseCarrierUnitMapper.selectByParameters(obj1);
        if ( ! com1.isEmpty())
        {
            return "该账号已存在！！";
        }
        HashMap<String, Object> obj2 = new HashMap<>();
        obj2.put("carrierUnitName", model.getCarrierUnitName());
         com1 = meterBaseCarrierUnitMapper.selectByParameters(obj2);
        if (! com1.isEmpty()) {
            return "此承运单位名称已存在";
        }
        String no = meterBaseCarrierUnitMapper.GetNewID();
        model.setCarrierUnitNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        meterBaseCarrierUnitMapper.insertSelective(model);
        return "";
    }

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

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

    @Override
    public String checkandupdate(String id, MeterBaseCarrierUnit model){
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj1 = new HashMap<>();
        obj1.put("carrierUnitLogin", model.getCarrierUnitLogin());
        obj1.put("carrierUnitNo", id);
        List<MeterBaseCarrierUnit> com1 = meterBaseCarrierUnitMapper.selectForUpdate(obj1);
        if ( ! com1.isEmpty())
        {
            return "该账号已存在！！";
        }
        HashMap<String, Object> obj2 = new HashMap<>();
        obj2.put("carrierUnitName", model.getCarrierUnitName());
        obj2.put("carrierUnitNo", id);
        com1 = meterBaseCarrierUnitMapper.selectForUpdate(obj2);
        if (! com1.isEmpty()) {
            return "此承运单位名称已存在";
        }
        meterBaseCarrierUnitMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public List<MeterBaseCarrierUnit> queryAll(Map<String,Object> parmas)
    {
        return meterBaseCarrierUnitMapper.selectByParameters(parmas);
    }

    private String check (MeterBaseCarrierUnit model){
        if (model == null){
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getCarrierUnitName())){
            return "承运单位名称不能为空！";
        }
        if (StringUtils.isEmpty(model.getCarrierUnitLogin())){
            return "账号不能为空！";
        }
        if (StringUtils.isEmpty(model.getCarrierUnitPassword())){
            return "密码不能为空！";
        }
        if(StringUtils.isEmpty(model.getCarrierUnitNo())){
            model.setCreateTime(new Date());
            model.setValidFlag("1");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
