package com.steerinfo.meterwork.meterworksceneregister.service.impl;

import com.steerinfo.baseinfo.constants.DbConstants;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworksceneregister.model.MeterWorkSceneRegister;
import com.steerinfo.meterwork.meterworksceneregister.mapper.MeterWorkSceneRegisterMapper;
import com.steerinfo.meterwork.meterworksceneregister.service.IMeterWorkSceneRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

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

    @Autowired
    private MeterWorkSceneRegisterMapper meterWorkSceneRegisterMapper;

    @Override
    protected IBaseMapper<MeterWorkSceneRegister, String> getMapper() {
        return meterWorkSceneRegisterMapper;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public MeterWorkSceneRegister checkandadd(MeterWorkSceneRegister model)
    {
        try{
            if (model == null)
            {
                throw  new MarkerMetException(500,"参数不能为空！！");
            }
            check(model);
            String no=meterWorkSceneRegisterMapper.GetNewID();
            model.setSceneCode(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
            int num=meterWorkSceneRegisterMapper.insertSelective(model);
            if (num == 1) {
                return model;
            }else {
                throw  new MarkerMetException(500,"新增异常！！");
            }
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int checkandupdate(MeterWorkSceneRegister model)
    {
        try{
            if (model == null)
            {
                throw  new MarkerMetException(500,"参数不能为空！！");
            }
            check(model);
            int num=meterWorkSceneRegisterMapper.updateByPrimaryKey(model);
            return num;
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int abolish(List<MeterWorkSceneRegister> models)
    {
        try{
            int num=0;
            if (models.size() <= 0 || models == null)
            {
                throw  new MarkerMetException(500,"参数不能为空！！");
            }
            for(MeterWorkSceneRegister model:models)
            {
                if (model.getValueFlag().equals(DbConstants.INVALID))
                {
                    throw  new MarkerMetException(500,"无法再次停用！！");
                }
                model.setSceneCode(model.getSceneCode());
                model.setValueFlag(DbConstants.INVALID);
                meterWorkSceneRegisterMapper.updateByPrimaryKey(model);
                num++;
            }
            return num;
        }catch (MarkerMetException e){
            throw  e;
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public int recover(List<MeterWorkSceneRegister> models)
    {
        try{
            int num=0;
            if (models.size() <= 0 || models == null)
            {
                throw  new MarkerMetException(500,"参数不能为空！！");
            }
            for(MeterWorkSceneRegister model:models)
            {
                if (model.getValueFlag().equals(DbConstants.VALID))
                {
                    throw  new MarkerMetException(500,"无法再次启用！！");
                }
                model.setSceneCode(model.getSceneCode());
                model.setValueFlag(DbConstants.VALID);
                meterWorkSceneRegisterMapper.updateByPrimaryKey(model);
                num++;
            }
            return num;
        }catch (MarkerMetException e){
            throw  e;
        }

    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<MeterWorkSceneRegister> queryAll(Map<String,Object> parmas){
        return meterWorkSceneRegisterMapper.selectByParameters(parmas);
    }

    private void check(MeterWorkSceneRegister model)
    {
        if(model.getSceneName()==null || model.getSceneName().equals(""))
        {
            throw  new MarkerMetException(500,"场景名称不能为空");
        }
        if(model.getSceneNote()==null || model.getSceneNote().equals(""))
        {
            throw  new MarkerMetException(500,"场景描述不能为空");
        }
        if(model.getSceneUrl()==null || model.getSceneUrl().equals(""))
        {
            throw  new MarkerMetException(500,"场景调用路径不能为空");
        }
        if(model.getSceneCode() == null || model.getSceneCode().equals(""))
            model.setValueFlag(DbConstants.VALID);
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if(model.getSceneCode() == null || model.getSceneCode().equals("")) {
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }else
        {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
    }
}
