package com.ruoyi.web.controller.branch;

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

import com.ruoyi.branch.domain.TBranchPlanItem;
import com.ruoyi.branch.service.ITBranchPlanItemService;
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.TBranchPlan;
import com.ruoyi.branch.service.ITBranchPlanService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 支部年度工作计划Controller
 * 
 * @author ruoyi
 * @date 2023-07-17
 */
@RestController
@RequestMapping("/branch/plan")
public class TBranchPlanController extends BaseController
{
    @Autowired
    private ITBranchPlanService tBranchPlanService;

    @Autowired
    private ITBranchPlanItemService tBranchPlanItemService;

    /**
     * 查询支部年度工作计划列表
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:list')")
    @GetMapping("/list")
    public TableDataInfo list(TBranchPlan tBranchPlan)
    {
        startPage();
        List<TBranchPlan> list = tBranchPlanService.selectTBranchPlanList(tBranchPlan);
        // 查询工作计划条目
        for (TBranchPlan branchPlan : list) {
            TBranchPlanItem tBranchPlanItem = new TBranchPlanItem();
            tBranchPlanItem.setPlanId(branchPlan.getPlanId());
            tBranchPlanItem.setItemStatus(tBranchPlan.getItemStatus());
            tBranchPlanItem.setPersonInCharge(tBranchPlan.getPersonInCharge());
            List<TBranchPlanItem> tBranchPlanItems = tBranchPlanItemService.selectTBranchPlanItemList(tBranchPlanItem);
            branchPlan.setItemList(tBranchPlanItems);
        }
        return getDataTable(list);
    }

    /**
     * 导出支部年度工作计划列表
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:export')")
    @Log(title = "支部年度工作计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TBranchPlan tBranchPlan)
    {
        List<TBranchPlan> list = tBranchPlanService.selectTBranchPlanList(tBranchPlan);
        ExcelUtil<TBranchPlan> util = new ExcelUtil<TBranchPlan>(TBranchPlan.class);
        util.exportExcel(response, list, "支部年度工作计划数据");
    }

    /**
     * 获取支部年度工作计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:query')")
    @GetMapping(value = "/{planId}")
    public AjaxResult getInfo(@PathVariable("planId") Long planId)
    {
        return success(tBranchPlanService.selectTBranchPlanByPlanId(planId));
    }

    /**
     * 新增支部年度工作计划
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:add')")
    @Log(title = "支部年度工作计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TBranchPlan tBranchPlan)
    {
        return toAjax(tBranchPlanService.insertTBranchPlan(tBranchPlan));
    }

    /**
     * 修改支部年度工作计划
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:edit')")
    @Log(title = "支部年度工作计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TBranchPlan tBranchPlan)
    {
        return toAjax(tBranchPlanService.updateTBranchPlan(tBranchPlan));
    }

    /**
     * 删除支部年度工作计划
     */
    @PreAuthorize("@ss.hasPermi('branch:plan:remove')")
    @Log(title = "支部年度工作计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{planIds}")
    public AjaxResult remove(@PathVariable Long[] planIds)
    {
        return toAjax(tBranchPlanService.deleteTBranchPlanByPlanIds(planIds));
    }
}
