package com.steerinfo.meterwork.meterworkmeterprocess.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.meterworkmeterprocess.model.MeterWorkMeterProcess;
import com.steerinfo.meterwork.meterworkmeterprocess.mapper.MeterWorkMeterProcessMapper;
import com.steerinfo.meterwork.meterworkmeterprocess.service.IMeterWorkMeterProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

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

    @Resource
    private MeterWorkMeterProcessMapper meterWorkMeterProcessMapper;

    @Override
    protected IBaseMapper<MeterWorkMeterProcess, String> getMapper() {
        return meterWorkMeterProcessMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkMeterProcess checkandadd(MeterWorkMeterProcess model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            check(model);
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("meterTypeNo", model.getMeterTypeNo());
                obj.put("meterTypeName",model.getMeterTypeName());
                List<MeterWorkMeterProcess> list = meterWorkMeterProcessMapper.selectByParameters(obj);
                if ( ! list.isEmpty()){
                    throw new MarkerMetException(500, model.getMeterTypeName()+"已配置流程，请直接修改！！");
                }
            String no = meterWorkMeterProcessMapper.getNewID();
            model.setProcessNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
            int num = meterWorkMeterProcessMapper.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(MeterWorkMeterProcess model) {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            check(model);
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("meterTypeNo", model.getMeterTypeNo());
            obj.put("meterTypeName",model.getMeterTypeName());
            obj.put("processNo",model.getProcessNo());
            List<MeterWorkMeterProcess> list = meterWorkMeterProcessMapper.selectByParameters(obj);
            if ( ! list.isEmpty()){
                throw new MarkerMetException(500, model.getMeterTypeName()+"已配置流程，请直接修改！！");
            }
            int num = meterWorkMeterProcessMapper.updateByPrimaryKey(model);
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

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

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

    }

    private void check(MeterWorkMeterProcess model) {
        if (model.getMeterTypeNo() == null || model.getMeterTypeNo().equals("")) {
            throw new MarkerMetException(500, "计量类型编号不能为空");
        }
        if (model.getMeterTypeName() == null || model.getMeterTypeName().equals("")) {
            throw new MarkerMetException(500, "计量类型名称不能为空");
        }
        if (model.getEditionNo() == null || model.getEditionNo().equals("")) {
            throw new MarkerMetException(500, "版本号不能为空");
        }
        if (model.getProcessNo() == null || model.getProcessNo().equals(""))
            model.setValueFlag(DbConstants.VALID);
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (model.getProcessNo() == null || model.getProcessNo().equals("")) {
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        } else {
            model.setUpdateTime(new Date());
            model.setUpdateManNo(userId);
            model.setUpdateManName(userName);
        }
    }
}

