package cn.iocoder.yudao.module.bpm.controller.admin.oa;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.incentive.BpmOAIncentiveCreateReqVO;
import cn.iocoder.yudao.module.bpm.controller.admin.oa.vo.incentive.BpmOAIncentiveRespVO;
import cn.iocoder.yudao.module.bpm.convert.oa.BpmOAIncentiveConvert;
import cn.iocoder.yudao.module.bpm.dal.dataobject.oa.BpmOAIncentiveDO;
import cn.iocoder.yudao.module.bpm.service.oa.BpmOAIncentiveService;
import cn.iocoder.yudao.module.system.api.dept.DeptApi;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

/**
 * OA 奖惩申请 Controller
 *
 * @author 符溶馨

 */
@Tag(name = "管理后台 - OA 奖惩申请")
@RestController
@RequestMapping("/bpm/oa/incentive")
@Validated
public class BpmOAIncentiveController {

    @Resource
    private BpmOAIncentiveService incentiveService;

    @Resource
    private AdminUserApi userApi;

    @Resource
    private DeptApi deptApi;

    @PostMapping("/create")
    @Operation(summary = "创建请求申请")
    public CommonResult<Long> createIncentive(@Valid @RequestBody BpmOAIncentiveCreateReqVO createReqVO) {

        return success(incentiveService.createIncentive(getLoginUserId(), createReqVO));
    }

    @GetMapping("/get")
    @Operation(summary = "获得奖惩申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<BpmOAIncentiveRespVO> getIncentive(@RequestParam("id") Long id) {

        BpmOAIncentiveDO incentive = incentiveService.getIncentive(id);

        //获得 用户信息集合
        List<AdminUserRespDTO> userRespDTO = userApi.getUserListByDeptIds(incentive.getIncentiveDeptIds()).getCheckedData();

        //获得 部门信息集合
        Map<Long, DeptRespDTO> deptRespDTOS = deptApi.getDeptMap(incentive.getIncentiveDeptIds());

        Map<String, List<String>> userInfo = new HashMap<>();
        //遍历 部门map集合
        deptRespDTOS.forEach((key, value) -> {

            List<String> userName = userRespDTO.stream().filter(user -> user.getDeptId().toString().contains(key.toString())
                            && incentive.getIncentiveUserIds().contains(user.getId()))
                    .map(AdminUserRespDTO::getNickname).collect(Collectors.toList());

            userInfo.put(value.getName(), userName);
        });

        BpmOAIncentiveRespVO bpmOAIncentiveRespVO = BpmOAIncentiveConvert.INSTANCE.convert(incentive);
        bpmOAIncentiveRespVO.setUserInfo(userInfo);
        return success(bpmOAIncentiveRespVO);
    }
}
