package com.crm.ucard.service.impl;

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.CardAppPrivacyPolicyDto;
import com.crm.rely.backend.core.entity.base.BaseDeleteEntities;
import com.crm.rely.backend.core.entity.ucard.CardAppPrivacyPolicyAddEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppPrivacyPolicyPageEntity;
import com.crm.rely.backend.core.entity.ucard.CardAppPrivacyPolicyUpdateEntity;
import com.crm.rely.backend.core.pojo.table.ucard.CardAppPrivacyPolicyTable;
import com.crm.rely.backend.exception.ServiceException;
import com.crm.ucard.dao.mapper.CardAppPrivacyPolicyMapper;
import com.crm.ucard.dao.repository.CardAppPrivacyPolicyRepository;
import com.crm.ucard.service.UcardAppPrivacyPolicyService;
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 UcardAppPrivacyPolicyServiceImpl implements UcardAppPrivacyPolicyService {

    @Autowired
    private CardAppPrivacyPolicyMapper appPrivacyPolicyMapper;
    @Autowired
    private CardAppPrivacyPolicyRepository appPrivacyPolicyRepository;

    /**
     * app隐私政策列表
     */
    @Override
    public ResultWithPagerDto<CardAppPrivacyPolicyDto> getCardAppPrivacyPolicyPageList(CardAppPrivacyPolicyPageEntity entity) throws ServiceException {
        try {
            // 获取总数
            Integer count = appPrivacyPolicyMapper.countList(entity);
            PageDto pageDto = PageDto.format(entity, count);
            if (count == null || count <= 0) {
                return ResultWithPagerDto.success(pageDto);
            }
            List<CardAppPrivacyPolicyDto> dtos = appPrivacyPolicyMapper.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 addCardAppPrivacyPolicy(CardAppPrivacyPolicyAddEntity entity) throws ServiceException {
        CardAppPrivacyPolicyTable table = appPrivacyPolicyRepository.findFirstByType(entity.getType());
        if (table == null){
            table = new CardAppPrivacyPolicyTable();
        }
        BeanUtils.copyProperties(entity, table);
        appPrivacyPolicyRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 修改app隐私政策
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto updateCardAppPrivacyPolicy(CardAppPrivacyPolicyUpdateEntity entity) throws ServiceException {
        CardAppPrivacyPolicyTable table = appPrivacyPolicyRepository.findFirstById(entity.getId());
        if (table != null && !table.getType().equals(entity.getType())){
            throw ServiceException.exception("该类型已存在");
        }
        if (table == null) {
            table = new CardAppPrivacyPolicyTable();
        }
        BeanUtils.copyProperties(entity, table);
        appPrivacyPolicyRepository.save(table);
        return BaseResultDto.success();
    }

    /**
     * 批量删除app隐私政策
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public BaseResultDto deleteCardAppPrivacyPolicy(BaseDeleteEntities entity) throws ServiceException {
        List<CardAppPrivacyPolicyTable> tables = Lists.newArrayList();
        for (Long id : entity.getIds()) {
            CardAppPrivacyPolicyTable table = new CardAppPrivacyPolicyTable();
            table.setId(id);
            tables.add(table);
        }
        appPrivacyPolicyRepository.deleteInBatch(tables);
        return BaseResultDto.success();
    }


}
