package com.ruoyi.system.service.impl;

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

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sql.SqlUtil;
import com.ruoyi.system.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysHesuanConfigurationMapper;
import com.ruoyi.system.domain.SysHesuanConfiguration;
import com.ruoyi.system.service.ISysHesuanConfigurationService;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;

/**
 * 对比规则配置Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-10-10
 */
@Service
public class SysHesuanConfigurationServiceImpl implements ISysHesuanConfigurationService 
{
    @Autowired
    private SysHesuanConfigurationMapper sysHesuanConfigurationMapper;
    @Autowired
    private SysDeptMapper deptMapper;



    private void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }

    /**
     * 查询对比规则配置
     * 
     * @param id 对比规则配置主键
     * @return 对比规则配置
     */
    @Override
    public SysHesuanConfiguration selectSysHesuanConfigurationById(Long id)
    {
        return sysHesuanConfigurationMapper.selectSysHesuanConfigurationById(id);
    }

    /**
     * 查询对比规则配置列表
     * 
     * @param sysHesuanConfiguration 对比规则配置
     * @return 对比规则配置
     */
    @Override
    public List<SysHesuanConfiguration> selectSysHesuanConfigurationList(SysHesuanConfiguration sysHesuanConfiguration)
    {
        SysUser user = getLoginUser().getUser();
        List<SysRole> roles = user.getRoles();
        List<String> listDeptId = new ArrayList<>();
        listDeptId.add(String.valueOf(user.getDeptId()));
        //判断账号下面所有子部门
        List<SysDept> depts = deptMapper.selectChildrenDeptById(Long.valueOf(user.getDeptId()));
        if(depts != null && depts.size()>0){
            for (SysDept dept : depts) {
                listDeptId.add(String.valueOf(dept.getDeptId()));
            }
        }
        for (SysRole role : roles) {
            if("超级管理员".equals(role.getRoleName())){
                listDeptId = null;
                break;
            }
        }
        sysHesuanConfiguration.setDeptIdList(listDeptId);
        startPage();
        return sysHesuanConfigurationMapper.selectSysHesuanConfigurationList(sysHesuanConfiguration);
    }

    /**
     * 新增对比规则配置
     * 
     * @param sysHesuanConfiguration 对比规则配置
     * @return 结果
     */
    @Override
    public int insertSysHesuanConfiguration(SysHesuanConfiguration sysHesuanConfiguration, SysUser user)
    {
        sysHesuanConfiguration.setDeptId(String.valueOf(user.getDeptId()));
        return sysHesuanConfigurationMapper.insertSysHesuanConfiguration(sysHesuanConfiguration);
    }

    /**
     * 修改对比规则配置
     * 
     * @param sysHesuanConfiguration 对比规则配置
     * @return 结果
     */
    @Override
    public int updateSysHesuanConfiguration(SysHesuanConfiguration sysHesuanConfiguration)
    {
        return sysHesuanConfigurationMapper.updateSysHesuanConfiguration(sysHesuanConfiguration);
    }

    /**
     * 批量删除对比规则配置
     * 
     * @param ids 需要删除的对比规则配置主键
     * @return 结果
     */
    @Override
    public int deleteSysHesuanConfigurationByIds(Long[] ids)
    {
        return sysHesuanConfigurationMapper.deleteSysHesuanConfigurationByIds(ids);
    }

    /**
     * 删除对比规则配置信息
     * 
     * @param id 对比规则配置主键
     * @return 结果
     */
    @Override
    public int deleteSysHesuanConfigurationById(Long id)
    {
        return sysHesuanConfigurationMapper.deleteSysHesuanConfigurationById(id);
    }
}
