package com.ozs.web.controller.base;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ozs.base.domain.BaseFileTemplate;
import com.ozs.base.service.BaseFileTemplateService;
import com.ozs.base.vo.BaseFileTemplatePageReqVo;
import com.ozs.common.annotation.Log;
import com.ozs.common.constant.ModularConstans;
import com.ozs.common.core.controller.BaseController;
import com.ozs.common.core.domain.AjaxResult;
import com.ozs.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Date;
import java.util.List;

@Api(tags = ModularConstans.fileTemplate)
@RestController
@RequestMapping("/base/file")
public class BaseFileTemplateController extends BaseController {

    @Autowired
    private BaseFileTemplateService baseFileTemplateService;


    @ApiOperation(value = "分页查询文件模板信息")
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermi('base:file:list')")
    @Log(title = ModularConstans.fileTemplate, businessType = BusinessType.QUERY)
    public AjaxResult page(@NotEmpty(message = "数据为空") @RequestBody BaseFileTemplatePageReqVo vo) {
        LambdaQueryWrapper<BaseFileTemplate> lw = new LambdaQueryWrapper<BaseFileTemplate>();
        if(!StringUtils.isBlank(vo.getFileName())){
            lw.like(BaseFileTemplate::getFileName,vo.getFileName());
        }
        if(!StringUtils.isBlank(vo.getFileType())){
            lw.eq(BaseFileTemplate::getFileType,vo.getFileType());
        }
        IPage<BaseFileTemplate> page = baseFileTemplateService.page(new Page<BaseFileTemplate>(vo.getPageNum(), vo.getPageSize()), lw);
        return success(page);
    }

    @ApiOperation(value = "新增文件模板信息")
    @PostMapping("/insert")
    @PreAuthorize("@ss.hasPermi('base:file:add')")
    @Log(title = ModularConstans.fileTemplate, businessType = BusinessType.INSERT)
    public AjaxResult insert(@NotEmpty(message = "数据为空")
                                 @RequestBody BaseFileTemplate baseFileTemplate) {
        baseFileTemplate.setCreated(getUserId().toString());
        baseFileTemplate.setCreateTime(new Date());
        baseFileTemplate.setUpdated(baseFileTemplate.getCreated());
        baseFileTemplate.setUpdateTime(baseFileTemplate.getCreateTime());
        return toAjax(baseFileTemplateService.save(baseFileTemplate));
    }

    @ApiOperation(value = "删除文件模板信息")
    @PostMapping("/remove")
    @PreAuthorize("@ss.hasPermi('base:file:remove')")
    @Log(title = ModularConstans.fileTemplate, businessType = BusinessType.DELETE)
    public AjaxResult remove(@NotEmpty(message = "主键id不能为空")
                             @RequestBody  List<Long> ids) {
        return toAjax(baseFileTemplateService.removeBatchByIds(ids));
    }


}
