package com.ruoyi.app.mendian;

import java.util.ArrayList;
import java.util.List;
import jakarta.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.MessageUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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.PosType;
import com.ruoyi.system.service.IPosTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门店分类Controller
 *
 * @author ruoyi
 * @date 2023-05-13
 */
@RestController
@RequestMapping("/fenlei/type")
public class PosTypeController extends BaseController
{
    @Autowired
    private IPosTypeService posTypeService;
    /**
     * 根据标识获取一级分类
     * @param tag  tag=food 美食  tag=mall商城
     */
    @Anonymous
    @GetMapping("/listByTag")
    public AjaxResult getListByTag(String  tag)
    {
        List<PosType> list =new ArrayList<>();
        PosType parent=posTypeService.getOne(new LambdaQueryWrapper<PosType>().eq(PosType::getTag,tag));
        if(parent!=null){
            list=posTypeService.list(new LambdaQueryWrapper<PosType>().eq(PosType::getParentId,parent.getId()));
        }
        return success(list);
    }

    /**
     * 查询门店分类列表
     */
    @Anonymous
    @GetMapping("/typelist")
    public AjaxResult typelist(PosType posType)
    {
        List<PosType> list = posTypeService.selectPosTypeList(posType);
        return success(MessageUtils.message("no.obtained.permission"),list);
    }
    /**
     * 查询门店分类列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(PosType posType)
    {
        startPage();
        List<PosType> list = posTypeService.selectPosTypeList(posType);
        return getDataTable(list);
    }

    /**
     * 导出门店分类列表
     */
    @PreAuthorize("@ss.hasPermi('fenlei:type:export')")
    @Log(title = "门店分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosType posType)
    {
        List<PosType> list = posTypeService.selectPosTypeList(posType);
        ExcelUtil<PosType> util = new ExcelUtil<PosType>(PosType.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.mendian.classify"));
    }

    /**
     * 查询门店分类树结构
     */
    @Anonymous
    @GetMapping("/treeList")
    public AjaxResult treeList(PosType posType)
    {
        List<PosType> list = posTypeService.selectPosTypeTreeList(posType);
        return success(list);
    }


    /**
     * 获取门店分类详细信息
     */
    @PreAuthorize("@ss.hasPermi('fenlei:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(posTypeService.selectPosTypeById(id));
    }

    /**
     * 新增门店分类
     */
    @PreAuthorize("@ss.hasPermi('fenlei:type:add')")
    @Log(title = "门店分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosType posType)
    {
        return toAjax(posTypeService.insertPosType(posType));
    }

    /**
     * 修改门店分类
     */
    @PreAuthorize("@ss.hasPermi('fenlei:type:edit')")
    @Log(title = "门店分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosType posType)
    {
        return toAjax(posTypeService.updatePosType(posType));
    }

    /**
     * 删除门店分类
     */
    @PreAuthorize("@ss.hasPermi('fenlei:type:remove')")
    @Log(title = "门店分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(posTypeService.deletePosTypeByIds(ids));
    }
}
