package com.greenorange.promotion.controller.project;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.greenorange.promotion.annotation.RequiresPermission;
import com.greenorange.promotion.annotation.SysLog;
import com.greenorange.promotion.common.BaseResponse;
import com.greenorange.promotion.common.ErrorCode;
import com.greenorange.promotion.common.ResultUtils;
import com.greenorange.promotion.constant.UserConstant;
import com.greenorange.promotion.exception.ThrowUtils;
import com.greenorange.promotion.model.dto.CommonBatchRequest;
import com.greenorange.promotion.model.dto.projectNotification.ProjectNotificationAddRequest;
import com.greenorange.promotion.model.dto.projectNotification.ProjectNotificationQueryRequest;
import com.greenorange.promotion.model.dto.projectNotification.ProjectNotificationUpdateRequest;
import com.greenorange.promotion.model.entity.Project;
import com.greenorange.promotion.model.entity.ProjectNotification;
import com.greenorange.promotion.model.vo.projectNotification.ProjectNotificationVO;
import com.greenorange.promotion.service.common.CommonService;
import com.greenorange.promotion.service.project.ProjectNotificationService;
import com.greenorange.promotion.service.project.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.greenorange.promotion.model.dto.CommonRequest;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * 项目通知 控制器
 */
@RestController
@RequestMapping("projectNotification")
@Slf4j
@Tag(name = "项目通知管理")
@Transactional
public class ProjectNotificationController {

    @Resource
    private ProjectNotificationService projectNotificationService;

    @Resource
    private CommonService commonService;

    @Resource
    private ProjectService projectService;


    /**
     * 小程序端用户根据id查询项目通知详情
     * @param commonRequest 项目通知添加请求体
     * @return 项目通知详情
     */
    @PostMapping("mini/query/id")
    @Operation(summary = "小程序端用户根据id查询项目通知详情", description = "参数：项目通知id，权限：小程序用户，方法名：miniQueryProjectNotificationById")
    @RequiresPermission(mustRole = UserConstant.DEFAULT_ROLE)
//    @SysLog(title = "项目通知管理", content = "小程序端用户根据id查询项目通知详情")
    public BaseResponse<ProjectNotificationVO> miniQueryProjectNotificationById(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        ProjectNotification projectNotification = projectNotificationService.getById(id);
        ProjectNotificationVO projectNotificationVO = commonService.copyProperties(projectNotification, ProjectNotificationVO.class);
        return ResultUtils.success(projectNotificationVO);
    }


    /**
     * web端管理员添加项目通知
     * @param projectNotificationAddRequest 项目通知添加请求体
     * @return 是否添加成功
     */
    @PostMapping("add")
    @Operation(summary = "web端管理员添加项目通知", description = "参数：项目通知添加请求体，权限：管理员，方法名：addProjectNotification")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "项目通知管理", content = "web端管理员添加项目通知")
    public BaseResponse<Boolean> addProjectNotification(@Valid @RequestBody ProjectNotificationAddRequest projectNotificationAddRequest) {
        Long proId = projectNotificationAddRequest.getProjectId();
        Project pro = projectService.getById(proId);
        ThrowUtils.throwIf(pro == null, ErrorCode.OPERATION_ERROR, "项目不存在");
        ProjectNotification projectNotification = commonService.copyProperties(projectNotificationAddRequest, ProjectNotification.class);
        projectNotificationService.save(projectNotification);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id修改项目通知信息
     * @param projectNotificationUpdateRequest 项目通知更新请求体
     * @return 是否更新成功
     */
    @PostMapping("update")
    @Operation(summary = "web端管理员更新项目通知", description = "参数：项目通知更新请求体，权限：管理员，方法名：updateProjectNotification")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "项目通知管理", content = "web端管理员根据id修改项目通知信息")
    public BaseResponse<Boolean> updateProjectNotification(@Valid @RequestBody ProjectNotificationUpdateRequest projectNotificationUpdateRequest) {
        ProjectNotification projectNotification = commonService.copyProperties(projectNotificationUpdateRequest, ProjectNotification.class);
        projectNotificationService.updateById(projectNotification);
        return ResultUtils.success(true);
    }

    /**
     * web端管理员根据id删除项目通知
     * @param commonRequest 项目通知删除请求体
     * @return 是否删除成功
     */
    @PostMapping("delete")
    @Operation(summary = "web端管理员根据id删除项目通知", description = "参数：项目通知删除请求体，权限：管理员，方法名：delProjectNotification")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "项目通知管理", content = "web端管理员根据id删除项目通知")
    public BaseResponse<Boolean> delProjectNotification(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        projectNotificationService.removeById(id);
        return ResultUtils.success(true);
    }


    /**
     * web端管理员根据id查询项目通知
     * @param commonRequest 项目通知查询请求体
     * @return 项目通知信息
     */
    @PostMapping("queryById")
    @Operation(summary = "web端管理员根据id查询项目通知", description = "参数：项目通知查询请求体，权限：管理员，方法名：queryProjectNotificationById")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "项目通知管理", content = "web端管理员根据id查询项目通知")
    public BaseResponse<ProjectNotificationVO> queryProjectNotificationById(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        ProjectNotification projectNotification = projectNotificationService.getById(id);
        ProjectNotificationVO projectNotificationVO = commonService.copyProperties(projectNotification, ProjectNotificationVO.class);
        return ResultUtils.success(projectNotificationVO);
    }


    /**
     * web端管理员根据项目id查询项目通知
     * @param commonRequest 项目id
     * @return 项目通知列表
     */
    @PostMapping("query/pid")
    @Operation(summary = "web端管理员根据项目id查询项目通知", description = "参数：项目id，权限：管理员，方法名：queryProjectNotificationByPid")
    @RequiresPermission(mustRole = UserConstant.ADMIN_ROLE)
//    @SysLog(title = "项目通知管理", content = "web端管理员根据项目id查询项目通知")
    public BaseResponse<List<ProjectNotificationVO>> queryProjectNotificationByPid(@Valid @RequestBody CommonRequest commonRequest) {
        Long id = commonRequest.getId();
        List<ProjectNotification> projectNotificationList = commonService.findByFieldEqTargetField(ProjectNotification::getProjectId, id, projectNotificationService);
        List<ProjectNotificationVO> projectNotificationVOS = commonService.convertList(projectNotificationList, ProjectNotificationVO.class);
        return ResultUtils.success(projectNotificationVOS);
    }


}