package com.liquidnet.service.sweet.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.entity.SweetManualSortTFC;
import com.liquidnet.service.sweet.mapper.SweetManualSortTFCMapper;
import com.liquidnet.service.sweet.service.ISweetManualSortTFCService;
import com.liquidnet.service.sweet.utils.RedisTFCDataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 电子宣传手册显示内容表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-08-09
 */
@Service
public class SweetManualSortTFCServiceImpl extends ServiceImpl<SweetManualSortTFCMapper, SweetManualSortTFC> implements ISweetManualSortTFCService {

    @Autowired
    private SweetManualSortTFCMapper sweetManualSortTFCMapper;
    @Autowired
    private RedisTFCDataUtils redisDataUtils;

    @Override
    public ResponseDto<SweetManualSortTFC> get(String manualId) {
        try {
            return ResponseDto.success(sweetManualSortTFCMapper.selectOne(Wrappers.lambdaQuery(SweetManualSortTFC.class).eq(SweetManualSortTFC::getManualId, manualId)));
        } catch (Exception e) {
            return ResponseDto.failure();
        }
    }

    @Override
    public ResponseDto<Boolean> add(String manualId, String content) {
        try {
            int count = sweetManualSortTFCMapper.selectCount(Wrappers.lambdaUpdate(SweetManualSortTFC.class).eq(SweetManualSortTFC::getManualId, manualId));
            if (count > 0) {
                SweetManualSortTFC sweetManualSort = SweetManualSortTFC.getNew();
                sweetManualSort.setManualId(manualId);
                sweetManualSort.setShowPosition(content);
                sweetManualSort.setUpdatedAt(LocalDateTime.now());
                sweetManualSortTFCMapper.update(sweetManualSort, Wrappers.lambdaUpdate(SweetManualSortTFC.class).eq(SweetManualSortTFC::getManualId, manualId));
            } else {
                SweetManualSortTFC sweetManualSort = SweetManualSortTFC.getNew();
                sweetManualSort.setManualId(manualId);
                sweetManualSort.setShowPosition(content);
                sweetManualSort.setCreatedAt(LocalDateTime.now());
                sweetManualSortTFCMapper.insert(sweetManualSort);
            }
            redisDataUtils.deleteSortRedisData(manualId);
        } catch (Exception e) {
            return ResponseDto.failure();
        }
        return ResponseDto.success();
    }

}
