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

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.meterworkmeterscene.model.MeterWorkMeterScene;
import com.steerinfo.meterwork.meterworkmeterscene.mapper.MeterWorkMeterSceneMapper;
import com.steerinfo.meterwork.meterworkmeterscene.service.IMeterWorkMeterSceneService;
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.HashMap;
import java.util.List;

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

    @Autowired
    private MeterWorkMeterSceneMapper meterWorkMeterSceneMapper;

    @Override
    protected IBaseMapper<MeterWorkMeterScene, String> getMapper() {
        return meterWorkMeterSceneMapper;
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkMeterScene checkandadd(MeterWorkMeterScene model)
    {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("processNo", model.getProcessNo());
            obj.put("linkNo",model.getLinkNo());
            obj.put("sceneOrderNum",model.getSceneOrderNum());
            List<MeterWorkMeterScene> list =meterWorkMeterSceneMapper.selectByParameters(obj);
            if ( ! list.isEmpty()){
                throw new MarkerMetException(500, "该环节的序号"+model.getSceneOrderNum()+"已配置，请先删除！！");
            }
            check(model);
            String no = meterWorkMeterSceneMapper.getNewID();
            model.setSceneNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
            int num = meterWorkMeterSceneMapper.insertSelective(model);
            if (num == 1) {
                return model;
            } else {
                throw new MarkerMetException(500, "新增异常！！");
            }
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteRe(List<MeterWorkMeterScene> models)
    {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            for (MeterWorkMeterScene model : models) {
                meterWorkMeterSceneMapper.deleteByPrimaryKey(model.getSceneNo());
                num++;
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void check (MeterWorkMeterScene model)
    {
        if (model.getProcessNo() == null || model.getProcessNo().equals("")) {
            throw new MarkerMetException(500, "流程编号不能为空");
        }
        if (model.getLinkNo() == null || model.getLinkNo().equals("")) {
            throw new MarkerMetException(500, "环节编号不能为空");
        }
        if (model.getSceneCode() == null || model.getSceneCode().equals("")) {
            throw new MarkerMetException(500, "场景编码不能为空");
        }
        if (model.getSceneName() == null || model.getSceneName().equals("")) {
            throw new MarkerMetException(500, "场景名称不能为空");
        }
        if (model.getSceneOrderNum() == null || model.getSceneOrderNum().equals("")) {
            throw new MarkerMetException(500, "序号不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (model.getSceneNo() == null || model.getSceneNo().equals("")) {
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }
    }
}
