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


import cn.iocoder.yudao.module.knowledge.service.TRegulatoryFrameworkService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.TRegulatoryFrameworkRequest;
import cn.iocoder.yudao.module.knowledge.entity.TRegulatoryFramework;
import cn.iocoder.yudao.module.knowledge.utils.ExcelUtils;
import cn.iocoder.yudao.module.knowledge.constants.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 规章制度表(TRegulatoryFramework)表控制层
 *
 * @author makejava
 * @since 2024-04-12 17:36:32
 */
@RestController
@RequestMapping("tRegulatoryFramework")
public class TRegulatoryFrameworkController {

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

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

    /**
     * 服务对象
     */
    @Autowired
    private TRegulatoryFrameworkService regulatoryService;

    /**
     * 分页查询所有数据
     *
     * @param regulatoryFrameworkRequest 查询实体
     * @return 所有数据
     */
    @PostMapping(value = "/findAll",produces = "application/json;charset=UTF-8")
    public Result findAll(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest) {
        Page<TRegulatoryFramework> page = new Page<>(regulatoryFrameworkRequest.getPageNo(),regulatoryFrameworkRequest.getPageSize());

        return Result.success(regulatoryService.select(page, regulatoryFrameworkRequest));
    }

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

    /**
     * 新增数据
     *
     * @param regulatoryFrameworkRequest 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/insert",produces = "application/json;charset=UTF-8")
    public Result insert(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest)throws Exception {
        return Result.success(regulatoryService.add(regulatoryFrameworkRequest));
    }

    /**
     * 修改数据
     *
     * @param regulatoryFrameworkRequest 实体对象
     * @return 修改结果
     */
    @PostMapping (value = "/update",produces = "application/json;charset=UTF-8")
    public Result update(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest) throws Exception {
        return Result.success(regulatoryService.updateByTLId(regulatoryFrameworkRequest));
    }

    /**
     * 历史记录
     *
     * @return
     */
    @GetMapping(value = "/history",produces = "application/json;charset=UTF-8")
    public Result history(TRegulatoryFrameworkRequest regulatoryFrameworkRequest) {

        return Result.success(regulatoryService.history(regulatoryFrameworkRequest));
    }

    /**
     * 归档
     * @param id
     * @return
     */
    @GetMapping(value = "/filing",produces = "application/json;charset=UTF-8")
    public Result filing(@RequestParam("id") long id ) throws Exception {
        regulatoryService.filing(id);
        return Result.success();
    }

    /**
     * 导出表格
     * @param response
     */
    @PostMapping(value = "/export",produces = "application/json;charset=UTF-8")
    public void export(HttpServletResponse response, TRegulatoryFrameworkRequest regulatoryFrameworkRequest) {
        //此处调用service查询需要导出的数据
        List<TRegulatoryFramework> tlawsList = regulatoryService.selectForExport(regulatoryFrameworkRequest);
        //导出
        ExcelUtils.exportExcel(tlawsList, TRegulatoryFramework.class, "规章制度文件表", response);

    }

    /**
     * 归档文件夹
     * @return
     */
    @GetMapping(value = "/archiveFolder",produces = "application/json;charset=UTF-8")
    public Result archiveFolder() {
        regulatoryService.archiveFolder();
        return Result.success();
    }

    /**
     * 记录查询
     * @param tLawsRequest
     * @return
     */
    @GetMapping(value = "/recordsQuery",produces = "application/json;charset=UTF-8")
    public Result recordsQuery(TRegulatoryFrameworkRequest tLawsRequest) {

        return Result.success(regulatoryService.recordsQuery(tLawsRequest));
    }


    /**
     * 删除数据
     * @param ids
     * @return
     */

    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        return Result.success(regulatoryService.delete(ids));
    }

    //======app
    /**
     * 分页查询所有数据
     *
     * @param regulatoryFrameworkRequest 查询实体
     * @return 所有数据
     */
    @PostMapping(value = "/app/findAll",produces = "application/json;charset=UTF-8")
    public Result findAllForApp(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest) throws ParseException {
        Page<TRegulatoryFramework> page = new Page<>(regulatoryFrameworkRequest.getPageNo(),regulatoryFrameworkRequest.getPageSize());

        return Result.success(regulatoryService.selectForApp(page, regulatoryFrameworkRequest));
    }

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

    /**
     * 新增数据
     *
     * @param regulatoryFrameworkRequest 实体对象
     * @return 新增结果
     */
    @PostMapping(value = "/app/insert",produces = "application/json;charset=UTF-8")
    public Result insertForApp(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest)throws Exception {
        return Result.success(regulatoryService.addForApp(regulatoryFrameworkRequest));
    }

    /**
     * 修改数据
     *
     * @param regulatoryFrameworkRequest 实体对象
     * @return 修改结果
     */
    @PostMapping (value = "/app/update",produces = "application/json;charset=UTF-8")
    public Result updateForApp(@RequestBody TRegulatoryFrameworkRequest regulatoryFrameworkRequest) throws Exception {
        return Result.success(regulatoryService.updateByTLIdForApp(regulatoryFrameworkRequest));
    }

    /**
     * 历史记录
     *
     * @return
     */
    @GetMapping(value = "/app/history",produces = "application/json;charset=UTF-8")
    public Result historyForApp(TRegulatoryFrameworkRequest regulatoryFrameworkRequest) {

        return Result.success(regulatoryService.historyForApp(regulatoryFrameworkRequest));
    }

    /**
     * 归档
     * @param id
     * @return
     */
    @GetMapping(value = "/app/filing",produces = "application/json;charset=UTF-8")
    public Result filingForApp(@RequestParam("id") long id ) throws Exception {
        regulatoryService.filingForApp(id);
        return Result.success();
    }

    /**
     * 归档文件夹
     * @return
     */
    @GetMapping(value = "/app/archiveFolder",produces = "application/json;charset=UTF-8")
    public Result archiveFolderForApp() {
        regulatoryService.archiveFolderForApp();
        return Result.success();
    }

    /**
     * 记录查询
     * @param tLawsRequest
     * @return
     */
    @GetMapping(value = "/app/recordsQuery",produces = "application/json;charset=UTF-8")
    public Result recordsQueryForApp(TRegulatoryFrameworkRequest tLawsRequest) {

        return Result.success(regulatoryService.recordsQueryForApp(tLawsRequest));
    }


    /**
     * 删除数据
     * @param ids
     * @return
     */

    @PostMapping(value = "/app/delete", produces = "application/json;charset=UTF-8")
    public Result deleteForApp(@RequestParam("ids") List<Long> ids) {
        return Result.success(regulatoryService.deleteForApp(ids));
    }


}

