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

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
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.ISysDictionaryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 数据字典表 服务实现类
 * </p>
 *
 * @author LiMengNan
 * @since 2023-03-08
 */
@Service
public class SysDictionaryServiceImpl extends ServiceImpl<SysDictionaryMapper
        , SysDictionary> implements ISysDictionaryService {

    @Resource
    private SysDictionaryMapper sysDictionaryMapper;

    @Resource
    private SysDictionaryItemsMapper sysDictionaryItemsMapper;

    /**********************************
     * 用途说明: 获取数据字典列表
     * 参数说明  dictType 字典类型
     * 返回值说明: 返回字典列表集合
     ***********************************/
    @Override
    public List<SysDictionary> getDictList(String dictType) {
        LambdaQueryWrapper<SysDictionary> queryWrapper =
                new LambdaQueryWrapper<>();
        queryWrapper.eq(SysDictionary::getDictType, dictType).orderByAsc(SysDictionary::getOrderNo);
        return sysDictionaryMapper.selectList(queryWrapper);
    }

    /**********************************
     * 用途说明: 新增字典
     * 参数说明  sysDictionary 字典对象
     * 返回值说明: 返回增加成功或者失败
     ***********************************/
    @Override
    public boolean addDict(SysDictionary sysDictionary) {
        //int orderNo = this.count() + 1;
        Integer maxNo =
                sysDictionaryMapper.selectMaxNo(sysDictionary.getDictType());
        if (maxNo == null) {
            maxNo = 0;
        }
        // 添加顺序号
        sysDictionary.setOrderNo(maxNo + 1);
        return this.save(sysDictionary);
    }

    /**********************************
     * 用途说明: 根据ID删除字典
     * 参数说明  id 字典ID
     * 返回值说明: com.yfd.platform.config.ResponseResult 返回删除结果成功或者失败
     ***********************************/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteDictById(String id) {
        // 根据字典编码查询字典项中是否关联
        boolean isok=true;
        QueryWrapper<SysDictionaryItems> Wrapper = new QueryWrapper<>();
        Wrapper.eq("dictid", id);
        if(sysDictionaryItemsMapper.delete(Wrapper)>0) {
            isok=true;
        }
        return isok&&this.removeById(id);
    }

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

}
