package com.xiaoniu.subsystem.business.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.common.core.result.R;
import com.xiaoniu.common.core.utils.BeanUtil;
import com.xiaoniu.subsystem.business.domain.Demand;
import com.xiaoniu.subsystem.business.domain.Project;
import com.xiaoniu.subsystem.business.domain.ProjectUnit;
import com.xiaoniu.subsystem.business.domain.vo.ProjectUnitVO;
import com.xiaoniu.subsystem.business.service.IDemandService;
import com.xiaoniu.subsystem.business.service.IProjectService;
import com.xiaoniu.subsystem.business.service.IProjectUnitService;
import com.xiaoniu.subsystem.system.domain.Unit;
import com.xiaoniu.subsystem.system.service.IUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUnitId;

/**
 * <p>
 * 培训计划关联单位管理表 前端控制器
 * </p>
 *
 * @author haoxuyang
 * @since 2024-12-17 03:03:41
 */
@RestController
@AllArgsConstructor
@RequestMapping("/projectUnit")
@Tag(name = "培训计划分配单位管理表", description = "培训计划分配单位管理表")
public class ProjectUnitController {

    private IProjectUnitService projectUnitService;
    private IUnitService unitService;
    private IDemandService demandService;
    private IProjectService projectService;
    /**
     * 分配员额列表
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 1)
    @Transactional(rollbackFor = Exception.class)
    @Operation(summary = "分配员额列表", description = "传入dto")
    public R<List<ProjectUnitVO>> list(@Parameter(description = "计划ID", required = true)
                                                       @RequestParam Long projectId) {
        List<ProjectUnit> list = projectUnitService.list(new LambdaQueryWrapper<ProjectUnit>()
                .eq(ProjectUnit::getProjectId, projectId));
        if(CollectionUtils.isEmpty(list)) {
            List<Unit> units = unitService.list(new LambdaQueryWrapper<Unit>()
                    .eq(Unit::getParentId, getUnitId()));
            if (CollectionUtils.isNotEmpty(units)){
                Project project = projectService.getById(projectId);
                Demand demand = demandService.getOne(new LambdaQueryWrapper<Demand>()
                        .eq(Demand::getFlightId,project.getFlightId())
                        .eq(Demand::getCreateDept,getUnitId()));
                return R.data(demandService.getPeopleNum(demand, units));
            }
        }
        return R.data(BeanUtil.copy(list,ProjectUnitVO.class));
    }

}
