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


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.DeleteRequest;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.TLawsRequest;
import cn.iocoder.yudao.module.knowledge.entity.TLaws;
import cn.iocoder.yudao.module.knowledge.service.TLawsService;
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;

/**
 * 法律法规表(TLaws)表控制层
 *
 * @author makejava
 * @since 2024-04-12 10:31:34
 */
@RestController
@RequestMapping("tLaws")
public class TLawsController {

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

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

    /**
     * 服务对象
     */
    @Autowired
    private TLawsService tLawsService;

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

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

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

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

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

        return Result.success(tLawsService.history(tLaws));
    }

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

    /**
     * 导出表格
     * @param response
     * @param tLaws
     */
    @PostMapping(value = "/export",produces = "application/json;charset=UTF-8")
    public void export(HttpServletResponse response, TLawsRequest tLaws) {
        //此处调用service查询需要导出的数据
        List<TLaws> tlawsList = tLawsService.selectForExport(tLaws);
        //导出
        ExcelUtils.exportExcel(tlawsList, TLaws.class, "法律法规文件表", response);
    }

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

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

        return Result.success(tLawsService.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(this.tLawsService.delete(ids));
    }

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

    /**
     * app首页文档
     * @param tLaws
     * @return
     */
    @PostMapping(value = "/app/findAllDocuments",produces = "application/json;charset=UTF-8")
    public Result findAllDocuments(@RequestBody TLawsRequest tLaws) {
        Page<TLaws> page = new Page<>(tLaws.getPageNo(),tLaws.getPageSize());
        return Result.success(tLawsService.findAllDocuments(page, tLaws));
    }

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

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

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

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

        return Result.success(tLawsService.historyForApp(tLaws));
    }

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

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

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

        return Result.success(tLawsService.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(tLawsService.deleteForApp(ids));
    }


}

