package com.ruoyi.business.controller;

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

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.business.domain.BusinessTemplatePropValue;
import com.ruoyi.business.service.IBusinessTemplatePropValueService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
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.business.domain.BusinessTemplateProp;
import com.ruoyi.business.service.IBusinessTemplatePropService;
import com.ruoyi.common.utils.poi.ExcelUtil;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模版属性Controller
 *
 * @author ruoyi
 * @date 2024-03-31
 */
@RestController
@RequestMapping("/business/templateProp")
public class BusinessTemplatePropController extends BaseController {
    @Autowired
    private IBusinessTemplatePropService businessTemplatePropService;

    @Autowired
    private IBusinessTemplatePropValueService businessTemplatePropValueService;


    /**
     * 查询模版属性列表
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessTemplateProp businessTemplateProp) {
        startPage();
        List<BusinessTemplateProp> list = businessTemplatePropService.list(new LambdaQueryWrapper<BusinessTemplateProp>()
        .eq(StringUtils.isNotBlank(businessTemplateProp.getCompType()),BusinessTemplateProp::getCompType,businessTemplateProp.getCompType())
        .eq(StringUtils.isNotBlank(businessTemplateProp.getFieldName()),BusinessTemplateProp::getFieldName,businessTemplateProp.getFieldName())
        .eq(StringUtils.isNotBlank(businessTemplateProp.getId()),BusinessTemplateProp::getId,businessTemplateProp.getId())
        .eq(BusinessTemplateProp::getTempId,businessTemplateProp.getTempId())
        );
        return getDataTable(list);
    }

    /**
     * 导出模版属性列表
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:export')")
    @Log(title = "模版属性" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusinessTemplateProp businessTemplateProp) {
        List<BusinessTemplateProp> list = businessTemplatePropService.list();
        ExcelUtil<BusinessTemplateProp> util = new ExcelUtil<BusinessTemplateProp>(BusinessTemplateProp.class);
        util.exportExcel(response, list, "模版属性数据");
    }

    /**
     * 获取模版属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(businessTemplatePropService.getById(id));
    }

    /**
     * 新增模版属性
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:add')")
    @Log(title = "模版属性" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTemplateProp businessTemplateProp) {
        return toAjax(businessTemplatePropService.save(businessTemplateProp));
    }

    /**
     * 修改模版属性
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:edit')")
    @Log(title = "模版属性" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessTemplateProp businessTemplateProp) {
        return toAjax(businessTemplatePropService.updateById(businessTemplateProp));
    }

    /**
     * 删除模版属性
     */
    @PreAuthorize("@ss.hasPermi('business:templateProp:remove')")
    @Log(title = "模版属性" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(businessTemplatePropService.removeByIds(Arrays.asList(ids)));
    }
}
