package com.bonus.sgzb.material.controller;

import com.bonus.sgzb.common.core.utils.poi.ExcelUtil;
import com.bonus.sgzb.common.core.web.controller.BaseController;
import com.bonus.sgzb.common.core.web.domain.AjaxResult;
import com.bonus.sgzb.common.core.web.page.TableDataInfo;
import com.bonus.sgzb.common.security.utils.SecurityUtils;
import com.bonus.sgzb.material.domain.*;
import com.bonus.sgzb.material.service.PlanManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 计划管理控制层
 * @Author ma_sh
 * @create 2024/4/9 9:30
 */
@Api(tags = " 计划管理")
@RestController
@Slf4j
@RequestMapping("/planManagement")
public class PlanManagementController extends BaseController {

    @Autowired
    private PlanManagementService planManagementService;

    /**
     * 获取计划管理列表
     * @param dto
     * @return
     */
    @ApiOperation(value = "获取计划管理列表")
    @GetMapping("/getList")
    public TableDataInfo getList(PlanManagementDto dto) {
        startPage();
        List<PlanVO> list = planManagementService.getList(dto);
        return getDataTable(list);
    }

    /**
     * 计划管理导出
     * @param response
     * @param dto
     */
    @ApiOperation("计划管理导出")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlanManagementDto dto)
    {
        List<PlanVO> list = planManagementService.getList(dto);
        ExcelUtil<PlanVO> util = new ExcelUtil<>(PlanVO.class);
        util.exportExcel(response, list, "计划管理数据");
    }

    /**
     * 新增或者修改计划管理
     * @param dto
     * @return
     */
    @ApiOperation("新增或者修改计划管理")
    @PostMapping("/addOrUpdate")
    public AjaxResult addOrUpdate(@ApiParam(value = "添加信息", required = true)
                                      @RequestBody PlanManagementDto dto){
        if (dto.getPlanId() != null) {
            log.info("更新计划管理：{}", dto);
            dto.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
            return AjaxResult.success(planManagementService.update(dto));
        }
        log.info("新增计划管理：{}", dto);
        dto.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        return planManagementService.insert(dto);
    }

    /**
     * 新增或者修改计划借调管理
     * @param dto
     * @return
     */
    @ApiOperation("新增或者修改计划借调管理")
    @PostMapping("/addOrUpdateMange")
    public AjaxResult addOrUpdateMange(@ApiParam(value = "添加信息", required = true)
                                  @RequestBody PlanManagementDto dto){
        if (dto.getBorrowId() != null) {
            log.info("更新计划借调管理：{}", dto);
            dto.setUpdateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
            return AjaxResult.success(planManagementService.update(dto));
        }
        log.info("新增计划借调管理：{}", dto);
        dto.setCreateBy(SecurityUtils.getLoginUser().getSysUser().getNickName());
        return planManagementService.insertManage(dto);
    }

    /**
     * 获取计划借调管理列表
     * @param dto
     * @return
     */
    @ApiOperation(value = "获取计划借调管理列表")
    @GetMapping("/getManageList")
    public TableDataInfo getManageList(PlanManagementDto dto) {
        startPage();
        List<PlanManagementVO> list = planManagementService.getManageList(dto);
        return getDataTable(list);
    }

    /**
     * 计划借调管理导出
     * @param response
     * @param dto
     */
    @ApiOperation("计划借调管理导出")
    @PostMapping("/exportManage")
    public void exportManage(HttpServletResponse response, PlanManagementDto dto)
    {
        List<PlanManagementVO> list = planManagementService.getManageList(dto);
        ExcelUtil<PlanManagementVO> util = new ExcelUtil<>(PlanManagementVO.class);
        util.exportExcel(response, list, "计划借调管理数据");
    }

    /**
     * 删除计划管理
     * @param planId
     * @return
     */
    @ApiOperation("根据id删除计划管理")
    @DeleteMapping("/deleteById/{planId}")
    public AjaxResult addOrUpdate(@PathVariable(value = "planId") Long planId){
        return AjaxResult.success(planManagementService.deleteById(planId));
    }

}
