package com.ruoyi.os.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.os.mapper.HealthcareOsConventionalValueMapper;
import com.ruoyi.os.domain.HealthcareOsConventionalValue;
import com.ruoyi.os.service.IHealthcareOsConventionalValueService;

/**
 * 字典Service业务层处理
 *
 * @author 何顺
 * @date 2024-09-29
 */
@Service
public class HealthcareOsConventionalValueServiceImpl implements IHealthcareOsConventionalValueService
{
    @Autowired
    private HealthcareOsConventionalValueMapper healthcareOsConventionalValueMapper;

    /**
     * 查询字典
     *
     * @param id 字典主键
     * @return 字典
     */
    @Override
    public HealthcareOsConventionalValue selectHealthcareOsConventionalValueById(Long id)
    {
        return healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueById(id);
    }

    /**
     * 查询字典列表
     *
     * @param healthcareOsConventionalValue 字典
     * @return 字典
     */
    @Override
    public List<HealthcareOsConventionalValue> selectHealthcareOsConventionalValueList(HealthcareOsConventionalValue healthcareOsConventionalValue)
    {
        return healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(healthcareOsConventionalValue);
    }

    /**
     * 新增字典
     *
     * @param healthcareOsConventionalValue 字典
     * @return 结果
     */
    @Override
    public int insertHealthcareOsConventionalValue(HealthcareOsConventionalValue healthcareOsConventionalValue)
    {
        List<HealthcareOsConventionalValue> list = healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(healthcareOsConventionalValue);
        if(list.size() > 0) {
            throw new RuntimeException("该字典已存在");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        healthcareOsConventionalValue.setCreateBy(loginUser.getUsername());
        healthcareOsConventionalValue.setCreateTime(DateUtils.getNowDate());
        return healthcareOsConventionalValueMapper.insertHealthcareOsConventionalValue(healthcareOsConventionalValue);
    }

    /**
     * 修改字典
     *
     * @param healthcareOsConventionalValue 字典
     * @return 结果
     */
    @Override
    public int updateHealthcareOsConventionalValue(HealthcareOsConventionalValue healthcareOsConventionalValue)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        healthcareOsConventionalValue.setUpdateBy(loginUser.getUsername());
        healthcareOsConventionalValue.setUpdateTime(DateUtils.getNowDate());
        return healthcareOsConventionalValueMapper.updateHealthcareOsConventionalValue(healthcareOsConventionalValue);
    }

    /**
     * 批量删除字典
     *
     * @param ids 需要删除的字典主键
     * @return 结果
     */
    @Override
    public int deleteHealthcareOsConventionalValueByIds(Long[] ids)
    {
        return healthcareOsConventionalValueMapper.deleteHealthcareOsConventionalValueByIds(ids);
    }

    /**
     * 删除字典信息
     *
     * @param id 字典主键
     * @return 结果
     */
    @Override
    public int deleteHealthcareOsConventionalValueById(Long id)
    {
        return healthcareOsConventionalValueMapper.deleteHealthcareOsConventionalValueById(id);
    }

    /**
     *    查询 字典名称 对 的 数值 列表
     *    @param name 字典父主键
     *    @return 结果
     */
    public List<HealthcareOsConventionalValue> queryDictionaryValueList(String name) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setDictName(name);
        return  queryDictionaryValueList(conventionalValue);
    }


    /**
     *   根据 数值 进行 模糊 查询
     *   @param name 字典父主键
     *   @return 结果
     */
    public List<HealthcareOsConventionalValue> queryDictionaryValueList(String name , String value) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setDictName(name);
        conventionalValue.setValue(value);
        return  queryDictionaryValueList(conventionalValue);
    }


    /**
     *   改装 的 查询 的  方法  集合
     *
     */
    private List<HealthcareOsConventionalValue> queryDictionaryValueList(HealthcareOsConventionalValue conventionalValue) {
        List<HealthcareOsConventionalValue> list1 = new ArrayList<>();
        HealthcareOsConventionalValue conventionalValue1 = new HealthcareOsConventionalValue();
        conventionalValue1.setDictName(conventionalValue.getDictName());
        conventionalValue1.setParentId(0L);
        try {
            if(healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(conventionalValue1).size() == 1) {
                List<HealthcareOsConventionalValue> list = healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(conventionalValue);
                //  模糊 查询 的
                BeanUtils.copyProperties(conventionalValue1,conventionalValue);
                conventionalValue1.setParentId(list.get(0).getId());
                conventionalValue1.setDictName(null);
                list1 = healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(conventionalValue1);
            }
        } catch (Exception e) {
            return list1;
        }
        return list1;
    }

    /**
     *   获取 id
     */
    public Long queryDictionaryId(String name) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setDictName(name);
        conventionalValue.setParentId(0L);
        List<HealthcareOsConventionalValue> list = healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(conventionalValue);
        if(list.size() == 1) {
            return list.get(0).getId();
        }
        return null;
    }

    /**
     *   添加  字典名称 对 的 数值
     */
    public int  addDictionaryValue(Long id , String value) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setParentId(id);
        conventionalValue.setValue(value);
        List<HealthcareOsConventionalValue> list = healthcareOsConventionalValueMapper.selectHealthcareOsConventionalValueList(conventionalValue);
        if(list.size() > 0) {
            throw new RuntimeException("数值重复");
        }
        // 时间
        conventionalValue.setCreateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        conventionalValue.setCreateBy(loginUser.getUsername());
        return  healthcareOsConventionalValueMapper.insertHealthcareOsConventionalValue(conventionalValue);
    }

    /**
     *    添加  字典名称 对 的 数值
     */
    public int  addDictionaryValue(Long id ,String name , String value) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setParentId(id);
        conventionalValue.setName(name);
        conventionalValue.setValue(value);
        // 时间
        conventionalValue.setCreateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        conventionalValue.setCreateBy(loginUser.getUsername());
        return  healthcareOsConventionalValueMapper.insertHealthcareOsConventionalValue(conventionalValue);
    }

    /**
     *   添加  字典名称 对 的 数值
     */
    public int  updateDictionaryValue(Long id , String value) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setId(id);
        conventionalValue.setValue(value);
        // 时间
        conventionalValue.setUpdateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        conventionalValue.setUpdateBy(loginUser.getUsername());
        return  healthcareOsConventionalValueMapper.updateHealthcareOsConventionalValue(conventionalValue);
    }
    /**
     *   添加  字典名称 对 的 数值
     */
    public int  updateDictionaryValue(Long id ,String name , String value) {
        HealthcareOsConventionalValue conventionalValue = new HealthcareOsConventionalValue();
        conventionalValue.setId(id);
        conventionalValue.setName(name);
        conventionalValue.setValue(value);
        // 时间
        conventionalValue.setUpdateTime(DateUtils.getNowDate());
        // 获取 用户 信息
        LoginUser loginUser = SecurityUtils.getLoginUser();
        conventionalValue.setUpdateBy(loginUser.getUsername());
        return  healthcareOsConventionalValueMapper.updateHealthcareOsConventionalValue(conventionalValue);
    }
}
