package com.bs.ct.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.ct.domain.CtGalleryShareUser;
import com.bs.ct.service.ICtGalleryShareUserService;
import javax.annotation.Resource;

/**
 * 图库目录共享用户Controller
 *
 * @author bs
 * @date 2025-06-14
 */
@Api(tags = "图库目录共享用户")
@RestController
@RequestMapping("/gallery/share/user")
public class CtGalleryShareUserController extends BaseController {
    @Resource
    private ICtGalleryShareUserService ctGalleryShareUserService;

    /**
     * 分页查询图库目录共享用户列表
     */
    @ApiOperation("分页查询图库目录共享用户列表")
    @GetMapping("/pageList")
    public TableDataInfo pageList(CtGalleryShareUser ctGalleryShareUser) {
        startPage();
        LambdaQueryWrapper<CtGalleryShareUser> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryShareUser);
        List<CtGalleryShareUser> list = ctGalleryShareUserService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 查询图库目录共享用户列表
     */
    @ApiOperation("查询图库目录共享用户列表")
    @GetMapping("/list")
    public AjaxResult list(CtGalleryShareUser ctGalleryShareUser) {
        LambdaQueryWrapper<CtGalleryShareUser> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryShareUser);
        List<CtGalleryShareUser> list = ctGalleryShareUserService.list(queryWrapper);
        return success(list);
    }

    /**
     * 导出图库目录共享用户列表
     */
    @ApiOperation("导出图库目录共享用户列表")
    @Log(title = "图库目录共享用户导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CtGalleryShareUser ctGalleryShareUser) {
        LambdaQueryWrapper<CtGalleryShareUser> queryWrapper = new LambdaQueryWrapper();
        condition(queryWrapper,ctGalleryShareUser);
        List<CtGalleryShareUser> list = ctGalleryShareUserService.list(queryWrapper);
        ExcelUtil<CtGalleryShareUser> util = new ExcelUtil<CtGalleryShareUser>(CtGalleryShareUser. class);
        util.exportExcel(response, list, "图库目录共享用户数据");
    }

    /**
     * 获取图库目录共享用户详细信息
     */
    @ApiOperation("获取图库目录共享用户详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(ctGalleryShareUserService.getById(id));
    }

    /**
     * 新增图库目录共享用户
     */
    @ApiOperation("新增图库目录共享用户")
    @Log(title = "图库目录共享用户新增", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CtGalleryShareUser ctGalleryShareUser) {
        return toAjax(ctGalleryShareUserService.save(ctGalleryShareUser));
    }

    /**
     * 修改图库目录共享用户
     */
    @ApiOperation("修改图库目录共享用户")
    @Log(title = "图库目录共享用户修改", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CtGalleryShareUser ctGalleryShareUser) {
        return toAjax(ctGalleryShareUserService.updateById(ctGalleryShareUser));
    }

    /**
     * 删除图库目录共享用户
     */
    @ApiOperation("删除图库目录共享用户")
    @Log(title = "图库目录共享用户删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(ctGalleryShareUserService.removeBatchByIds(ids));
    }

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


        //id
        if(null != ctGalleryShareUser.getId()){
            queryWrapper.eq(CtGalleryShareUser::getId,ctGalleryShareUser.getId());
        }

        //共享id
        if(null != ctGalleryShareUser.getShareId()){
            queryWrapper.eq(CtGalleryShareUser::getShareId,ctGalleryShareUser.getShareId());
        }

        //共享用户
        if(null != ctGalleryShareUser.getUserId()){
            queryWrapper.eq(CtGalleryShareUser::getUserId,ctGalleryShareUser.getUserId());
        }

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

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

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

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

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

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

        //租户ID
        if(StringUtils.isNotEmpty(ctGalleryShareUser.getTenantId())){
            queryWrapper.eq(CtGalleryShareUser::getTenantId,ctGalleryShareUser.getTenantId());
        }

        }

}
