package cn.iocoder.yudao.module.mes.controller.admin.workteam;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamDetailRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.workteam.vo.WorkTeamSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.workteam.WorkTeamDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.workteam.WorkTeamDetailDO;
import cn.iocoder.yudao.module.mes.service.workteam.WorkTeamService;
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.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 生产班组")
@RestController
@RequestMapping("/mes/work-team")
@Validated
public class WorkTeamController {

    @Resource
    private WorkTeamService workTeamService;

    @PostMapping("/create")
    @Operation(summary = "创建生产班组")
    @PreAuthorize("@ss.hasPermission('mes:work-team:create')")
    public CommonResult<Long> createWorkTeam(@Valid @RequestBody WorkTeamSaveReqVO createReqVO) {
        return success(workTeamService.createWorkTeam(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新生产班组")
    @PreAuthorize("@ss.hasPermission('mes:work-team:update')")
    public CommonResult<Boolean> updateWorkTeam(@Valid @RequestBody WorkTeamSaveReqVO updateReqVO) {
        workTeamService.updateWorkTeam(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除生产班组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:work-team:delete')")
    public CommonResult<Boolean> deleteWorkTeam(@RequestParam("id") Long id) {
        workTeamService.deleteWorkTeam(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得生产班组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:work-team:query')")
    public CommonResult<WorkTeamRespVO> getWorkTeam(@RequestParam("id") Long id) {
        WorkTeamDO workTeam = workTeamService.getWorkTeam(id);
        return success(BeanUtils.toBean(workTeam, WorkTeamRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得生产班组分页")
    @PreAuthorize("@ss.hasPermission('mes:work-team:query')")
    public CommonResult<PageResult<WorkTeamRespVO>> getWorkTeamPage(@Valid WorkTeamPageReqVO pageReqVO) {
        PageResult<WorkTeamRespVO> pageResult = workTeamService.getWorkTeamPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出生产班组 Excel")
    @PreAuthorize("@ss.hasPermission('mes:work-team:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportWorkTeamExcel(@Valid WorkTeamPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WorkTeamRespVO> list = workTeamService.getWorkTeamPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "生产班组.xls", "数据", WorkTeamRespVO.class,
                list);
    }

    // ==================== 子表（班组成员） ====================

    @GetMapping("/work-team-detail/page")
    @Operation(summary = "获得班组成员分页")
    @Parameter(name = "workTeamId", description = "班组id")
    @PreAuthorize("@ss.hasPermission('mes:work-team:query')")
    public CommonResult<PageResult<WorkTeamDetailRespVO>> getWorkTeamDetailPage(PageParam pageReqVO,
                                                                                @RequestParam("workTeamId") Long workTeamId) {
        return success(workTeamService.getWorkTeamDetailPage(pageReqVO, workTeamId));
    }

    @PostMapping("/work-team-detail/create")
    @Operation(summary = "创建班组成员")
    @PreAuthorize("@ss.hasPermission('mes:work-team:create')")
    public CommonResult<Long> createWorkTeamDetail(@Valid @RequestBody WorkTeamDetailDO workTeamDetail) {
        return success(workTeamService.createWorkTeamDetail(workTeamDetail));
    }

    @PutMapping("/work-team-detail/update")
    @Operation(summary = "更新班组成员")
    @PreAuthorize("@ss.hasPermission('mes:work-team:update')")
    public CommonResult<Boolean> updateWorkTeamDetail(@Valid @RequestBody WorkTeamDetailDO workTeamDetail) {
        workTeamService.updateWorkTeamDetail(workTeamDetail);
        return success(true);
    }

    @DeleteMapping("/work-team-detail/delete")
    @Parameter(name = "id", description = "编号", required = true)
    @Operation(summary = "删除班组成员")
    @PreAuthorize("@ss.hasPermission('mes:work-team:delete')")
    public CommonResult<Boolean> deleteWorkTeamDetail(@RequestParam("id") Long id) {
        workTeamService.deleteWorkTeamDetail(id);
        return success(true);
    }

    @GetMapping("/work-team-detail/get")
    @Operation(summary = "获得班组成员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:work-team:query')")
    public CommonResult<WorkTeamDetailDO> getWorkTeamDetail(@RequestParam("id") Long id) {
        return success(workTeamService.getWorkTeamDetail(id));
    }

    @GetMapping("/work-team-detail/getUserList")
    @Operation(summary = "获得班组成员")
    @Parameter(name = "processType", description = "班组类型", required = true)
    public CommonResult<List<AdminUserRespDTO>> getUserList(@RequestParam("processType") String processType,String groupType) {
        return success(workTeamService.getUserList(processType,groupType));
    }
    @GetMapping("/work-team-detail/getUserList2")
    @Operation(summary = "获得班组成员")
    @Parameter(name = "processType", description = "班组类型", required = true)
    public CommonResult<List<AdminUserRespDTO>> getUserList2(@RequestParam("orgId") Long orgId,String groupType) {
        return success(workTeamService.getUserList2(orgId,groupType));
    }
}