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

import cn.hutool.core.lang.Assert;
import com.jisco.dynamicweight.dynamicbasematerial.mapper.DynamicBaseMaterialMapper;
import com.jisco.dynamicweight.dynamicbasematerial.model.DynamicBaseMaterial;
import com.jisco.dynamicweight.dynamicbasematerial.service.IDynamicBaseMaterialService;
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;

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

    @Autowired
    private DynamicBaseMaterialMapper dynamicBaseMaterialMapper;

    @Override
    protected IBaseMapper<DynamicBaseMaterial, String> getMapper() {
        return dynamicBaseMaterialMapper;
    }

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

    /**
     * 启用
     * @param id
     * @return
     */
    @Override
    public boolean enable(String id) {
        DynamicBaseMaterial byId = this.getById(id);
        Assert.notNull(byId, "记录不存在");
        if (byId.getValidFlag().equals("1")) return false;
        DynamicBaseMaterial config = new DynamicBaseMaterial();
        config.setMaterialNo(id);
        setUpdateManInfo(config);
        return dynamicBaseMaterialMapper.enable(config) == 1;
    }

    /**
     * 禁用
     * @param id
     * @return
     */
    @Override
    public boolean disable(String id) {
        DynamicBaseMaterial byId = this.getById(id);
        if (byId.getValidFlag() == "0") return false;
        DynamicBaseMaterial config = new DynamicBaseMaterial();
        config.setMaterialNo(id);
        setUpdateManInfo(config);
        return dynamicBaseMaterialMapper.disable(config) == 1;
    }

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