package com.ruoyi.system.controller;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.utils.JwtUtil;
import jakarta.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.PosOrder;
import com.ruoyi.system.service.IPosOrderService;
import com.ruoyi.system.utils.Auth;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PosAppeal;
import com.ruoyi.system.service.IPosAppealService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 留言信息Controller
 *
 * @author ruoyi
 * @date 2024-07-07
 */
@RestController
@RequestMapping("/system/appeal")
public class PosAppealController extends BaseController
{
    @Autowired
    private IPosAppealService posAppealService;
    @Autowired
    private IPosOrderService posOrderService;

    //前端api--start
    /**
     * 新增留言信息
     */
    @Anonymous
    @Auth
    @Log(title = "留言信息", businessType = BusinessType.INSERT)
    @PostMapping("/apiAdd")
    public AjaxResult apiAdd(@RequestHeader String token,@RequestBody PosAppeal posAppeal)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        Long uid=Long.valueOf(id);
        posAppeal.setUserId(uid);
        PosOrder po = new PosOrder();
        po.setDdId(posAppeal.getDdId());
        List<PosOrder> list = posOrderService.selectPosOrderList(po);
        if(list!=null && list.size()>0){
            for(PosOrder od:list){
                od.setKefuState(2L);
                posOrderService.saveOrUpdate(od);
            }
        }
        return success(posAppealService.insertPosAppeal(posAppeal));
    }
    /**
     * 查询留言信息列表
     */
    @Anonymous
    @Auth
    @GetMapping("/apiList")
    public AjaxResult list(@RequestHeader String token,@RequestParam(defaultValue = "",required = false)String ddId, @RequestParam Integer page, @RequestParam Integer size)
    {
        JwtUtil jwtUtil = new JwtUtil();
        String id = jwtUtil.getusid(token);
        Long uid=Long.valueOf(id);
        Page<PosAppeal> pageInput=new Page<>(page,size);
        LambdaQueryWrapper<PosAppeal> query=new LambdaQueryWrapper<>();
        query.eq(PosAppeal::getUserId,uid);
        query.eq(!StrUtil.isEmpty(ddId),PosAppeal::getDdId,ddId);
        IPage<PosAppeal> result= posAppealService.page(pageInput,query);
        if(!result.getRecords().isEmpty()){
           List<String>ddIds= result.getRecords().stream().map(PosAppeal::getDdId).collect(Collectors.toList());
            List<PosAppeal> child= posAppealService.list(new LambdaQueryWrapper<PosAppeal>().in(PosAppeal::getDdId,ddIds).eq(PosAppeal::getUserId,0L).orderByDesc(PosAppeal::getId));
            for (PosAppeal data:result.getRecords()){
               var sub= child.stream().filter(x-> data.getDdId().equals(x.getDdId())).toList();
               data.setChild(sub);
            }
        }

        return success(result);
    }

    //前端api--end

    /**
     * 查询留言信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosAppeal posAppeal)
    {
        startPage();
        List<PosAppeal> list = posAppealService.selectPosAppealList(posAppeal);
        return getDataTable(list);
    }

    /**
     * 导出留言信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:export')")
    @Log(title = "留言信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosAppeal posAppeal)
    {
        List<PosAppeal> list = posAppealService.selectPosAppealList(posAppeal);
        ExcelUtil<PosAppeal> util = new ExcelUtil<PosAppeal>(PosAppeal.class);
        util.exportExcel(response, list, "留言信息数据");
    }

    /**
     * 获取留言信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(posAppealService.selectPosAppealById(id));
    }

    /**
     * 新增留言信息
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:add')")
    @Log(title = "留言信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosAppeal posAppeal)
    {
        posAppeal.setUserId(0L);
        return toAjax(posAppealService.insertPosAppeal(posAppeal));
    }

    /**
     * 修改留言信息
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:edit')")
    @Log(title = "留言信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosAppeal posAppeal)
    {
        return toAjax(posAppealService.updatePosAppeal(posAppeal));
    }

    /**
     * 删除留言信息
     */
    @PreAuthorize("@ss.hasPermi('system:appeal:remove')")
    @Log(title = "留言信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(posAppealService.deletePosAppealByIds(ids));
    }
}
