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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.multictrl.common.constant.BusinessConstant;
import com.multictrl.common.constant.DJIImage;
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.SysDictDataDTO;
import com.multictrl.modules.sys.entity.SysDictDataEntity;
import com.multictrl.modules.sys.entity.SysDictTypeEntity;
import com.multictrl.modules.sys.service.SysDictDataService;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 字典类型
 *
 * @author Sdy
 */
@Service
@RequiredArgsConstructor
public class SysDictDataServiceImpl extends BaseServiceImpl<SysDictDataDao, SysDictDataEntity> implements SysDictDataService {
    private final SysDictTypeDao sysDictTypeDao;

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

        return getPageData(page, SysDictDataDTO.class);
    }

    private QueryWrapper<SysDictDataEntity> getWrapper(Map<String, Object> params) {
        Long dictTypeId = Long.parseLong((String) params.get("dictTypeId"));
        String dictLabel = (String) params.get("dictLabel");
        String dictValue = (String) params.get("dictValue");

        QueryWrapper<SysDictDataEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type_id", dictTypeId);
        wrapper.like(StrUtil.isNotBlank(dictLabel), "dict_label", dictLabel);
        wrapper.like(StrUtil.isNotBlank(dictValue), "dict_value", dictValue);

        return wrapper;
    }

    @Override
    public SysDictDataDTO get(Long id) {
        SysDictDataEntity entity = baseDao.selectById(id);

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

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);

        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysDictDataDTO dto) {
        SysDictDataEntity entity = ConvertUtils.sourceToTarget(dto, SysDictDataEntity.class);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //删除
        deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<SysDictDataDTO> getDictDataByType(String type) {
        SysDictTypeEntity typeEntity = sysDictTypeDao.selectOne(new QueryWrapper<SysDictTypeEntity>().eq("dict_type", type));
        if (typeEntity != null) {
            List<SysDictDataEntity> dataList = baseDao.selectList(new QueryWrapper<SysDictDataEntity>()
                    .eq("dict_type_id", typeEntity.getId()).orderByAsc("sort"));
            for (SysDictDataEntity dictDataEntity : dataList) {
                String dictValue = dictDataEntity.getDictValue();
                if (BusinessConstant.DJI_SIGN.equals(dictValue)) {
                    String imageUrl = DJIImage.getImageUrlByName(BusinessConstant.DJI_SIGN);
                    dictDataEntity.setImageUrl(BusinessConstant.IMAGE_PATH + imageUrl);
                } else {
                    String imageUrl = dictDataEntity.getImageUrl();
                    if (StrUtil.isNotBlank(imageUrl)) {
                        dictDataEntity.setImageUrl(BusinessConstant.IMAGE_PATH + imageUrl);
                    }
                }
            }
            return ConvertUtils.sourceToTarget(dataList, SysDictDataDTO.class);
        }

        return List.of();
    }
}