package com.ruoyi.postCheck.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.postCheck.domain.PostTeam;
import com.ruoyi.postCheck.service.IPostTeamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 *
 * 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();
        Map<String, Object> params = postTeam.getParams();
        if (!params.containsKey("beginTime") || !params.containsKey("endTime")) {
//            throw new BaseException("未设置开始、结束时间，请检查！");
        }
        postTeam.setId(null);
        //1、领导小组 lList
        postTeam.setParentId(0l);
        List<PostTeam> listOne = postTeamService.selectPostTeamList(postTeam);
        if (CollectionUtils.isEmpty(listOne))
            return getDataTable(new ArrayList<>());
        PostTeam postTeamOne = listOne.get(0);
        if (postTeamOne == null)
            return getDataTable(new ArrayList<>());


        //2、岗检联合办公室 bList
        postTeam.setParentId(postTeamOne.getId());
        List<PostTeam> listTwo = postTeamService.selectPostTeamList(postTeam);
        if (CollectionUtils.isEmpty(listTwo))
            return getDataTable(listOne);
        PostTeam postTeamTwo = listTwo.get(0);
        if (postTeamTwo == null)
            return getDataTable(listOne);
        //领导小组添加联合办公室
        postTeamOne.setChildren(listTwo);


        //3、工作组 groupChildrenList
        postTeam.setParentId(postTeamTwo.getId());
        List<PostTeam> listThree = postTeamService.selectPostTeamList(postTeam);
        //联合办公室添加工作组
        postTeamTwo.setChildren(listThree);

        //4、查工作小组
        for (PostTeam postTeamThree : listThree) {
//            PostTeam teamParent = new PostTeam();
            postTeam.setParentId(postTeamThree.getId());
            List<PostTeam> listFour = postTeamService.selectPostTeamList(postTeam);
            //工作组添加小组
            postTeamThree.setChildren(listFour);
        }

        return getDataTable(listOne);
    }

    /**
     * 导出岗检小组列表
     @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(
            @ApiParam(name = "id", value = "岗检小组id", required = true)
            @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 = "类型：0-岗检领导小组；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(
            @ApiParam(name = "ids", value = "岗检小组ids", required = true)
            @PathVariable Long[] ids) {
        return toAjax(postTeamService.deletePostTeamByIds(ids));
    }

    /**
     * Tools-查询岗检工作组列表-查询工作小组列表
     * 调用模块：岗检计划新增
     */
    @ApiOperation("Tools-查询岗检工作组列表(三级)")
    @GetMapping("/groupList")
    public AjaxResult groupList(PostTeam postTeam) {
        //如果前端没有设置年份，这里默认拿出今年的数据
        Map<String, Object> params = postTeam.getParams();
        if (params.get("beginTime") == null || params.get("endTime") == null) {
            String year = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
            params.put("beginTime", year + "-01-01");
            params.put("endTime", year + "-12-31");
            postTeam.setParams(params);
        }

        // 1、领导小组
        postTeam.setParentId(0l);
        List<PostTeam> list = postTeamService.selectPostTeamList(postTeam);
        if (CollectionUtils.isEmpty(list)) {
            return success(new ArrayList<>());
        }

        // 2、联合办公室
        postTeam.setParentId(list.get(0).getId());
        List<PostTeam> twoList = postTeamService.selectPostTeamList(postTeam);
        if (CollectionUtils.isEmpty(twoList)) {
            return success(new ArrayList<>());
        }

        // 3、综合工作组/生产工作组
        postTeam.setParentId(twoList.get(0).getId());
        List<PostTeam> threeList = postTeamService.selectPostTeamList(postTeam);
        return success(threeList);
    }

    /**
     * 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);
    }
}
