package cn.ysk.cashier.service.impl;

import cn.ysk.cashier.dto.TbVersionDto;
import cn.ysk.cashier.dto.TbVersionQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mapper.TbVersionMapper;
import cn.ysk.cashier.pojo.TbVersion;
import cn.ysk.cashier.repository.TbVersionRepository;
import cn.ysk.cashier.service.TbVersionService;
import cn.ysk.cashier.utils.*;
import lombok.RequiredArgsConstructor;
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;

/**
 * @author ww
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2024-03-29
 **/
@Service
@RequiredArgsConstructor
public class TbVersionServiceImpl implements TbVersionService {

    private final TbVersionRepository tbVersionRepository;
    private final TbVersionMapper tbVersionMapper;

    private final RedisUtils redisUtils;

    @Override
    public Map<String, Object> queryAllPage(TbVersionQueryCriteria criteria) {
        Pageable pageable = PageRequest.of(criteria.getPage(), criteria.getPageSize(), Sort.by(Sort.Direction.DESC, "createdAt"));
        Page<TbVersion> page = tbVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        return PageUtil.toPage(page.map(tbVersionMapper::toDto));
    }

    @Override
    public List<TbVersionDto> queryAll(TbVersionQueryCriteria criteria) {
        return tbVersionMapper.toDto(tbVersionRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    @Override
    @Transactional
    public TbVersionDto findById(Integer id) {
        TbVersion tbVersion = tbVersionRepository.findById(id).orElseGet(TbVersion::new);
        ValidationUtil.isNull(tbVersion.getId(), "TbVersion", "id", id);
        return tbVersionMapper.toDto(tbVersion);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TbVersionDto create(TbVersion resources) {
        int exist = tbVersionRepository.isExist(resources.getSource(),  resources.getVersion());
        if (exist > 0) {
            throw new BadRequestException("该版本已存在。");
        }
        resources.setCreatedAt(Instant.now().toEpochMilli());
        TbVersionDto dto = tbVersionMapper.toDto(tbVersionRepository.save(resources));
//        if (dto.getIsUp() == 1) {
//            //产品标识：型号：版本
//            //VERSION:PC::version   存在即需要强制更新
//            redisUtils.set(CacheKey.VERSION + dto.getSource() + ":" + dto.getVersion(), dto);
//        }
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TbVersion resources) {
        TbVersion tbVersion = tbVersionRepository.findById(resources.getId()).orElseGet(TbVersion::new);
        ValidationUtil.isNull(tbVersion.getId(), "TbVersion", "id", resources.getId());
//        redisUtils.del(tbVersion.getSource() + "_VERSION:" + tbVersion.getType() + ":" + tbVersion.getVersion());
        tbVersion.copy(resources);
        tbVersion.setUpdatedAt(Instant.now().toEpochMilli());
        tbVersionRepository.save(tbVersion);
//        if (resources.getIsUp() == 1) {
//            //产品标识：型号：版本
//            //LDBL_APP_VERSION:ios:version   存在即需要强制更新
//            redisUtils.set(tbVersion.getSource() + "_VERSION:" + tbVersion.getType() + ":" + tbVersion.getVersion(), tbVersion.getMessage());
//        } else {
//            redisUtils.del(tbVersion.getSource() + "_VERSION:" + tbVersion.getType() + ":" + tbVersion.getVersion());
//        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSel(TbVersion resources) {
        tbVersionRepository.updateSelBySource(resources.getSource(),resources.getId());
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            TbVersion tbVersion = tbVersionRepository.findById(id).orElseGet(TbVersion::new);
            ValidationUtil.isNull(tbVersion.getId(), "TbVersion", "id", id);
            tbVersionRepository.deleteById(id);
//            if (tbVersion.getIsUp() == 1) {
//                redisUtils.del(tbVersion.getSource() + "_VERSION:" + tbVersion.getType() + ":" + tbVersion.getVersion());
//            }
        }
    }

}