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.TBranchDemeanor;
import com.ruoyi.branch.service.ITBranchDemeanorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支部园地风采Controller
 * 
 * @author ruoyi
 * @date 2023-07-27
 */
@RestController
@RequestMapping("/branch/demeanor")
public class TBranchDemeanorController extends BaseController
{
    @Autowired
    private ITBranchDemeanorService tBranchDemeanorService;

    @Autowired
    private ITFileService fileService;

    /**
     * 查询首页置顶图片轮播
     */
    @PreAuthorize("@ss.hasPermi('branch:publicize:list')")
    @GetMapping("/listProfiles")
    public AjaxResult listProfiles()
    {
        TBranchDemeanor tBranchDemeanor = new TBranchDemeanor();
        tBranchDemeanor.setIsPinned("1");
        List<TBranchDemeanor> tBranchDemeanors = tBranchDemeanorService.selectTBranchDemeanorList(tBranchDemeanor);
        List<ProfilesVO> profileVOs = new ArrayList<>();
        for (TBranchDemeanor BranchDemeanor : tBranchDemeanors) {
            TFile tFile = new TFile();
            tFile.setTableName("branchDemeanor");
            tFile.setTableId(BranchDemeanor.getDemeanorId());
            List<TFile> tFiles = fileService.selectTFileList(tFile);
            if (tFiles != null) {
                TFile file = tFiles.get(0);
                ProfilesVO vo = new ProfilesVO();
                vo.setUrl(file.getUrl());
                vo.setTitle(BranchDemeanor.getDemeanorTitle());
                profileVOs.add(vo);
            }
        }
        return AjaxResult.success(profileVOs);
    }

    /**
     * 查询支部园地风采列表（不分页）
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:list')")
    @GetMapping("/listNoPage")
    public AjaxResult listNoPage(TBranchDemeanor tBranchDemeanor)
    {
        List<TBranchDemeanor> tBranchDemeanors = tBranchDemeanorService.selectTBranchDemeanorList(tBranchDemeanor);
        List<ProfilesVO> profileVOs = new ArrayList<>();
        for (TBranchDemeanor BranchDemeanor : tBranchDemeanors) {
            TFile tFile = new TFile();
            tFile.setTableName("branchDem2");
            tFile.setTableId(BranchDemeanor.getDemeanorId());
            List<TFile> tFiles = fileService.selectTFileList(tFile);
            ProfilesVO vo = new ProfilesVO();
            vo.setTitle(BranchDemeanor.getDemeanorTitle());
            vo.setDate(BranchDemeanor.getCreateTime());
            if (tFiles.size() != 0) {
                TFile file = tFiles.get(0);
                vo.setUrl(file.getUrl());
            }
            profileVOs.add(vo);
        }
        return AjaxResult.success(profileVOs);
    }

    /**
     * 查询支部园地风采列表
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBranchDemeanor tBranchDemeanor)
    {
        startPage();
        List<TBranchDemeanor> list = tBranchDemeanorService.selectTBranchDemeanorList(tBranchDemeanor);
        return getDataTable(list);
    }

    /**
     * 导出支部园地风采列表
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:export')")
    @Log(title = "支部园地风采", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBranchDemeanor tBranchDemeanor)
    {
        List<TBranchDemeanor> list = tBranchDemeanorService.selectTBranchDemeanorList(tBranchDemeanor);
        ExcelUtil<TBranchDemeanor> util = new ExcelUtil<TBranchDemeanor>(TBranchDemeanor.class);
        util.exportExcel(response, list, "支部园地风采数据");
    }

    /**
     * 获取支部园地风采详细信息
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:query')")
    @GetMapping(value = "/{demeanorId}")
    public AjaxResult getInfo(@PathVariable("demeanorId") Long demeanorId)
    {
        return success(tBranchDemeanorService.selectTBranchDemeanorByDemeanorId(demeanorId));
    }

    /**
     * 新增支部园地风采
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:add')")
    @Log(title = "支部园地风采", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBranchDemeanor tBranchDemeanor)
    {
        tBranchDemeanor.setCreateBy(getUsername());
        return toAjax(tBranchDemeanorService.insertTBranchDemeanor(tBranchDemeanor));
    }

    /**
     * 修改支部园地风采
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:edit')")
    @Log(title = "支部园地风采", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBranchDemeanor tBranchDemeanor)
    {
        return toAjax(tBranchDemeanorService.updateTBranchDemeanor(tBranchDemeanor));
    }

    /**
     * 删除支部园地风采
     */
    @PreAuthorize("@ss.hasPermi('branch:demeanor:remove')")
    @Log(title = "支部园地风采", businessType = BusinessType.DELETE)
	@DeleteMapping("/{demeanorIds}")
    public AjaxResult remove(@PathVariable Long[] demeanorIds)
    {
        return toAjax(tBranchDemeanorService.deleteTBranchDemeanorByDemeanorIds(demeanorIds));
    }
}
