package com.ruoyi.app.mendian;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.PosReview;
import com.ruoyi.system.domain.PosStore;
import com.ruoyi.system.service.IPosReviewService;
import com.ruoyi.system.service.IPosStoreService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 门店订单评论
 */
@RestController
@RequestMapping("/system/shPosReview")
public class ShPosReviewController extends BaseController{
    @Autowired
    private IPosReviewService posReviewService;
    @Autowired
    private IPosStoreService  posStoreService;


    /**
     * 查询review列表
     */
    @Auth
    @GetMapping("/list")
    public AjaxResult list(@RequestHeader String token,
                              @RequestParam Integer page,
                              @RequestParam Integer size)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        List<PosStore> stores=posStoreService.list(new LambdaQueryWrapper<>(PosStore.class).eq(PosStore::getUserId,id));
        List<Integer> mdIds=stores.stream().map(PosStore::getId).collect(Collectors.toList());
        IPage<PosReview> palist = new Page<>(page,size);
        LambdaQueryWrapper<PosReview> Wrapper= new LambdaQueryWrapper<>();
        Wrapper.in(PosReview::getMdId,mdIds);
        IPage<PosReview> list =  posReviewService.page(palist,Wrapper);
        return success(list);
    }

//    /**
//     * 导出review列表
//     */
//
//
//    @Log(title = "review", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, PosReview posReview)
//    {
//        List<PosReview> list = posReviewService.selectPosReviewList(posReview);
//        ExcelUtil<PosReview> util = new ExcelUtil<PosReview>(PosReview.class);
//        util.exportExcel(response, list, MessageUtils.message("no.export.excel.review"));
//    }

    /**
     * 获取review详细信息
     */

    @Anonymous
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(posReviewService.selectPosReviewById(id));
    }



    /**
     * 修改review
     */

    @Log(title = "review", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosReview posReview)
    {
        return toAjax(posReviewService.updatePosReview(posReview));
    }

    /**
     * 删除review
     */

    @Log(title = "review", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(posReviewService.deletePosReviewByIds(ids));
    }
}
