package org.dromara.project.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.project.domain.BusUserProjectRelevancy;
import org.dromara.project.domain.req.project.ProjectBatchByProjectListReq;
import org.dromara.project.domain.req.userprojectrelevancy.UserProjectRelevancyCreateReq;
import org.dromara.project.domain.req.userprojectrelevancy.UserProjectRelevancyQueryReq;
import org.dromara.project.domain.req.userprojectrelevancy.UserProjectRelevancyUpdateReq;
import org.dromara.project.domain.resp.userprojectrelevancy.LoginUserProjectRelevancyResp;
import org.dromara.project.domain.vo.BusUserProjectRelevancyVo;
import org.dromara.project.service.IBusUserProjectRelevancyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户与项目关联
 *
 * @author lcj
 * @date 2025-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/project/projectRelevancy")
public class BusUserProjectRelevancyController extends BaseController {

    private final IBusUserProjectRelevancyService userProjectRelevancyService;

    /**
     * 查询用户与项目关联列表
     */
    @SaCheckPermission("project:projectRelevancy:list")
    @GetMapping("/list")
    public TableDataInfo<BusUserProjectRelevancyVo> list(UserProjectRelevancyQueryReq req, PageQuery pageQuery) {
        return userProjectRelevancyService.queryPageList(req, pageQuery);
    }

    /**
     * 查询登录用户与项目关联列表
     */
    @SaCheckPermission("project:projectRelevancy:list")
    @GetMapping("/login/list")
    public R<List<LoginUserProjectRelevancyResp>> listByLoginUser() {
        Long userId = LoginHelper.getUserId();
        return R.ok(userProjectRelevancyService.queryListByUserId(userId));
    }

    /**
     * 查询登录用户与项目关联分页
     */
    @SaCheckPermission("project:projectRelevancy:list")
    @GetMapping("/login/page")
    public TableDataInfo<BusUserProjectRelevancyVo> pageByLoginUser(UserProjectRelevancyQueryReq req, PageQuery pageQuery) {
        Long userId = LoginHelper.getUserId();
        return userProjectRelevancyService.queryPageByUserId(userId, req, pageQuery);
    }

    /**
     * 导出用户与项目关联列表
     */
    @SaCheckPermission("project:projectRelevancy:export")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(UserProjectRelevancyQueryReq req, HttpServletResponse response) {
        List<BusUserProjectRelevancyVo> list = userProjectRelevancyService.queryList(req);
        ExcelUtil.exportExcel(list, "系统用户与项目关联", BusUserProjectRelevancyVo.class, response);
    }

    /**
     * 获取用户与项目关联详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("project:projectRelevancy:query")
    @GetMapping("/{id}")
    public R<BusUserProjectRelevancyVo> getInfo(@NotNull(message = "主键不能为空")
                                                @PathVariable Long id) {
        return R.ok(userProjectRelevancyService.queryById(id));
    }

    /**
     * 新增用户与项目关联
     */
    @SaCheckPermission("project:projectRelevancy:add")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Long> add(@Validated(AddGroup.class) @RequestBody UserProjectRelevancyCreateReq req) {
        return R.ok(userProjectRelevancyService.insertByBo(req));
    }

    /**
     * 新增用户与项目列表关联
     */
    @SaCheckPermission("project:projectRelevancy:add")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/add/project/list")
    public R<Boolean> addBatchByProjectList(@RequestBody ProjectBatchByProjectListReq req) {
        Boolean result = userProjectRelevancyService.insertBatchByProjectList(req);
        return R.ok(result);
    }

    /**
     * 移除用户与项目列表关联
     */
    @SaCheckPermission("project:projectRelevancy:remove")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @DeleteMapping("/remove/project/list")
    public R<Boolean> removeBatchByProjectList(@RequestBody ProjectBatchByProjectListReq req) {
        Long userId = req.getUserId();
        Long[] projectIdList = req.getProjectIdList();
        QueryWrapper<BusUserProjectRelevancy> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).in("project_id", (Object[]) projectIdList);
        boolean result = userProjectRelevancyService.remove(wrapper);
/*        List<UserProjectRelevancy> userProjectRelevancyList = Arrays.stream(projectIdList).map(projectId -> {
            UserProjectRelevancy userProjectRelevancy = new UserProjectRelevancy();
            userProjectRelevancy.setUserId(userId);
            userProjectRelevancy.setProjectId(projectId);
            return userProjectRelevancy;
        }).toList();
        boolean result = userProjectRelevancyService.removeBatchByIds(userProjectRelevancyList);*/
        return R.ok(result);
    }


    /**
     * 修改用户与项目关联
     */
    @SaCheckPermission("project:projectRelevancy:edit")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody UserProjectRelevancyUpdateReq req) {
        return toAjax(userProjectRelevancyService.updateByBo(req));
    }

    /**
     * 删除用户与项目关联
     *
     * @param ids 主键串
     */
    @SaCheckPermission("project:projectRelevancy:remove")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userProjectRelevancyService.deleteWithValidByIds(List.of(ids), true));
    }
}
