package com.steerinfo.meterwork.meterworkmeterlink.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.constants.DbConstants;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.meterwork.meterworkmeterlink.model.MeterWorkMeterLink;
import com.steerinfo.meterwork.meterworkmeterlink.mapper.MeterWorkMeterLinkMapper;
import com.steerinfo.meterwork.meterworkmeterlink.service.IMeterWorkMeterLinkService;
import com.steerinfo.meterwork.meterworkmeterscene.mapper.MeterWorkMeterSceneMapper;
import com.steerinfo.meterwork.meterworkmeterscene.model.MeterWorkMeterScene;
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;

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

    @Resource
    private MeterWorkMeterLinkMapper meterWorkMeterLinkMapper;

    @Resource
    private MeterWorkMeterSceneMapper meterWorkMeterSceneMapper;

    @Override
    protected IBaseMapper<MeterWorkMeterLink, String> getMapper() {
        return meterWorkMeterLinkMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public MeterWorkMeterLink checkandadd(MeterWorkMeterLink model)
    {
        try {
            if (model == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            check(model);
            HashMap<String, Object> obj = new HashMap<>();
                obj.put("processNo", model.getProcessNo());
                obj.put("linkOrderNum",model.getLinkOrderNum());
            List<MeterWorkMeterLink> list =meterWorkMeterLinkMapper.selectByParameters(obj);
            if ( ! list.isEmpty()){
                throw new MarkerMetException(500, "该流程的序号"+model.getLinkOrderNum()+"已配置，请先删除！！");
            }
            String no = meterWorkMeterLinkMapper.getNewID();
            model.setLinkNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
            int num = meterWorkMeterLinkMapper.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<MeterWorkMeterLink> models)
    {
        try {
            int num = 0;
            if (models.size() <= 0 || models == null) {
                throw new MarkerMetException(500, "参数不能为空！！");
            }
            for (MeterWorkMeterLink model : models) {
                model.setProcessNo(model.getProcessNo());
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("processNo", model.getProcessNo());
                List<MeterWorkMeterScene> mwmss=meterWorkMeterSceneMapper.selectByParameters(obj);
                for(MeterWorkMeterScene mwms:mwmss) {
                    mwms.setSceneNo(mwms.getSceneNo());
                    meterWorkMeterSceneMapper.deleteByPrimaryKey(mwms.getSceneNo());
                }
                meterWorkMeterLinkMapper.deleteByPrimaryKey(model.getLinkNo());
                num++;
            }
            return num;
        } catch (MarkerMetException e) {
            throw e;
        }
    }

    private void check (MeterWorkMeterLink model)
    {
        if (model.getProcessNo() == null || model.getProcessNo().equals("")) {
            throw new MarkerMetException(500, "流程编号不能为空");
        }
        if (model.getLinkCode() == null || model.getLinkCode().equals("")) {
            throw new MarkerMetException(500, "计量环节编码不能为空");
        }
        if (model.getLinkName() == null || model.getLinkName().equals("")) {
            throw new MarkerMetException(500, "计量环节名称不能为空");
        }
        if (model.getLinkOrderNum() == null || model.getLinkOrderNum().equals("")) {
            throw new MarkerMetException(500, "序号不能为空");
        }
        UserPayload payload = UserPayload.getCurrUser();
        String userId = payload.getUserCode();
        String userName = payload.getUserName();
        if (model.getLinkNo() == null || model.getLinkNo().equals("")) {
            model.setCreateTime(new Date());
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }
    }
}
