package org.dromara.system.controller.project;

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.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.system.domain.BusUserProjectRelevancy;
import org.dromara.system.domain.dto.project.BusProjectBatchByProjectListReq;
import org.dromara.system.domain.dto.userprojectrelevancy.BusUserProjectRelevancyCreateReq;
import org.dromara.system.domain.dto.userprojectrelevancy.BusUserProjectRelevancyQueryReq;
import org.dromara.system.domain.dto.userprojectrelevancy.BusUserProjectRelevancyUpdateReq;
import org.dromara.system.domain.vo.userprojectrelevancy.BusLoginUserProjectRelevancyVo;
import org.dromara.system.domain.vo.userprojectrelevancy.BusUserProjectRelevancyVo;
import org.dromara.system.service.IBusUserProjectRelevancyService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 系统用户与项目关联
 *
 * @author lilemy
 * @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(BusUserProjectRelevancyQueryReq req, PageQuery pageQuery) {
        return userProjectRelevancyService.queryPageList(req, pageQuery);
    }

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

    /**
     * 查询登录用户与项目关联分页
     */
    @GetMapping("/login/page")
    public TableDataInfo<BusUserProjectRelevancyVo> pageByLoginUser(BusUserProjectRelevancyQueryReq 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(BusUserProjectRelevancyQueryReq 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 @RequestBody BusUserProjectRelevancyCreateReq req) {
        return R.ok(userProjectRelevancyService.insertByBo(req));
    }

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

    /**
     * 移除用户与项目列表关联
     */
    @SaCheckPermission("project:projectRelevancy:removeProjectList")
    @Log(title = "系统用户与项目关联", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @DeleteMapping("/remove/project/list")
    public R<Boolean> removeBatchByProjectList(@RequestBody BusProjectBatchByProjectListReq req) {
        Long userId = req.getUserId();
        List<Long> projectIdList = req.getProjectIdList();
        QueryWrapper<BusUserProjectRelevancy> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId).in("project_id", 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 @RequestBody BusUserProjectRelevancyUpdateReq 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));
    }
}
