package com.ruoyi.web.controller.branch;

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

import com.ruoyi.branch.domain.TBranchPublicize;
import com.ruoyi.branch.domain.TBranchStudy;
import com.ruoyi.branch.domain.TFile;
import com.ruoyi.branch.service.ITFileService;
import com.ruoyi.common.utils.StringUtils;
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.TBranchManage;
import com.ruoyi.branch.service.ITBranchManageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支部党课学习资料管理Controller
 *
 * @author ruoyi
 * @date 2023-07-14
 */
@RestController
@RequestMapping("/branch/manage")
public class TBranchManageController extends BaseController {

    @Autowired
    private ITBranchManageService tBranchManageService;

    @Autowired
    private ITFileService tFileService;

    /**
     * 查询支部党课学习资料管理列表（当年、当月）
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:list')")
    @GetMapping("/listCurrentYearMonth")
    public AjaxResult listCurrentYearMonth()
    {
        Calendar calendar = Calendar.getInstance();//当前日期
        int year = calendar.get(Calendar.YEAR);//年
        int month = calendar.get(Calendar.MONTH) + 1;//月

        TBranchManage tBranchManage = new TBranchManage();
        tBranchManage.setYear(year + "");
        tBranchManage.setMonthly(month + "");
        List<TBranchManage> tBranchManages = tBranchManageService.selectTBranchManageList(tBranchManage);

        TFile tFile = new TFile();
        tFile.setTableName("branchManage");
        tFile.setTableId(tBranchManages.get(0).getManageId());
        List<TFile> tFiles = tFileService.selectTFileList(tFile);

        return AjaxResult.success(tFiles);
    }

    /**
     * 查询支部党课学习资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBranchManage tBranchManage) {
        startPage();
        List<TBranchManage> list = tBranchManageService.selectTBranchManageList(tBranchManage);
        return getDataTable(list);
    }

    /**
     * 导出支部党课学习资料管理列表
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:export')")
    @Log(title = "支部党课学习资料管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBranchManage tBranchManage) {
        List<TBranchManage> list = tBranchManageService.selectTBranchManageList(tBranchManage);
        ExcelUtil<TBranchManage> util = new ExcelUtil<TBranchManage>(TBranchManage.class);
        util.exportExcel(response, list, "支部党课学习资料管理数据");
    }

    /**
     * 获取支部党课学习资料管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:query')")
    @GetMapping(value = "/{manageId}")
    public AjaxResult getInfo(@PathVariable("manageId") Long manageId) {
        return success(tBranchManageService.selectTBranchManageByManageId(manageId));
    }

    /**
     * 新增支部党课学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:add')")
    @Log(title = "支部党课学习资料管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBranchManage tBranchManage) {
        return toAjax(tBranchManageService.insertTBranchManage(tBranchManage));
    }

    /**
     * 修改支部党课学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:edit')")
    @Log(title = "支部党课学习资料管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBranchManage tBranchManage) {
        if (StringUtils.isNotEmpty(tBranchManage.getFilesId())) {
            TBranchManage manage = tBranchManageService.selectTBranchManageByManageId(tBranchManage.getManageId());
            if (StringUtils.isNotEmpty(manage.getFilesId())) {
                tBranchManage.setFilesId(manage.getFilesId() + "," + tBranchManage.getFilesId());
            }
        }
        return toAjax(tBranchManageService.updateTBranchManage(tBranchManage));
    }

    /**
     * 删除支部党课学习资料管理
     */
    @PreAuthorize("@ss.hasPermi('branch:manage:remove')")
    @Log(title = "支部党课学习资料管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{manageIds}")
    public AjaxResult remove(@PathVariable Long[] manageIds) {
        return toAjax(tBranchManageService.deleteTBranchManageByManageIds(manageIds));
    }

    @PreAuthorize("@ss.hasPermi('branch:manage:remove')")
    @Log(title = "支部党课学习资料管理删除附件", businessType = BusinessType.DELETE)
    @PutMapping("/delFile")
    public AjaxResult removeFile(@RequestBody TBranchManage tBranchManage) {
        TBranchManage mange = tBranchManageService.selectTBranchManageByManageId(tBranchManage.getManageId());
        mange.setFilesId(mange.getFilesId().replace(tBranchManage.getFilesId(), ""));
        if (mange.getFilesId().contains(",,")){
            mange.setFilesId(mange.getFilesId().replace(",,", ","));
        }
        if (mange.getFilesId().endsWith(",")) {
            mange.setFilesId(mange.getFilesId().substring(0, mange.getFilesId().length()-1));
        }
        tBranchManageService.updateTBranchManage(mange);
        return toAjax(tFileService.deleteTFileById(Long.valueOf(tBranchManage.getFilesId())));
    }
}
