package cn.ysk.cashier.service.impl;

import cn.ysk.cashier.dto.TbPlatformDictDto;
import cn.ysk.cashier.dto.TbPlatformDictQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mapper.TbPlatformDictMapper;
import cn.ysk.cashier.pojo.TbPlatformDict;
import cn.ysk.cashier.repository.TbPlatformDictRepository;
import cn.ysk.cashier.repository.TbPlatformDictTypeRepository;
import cn.ysk.cashier.service.TbPlatformDictService;
import cn.ysk.cashier.utils.PageUtil;
import cn.ysk.cashier.utils.QueryHelp;
import cn.ysk.cashier.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Instant;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbPlatformDictServiceImpl implements TbPlatformDictService {
    private final TbPlatformDictRepository tbPlatformDictRepository;
    private final TbPlatformDictTypeRepository dictTypeRepository;
    private final TbPlatformDictMapper tbPlatformDictMapper;

    @Override
    public Map<String,Object> queryAllPage(TbPlatformDictQueryCriteria criteria){
//        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getPageSize(), Sort.by("type").and(Sort.by("sort")));
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getPageSize(), Sort.by("type","sort"));
        Page<TbPlatformDict> page = tbPlatformDictRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        Page<TbPlatformDictDto> map = page.map(tbPlatformDictMapper::toDto);
        for (TbPlatformDictDto tbPlatformDictDto : map.getContent()) {
            if (StringUtils.isNotBlank(tbPlatformDictDto.getType())) {
                tbPlatformDictDto.setType(dictTypeRepository.queryNameByKey(tbPlatformDictDto.getType()));
            }
        }
        return PageUtil.toPage(map);
    }

    @Override
    public List<TbPlatformDictDto> queryAll(TbPlatformDictQueryCriteria criteria){
        return tbPlatformDictMapper.toDto(tbPlatformDictRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<TbPlatformDictDto> queryByType(String type){
        return tbPlatformDictMapper.toDto(tbPlatformDictRepository.findAllByTypeAndIsShowCash(type,1));
    }

    @Override
    public List<TbPlatformDictDto> queryByIds(List ids){
        return tbPlatformDictRepository.findAllById(ids);
    }

    @Override
    @Transactional
    public TbPlatformDictDto findById(Integer id) {
        TbPlatformDict tbPlatformDict = tbPlatformDictRepository.findById(id).orElseGet(TbPlatformDict::new);
        ValidationUtil.isNull(tbPlatformDict.getId(),"TbPlatformDict","id",id);
        TbPlatformDictDto dto = tbPlatformDictMapper.toDto(tbPlatformDict);
        if (StringUtils.isNotBlank(dto.getType())) {
            dto.setType(dictTypeRepository.queryNameByKey(dto.getType()));
        }
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbPlatformDictDto create(TbPlatformDict resources) {
        if (StringUtils.isBlank(resources.getType())) {
            throw new BadRequestException("类型不可为空。");
        }
        if (StringUtils.isBlank(resources.getName())) {
            throw new BadRequestException("名称不可为空。");
        }
        int exist = tbPlatformDictRepository.isExist(resources.getType(), resources.getName());
        if (exist > 0) {
            throw new BadRequestException("该类型下已存在该名称内容。");
        }
        resources.setCreatedAt(Instant.now().toEpochMilli());
        return tbPlatformDictMapper.toDto(tbPlatformDictRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbPlatformDict resources) {
        TbPlatformDict tbPlatformDict = tbPlatformDictRepository.findById(resources.getId()).orElseGet(TbPlatformDict::new);
        String type = tbPlatformDict.getType();
        resources.setUpdatedAt(Instant.now().toEpochMilli());
        ValidationUtil.isNull( tbPlatformDict.getId(),"TbPlatformDict","id",resources.getId());
        tbPlatformDict.copy(resources);
        tbPlatformDict.setType(type);
        tbPlatformDictRepository.save(tbPlatformDict);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            tbPlatformDictRepository.deleteById(id);
        }
    }

}
