package com.bs.cm.controller;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bs.common.annotation.Log;
import com.bs.common.core.controller.BaseController;
import com.bs.common.core.domain.AjaxResult;
import com.bs.common.core.page.TableDataInfo;
import com.bs.common.enums.BusinessType;
import com.bs.common.utils.poi.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang3.StringUtils;
import com.bs.cm.domain.CmAttach;
import com.bs.cm.service.ICmAttachService;
import javax.annotation.Resource;

/**
 * 附近信息Controller
 *
 * @author bs
 * @date 2025-03-04
 */
@Api(tags = "附近信息")
@RestController
@RequestMapping("/cm/attach")
public class CmAttachController extends BaseController {
    @Resource
    private ICmAttachService cmAttachService;

    /**
     * 分页查询附近信息列表
     */
    @ApiOperation("分页查询附近信息列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CmAttach cmAttach) {
        startPage();
        LambdaQueryWrapper<CmAttach> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmAttach);
        List<CmAttach> list = cmAttachService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询附近信息列表
     */
    @ApiOperation("查询附近信息列表")
    @GetMapping("/list")
    public AjaxResult list(CmAttach cmAttach) {
        LambdaQueryWrapper<CmAttach> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmAttach);
        List<CmAttach> list = cmAttachService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出附近信息列表
     */
    @ApiOperation("导出附近信息列表")
    @Log(title = "附近信息导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CmAttach cmAttach) {
        LambdaQueryWrapper<CmAttach> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,cmAttach);
        List<CmAttach> list = cmAttachService.list(queryWrapper);
        ExcelUtil<CmAttach> util = new ExcelUtil<CmAttach>(CmAttach. class);
        util.exportExcel(response, list, "附近信息数据");
    }

    /**
     * 获取附近信息详细信息
     */
    @ApiOperation("获取附近信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(cmAttachService.getById(id));
    }

    /**
     * 新增附近信息
     */
    @ApiOperation("新增附近信息")
    @Log(title = "附近信息新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmAttach cmAttach) {
        return toAjax(cmAttachService.save(cmAttach));
    }

    /**
     * 修改附近信息
     */
    @ApiOperation("修改附近信息")
    @Log(title = "附近信息修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmAttach cmAttach) {
        return toAjax(cmAttachService.updateById(cmAttach));
    }

    /**
     * 删除附近信息
     */
    @ApiOperation("删除附近信息")
    @Log(title = "附近信息删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(cmAttachService.removeBatchByIds(ids));
    }

    /**
      * 条件设置
      */
    private void condition (LambdaQueryWrapper<CmAttach> queryWrapper,CmAttach cmAttach){


        //用户id
        if(null != cmAttach.getId()){
            queryWrapper.eq(CmAttach::getId,cmAttach.getId());
        }

        //文件组id
        if(StringUtils.isNotEmpty(cmAttach.getFileId())){
            queryWrapper.eq(CmAttach::getFileId,cmAttach.getFileId());
        }

        //文件排序
        if(null != cmAttach.getFileSort()){
            queryWrapper.eq(CmAttach::getFileSort,cmAttach.getFileSort());
        }

        //文件名称（编译后）
        if(StringUtils.isNotEmpty(cmAttach.getAttachName())){
            queryWrapper.eq(CmAttach::getAttachName,cmAttach.getAttachName());
        }

        //文件类型
        if(StringUtils.isNotEmpty(cmAttach.getAttachFileType())){
            queryWrapper.eq(CmAttach::getAttachFileType,cmAttach.getAttachFileType());
        }

        //文件编码类型
        if(StringUtils.isNotEmpty(cmAttach.getAttachContentType())){
            queryWrapper.eq(CmAttach::getAttachContentType,cmAttach.getAttachContentType());
        }

        //文件大小
        if(null != cmAttach.getAttachFileSize()){
            queryWrapper.eq(CmAttach::getAttachFileSize,cmAttach.getAttachFileSize());
        }

        //文件路径
        if(StringUtils.isNotEmpty(cmAttach.getAttachFileUrl())){
            queryWrapper.eq(CmAttach::getAttachFileUrl,cmAttach.getAttachFileUrl());
        }

        //文件名（原始）
        if(StringUtils.isNotEmpty(cmAttach.getOldName())){
            queryWrapper.eq(CmAttach::getOldName,cmAttach.getOldName());
        }

        //文件版本号
        if(StringUtils.isNotEmpty(cmAttach.getVersionNo())){
            queryWrapper.eq(CmAttach::getVersionNo,cmAttach.getVersionNo());
        }

        //创建部门
        if(null != cmAttach.getCreateDept()){
            queryWrapper.eq(CmAttach::getCreateDept,cmAttach.getCreateDept());
        }

        //创建人
        if(null != cmAttach.getCreateBy()){
            queryWrapper.eq(CmAttach::getCreateBy,cmAttach.getCreateBy());
        }

        //创建时间
        if(null != cmAttach.getCreateTime()){
            queryWrapper.eq(CmAttach::getCreateTime,cmAttach.getCreateTime());
        }

        //修改人
        if(null != cmAttach.getUpdateBy()){
            queryWrapper.eq(CmAttach::getUpdateBy,cmAttach.getUpdateBy());
        }

        //修改时间
        if(null != cmAttach.getUpdateTime()){
            queryWrapper.eq(CmAttach::getUpdateTime,cmAttach.getUpdateTime());
        }

        //备注
        if(StringUtils.isNotEmpty(cmAttach.getRemark())){
            queryWrapper.eq(CmAttach::getRemark,cmAttach.getRemark());
        }

        //删除标志（0代表存在 2代表删除）
        if(StringUtils.isNotEmpty(cmAttach.getDelFlag())){
            queryWrapper.eq(CmAttach::getDelFlag,cmAttach.getDelFlag());
        }

        //${column.columnComment}
        if(StringUtils.isNotEmpty(cmAttach.getTenantId())){
            queryWrapper.eq(CmAttach::getTenantId,cmAttach.getTenantId());
        }

        }

}
