package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.product.StoreProductProblem;
import com.zbkj.common.model.user.User;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.CommitProblemRequest;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.response.ProductProblemResponse;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.service.dao.StoreProductProblemDao;
import com.zbkj.service.service.StoreProductProblemService;
import com.zbkj.service.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StoreProductProblemServiceImpl extends ServiceImpl<StoreProductProblemDao, StoreProductProblem> implements StoreProductProblemService {

    @Resource private StoreProductProblemDao dao;
    @Autowired private UserService userService;
    @Autowired private RedisUtil redisUtil;
    private static final String praise_key = "RESPONSE_PRAISE:";

    @Override
    public CommonPage<ProductProblemResponse> getResponseList(Integer productId, PageParamRequest page) {
        User user = userService.getInfo();
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<ProductProblemResponse> list = dao.getResponseList(productId);
        for (ProductProblemResponse vo : list) {
            Long praiseCount = redisUtil.getHashSize(praise_key + vo.getMajorId());
            vo.setDelFlag(user == null ? 0 : user.getUid().toString().equals(vo.getUserId()) ? 1 : 0);
            vo.setPraiseCount(null == praiseCount ? 0 : praiseCount.intValue());
            if (null != praiseCount && praiseCount > 0 && null != user) {
                Object o = redisUtil.hmGet(praise_key + vo.getMajorId(), user.getUid().toString());
                vo.setPraise(null == o ? 0 : 1);
            }
            vo.setUserId(null);
        }
        return CommonPage.restPage(list);
    }

    @Override
    public CommonPage<ProductProblemResponse> getSonResponseList(Integer majorId, PageParamRequest page) {
        User user = userService.getInfo();
        PageHelper.startPage(page.getPage(), page.getLimit());
        List<ProductProblemResponse> list = dao.getSonResponseList(majorId);
        for (ProductProblemResponse vo : list) {
            Long praiseCount = redisUtil.getHashSize(praise_key + vo.getMajorId());
            vo.setDelFlag(user == null ? 0 : user.getUid().toString().equals(vo.getUserId()) ? 1 : 0);
            vo.setPraiseCount(null == praiseCount ? 0 : praiseCount.intValue());
            if (null != praiseCount && praiseCount > 0 && null != user) {
                Object o = redisUtil.hmGet(praise_key + vo.getMajorId(), user.getUid().toString());
                vo.setPraise(null == o ? 0 : 1);
            }
            vo.setUserId(null);
        }
        return CommonPage.restPage(list);
    }

    @Override
    public Integer commitProblem(CommitProblemRequest request) {
        User user = userService.getInfo();
        Integer pId = 0;
        Integer topId = 0;
        String businessId = request.getBusinessId();
        // 说明不是一级评论
        if (StringUtil.isNotEmpty(request.getMajorId())) {
            StoreProductProblem problem = dao.selectById(Integer.parseInt(request.getMajorId()));
            if (null == problem) {
                throw new CrmebException("未找到您要回复的对象，可能对方已删除");
            }
            pId = problem.getId();
            businessId = problem.getBusinessId() + "";
            topId = (problem.getParentId() == 0L ? problem.getId() : problem.getTopId());
        } else if (StringUtil.isEmpty(businessId)) {
            throw new CrmebException("请选择您要提问的商品");
        }
        StoreProductProblem add = new StoreProductProblem();
        add.setBusinessId(Integer.parseInt(businessId));
        add.setUid(user.getUid());
        add.setContent(request.getContent());
        add.setParentId(pId);
        add.setIsDel(0);
        add.setTopId(topId);
        return dao.insert(add);
    }

    @Override
    public Integer delProblem(Integer majorId) {
        User user = userService.getInfo();
        StoreProductProblem problem = dao.selectById(majorId);
        if (null == problem) {
            throw new CrmebException("未查询到记录");
        }
        if (!problem.getUid().toString().equals(user.getUid().toString())) {
            throw new CrmebException("您无权限操作");
        }
        StoreProductProblem upd = new StoreProductProblem();
        upd.setId(problem.getId());
        upd.setIsDel(1);
        dao.updateById(upd);
        recursionDel(new ArrayList() {{ add(problem.getId()); }});
        return 1;
    }

    @Override
    public Integer praiseProblem(Integer majorId) {
        User user = userService.getInfo();
        Object o = redisUtil.hmGet(praise_key + majorId, user.getUid().toString());
        if (null != o) {
            redisUtil.hmDelete(praise_key + majorId, user.getUid().toString());
        } else {
            redisUtil.hmSet(praise_key + majorId, user.getUid().toString(), 1);
        }
        return 1;
    }

    private void recursionDel(List<Integer> ids) {
        if (ids.isEmpty()) { return; }
        LambdaQueryWrapper<StoreProductProblem> lq = new LambdaQueryWrapper<>();
        lq.select(StoreProductProblem::getId);
        lq.in(StoreProductProblem::getParentId, ids);
        List<StoreProductProblem> problems = dao.selectList(lq);
        for (StoreProductProblem problem : problems) {
            StoreProductProblem upd = new StoreProductProblem();
            upd.setId(problem.getId());
            upd.setIsDel(2);
            dao.updateById(upd);
        }
        ids = problems.stream().map(StoreProductProblem::getId).collect(Collectors.toList());
        recursionDel(ids);
    }
}
