package com.jisco.dynamicweight.dynamicbasebatchstation.service.impl;

import cn.hutool.core.lang.Assert;
import com.jisco.dynamicweight.dynamicbasebatchdbconfig.model.DynamicBaseBatchdbConfig;
import com.jisco.dynamicweight.dynamicbasebatchstation.mapper.DynamicBaseBatchStationMapper;
import com.jisco.dynamicweight.dynamicbasebatchstation.model.DynamicBaseBatchStation;
import com.jisco.dynamicweight.dynamicbasebatchstation.service.IDynamicBaseBatchStationService;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.meterbasecalibrarelation.model.MeterBaseCalibraRelation;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

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

    @Autowired
    private DynamicBaseBatchStationMapper dynamicBaseBatchStationMapper;

    @Override
    protected IBaseMapper<DynamicBaseBatchStation, String> getMapper() {
        return dynamicBaseBatchStationMapper;
    }

    @Override
    public List<MeterBaseCalibraRelation> queryall(HashMap parmas) {
        return dynamicBaseBatchStationMapper.selectByParameters(parmas);
    }

    @Override
    public boolean enable(String id) {
        DynamicBaseBatchStation byId = this.getById(id);
        Assert.notNull(byId, "记录不存在");
        if (byId.getValidFlag().equals("1")) return false;
        DynamicBaseBatchStation config = new DynamicBaseBatchStation();
        config.setStationNo(id);
        setUpdateManInfo(config);
        return dynamicBaseBatchStationMapper.enable(config) == 1;
    }

    @Override
    public boolean disable(String id) {
        DynamicBaseBatchStation byId = this.getById(id);
        if (byId.getValidFlag() == "0") return false;
        DynamicBaseBatchStation config = new DynamicBaseBatchStation();
        config.setStationNo(id);
        setUpdateManInfo(config);
        return dynamicBaseBatchStationMapper.disable(config) == 1;
    }

    private void setUpdateManInfo(DynamicBaseBatchStation config) {
        config.setUpdateManNo(UserUtil.getCurrentUserId());
        config.setUpdateManName(UserUtil.getCurrentUserName());
        config.setUpdateTime(new Date());
    }
}
