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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasevoice.model.MeterBaseVoice;
import com.steerinfo.baseinfo.meterbasevoice.mapper.MeterBaseVoiceMapper;
import com.steerinfo.baseinfo.meterbasevoice.service.IMeterBaseVoiceService;
import com.steerinfo.framework.utils.base.StringUtils;
import com.steerinfo.util.UploadHelper;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;



import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private MeterBaseVoiceMapper meterBaseVoiceMapper;


    @Autowired
    private UploadHelper uploadHelper;


    @Override
    protected IBaseMapper<MeterBaseVoice, String> getMapper() {
        return meterBaseVoiceMapper;
    }

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


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

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

    @Override
    public String checkandadd(MeterBaseVoice model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("voiceName", model.getVoiceName());
        List<MeterBaseVoice> com =meterBaseVoiceMapper.selectByParameters(obj);
        if(!com.isEmpty()){
                return "该语音名称已存在";
            }
        String no=meterBaseVoiceMapper.GetNewID();
        model.setVoiceNo(no);
        model.setVoiceEdition("1.0");
        model.setValidFlag("1");
        meterBaseVoiceMapper.insertSelective(model);
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseVoice model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("voiceName", model.getVoiceName());
        obj.put("voiceNo", model.getVoiceNo());
        List<MeterBaseVoice> com =meterBaseVoiceMapper.selectForUpdate(obj);
        if( ! com.isEmpty() ){
            return "该语音名称已存在";
        }
        model.setUpdateTime(new Date());
        meterBaseVoiceMapper.updateByPrimaryKey(model);
        return "";
    }

    private String check(MeterBaseVoice model){
        if (model == null){
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getVoiceName())) {
            return "语音名称不能为空";
        }
        if (StringUtils.isEmpty(model.getVoiceFileName())) {
            return "文件名称不能为空";
        }
        if (StringUtils.isEmpty(model.getVoiceFileType())) {
            return "文件类型不能为空";
        }
        if (StringUtils.isEmpty(model.getVoiceFileUrl())) {
            return "文件存放地址不能为空";
        }
        if(StringUtils.isEmpty(model.getVoiceNo())){
            model.setCreateTime(new Date());
            model.setValidFlag("1");
        }else{
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
