package com.zbkj.service.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.constants.Constants;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.article.Article;
import com.zbkj.common.model.category.Category;
import com.zbkj.common.model.system.SystemHelpProblem;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.*;
import com.zbkj.common.response.ArticleResponse;
import com.zbkj.common.utils.RedisUtil;
import com.zbkj.common.vo.ArticleVo;
import com.zbkj.service.dao.ArticleDao;
import com.zbkj.service.dao.HelpDao;
import com.zbkj.service.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
* ArticleServiceImpl 接口实现

*/
@Service
public class HelpServiceImpl extends ServiceImpl<HelpDao, SystemHelpProblem> implements HelpService {

    private Logger logger = LoggerFactory.getLogger(HelpServiceImpl.class);

    @Resource
    private HelpDao dao;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SystemAttachmentService systemAttachmentService;

    @Override
    public List<SystemHelpProblem> getListHelp(String cid, PageParamRequest page) {
        PageHelper.startPage(page.getPage(), page.getLimit());
        return dao.getListHelp(cid);
    }

    /**
     * 获取分类列表(移动端)
     * @return List<Category>
     */
    @Override
    public List<Category> getCategoryList() {
        return categoryService.findCategoryList(Constants.CATEGORY_TYPE_HELP);
    }

    /**
     * 新增
     * @param request 新增参数
     * @return Boolean
     */
    @Override
    public Boolean create(HelpRequest request) {
        SystemHelpProblem entity = JSON.parseObject(JSON.toJSONString(request), SystemHelpProblem.class);
        entity.setImage(systemAttachmentService.clearPrefix(request.getImage()));
        entity.setContent(systemAttachmentService.clearPrefix(request.getContent()));
        return save(entity);
    }

    /**
     * 删除
     * @param id id
     * @return Boolean
     */
    @Override
    public Boolean deleteById(Integer id) {
        SystemHelpProblem entity = getById(id);
        if (ObjectUtil.isNull(entity)) {
            throw new CrmebException("已删除");
        }
        return removeById(id);
    }

    /**
     * 修改
     * @param id id
     * @param request 修改参数
     */
    @Override
    public Boolean updateHelp(Integer id, HelpRequest request) {
        SystemHelpProblem entity = JSON.parseObject(JSON.toJSONString(request), SystemHelpProblem.class);
        entity.setId(id);
        entity.setImage(systemAttachmentService.clearPrefix(request.getImage()));
        entity.setContent(systemAttachmentService.clearPrefix(request.getContent()));
        return updateById(entity);
    }

    /**
     * 获取详情
     * @param id 文章id
     * @return Article
     */
    @Override
    public SystemHelpProblem getDetail(Integer id) {
        SystemHelpProblem entity = getById(id);
        if (ObjectUtil.isNull(entity)) {
            throw new CrmebException("不存在");
        }
        return entity;
    }

    @Override
    public Boolean feedback(Integer id, Integer type) {
        SystemHelpProblem entity = getById(id);
        if (ObjectUtil.isNull(entity)) {
            throw new CrmebException("不存在");
        }
        SystemHelpProblem upd = new SystemHelpProblem();
        upd.setId(id);
        if (type == 1) {
            upd.setUsefulNum(entity.getUsefulNum() + 1);
        } else {
            upd.setUselessNum(entity.getUselessNum() + 1);
        }
        return updateById(upd);
    }
}

