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

import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecarblacelist.model.MeterBaseCarBlacelist;
import com.steerinfo.baseinfo.meterbasecarblacelist.mapper.MeterBaseCarBlacelistMapper;
import com.steerinfo.baseinfo.meterbasecarblacelist.service.IMeterBaseCarBlacelistService;
import com.steerinfo.framework.utils.base.StringUtils;
import com.steerinfo.meterwork.meterworkrailwayactfirst.model.MeterWorkRailwayActFirst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

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

    @Resource
    private MeterBaseCarBlacelistMapper meterBaseCarBlacelistMapper;

    @Override
    protected IBaseMapper<MeterBaseCarBlacelist, String> getMapper() {
        return meterBaseCarBlacelistMapper;
    }

    @Override
    public String checkandadd(MeterBaseCarBlacelist model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("carNo", model.getCarNo());
        obj.put("valueFlag","1");
        List<MeterBaseCarBlacelist> com =meterBaseCarBlacelistMapper.selectByParameters(obj);
        if( !com.isEmpty()) {
            return "该车号已存在有效的黑名单数据！";
        }
        String no=meterBaseCarBlacelistMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        meterBaseCarBlacelistMapper.insert(model);
        return "";
    }

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

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

    @Override
    public String checkandupdate(String id,MeterBaseCarBlacelist model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("carNo", model.getCarNo());
        obj.put("blacelistNo", model.getBlacelistNo());
        List<MeterBaseCarBlacelist> com =meterBaseCarBlacelistMapper.selectForUpdate(obj);
        if( !com.isEmpty()) {
            return "该车号已存在";
        }
        model.setUpdateTime(new Date());
        meterBaseCarBlacelistMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check(MeterBaseCarBlacelist model)
    {
        if (model == null){
            return "参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarNo())){
            return "车号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getSuppllier())){
            return "供应商不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarOwnerName())){
            return "车主不能为空！！";
        }
//        if (StringUtils.isEmpty(model.getPunishTypeNo())){
//            return "违规类型编号不能为空！！";
//        }
        if (StringUtils.isEmpty(model.getPunishTypeName())){
            return "违规类型名称不能为空！！";
        }
//        if (StringUtils.isEmpty(model.getBlackLevelNo())){
//            return "黑名单级别编码不能为空！！";
//        }
        if (StringUtils.isEmpty(model.getBlackLevelName())){
            return "黑名单级别名称不能为空！！";
        }
        if(StringUtils.isEmpty(model.getBlacelistNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("1");
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }

    @Override
    public RESTfulResult blackCarByCarNo(HashMap<String, Object> parmas) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            String carNo = parmas.get("carNo") == null ? "": parmas.get("carNo").toString();
            if(StringUtils.isEmpty(carNo)) {
                rm.setMessage("车号为空，请确认！！");
                return rm;
            }
            carNo = carNo.replaceAll("-", "");
            //查询车号在黑名单是否在一级黑名单
            HashMap<String, Object> map = new HashMap<>();
            map.put("blackLevelName", "1");
            map.put("valueFlag", "1");
            map.put("carNo", carNo);
            List<MeterBaseCarBlacelist> models = meterBaseCarBlacelistMapper.selectByParameters(map);
            rm.setSucceed();
            rm.setCode("0");
            if(models.size() >= 1) {
                rm.setMessage("1");
            }else {
                rm.setMessage("0");
            }
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！！>>>" + e.getMessage());
        }
        return rm;
    }

    @Override
    public RESTfulResult allBlackCarByCarNo() {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            //查询车号在黑名单是否在一级黑名单
            HashMap<String, Object> map = new HashMap<>();
            map.put("blackLevelName", "1");
            map.put("valueFlag", "1");
            List<String> models = meterBaseCarBlacelistMapper.allBlackCarByCarNo(map).stream().map(MeterBaseCarBlacelist::getCarNo).collect(Collectors.toList());
            rm.setSucceed();
            rm.setCode("0");
            rm.setMessage("操作成功！！");
            if(models.size() >= 1) {
                rm.setData(models);
            }
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常！！！>>>" + e.getMessage());
        }
        return rm;
    }
}
