package cn.iocoder.yudao.module.knowledge.controller.admin;


import cn.iocoder.yudao.module.knowledge.constants.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.TArchivesRequest;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.UserInfoRequest;
import cn.iocoder.yudao.module.knowledge.entity.TArchives;
import cn.iocoder.yudao.module.knowledge.service.TArchivesService;
import cn.iocoder.yudao.module.knowledge.utils.ExcelUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.iocoder.yudao.module.knowledge.constants.Result;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 档案表(TArchives)表控制层
 *
 * @author makejava
 * @since 2024-04-11 10:25:58
 * 弃用 新实现在di-module-company
 */
@Deprecated
@RestController
@RequestMapping("/system/tArchives")
public class TArchivesController {


    /**
     * 错误日志
     */
    private static Logger errorLog = LoggerFactory.getLogger("errorLog");

    /**
     * 埋点info日志
     */
    private static Logger buriedLog = LoggerFactory.getLogger("buriedLog");

    /**
     * 服务对象
     */
    @Autowired
    private TArchivesService tArchivesService;

    /**
     * 日常使用测试接口
     *
     * @param tArchives
     * @return
     */
    @Operation(summary = "getTArchives", description = "查询文件信息")
    @Parameters({
            @Parameter(name = "id", description = "文件id"),
    })
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "successful operation",
                    content = {@Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class)
                    )}),
            @ApiResponse(responseCode = "250000", description = "paramter error operation",
                    content = {@Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class)
                    )})
    })
    @GetMapping(value = "/findAll", produces = "application/json;charset=UTF-8")
    public Result selectAll(TArchivesRequest tArchives) {
        Page<TArchives> page = new Page<>(tArchives.getPageNo(), tArchives.getPageSize());
        return Result.success(this.tArchivesService.findAll(page, tArchives));
    }

    @Operation(summary = "selectOne", description = "查询文件信息")
    @Parameters({
            @Parameter(
                    content = {@Content(mediaType = "application/json",
                            schema = @Schema(implementation = UserInfoRequest.class)
                    )})
    })
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "successful operation",
                    content = {@Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class)
                    )}),
            @ApiResponse(responseCode = "250000", description = "paramter error operation",
                    content = {@Content(mediaType = "application/json",
                            schema = @Schema(implementation = Result.class)
                    )})
    })


    @GetMapping(value = "/findAllDocList", produces = "application/json;charset=UTF-8")
    public Result findAllDocList(TArchivesRequest tArchives) {
        Page<TArchives> page = new Page<>(tArchives.getPageNo(), tArchives.getPageSize());
        return Result.success(this.tArchivesService.findAllDocList(page, tArchives));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/findOne", produces = "application/json;charset=UTF-8")
    public Result selectOne(HttpServletRequest request, @RequestParam("id") Integer id) {
        return Result.success(this.tArchivesService.getById(id));
    }

    /**
     * 通过id查询父id
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping(value = "/getParentId", produces = "application/json;charset=UTF-8")
    public Result getParentId(HttpServletRequest request, @RequestParam(value = "id",required = false) Long id) {
        return Result.success(this.tArchivesService.getParentId(id));
    }

    /**
     * 文件保存
     * 新建文件夹
     *
     * @param tArchivesRequest 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/save", produces = "application/json;charset=UTF-8")
    public Result insert(@RequestBody TArchivesRequest tArchivesRequest) throws Exception{
        return this.tArchivesService.add(tArchivesRequest);
    }

    /**
     * 更改文件名以及文件夹名
     *
     * @param request
     * @param id
     * @param name
     * @return
     */
    @GetMapping(value = "/update", produces = "application/json;charset=UTF-8")
    public Result update(HttpServletRequest request, @RequestParam("id") Long id, @RequestParam("name") String name) {
        TArchives tArchives = new TArchives();
        tArchives.setId(id);
        tArchives.setCurFilename(name);
        return Result.success(this.tArchivesService.updateByTaId(tArchives));
    }

    /**
     * 删除文件或文件夹
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public Result delete(@RequestParam(value = "ids",required = false) List<Long> ids) throws Exception {
        return this.tArchivesService.delete(ids);
    }

    /**
     * 还原
     *
     * @param ids
     * @return
     */
    @PostMapping(value = "/restore", produces = "application/json;charset=UTF-8")
    public Result restore(@RequestParam("ids") List<Long> ids) {
        return Result.success(this.tArchivesService.restore(ids));
    }

    /**
     * 文件移动
     *
     * @param tArchivesRequest 实体对象
     * @return 修改结果
     */
    @PostMapping(value = "/move", produces = "application/json;charset=UTF-8")
    public Result move(@RequestBody TArchivesRequest tArchivesRequest) {
        return Result.success(this.tArchivesService.move(tArchivesRequest));
    }

    /**
     * 更改文件地址
     *
     * @param request
     * @param code
     * @param url
     * @return
     */
    @GetMapping(value = "/updateUrl", produces = "application/json;charset=UTF-8")
    public Result updateUrl(HttpServletRequest request, @RequestParam("code ") String code, @RequestParam("url ") String url) {
        TArchives tArchives = this.tArchivesService.selectByCode(code);
        tArchives.setFileUrl(url);
        return Result.success(this.tArchivesService.updateByTaId(tArchives));
    }

    /**
     * 导出
     */
    @PostMapping(value = "/export", produces = "application/json;charset=UTF-8")
    public void export(HttpServletResponse response, TArchivesRequest tArchives) {

        List<TArchives> tArchivesList = tArchivesService.findAllDoc(tArchives);

        //导出
        ExcelUtils.exportExcel(tArchivesList, TArchives.class, "档案文件表", response);
    }

    /**
     * 彻底删除
     *
     * @param ids 主键结合
     * @return 删除结果
     */
    @PostMapping(value = "ackDelete", produces = "application/json;charset=UTF-8")
    public Result ackDelete(@RequestParam(value = "ids",required = false) List<Long> ids) {
        return Result.success(this.tArchivesService.ackDelete(ids));
    }

    @PostMapping(value = "/app/findAll", produces = "application/json;charset=UTF-8")
    public Result appFindAll(@RequestBody TArchivesRequest tArchives) {
        Page<TArchives> page = new Page<>(tArchives.getPageNo(), tArchives.getPageSize());
        return Result.success(this.tArchivesService.findAll(page, tArchives));
    }

    @GetMapping(value = "/app/findByParentId", produces = "application/json;charset=UTF-8")
    public Result appFindByParentId(TArchivesRequest tArchives) {
        Page<TArchives> page = new Page<>(tArchives.getPageNo(), tArchives.getPageSize());
        return Result.success(this.tArchivesService.appFindByParentId(page, tArchives));
    }

    @GetMapping(value = "/app/getParentId", produces = "application/json;charset=UTF-8")
    public Result appGetParentId(HttpServletRequest request, @RequestParam(value = "id",required = false) String id) {
        if (!id.equals("null")){
            return Result.success(this.tArchivesService.getParentId(Long.parseLong(id)));
        }else {
            return Result.success();
        }

    }
}

