package jnpf.personnels.controller.web.black;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.cultivate.CultivatePage;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlackListDTO;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlackUpdateDTO;
import jnpf.model.personnels.dto.black.FtbPersonnelsBlacklistAddDTO;
import jnpf.model.personnels.vo.black.FtbPersonnelsBlackListVO;
import jnpf.personnels.service.FtbPersonnelsBlacklistService;
import jnpf.util.excel.EasyExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

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

/**
 * web黑名单管理模块
 *
 * @author wcx
 * @since 2024-05-08
 */
@RestController
@RequestMapping("/ftb-personnels-blacklist")
public class FtbPersonnelsBlacklistController {


    @Autowired
    private FtbPersonnelsBlacklistService ftbPersonnelsBlacklistService;

    /**
     * 黑名单列表
     *
     * @return {@link ActionResult }
     */
    @GetMapping(value = "/list")
    public ActionResult<PageListVO<FtbPersonnelsBlackListVO>> blacklist(CultivatePage cultivatePage
            , FtbPersonnelsBlackListDTO dto) {
        Page<FtbPersonnelsBlackListVO> page = cultivatePage.coverCultivatePage("a.F_CreatorTime");
        page = ftbPersonnelsBlacklistService.blacklist(page, dto);
        PageListVO<FtbPersonnelsBlackListVO> pageListVO = CultivatePage.coverPageList(page);
        return ActionResult.success(pageListVO);
    }

    /**
     * 黑名单列表导出
     *
     * @return {@link ActionResult }
     */
    @GetMapping(value = "/list-export")
    public void listExport(CultivatePage cultivatePage
            , FtbPersonnelsBlackListDTO dto) throws IOException {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse httpServletResponse = requestAttributes.getResponse();
        Page<FtbPersonnelsBlackListVO> page = cultivatePage.coverCultivatePage("a.F_CreatorTime");
        page.setSize(-1);
        page = ftbPersonnelsBlacklistService.blacklist(page, dto);
        EasyExcelUtils.exportExcel(httpServletResponse, "黑名单", page.getRecords(), FtbPersonnelsBlackListVO.class);
    }

    /**
     * 移除黑名单
     *
     * @param id 黑名单主键id(必传)
     */
    @PostMapping(value = "/remove-blacklist/{id}")
    public ActionResult removeBlacklist(@PathVariable(value = "id") String id) {
        ftbPersonnelsBlacklistService.removeBlacklist(id);
        return ActionResult.success();
    }

    /**
     * 编辑黑名单
     */
    @PostMapping(value = "/edit-blacklist")
    public ActionResult editBlacklist(@Validated @RequestBody FtbPersonnelsBlackUpdateDTO fdto) {
        ftbPersonnelsBlacklistService.editBlacklist(fdto);
        return ActionResult.success();
    }

    /**
     * 黑名单类型-OA专用
     *
     * @return {@link ActionResult }<{@link List }<{@link FtbPersonnelsBlackListVO.BlackTermEnumVO }>>
     */
    @GetMapping("/black-type")
    public ActionResult<List<FtbPersonnelsBlackListVO.BlackTermEnumVO>> getBlacklist() {
        List<FtbPersonnelsBlackListVO.BlackTermEnumVO> result = ftbPersonnelsBlacklistService.getBlacklist();
        return ActionResult.success(result);
    }

    /**
     * 添加黑名单
     */
    @PostMapping(value = "/add-blacklist")
    public ActionResult<Void> addBlacklist( @RequestBody FtbPersonnelsBlacklistAddDTO ftbPersonnelsBlackListVO) {
        ftbPersonnelsBlacklistService.addBlacklist(ftbPersonnelsBlackListVO);
        return ActionResult.success();
    }

}
