package com.ruoyi.app.mendian;

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

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.system.domain.PosFood;
import com.ruoyi.system.domain.PosStore;
import com.ruoyi.system.service.IPosFoodService;
import com.ruoyi.system.service.MerchantStoreAccessService;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.AuthContext;
import com.ruoyi.system.utils.JwtUtil;
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.PosFenlei;
import com.ruoyi.system.service.IPosFenleiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * spfenleiController
 *
 * @author ruoyi
 * @date 2023-05-21
 */
@RestController
@RequestMapping("/system/fenlei")
public class PosFenleiController extends BaseController
{
    @Autowired
    private IPosFenleiService posFenleiService;
    @Autowired  //商品
    private IPosFoodService posFoodService;
    @Autowired
    private MerchantStoreAccessService merchantStoreAccessService;

    /**
     * 删除商品分类
     */
    @Anonymous
    @Auth(session = true)
    @GetMapping("/delefenlei")
    public AjaxResult delefenlei(@RequestHeader String token, @RequestParam String id){
        PosFenlei existing = posFenleiService.getById(Long.valueOf(id));
        if (existing == null) {
            return error(MessageUtils.message("no.data.not.exist"));
        }
        merchantStoreAccessService.requireStoreAccess(
                AuthContext.requireUserId(), existing.getMendid());
        QueryWrapper<PosFood> query = new QueryWrapper<>();
        query.eq("fl_id",id);
        List<PosFood> fdlist = posFoodService.list(query);
        if(fdlist.size()>0){
            return error(MessageUtils.message("no.data.exist.not.del"));
        }else {
            return toAjax(posFenleiService.deletePosFenleiById(Long.valueOf(id)));
        }
    }
    /**
     * 添加或修改商品分类
     */
    @Anonymous
    @Auth(session = true)
    @PostMapping("/addfenlei")
    public AjaxResult addfenlei(@RequestHeader String token, @RequestBody PosFenlei posFenlei){
        Long userId = AuthContext.requireUserId();
        if (posFenlei.getId() == null) {
            merchantStoreAccessService.requireStoreAccess(userId, posFenlei.getMendid());
        } else {
            PosFenlei existing = posFenleiService.getById(posFenlei.getId());
            if (existing == null) {
                return error(MessageUtils.message("no.data.not.exist"));
            }
            merchantStoreAccessService.requireStoreAccess(userId, existing.getMendid());
            posFenlei.setMendid(existing.getMendid());
        }
        Boolean org = posFenleiService.saveOrUpdate(posFenlei);
        if(org){
            return success();
        }else {
            return error();
        }

    }
    /**
     * 查询商品分类
     */
    @Anonymous
    @GetMapping("/getfenlei")
    public AjaxResult getfenlei(@RequestParam Integer id,@RequestParam(defaultValue = "") String language){
        QueryWrapper<PosFenlei> queryWrapper= new QueryWrapper<>();
        queryWrapper.select().orderByDesc("sort");
        queryWrapper.eq("mendid",id);
        String lang = "0";
        if(!"".equals(language)){
            lang = language;
        }
        queryWrapper.eq("language",lang);
        List<PosFenlei> list = posFenleiService.list(queryWrapper);
        return success(list);
    }
    /**
     * 查询spfenlei列表
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:list')")
    @GetMapping("/list")
    public TableDataInfo list(PosFenlei posFenlei)
    {
        startPage();
        List<PosFenlei> list = posFenleiService.selectPosFenleiList(posFenlei);
        return getDataTable(list);
    }

    /**
     * 导出spfenlei列表
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:export')")
    @Log(title = "spfenlei", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PosFenlei posFenlei)
    {
        List<PosFenlei> list = posFenleiService.selectPosFenleiList(posFenlei);
        ExcelUtil<PosFenlei> util = new ExcelUtil<PosFenlei>(PosFenlei.class);
        util.exportExcel(response, list, MessageUtils.message("no.export.excel.goods.classify"));
    }

    /**
     * 获取spfenlei详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(posFenleiService.selectPosFenleiById(id));
    }

    /**
     * 新增spfenlei
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:add')")
    @Log(title = "spfenlei", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PosFenlei posFenlei)
    {
        return toAjax(posFenleiService.insertPosFenlei(posFenlei));
    }

    /**
     * 修改spfenlei
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:edit')")
    @Log(title = "spfenlei", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PosFenlei posFenlei)
    {
        return toAjax(posFenleiService.updatePosFenlei(posFenlei));
    }

    /**
     * 删除spfenlei
     */
    @PreAuthorize("@ss.hasPermi('system:fenlei:remove')")
    @Log(title = "spfenlei", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(posFenleiService.deletePosFenleiByIds(ids));
    }
}
