package com.crm.ucard.service.impl;

import com.crm.rely.backend.core.constant.Constants;
import com.crm.rely.backend.core.dto.base.BaseResultDto;
import com.crm.rely.backend.core.dto.base.PageDto;
import com.crm.rely.backend.core.dto.base.ResultWithPagerDto;
import com.crm.rely.backend.core.dto.ucard.CardAppVersionDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardAppVersionAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppVersionDetailEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppVersionPageEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppVersionUpdateEntity;
import com.crm.rely.backend.core.pojo.table.ucard.CardAppVersionTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.mapper.CardAppVersionMapper;
import com.crm.ucard.dao.repository.CardAppVersionRepository;
import com.crm.ucard.service.UcardAppVersionService;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
public class UcardAppVersionServiceImpl implements UcardAppVersionService {

    @Autowired
    private CardAppVersionMapper appVersionMapper;
    @Autowired
    private CardAppVersionRepository appVersionRepository;

    /**
     * app版本配置列表
     */
    @Override
    public ResultWithPagerDto<CardAppVersionDto> getCardAppVersionPageList(CardAppVersionPageEntity entity) throws ServiceException {
        try {
            // 获取总数
            Integer count = appVersionMapper.countList(entity);
            PageDto pageDto = PageDto.format(entity, count);
            if (count == null || count <= 0) {
                return ResultWithPagerDto.success(pageDto);
            }
            List<CardAppVersionDto> dtos = appVersionMapper.pageList(entity);
            return ResultWithPagerDto.success(pageDto, dtos);
        } catch (Exception e) {
            log.error("获取app版本配置列表失败", e);
            throw new ServiceException("获取app版本配置列表失败");
        }
    }

    /**
     * 新增app版本配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto addCardAppVersion(CardAppVersionAddEntity entity) throws ServiceException {
        CardAppVersionTable table = new CardAppVersionTable();
        BeanUtils.copyProperties(entity, table);
        appVersionRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 修改app版本配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto updateCardAppVersion(CardAppVersionUpdateEntity entity) throws ServiceException {
        CardAppVersionTable table = appVersionRepository.findFirstById(entity.getId());
        if (table == null) {
            throw ServiceException.exception(Constants.INFO_NOT_FOUND);
        }
        BeanUtils.copyProperties(entity, table);
        appVersionRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 批量删除app版本配置
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto deleteCardAppVersion(BaseDeleteEntities entity) throws ServiceException {
        List<CardAppVersionTable> tables = Lists.newArrayList();
        for (Long id : entity.getIds()) {
            CardAppVersionTable table = new CardAppVersionTable();
            table.setId(id);
            tables.add(table);
        }
        appVersionRepository.deleteInBatch(tables);
        return BaseResultDto.success();
    }

    /**
     * 获取最新一条版本配置
     */
    @Override
    public BaseResultDto getLatestCardAppVersion(CardAppVersionDetailEntity entity) throws ServiceException {
        CardAppVersionTable table = appVersionRepository.findFirstByEquipmentTypeOrderByAddTimeDesc(entity.getEquipmentType());
        if (table == null){
            return BaseResultDto.success();
        }
        return BaseResultDto.success(table);
    }
}
