package com.multictrl.modules.sys.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.multictrl.common.exception.ErrorCode;
import com.multictrl.common.exception.RenException;
import com.multictrl.common.page.PageData;
import com.multictrl.common.service.impl.BaseServiceImpl;
import com.multictrl.common.utils.ConvertUtils;
import com.multictrl.modules.sys.dao.SysDictDataDao;
import com.multictrl.modules.sys.dao.SysDictTypeDao;
import com.multictrl.modules.sys.dto.SysDictTypeDTO;
import com.multictrl.modules.sys.entity.DictData;
import com.multictrl.modules.sys.entity.DictType;
import com.multictrl.modules.sys.entity.SysDictTypeEntity;
import com.multictrl.modules.sys.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * 字典类型
 *
 * @author Sdy
 */
@Service
@RequiredArgsConstructor
public class SysDictTypeServiceImpl extends BaseServiceImpl<SysDictTypeDao, SysDictTypeEntity> implements SysDictTypeService {
    private final SysDictDataDao sysDictDataDao;
    private final static List<String> DICT_TYPE_LIST = new ArrayList<>() {{
        add("dockType");
//        add("dockModel");
    }};

    @Override
    public PageData<SysDictTypeDTO> page(Map<String, Object> params) {
        IPage<SysDictTypeEntity> page = baseDao.selectPage(
                getPage(params, "sort", true),
                getWrapper(params)
        );

        return getPageData(page, SysDictTypeDTO.class);
    }

    private QueryWrapper<SysDictTypeEntity> getWrapper(Map<String, Object> params) {
        String dictType = (String) params.get("dictType");
        String dictName = (String) params.get("dictName");

        QueryWrapper<SysDictTypeEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(dictType), "dict_type", dictType);
        wrapper.like(StrUtil.isNotBlank(dictName), "dict_name", dictName);

        return wrapper;
    }

    @Override
    public SysDictTypeDTO get(Long id) {
        SysDictTypeEntity entity = baseDao.selectById(id);

        return ConvertUtils.sourceToTarget(entity, SysDictTypeDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictTypeDTO dto) {
        List<SysDictTypeEntity> list = baseDao.selectList(new QueryWrapper<SysDictTypeEntity>()
                .eq("dict_type", dto.getDictType()));
        if (CollectionUtil.isNotEmpty(list)) {
            throw new RenException(ErrorCode.DICT_EXIST, dto.getDictType());
        }
        SysDictTypeEntity entity = ConvertUtils.sourceToTarget(dto, SysDictTypeEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictTypeDTO dto) {
        SysDictTypeEntity typeEntity = selectById(dto.getId());
        if (DICT_TYPE_LIST.contains(typeEntity.getDictType())) {
            dto.setDictType(typeEntity.getDictType());
        }
        List<SysDictTypeEntity> list = baseDao.selectList(new QueryWrapper<SysDictTypeEntity>()
                .eq("dict_type", dto.getDictType()).ne("id", dto.getId()));
        if (CollectionUtil.isNotEmpty(list)) {
            throw new RenException(ErrorCode.DICT_EXIST, dto.getDictType());
        }
        SysDictTypeEntity entity = ConvertUtils.sourceToTarget(dto, SysDictTypeEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        List<Long> idList = new ArrayList<>();
        for (Long id : ids) {
            SysDictTypeEntity typeEntity = selectById(id);
            if (typeEntity != null) {
                String dictType = typeEntity.getDictType();
                if (!DICT_TYPE_LIST.contains(dictType)) {
                    idList.add(id);
                }
            }
        }
        if (idList.isEmpty()) {
            throw new RenException(ErrorCode.DICT_NOT_DELETE, "[机库类型]");
        }
        //删除
        deleteBatchIds(idList);
    }

    @Override
    public List<DictType> getAllList() {
        List<DictType> typeList = baseDao.getDictTypeList();
        List<DictData> dataList = sysDictDataDao.getDictDataList();
        for (DictType type : typeList) {
            for (DictData data : dataList) {
                if (type.getId().equals(data.getDictTypeId())) {
                    type.getDataList().add(data);
                }
            }
        }
        return typeList;
    }

}