package com.iotechn.unimall.app.api.sys;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.SysDictDataDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.mapper.SysDictDataMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import com.iotechn.unimall.data.model.Page;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * Generate Code By Unimall
 */
@Service
public class DictAppAppServiceImpl implements DictAppService {

    @Autowired
    private SysDictDataMapper sysDictDataMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<SysDictDataDO> getDictDataList(String dictType,Long userId) {
        List<SysDictDataDO> dictDataList = new ArrayList<>();
        if(userId != null){
            UserDO userDO = userMapper.selectById(userId);
            Wrapper<SysDictDataDO> wrapper = new EntityWrapper<SysDictDataDO>();
            if (dictType != null) {
                wrapper.eq("dict_type", dictType);
            }
            if(userDO != null && userDO.getCompanyId() != null){
                wrapper.eq("company_id", userDO.getCompanyId());
            }
            else{
                wrapper.eq("company_id", 1);
            }
            wrapper.orderBy("sort",true);
            dictDataList = sysDictDataMapper.selectList(wrapper);
            return dictDataList;
        }
        else{
            Wrapper<SysDictDataDO> wrapper = new EntityWrapper<SysDictDataDO>();
            wrapper.eq("dict_type", dictType);
            wrapper.eq("company_id", 1);
            wrapper.orderBy("sort",true);
            dictDataList = sysDictDataMapper.selectList(wrapper);
            return dictDataList;
        }

    }

}
