package com.ruoyi.web.controller.branch;

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

import com.ruoyi.branch.domain.TBranchPublicize;
import com.ruoyi.branch.domain.TFile;
import com.ruoyi.branch.service.ITFileService;
import com.ruoyi.web.controller.branch.vo.ProfilesVO;
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.branch.domain.TBranchTemplate;
import com.ruoyi.branch.service.ITBranchTemplateService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 常用模板Controller
 * 
 * @author ruoyi
 * @date 2023-08-21
 */
@RestController
@RequestMapping("/branch/template")
public class TBranchTemplateController extends BaseController
{
    @Autowired
    private ITBranchTemplateService tBranchTemplateService;

    @Autowired
    private ITFileService fileService;

    /**
     * 查询常用模板列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('branch:template:list')")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(TBranchTemplate tBranchTemplate)
    {
        List<TBranchTemplate> tBranchTemplateList = tBranchTemplateService.selectTBranchTemplateList(tBranchTemplate);
        List<TFile> tFileList = new ArrayList<>();
        for (TBranchTemplate branchTemplate : tBranchTemplateList) {
            TFile tFile = new TFile();
            tFile.setTableName("branchTemplate");
            tFile.setTableId(branchTemplate.getTemplateId());
            List<TFile> tFiles = fileService.selectTFileList(tFile);
            if (tFiles != null) {
                tFileList.add(tFiles.get(0));
            }
        }
        return AjaxResult.success(tFileList);
    }

    /**
     * 查询常用模板列表
     */
    @PreAuthorize("@ss.hasPermi('branch:template:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBranchTemplate tBranchTemplate)
    {
        startPage();
        List<TBranchTemplate> list = tBranchTemplateService.selectTBranchTemplateList(tBranchTemplate);
        return getDataTable(list);
    }

    /**
     * 导出常用模板列表
     */
    @PreAuthorize("@ss.hasPermi('branch:template:export')")
    @Log(title = "常用模板", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBranchTemplate tBranchTemplate)
    {
        List<TBranchTemplate> list = tBranchTemplateService.selectTBranchTemplateList(tBranchTemplate);
        ExcelUtil<TBranchTemplate> util = new ExcelUtil<TBranchTemplate>(TBranchTemplate.class);
        util.exportExcel(response, list, "常用模板数据");
    }

    /**
     * 获取常用模板详细信息
     */
    @PreAuthorize("@ss.hasPermi('branch:template:query')")
    @GetMapping(value = "/{templateId}")
    public AjaxResult getInfo(@PathVariable("templateId") Long templateId)
    {
        return success(tBranchTemplateService.selectTBranchTemplateByTemplateId(templateId));
    }

    /**
     * 新增常用模板
     */
    @PreAuthorize("@ss.hasPermi('branch:template:add')")
    @Log(title = "常用模板", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBranchTemplate tBranchTemplate)
    {
        return toAjax(tBranchTemplateService.insertTBranchTemplate(tBranchTemplate));
    }

    /**
     * 修改常用模板
     */
    @PreAuthorize("@ss.hasPermi('branch:template:edit')")
    @Log(title = "常用模板", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBranchTemplate tBranchTemplate)
    {
        return toAjax(tBranchTemplateService.updateTBranchTemplate(tBranchTemplate));
    }

    /**
     * 删除常用模板
     */
    @PreAuthorize("@ss.hasPermi('branch:template:remove')")
    @Log(title = "常用模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{templateIds}")
    public AjaxResult remove(@PathVariable Long[] templateIds)
    {
        return toAjax(tBranchTemplateService.deleteTBranchTemplateByTemplateIds(templateIds));
    }
}
