package com.ruoyi.postCheck.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.postCheck.domain.PostTeam;
import com.ruoyi.postCheck.service.IPostTeamService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;

/**
 * 岗检小组Controller
 * TODO $岗检小组业务说明
 * 1、【岗检工作组】下的【岗检小组】是自定义的名称与部门无关；
 * 2、第一个【岗检小组】被设置为【牵头小组】；
 * 3、【岗检小组】小组中的人员是从全公司下抽调的，第一个被抽调的人设置为【组长】。
 * 4、【岗检小组】【组长】即牵头部门经理。
 *
 * @author csg
 * @date 2023-12-19
 */
@Api(tags = "2.2.1-岗检小组接口")
@RestController
@RequestMapping("/postCheck/team")
public class PostTeamController extends BaseController {
    @Autowired
    private IPostTeamService postTeamService;

    /**
     * 查询岗检小组列表
     */
    @ApiOperation("查询岗检小组列表")
    @PreAuthorize("@ss.hasPermi('postCheck:team:list')")
    @GetMapping("/list")
    public TableDataInfo list(PostTeam postTeam) {
        startPage();
        //岗检联合办公室
        postTeam.setParentId(0l);
        List<PostTeam> list = postTeamService.selectPostTeamList(postTeam);
//        if (!list.isEmpty()) {
        for (PostTeam root : list) {
            //查工作组（二级）：综合岗检工作组 / 生产岗检工作组
            PostTeam geoupTeamParent = new PostTeam();
            geoupTeamParent.setParentId(root.getId());
            List<PostTeam> groupChildrenList = postTeamService.selectPostTeamList(geoupTeamParent);
            for (PostTeam geoupTeam : groupChildrenList) {
                //查工作小组（三级）
                PostTeam teamParent = new PostTeam();
                teamParent.setParentId(geoupTeam.getId());
                List<PostTeam> teamChildrenList = postTeamService.selectPostTeamList(teamParent);
                //把工作小组添加到列表中
                geoupTeam.setChildren(teamChildrenList);
            }
            //把工作组添加到列表中
            root.setChildren(groupChildrenList);
        }
//        }


        return getDataTable(list);
    }

    /**
     * 导出岗检小组列表
    @ApiOperation("导出岗检小组列表")
    @PreAuthorize("@ss.hasPermi('postCheck:team:export')")
    @Log(title = "岗检小组", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PostTeam postTeam) {
        List<PostTeam> list = postTeamService.selectPostTeamList(postTeam);
        ExcelUtil<PostTeam> util = new ExcelUtil<PostTeam>(PostTeam.class);
        util.exportExcel(response, list, "岗检小组数据");
    }
     */

    /**
     * 获取岗检小组详细信息
     */
    @ApiOperation("获取岗检小组详细信息")
    @PreAuthorize("@ss.hasPermi('postCheck:team:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(postTeamService.selectPostTeamById(id));
    }

    /**
     * 新增岗检小组
     */
    @ApiOperation("新增岗检小组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "name", value = "名称", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "parentId", value = "父ID（0代表根节点）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "status", value = "状态（1-启用；2-未启用）", dataType = "Long", dataTypeClass = Long.class),
//            @ApiImplicitParam(name = "delFlag", value = "删除标志（0代表存在 2代表删除）", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "createUserId", value = "创建者用户ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "type", value = "类型：1-联合办公室；2-岗检工作组；3-工作小组；", dataType = "Integer", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "teamType", value = "type=3时，工作小组权限（1-牵头；2-成员）", dataType = "Integer", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "children", value = "子对象", dataType = "PostTeam.java", dataTypeClass = PostTeam.class),
    })
    @PreAuthorize("@ss.hasPermi('postCheck:team:add')")
    @Log(title = "岗检小组", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PostTeam postTeam) {
        return toAjax(postTeamService.insertPostTeam(postTeam));
    }

    /**
     * 修改岗检小组
     */
    @ApiOperation("修改岗检小组")
    @PreAuthorize("@ss.hasPermi('postCheck:team:edit')")
    @Log(title = "岗检小组", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PostTeam postTeam) {
        return toAjax(postTeamService.updatePostTeam(postTeam));
    }

    /**
     * 删除岗检小组
     */
    @ApiOperation("删除岗检小组")
    @PreAuthorize("@ss.hasPermi('postCheck:team:remove')")
    @Log(title = "岗检小组", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(postTeamService.deletePostTeamByIds(ids));
    }

    /**
     * Tools-查询岗检工作组列表-查询工作小组列表
     * 调用模块：岗检计划新增
     */
    @ApiOperation("Tools-查询岗检工作组列表")
    @GetMapping("/groupList")
    public AjaxResult list() {
        List<PostTeam> resultList = new ArrayList<>();
        //TODO 岗检联合办公室：多个年数据时需要得到最新的组，跟进度暂时不考虑
        PostTeam postTeam = new PostTeam();
        postTeam.setParentId(0l);
        List<PostTeam> list = postTeamService.selectPostTeamList(postTeam);

        //查询工作组
        if (!CollectionUtils.isEmpty(list)) {
            postTeam = new PostTeam();
            postTeam.setParentId(list.get(0).getId());
            resultList = postTeamService.selectPostTeamList(postTeam);
        }

        return success(resultList);
    }

    /**
     * Tools-查询岗检工作组列表-查询工作小组列表
     * 调用模块：岗检计划新增
     */
    @ApiOperation("Tools-查询工作小组列表")
    @GetMapping("/teamListByGroupId/{parentId}")
    public AjaxResult list(@PathVariable("parentId") Long parentId) {
        PostTeam postTeam = new PostTeam();
        postTeam.setParentId(parentId);
        List<PostTeam> list = postTeamService.selectPostTeamList(postTeam);
        return success(list);
    }
}
