package com.app.service.platform;

import com.app.dto.PageResult;
import com.app.dto.platform.SearchDictDto;
import com.app.dto.platform.SearchDictEntryDto;
import com.app.entity.platform.system.Dict;
import com.app.entity.platform.system.DictEntry;
import com.app.mapper.platform.DictEntryMapper;
import com.app.mapper.platform.DictMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

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

@Service
public class DictService {

    @Resource
    private DictMapper dictMapper;

    @Resource
    private DictEntryMapper dictEntryMapper;


    /**
     * 保存或更新【字典】
     *
     */
    @Transactional
    public Dict saveOrUpdate(Dict dict) {
        Dict _dict = null;
        if (StringUtils.isEmpty(dict.getId())) {
            Integer count = dictMapper.selectCount(new QueryWrapper<Dict>().lambda().eq(Dict::getName, dict.getName())
                    .eq(Dict::getIsDel,Boolean.FALSE));
            if(count!=0){
                return null;
            }
            _dict = new Dict();
            _dict.setName(dict.getName());
            _dict.setRemarks(dict.getRemarks());
            dictMapper.insert(_dict);
        } else {
            Optional<Dict> newsOptional = Optional.ofNullable(dictMapper.selectById(dict.getId()));
            if (newsOptional.isPresent()) {
                _dict = newsOptional.get();
                Integer count = dictMapper.selectCount(new QueryWrapper<Dict>().lambda().ne(Dict::getId, _dict.getId())
                        .eq(Dict::getName, dict.getName()).eq(Dict::getIsDel,Boolean.FALSE));
                if(count!=0){
                    return null;
                }
                _dict.setRemarks(dict.getRemarks());
                _dict.setName(dict.getName());
                dictMapper.updateById(_dict);
            }
        }
        return _dict;
    }


    /**
     * 查询【字典】分页
     */
    public PageResult<Dict> page(SearchDictDto dto) {
        LambdaQueryWrapper<Dict> wrapper = new QueryWrapper<Dict>().lambda();
        if(!StringUtils.isEmpty(dto.getName())){
            wrapper.like(Dict::getName,dto.getName());
        }
        if(dto.getIsDel()==null){
            dto.setIsDel(false);
        }
        wrapper.eq(Dict::getIsDel,dto.getIsDel());
        Page<Dict> page = dictMapper.selectPage(new Page<>(dto.getPage(), dto.getSize()), wrapper);
        return new PageResult<>(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
    }

    /**
     * 删除【字典】
     */
    public void handleIsDel(String id) {
        Optional<Dict> optional = Optional.ofNullable(dictMapper.selectById(id));
        if (optional.isPresent()) {
            Dict dict = optional.get();
            dict.setIsDel(!dict.getIsDel());
            dictMapper.updateById(dict);
        }
    }

    /**
     * 查询【部门实体】
     */
    public List<DictEntry> entryList(SearchDictEntryDto params) {
        LambdaQueryWrapper<DictEntry> wrapper = new QueryWrapper<DictEntry>().lambda();
        wrapper.eq(DictEntry::getDictId,params.getDictId());
        wrapper.eq(DictEntry::getIsDel,params.getIsDel());
        wrapper.orderByDesc(DictEntry::getSort);
        return dictEntryMapper.selectList(wrapper);
    }

    /**
     * 更新【部门实体】
     */
    public DictEntry entrySaveOrUpdate(DictEntry entry) {
        DictEntry _dictEntry = null;
        if (StringUtils.isEmpty(entry.getId())) {
            Integer count = dictEntryMapper.selectCount(new QueryWrapper<DictEntry>().lambda()
                    .eq(DictEntry::getName, entry.getName()).eq(DictEntry::getIsDel,Boolean.FALSE)
                    .eq(DictEntry::getDictId,entry.getDictId()));
            if(count!=0){
                return null;
            }
            _dictEntry = new DictEntry();
            _dictEntry.setName(entry.getName());
            _dictEntry.setDictId(entry.getDictId());
            _dictEntry.setValue(entry.getValue());
            _dictEntry.setSort(entry.getSort());
            dictEntryMapper.insert(_dictEntry);
        } else {
            Optional<DictEntry> newsOptional = Optional.ofNullable(dictEntryMapper.selectById(entry.getId()));
            if (newsOptional.isPresent()) {
                _dictEntry = newsOptional.get();
                Integer count = dictEntryMapper.selectCount(new QueryWrapper<DictEntry>().lambda()
                        .ne(DictEntry::getId, entry.getId()).eq(DictEntry::getName, entry.getName())
                        .eq(DictEntry::getIsDel,Boolean.FALSE).eq(DictEntry::getDictId,entry.getDictId()));
                if(count!=0){
                    return null;
                }
                _dictEntry.setName(entry.getName());
                _dictEntry.setDictId(entry.getDictId());
                _dictEntry.setValue(entry.getValue());
                _dictEntry.setSort(entry.getSort());
                dictEntryMapper.updateById(_dictEntry);
            }
        }
        return _dictEntry;
    }

    /**
     * 删除【部门实体】
     */
    public void entryHandleIsDel(String id) {
        Optional<DictEntry> optional = Optional.ofNullable(dictEntryMapper.selectById(id));
        if (optional.isPresent()) {
            DictEntry dictEntry = optional.get();
            dictEntry.setIsDel(!dictEntry.getIsDel());
            dictEntryMapper.updateById(dictEntry);
        }
    }
}
