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.constant.SweetManualPosition;
import com.liquidnet.service.sweet.entity.SweetManualSort;
import com.liquidnet.service.sweet.mapper.SweetManualSortMapper;
import com.liquidnet.service.sweet.service.ISweetManualSortService;
import com.liquidnet.service.sweet.utils.RedisDataUtils;
import com.liquidnet.service.sweet.vo.SweetManualSortOptionVo;
import com.liquidnet.service.sweet.vo.SweetManualSortVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 电子宣传手册显示内容表 服务实现类
 * </p>
 *
 * @author liquidnet
 * @since 2021-07-23
 */
@Service
public class SweetManualSortServiceImpl extends ServiceImpl<SweetManualSortMapper, SweetManualSort> implements ISweetManualSortService {

    @Autowired
    private SweetManualSortMapper sweetManualSortMapper;
    @Autowired
    private RedisDataUtils redisDataUtils;

    @Override
    public ResponseDto<SweetManualSortVo> get(String manualId) {
        try {
            SweetManualSort sort = sweetManualSortMapper.selectOne(
                    Wrappers.lambdaQuery(SweetManualSort.class).eq(SweetManualSort::getManualId, manualId));
            return ResponseDto.success(toVo(sort, manualId));
        } catch (Exception e) {
            return ResponseDto.failure();
        }
    }

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

    @Override
    public ResponseDto<List<SweetManualSortOptionVo>> options() {
        return ResponseDto.success(SweetManualPosition.allOptions());
    }

    private SweetManualSortVo toVo(SweetManualSort sort, String manualId) {
        SweetManualSortVo vo = new SweetManualSortVo();
        vo.setManualId(manualId);
        vo.setOptions(SweetManualPosition.allOptions());
        if (sort == null) {
            vo.setPositions(SweetManualPosition.parsePositions(null));
            return vo;
        }
        vo.setMid(sort.getMid());
        vo.setShowPosition(sort.getShowPosition());
        vo.setPositions(SweetManualPosition.parsePositions(sort.getShowPosition()));
        vo.setCreatedAt(sort.getCreatedAt());
        vo.setUpdatedAt(sort.getUpdatedAt());
        return vo;
    }
}
