package com.gree.mall.manager.logic.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.admin.AdminUserCom;
import com.gree.mall.manager.logic.common.CommonLogic;
import com.gree.mall.manager.plus.entity.CommentTag;
import com.gree.mall.manager.plus.entity.OrderComment;
import com.gree.mall.manager.plus.entity.OrderCommentTag;
import com.gree.mall.manager.plus.service.CommentTagService;
import com.gree.mall.manager.plus.service.OrderCommentService;
import com.gree.mall.manager.plus.service.OrderCommentTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CommentTagLogic {

    @Autowired
    CommonLogic commonLogic;
    @Autowired
    CommentTagService commentTagService;
    @Autowired
    OrderCommentService orderCommentService;
    @Autowired
    OrderCommentTagService orderCommentTagService;

    /**
     * 标签列表
     * @param pageNo
     * @param pageSize
     * @return
     */
    public IPage<CommentTag> list(HttpServletRequest request,Integer pageNo, Integer pageSize){

        AdminUserCom adminUser = commonLogic.getAdminUser(request);

        IPage<CommentTag> page = commentTagService.lambdaQuery()
                .orderByDesc(CommentTag::getCreateTime)
                .page(new Page<>(pageNo, pageSize));
        return page;
    }

    /**
     * 新增标签
     */
    public void add(String tag,HttpServletRequest request){
        AdminUserCom adminUser = commonLogic.getAdminUser(request);
        CommentTag commentTag = new CommentTag();
        commentTag.setTag(tag);
        commentTag.setCompanyId(null);
        commentTag.setCreateTime(new Date());
        commentTag.insert();
    }

    /**
     * 修改标签
     */
    public void update(String commentTagId,String tag){
        CommentTag commentTag = new CommentTag();
        commentTag.setTag(tag);
        commentTag.setCommentTagId(commentTagId);
        commentTag.updateById();
    }

    /**
     * 删除标签
     */
    public void delete(String commentTagId){
        commentTagService.removeById(commentTagId);
    }

    /**
     * 标签列表
     */
    public List<String> getCommentTags(String orderId){
        List<OrderCommentTag> list = orderCommentTagService.lambdaQuery().eq(OrderCommentTag::getOrderId, orderId).list();
        if(list.size() == 0){
            return null;
        }
        return list.stream().map(OrderCommentTag::getTag).collect(Collectors.toList());
    }

    /**
     * 评价内容
     */
    public OrderComment getOrderComment(String orderId){
        return orderCommentService.lambdaQuery().eq(OrderComment::getOrderId,orderId).one();
    }


    /**
     * 显示评价
     */
    public void updateShow(String orderId,Boolean isShow){
        orderCommentService.lambdaUpdate().set(OrderComment::getIsShow,isShow).eq(OrderComment::getOrderId,orderId).update();
    }


}
