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


import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.BidManagementRequest;
import cn.iocoder.yudao.module.knowledge.controller.requestdto.TViolationRequest;
import cn.iocoder.yudao.module.knowledge.entity.TViolation;
import cn.iocoder.yudao.module.knowledge.service.TViolationService;
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.util.List;

/**
 * 违章表(TViolation)表控制层
 *
 * @author makejava
 * @since 2024-04-13 14:58:19
 */
@RestController
@RequestMapping("tViolation")
public class TViolationController  {

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

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

    /**
     * 服务对象
     */
    @Autowired
    private TViolationService violationService;

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

    /**
     * 导出表格
     * @param response
     * @param tViolation
     */
    @PostMapping(value = "/export",produces = "application/json;charset=UTF-8")
    public void export(HttpServletResponse response, TViolationRequest tViolation) {
        //此处调用service查询需要导出的数据

        List<TViolation> exportList = violationService.selectForExport(tViolation);
        //导出
        ExcelUtils.exportExcel(exportList, TViolation.class, "违章管理文件表", response);
    }

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


    /**
     * 新增数据
     * @param request
     * @return
     */
    @PostMapping(value = "/insert",produces = "application/json;charset=UTF-8")
    public Result insert(@RequestBody TViolationRequest request) {
        return Result.success(violationService.add(request));
    }

    /**
     * 修改数据
     * @return 修改结果
     */
    @PostMapping (value = "/update",produces = "application/json;charset=UTF-8")
    public Result update(@RequestBody TViolationRequest request) {
        violationService.updateByIdVo(request);
        return Result.success();
    }


    /**
     * 删除数据
     * @param ids
     * @return
     */
    @PostMapping(value = "/delete", produces = "application/json;charset=UTF-8")
    public Result delete(@RequestParam("ids") List<Long> ids) {
        return Result.success(violationService.delete(ids));
    }


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


    /**
     * 新增数据
     * @param request
     * @return
     */
    @PostMapping(value = "/app/insert",produces = "application/json;charset=UTF-8")
    public Result insertForApp(@RequestBody TViolationRequest request) {
        return Result.success(violationService.addForApp(request));
    }

    /**
     * 修改数据
     * @return 修改结果
     */
    @PostMapping (value = "/app/update",produces = "application/json;charset=UTF-8")
    public Result updateForApp(@RequestBody TViolationRequest request) {
        violationService.updateByIdVoForApp(request);
        return Result.success();
    }


    /**
     * 删除数据
     * @param ids
     * @return
     */
    @PostMapping(value = "/app/delete", produces = "application/json;charset=UTF-8")
    public Result deleteForApp(@RequestParam("ids") List<Long> ids) {
        return Result.success(violationService.deleteForApp(ids));
    }
    /**
     * 推送
     * @param id
     * @return
     */
    @GetMapping(value = "/app/push",produces = "application/json;charset=UTF-8")
    public Result pushForApp(@RequestParam(value = "id") Long id) {
        violationService.pushForApp(id);
        return Result.success();
    }

    @PostMapping(value = "/app/findAll",produces = "application/json;charset=UTF-8")
    public Result appFindAll(@RequestBody TViolationRequest tViolation) {
        Page<TViolation> page = new Page<>(tViolation.getPageNo(),tViolation.getPageSize());
        return Result.success(violationService.appFindAll(page, tViolation));
    }

}

