package com.ruoyi.web.controller.system;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.SysConfig;
import com.ruoyi.system.domain.vo.BillTemplateDataVO;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.WyPlatformBillTemplate;
import com.ruoyi.system.service.IWyPlatformBillTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 酒单套打模板Controller
 * 
 * @author ruoyi
 * @date 2025-02-15
 */
@Api(tags = "酒单套打模板管理")
@RestController
@RequestMapping("/system/billTemplate")
public class WyPlatformBillTemplateController extends BaseController
{
    @Autowired
    private IWyPlatformBillTemplateService wyPlatformBillTemplateService;
    @Autowired
    private ISysConfigService configService;
    /**
     * 查询酒单套打模板列表
     */
    @ApiOperation(value = "查询酒单套打模板列表", notes = "查询酒单套打模板列表")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:list')")
    @GetMapping("/list")
    public TableDataInfo list(WyPlatformBillTemplate wyPlatformBillTemplate)
    {
        startPage();
        List<WyPlatformBillTemplate> list = wyPlatformBillTemplateService.selectWyPlatformBillTemplateList(wyPlatformBillTemplate);
        return getDataTable(list);
    }

    /**
     * 导出酒单套打模板列表
     */
    @ApiOperation(value = "导出酒单套打模板列表", notes = "导出酒单套打模板列表")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:export')")
    @Log(title = "酒单套打模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WyPlatformBillTemplate wyPlatformBillTemplate)
    {
        List<WyPlatformBillTemplate> list = wyPlatformBillTemplateService.selectWyPlatformBillTemplateList(wyPlatformBillTemplate);
        ExcelUtil<WyPlatformBillTemplate> util = new ExcelUtil<WyPlatformBillTemplate>(WyPlatformBillTemplate.class);
        util.exportExcel(response, list, "酒单套打模板数据");
    }

    /**
     * 获取酒单套打模板详细信息
     */
    @ApiOperation(value = "获取酒单套打模板详细信息", notes = "获取酒单套打模板详细信息")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wyPlatformBillTemplateService.selectWyPlatformBillTemplateById(id));
    }

    /**
     * 新增酒单套打模板
     */
    @ApiOperation(value = "新增酒单套打模板", notes = "新增酒单套打模板")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:add')")
    @Log(title = "酒单套打模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WyPlatformBillTemplate wyPlatformBillTemplate)
    {
        return toAjax(wyPlatformBillTemplateService.insertWyPlatformBillTemplate(wyPlatformBillTemplate));
    }

    /**
     * 修改酒单套打模板
     */
    @ApiOperation(value = "修改酒单套打模板", notes = "修改酒单套打模板")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:edit')")
    @Log(title = "酒单套打模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WyPlatformBillTemplate wyPlatformBillTemplate)
    {
        return toAjax(wyPlatformBillTemplateService.updateWyPlatformBillTemplate(wyPlatformBillTemplate));
    }

    /**
     * 删除酒单套打模板
     */
    @ApiOperation(value = "删除酒单套打模板", notes = "删除酒单套打模板")
    @PreAuthorize("@ss.hasPermi('system:billTemplate:remove')")
    @Log(title = "酒单套打模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wyPlatformBillTemplateService.deleteWyPlatformBillTemplateByIds(ids));
    }

    /**
     * 修改酒单套打模板演示数据
     */
    @ApiOperation(value = "修改酒单套打模板演示数据", notes = "修改酒单套打模板演示数据")
    @PreAuthorize("@ss.hasPermi('system:billTemplateData:edit')")
    @Log(title = "修改酒单套打模板演示数据", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDemoData")
    public AjaxResult updateDemo(@Validated @RequestBody BillTemplateDataVO demoData)
    {
        final String demoDataKey = "billTemplateDemoData";
        final String demo = configService.selectConfigByKey(demoDataKey);
        final String jsonString = JSON.toJSONString(demoData);
        SysConfig config = new SysConfig();
        config.setConfigKey(demoDataKey);
        config.setConfigValue(jsonString);
        config.setConfigType("N");
        if (demo == null || demo.isEmpty()) {
            config.setCreateBy(getUsername());
            return toAjax(configService.insertConfig(config));
        }else{
            config.setUpdateBy(getUsername());
            return toAjax(configService.updateConfig(config));
        }
    }

    /**
     * 获取酒单套打模板演示数据
     */
    @ApiOperation(value = "获取酒单套打模板演示数据", notes = "获取酒单套打模板演示数据")
    @PreAuthorize("@ss.hasPermi('system:billTemplateData:query')")
    @GetMapping("/getDemoDate")
    public AjaxResult getDemoDate()
    {
        final String demoDataKey = "billTemplateDemoData";
        final String demo = configService.selectConfigByKey(demoDataKey);
        if (demo == null || demo.isEmpty()) {
            final BillTemplateDataVO billTemplateDataVO = new BillTemplateDataVO();
            return AjaxResult.success(billTemplateDataVO);
        }else{
            BillTemplateDataVO demoData = JSONObject.parseObject(demo,BillTemplateDataVO.class);
            return AjaxResult.success(demoData);
        }

    }
}
