package com.zm.order.tool.server.service.impl;

import com.zm.order.tool.server.constants.MapConst1;
import com.zm.order.tool.server.dao.OrderCommentCountDao;
import com.zm.order.tool.server.dao.OrderCommentDao;
import com.zm.order.tool.server.model.OrderComment;
import com.zm.order.tool.server.model.OrderCommentCount;
import com.zm.order.tool.server.service.ExceptionConstants;
import com.zm.order.tool.server.service.OrderCommentService;
import com.zm.util.dto.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * @author nianchang
 * @ClassName OrderCommentServiceImpl
 * @date 2021/08/30 2:14 下午
 */
@Service
@SuppressWarnings("all")
public class OrderCommentServiceImpl implements OrderCommentService {

    private static final Logger logger = LoggerFactory.getLogger(OrderCommentServiceImpl.class);

    @Autowired
    private OrderCommentDao commentDao;

    @Autowired
    private OrderCommentCountDao commentCountDao;

    @Override
    public Result updateComment() {
        int total = 0;
        try {
//            Set<Map.Entry<String, Long>> entries = MapConst.map.entrySet();
//            for (Map.Entry<String, Long> entry : entries) {
//                OrderComment orderComment = commentDao.selectOne(entry.getKey(), entry.getValue());
//                if (orderComment != null) {
//                    if (Objects.equals(orderComment.getCommentImage(), "[\"[]\"]")) {
//                        OrderComment update = new OrderComment();
//                        update.setId(orderComment.getId());
//                        update.setSellerId(orderComment.getSellerId());
//                        update.setCommentImage("[]");
//                        update.setExistImage(0);
//                        commentDao.update(update);
//
//                        OrderCommentCount count = commentCountDao.selectOne(orderComment.getSellerId(), orderComment.getStoreId());
//                        OrderCommentCount updateCount = new OrderCommentCount();
//                        updateCount.setId(count.getId());
//                        updateCount.setImageCount(count.getImageCount() > 0 ? count.getImageCount() - 1 : 0);
//                        commentCountDao.update(updateCount);
//                        total ++;
//                    }
////                    if (count.getBadCount() > 0) {
////                        updateCount.setBadCount(count.getBadCount() - 1);
////                    }
//                }
//            }

            Set<Map.Entry<String, Long>> entries1 = MapConst1.map.entrySet();
            for (Map.Entry<String, Long> entry : entries1) {
                OrderComment orderComment = commentDao.selectOne(entry.getKey(), entry.getValue());
                if (orderComment != null) {
                    if (Objects.equals(orderComment.getCommentImage(), "[\"[]\"]")) {
                        OrderComment update = new OrderComment();
                        update.setId(orderComment.getId());
                        update.setSellerId(orderComment.getSellerId());
                        update.setCommentImage("[]");
                        update.setExistImage(0);
                        commentDao.update(update);

                        OrderCommentCount count = commentCountDao.selectOne(orderComment.getSellerId(), orderComment.getStoreId());
                        OrderCommentCount updateCount = new OrderCommentCount();
                        updateCount.setId(count.getId());
                        updateCount.setImageCount(count.getImageCount() > 0 ? count.getImageCount() - 1 : 0);
                        commentCountDao.update(updateCount);
                        total ++;
                    }
//                    if (count.getBadCount() > 0) {
//                        updateCount.setBadCount(count.getBadCount() - 1);
//                    }
                }
            }
            logger.info("updateComment_count:{}", total);
        } catch (Exception e) {
            logger.error("TemporaryController[OrderCommentServiceImpl]failed,cause by: {}", e);
            return Result.failureData(ExceptionConstants.SYSTEM_ERROR);
        }
        return Result.successNullData();
    }
}
