package com.jeeplus.test.klgBase.question.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.demo.annotation.DemoMode;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.excel.utils.EasyPoiUtil;
import com.jeeplus.sys.constant.enums.LogTypeEnum;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.test.finance.invoice.service.FinanceInvoiceService;
import com.jeeplus.test.finance.invoice.service.dto.FinanceDTO;
import com.jeeplus.test.finance.invoice.service.dto.FinanceInvoiceDTO;
import com.jeeplus.test.finance.invoice.service.dto.FinanceInvoiceDetailDTO;
import com.jeeplus.test.klgBase.question.service.KlgBaseQuestionsService;
import com.jeeplus.test.klgBase.question.service.dto.KlgBaseQuestionsDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.jeeplus.test.jobPosion.utils.BeanUtils.objectCheckIsNull;

@Api("知识库-业务提问")
@RestController
@RequestMapping(value = "/klg/questions")
public class KlgBaseQuestionsController {

    @Resource
    private KlgBaseQuestionsService klgBaseQuestionsService;

    /**
     * 查询业务提问列表
     * @param klgBaseQuestionsDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询业务提问列表")
    @PreAuthorize("hasAuthority('klg:question:list')")
    @GetMapping("list")
    public ResponseEntity<IPage<KlgBaseQuestionsDTO>> data(KlgBaseQuestionsDTO klgBaseQuestionsDTO, Page<KlgBaseQuestionsDTO> page) throws Exception {
        IPage<KlgBaseQuestionsDTO> result = new Page<KlgBaseQuestionsDTO>();
        result = klgBaseQuestionsService.findList (page,klgBaseQuestionsDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询关注的业务提问列表
     * @param klgBaseQuestionsDTO
     * @param page
     * @return
     * @throws Exception
     */
    @ApiLog("查询关注的业务提问列表")
    @PreAuthorize("hasAuthority('klg:question:list')")
    @GetMapping("list_focus")
    public ResponseEntity<IPage<KlgBaseQuestionsDTO>> findListFocus(KlgBaseQuestionsDTO klgBaseQuestionsDTO, Page<KlgBaseQuestionsDTO> page) throws Exception {
        IPage<KlgBaseQuestionsDTO> result = new Page<KlgBaseQuestionsDTO>();
        result = klgBaseQuestionsService.findListFocus (page,klgBaseQuestionsDTO);
        return ResponseEntity.ok (result);
    }

    /**
     * 查询业务提问数据
     * @param id
     * @return
     */
    @ApiLog("查询业务提问数据")
    @PreAuthorize ("hasAnyAuthority('klg:question:view','klg:question:add','klg:question:edit')")
    @GetMapping("queryById")
    public ResponseEntity queryById(@RequestParam("id") String id) {
        KlgBaseQuestionsDTO klgBaseQuestionsDTO = klgBaseQuestionsService.queryById ( id );
        return ResponseEntity.ok (klgBaseQuestionsDTO);
    }

    /**
     * 保存业务提问
     * @param klgBaseQuestionsDTO
     * @return
     */
    @ApiLog(value = "修改/新增业务提问", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('klg:question:add','klg:question:edit')")
    @PostMapping("save")
    public ResponseEntity save(@Valid @RequestBody KlgBaseQuestionsDTO klgBaseQuestionsDTO) throws Exception {
        klgBaseQuestionsService.saveQuestion(klgBaseQuestionsDTO);
        return ResponseEntity.ok ("操作成功");
    }

    /**
     * 保存业务提问-流程
     * @param klgBaseQuestionsDTO
     * @return
     */
    @ApiLog(value = "保存业务提问-流程", type = LogTypeEnum.SAVE)
    @PreAuthorize("hasAnyAuthority('klg:question:add','klg:question:edit')")
    @PostMapping("saveForm")
    public ResponseEntity saveForm(@Valid @RequestBody KlgBaseQuestionsDTO klgBaseQuestionsDTO) throws Exception {
        String id = klgBaseQuestionsService.saveForm(klgBaseQuestionsDTO);
        return ResponseUtil.newInstance().add("businessTable", "klg_base_questions").add("businessId", id).ok ("操作成功");
    }

    /**
     * 删除业务提问
     * @param ids
     * @return
     */
    @ApiLog(value = "删除业务提问", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('klg:question:del')")
    @DeleteMapping("delete")
    public ResponseEntity delete(String ids) {
        return klgBaseQuestionsService.deleteByIds(ids);
    }

    /**
     * 根据id修改状态值status
     * @param dto
     * @return
     */
    @ApiOperation(value = "根据id修改状态值status")
    @PostMapping(value = "/updateStatusById")
    public ResponseEntity<String> updateStatusById(@RequestBody KlgBaseQuestionsDTO dto) {
        String s = klgBaseQuestionsService.updateStatusById(dto);
        return ResponseEntity.ok(s);
    }

    /**
     * 关注业务提问
     * @param ids
     * @return
     */
    @ApiLog(value = "关注业务提问", type = LogTypeEnum.SAVE)
    @PreAuthorize ("hasAuthority('klg:question:add')")
    @GetMapping("focusByIds")
    public ResponseEntity focusByIds(String ids) {
        return klgBaseQuestionsService.focusByIds(ids);
    }
}
