package cn.cslg.pas.controller;

import cn.cslg.pas.common.PageHelperT;
import cn.cslg.pas.common.core.base.Constants;
import cn.cslg.pas.common.model.QueryPREntity;
import cn.cslg.pas.common.model.dto.PermissionRecordDTO;
import cn.cslg.pas.common.model.dto.PermissionRecordUpdateDTO;
import cn.cslg.pas.common.model.vo.PermissionRecordVO;
import cn.cslg.pas.common.utils.Response;
import cn.cslg.pas.mapper.PermissionRecordMapper;
import cn.cslg.pas.service.IPermissionRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 许可记录的Controller层
 *
 * @Author xiexiang
 * @Date 2023/3/9
 */
@Tag(name = "许可记录模块")
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(Constants.API_VERSION_V2 + "/permissionRecord")
public class PermissionRecordController {
    private final IPermissionRecordService permissionRecordService;  //许可记录的Service层装配
    private final PermissionRecordMapper permissionRecordMapper;

    @Operation(summary = "新增许可记录")
    @PostMapping("/add")
    public String add(@RequestBody PermissionRecordDTO permissionRecordDTO) {
        if (permissionRecordDTO == null) {
            return Response.error("请求参数为null");
        } else {
            log.info("开始处理【新增许可记录】的请求，请求参数为：{}", permissionRecordDTO);
            permissionRecordService.add(permissionRecordDTO);
            return Response.success("新增许可记录完成");
        }
    }

    @Operation(summary = "修改许可记录")
    @PostMapping("/update")
    public String update(@RequestBody PermissionRecordUpdateDTO permissionRecordUpdateDTO) {
        if (permissionRecordUpdateDTO == null) {
            return Response.error("请求参数为null");
        } else {
            log.info("开始处理【修改许可记录】的请求，请求参数为：{}", permissionRecordUpdateDTO);
            permissionRecordService.update(permissionRecordUpdateDTO);
            return Response.success("修改许可记录完成");
        }
    }

    @Operation(summary = "根据许可id删除许可记录数据信息")
    @GetMapping("/delete")
    public String delete(Integer id) {
        log.info("开始处理【删除许可记录】的请求，请求参数为：{}", id);
        if (id != null) {
            permissionRecordService.delete(id);
            return Response.success("删除许可记录完成");
        }
        return Response.error("请求参数为null");
    }

    @Operation(summary = "根据专利号分页查询许可记录数据信息")
    @PostMapping("/query")
    public String query(@RequestBody QueryPREntity queryPREntity) {
        List<PermissionRecordVO> queryResult = permissionRecordService.findAllByPage(queryPREntity);
        PageHelperT pageHelperT = new PageHelperT();
        pageHelperT.setList(queryResult);
        pageHelperT.setCurrent(queryPREntity.getCurrent());
        pageHelperT.setSize(queryPREntity.getSize());
        pageHelperT.setTotal(permissionRecordService.countByPatentNo(queryPREntity.getPatentNo()));
        return Response.success(pageHelperT);
    }
}
