package com.bonus.material.basic.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.bonus.common.biz.constant.BmConfigItems;
import com.bonus.common.core.utils.DateUtils;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.security.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bonus.material.basic.mapper.BmConfigMapper;
import com.bonus.material.basic.domain.BmConfig;
import com.bonus.material.basic.service.IBmConfigService;

/**
 * 功能参数配置Service业务层处理
 * 
 * @author xsheng
 * @date 2024-09-26
 */
@Service
public class BmConfigServiceImpl implements IBmConfigService 
{
    @Autowired
    private BmConfigMapper bmConfigMapper;

    /**
     * 查询功能参数配置
     * 
     * @param id 功能参数配置主键
     * @return 功能参数配置
     */
    @Override
    public BmConfig selectBmConfigById(Long id)
    {
        return bmConfigMapper.selectBmConfigById(id);
    }

    /**
     * 查询功能参数配置
     *
     * @param itemName 功能参数配置名称
     * @return 功能参数配置
     */
    @Override
    public BmConfig selectBmConfigByItemName(String itemName)
    {
        return bmConfigMapper.selectBmConfigByItemName(itemName);
    }

    /**
     * 查询功能参数配置列表
     * 
     * @param bmConfig 功能参数配置
     * @return 功能参数配置
     */
    @Override
    public List<BmConfig> selectBmConfigList(BmConfig bmConfig)
    {
        return bmConfigMapper.selectBmConfigList(bmConfig);
    }

    /**
     * 新增功能参数配置
     * 
     * @param bmConfig 功能参数配置
     * @return 结果
     */
    @Override
    public int insertBmConfig(BmConfig bmConfig)
    {
        bmConfig.setCreateTime(DateUtils.getNowDate());
        bmConfig.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        bmConfigMapper.deleteBmConfigByName(bmConfig.getItemName());
        return bmConfigMapper.insertBmConfig(bmConfig);
    }

    /**
     * 修改功能参数配置
     * 
     * @param bmConfig 功能参数配置
     * @return 结果
     */
    @Override
    public int updateBmConfig(BmConfig bmConfig)
    {
        bmConfig.setUpdateTime(DateUtils.getNowDate());
        return bmConfigMapper.updateBmConfig(bmConfig);
    }

    /**
     * 批量删除功能参数配置
     * 
     * @param ids 需要删除的功能参数配置主键
     * @return 结果
     */
    @Override
    public int deleteBmConfigByIds(Long[] ids)
    {
        return bmConfigMapper.deleteBmConfigByIds(ids);
    }

    /**
     * 删除功能参数配置信息
     * 
     * @param id 功能参数配置主键
     * @return 结果
     */
    @Override
    public int deleteBmConfigById(Long id)
    {
        return bmConfigMapper.deleteBmConfigById(id);
    }

    private String getValueWithDefault (String value1, String value2) {
        if (StringUtils.isEmpty(value1)) {
            return value2;
        } else {
            return value1.trim();
        }
    }

    public boolean isBmConfigEnabledWithDefaultFalse(String itemName) {
        boolean result = false;
        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(itemName);
        if (Objects.nonNull(bmConfig)) {
            String value = getValueWithDefault(bmConfig.getItemValue(), "0");
            if (StringUtils.isNotEmpty(value)) {
                result = "1".equals(value) ? true : false;
            }
        }
        return result;
    }

    public boolean isBmConfigEnabledWithDefaultTrue(String itemName) {
        boolean result = false;
        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(itemName);
        if (Objects.nonNull(bmConfig)) {
            String value = getValueWithDefault(bmConfig.getItemValue(), "1");
            if (StringUtils.isNotEmpty(value)) {
                result = "1".equals(value) ? true : false;
            }
        }
        return result;
    }

    public String getBmConfigResultAsString(String itemName) {
        String value = "";
        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(itemName);
        if (Objects.nonNull(bmConfig)) {
            value = getValueWithDefault(bmConfig.getItemValue(), "");
        }
        return value;
    }

    public List<String> getBmConfigResultAsList(String itemName) {
        List<String> list = new ArrayList();
        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(itemName);
        if (Objects.nonNull(bmConfig)) {
            String value = getValueWithDefault(bmConfig.getItemValue(), "");
            if (StringUtils.isNotEmpty(value)) {
                list = Arrays.asList(value.split(","));
            }
        }
        return list;
    }

    /**获取所设定的选项：领料审核角色列表*/
//    public List<String> getLeaseTaskAuditRoleIds() {
//        List<String> list = new ArrayList();
//        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(BmConfigItems.LEASE_TASK_AUDIT_ROLE_IDS);
//        if (Objects.nonNull(bmConfig)) {
//            String value = getValueWithDefault(bmConfig.getItemValue(), "");
//            if (StringUtils.isNotEmpty(value)) {
//                list = Arrays.asList(value.split(","));
//            }
//        }
//        return list;
//    }
//
//    /**获取所设定的选项：退料审核角色列表*/
//    public List<String> getBackTaskAuditRoleIds() {
//        List<String> list = new ArrayList();
//        BmConfig bmConfig = bmConfigMapper.selectBmConfigByItemName(BmConfigItems.BACK_TASK_AUDIT_ROLE_IDS);
//        if (Objects.nonNull(bmConfig)) {
//            String value = getValueWithDefault(bmConfig.getItemValue(), "");
//            if (StringUtils.isNotEmpty(value)) {
//                list = Arrays.asList(value.split(","));
//            }
//        }
//        return list;
//    }
}
