package com.yfd.platform.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfd.platform.system.domain.SysDictionary;
import com.yfd.platform.system.domain.SysDictionaryItems;
import com.yfd.platform.system.mapper.SysDictionaryItemsMapper;
import com.yfd.platform.system.mapper.SysDictionaryMapper;
import com.yfd.platform.system.service.ISysDictionaryItemsService;
import com.yfd.platform.utils.FileUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 数据字典明细 服务实现类
 * </p>
 *
 * @author LiMengNan
 * @since 2023-03-08
 */
@Service
public class SysDictionaryItemsServiceImpl extends ServiceImpl<SysDictionaryItemsMapper, SysDictionaryItems> implements ISysDictionaryItemsService {

    @Resource
    private SysDictionaryItemsMapper sysDictionaryItemsMapper;

    @Resource
    private SysDictionaryMapper sysDictionaryMapper;
    /**********************************
     * 用途说明: 分页查询字典项信息
     * 参数说明  dictID 字典ID ItemName 字典项名称  pageNum 当前页
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回分页查询结果
     ***********************************/
    @Override
    public Page<SysDictionaryItems> getDictItemPage(String dictId,
                                                    String itemName,
                                                    Page<SysDictionaryItems> page) {
        LambdaQueryWrapper<SysDictionaryItems> queryWrapper =
                new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(itemName)) {
            queryWrapper.like(SysDictionaryItems::getDictName, itemName);
        }
        queryWrapper.eq(SysDictionaryItems::getDictId, dictId).orderByAsc(SysDictionaryItems::getOrderNo);
        return sysDictionaryItemsMapper.selectPage(page, queryWrapper);
    }

    /**********************************
     * 用途说明: 增加字典项
     * 参数说明  sysDictionaryItems 字典项信息
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回增加成功或者失败
     ***********************************/
    @Override
    public boolean addDictionaryItem(SysDictionaryItems sysDictionaryItems) {
        LambdaQueryWrapper<SysDictionaryItems> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionaryItems::getDictId,sysDictionaryItems.getDictId());
        int orderNo = this.count(queryWrapper) + 1;
        // 添加顺序号
        sysDictionaryItems.setOrderNo(orderNo);
        return this.save(sysDictionaryItems);
    }

    /**********************************
     * 用途说明: 拖动修改字典项顺序
     * 参数说明  fromID 当前ID  toID 到达ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回拖动成功或者失败
     ***********************************/
    @Override
    public boolean changeItemOrder(String fromID, String toID) {
        SysDictionaryItems fromSysDictionaryItems =
                sysDictionaryItemsMapper.selectById(fromID);
        SysDictionaryItems toSysDictionaryItems =
                sysDictionaryItemsMapper.selectById(toID);
        // 如果数据字典项不存在拖动失败
        if (fromSysDictionaryItems == null || toSysDictionaryItems == null) {
            return false;
        }
        Integer fromOrderNo = fromSysDictionaryItems.getOrderNo();
        Integer toOrderNo = toSysDictionaryItems.getOrderNo();
        // 如果数据字典的顺序号不存在拖动失败
        if (fromOrderNo == null || toOrderNo == null) {
            return false;
        }
        // 将顺序号放入字典对象中
        fromSysDictionaryItems.setOrderNo(toOrderNo);
        toSysDictionaryItems.setOrderNo(fromOrderNo);
        // 更改顺序号
        boolean fromBool = this.updateById(fromSysDictionaryItems);
        boolean toBool = this.updateById(toSysDictionaryItems);
        return fromBool && toBool;

    }

    /**********************************
     * 用途说明: 导出数据字典项数据
     * 参数说明  sysDictionaryItemsList  所需导出的字典项集合
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回导出成功或失败
     ***********************************/
    @Override
    public void exportExcel(List<SysDictionaryItems> sysDictionaryItems,
                            HttpServletResponse response) {
        try {
            List<Map<String, Object>> list = new LinkedList<>();
            for (SysDictionaryItems sysDictionaryItem : sysDictionaryItems) {
                Map<String, Object> map = new LinkedHashMap<>();
                map.put("项编号", sysDictionaryItem.getItemCode());
                map.put("项名称", sysDictionaryItem.getDictName());
                map.put("父编码", sysDictionaryItem.getParentCode());
                map.put("备注", sysDictionaryItem.getCustom1());
                list.add(map);
            }
            FileUtil.downloadExcel(list, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**********************************
     * 用途说明: 获取类型
     * 参数说明 dictcode
     * 返回值说明: java.util.List<java.util.Map<java.lang.String,java.lang.Object>>
     ***********************************/
    @Override
    public List<Map<String, Object>> getDeviceByType(String dictcode) {
        SysDictionary sysDictionary =
                sysDictionaryMapper.selectOne(new LambdaQueryWrapper<SysDictionary>().eq(SysDictionary::getDictCode,
                        dictcode));
        String id = sysDictionary.getId();
        LambdaQueryWrapper<SysDictionaryItems> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionaryItems::getDictId, id);
        queryWrapper.select(SysDictionaryItems::getParentCode,SysDictionaryItems::getId, SysDictionaryItems::getItemCode,
                SysDictionaryItems::getDictName,SysDictionaryItems::getCustom1, SysDictionaryItems::getCustom2,SysDictionaryItems::getOrderNo);
        List<Map<String, Object>> maps = sysDictionaryItemsMapper.selectMaps(queryWrapper);
        List<Map<String, Object>> itemCode = maps.stream().sorted(Comparator.comparing(m -> Integer.valueOf(m.get(
                "orderno").toString()))).collect(Collectors.toList());
        return itemCode;
    }


}
