/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.manager.controller;

import io.swagger.annotations.*;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.WbsInfoDTO;
import org.springblade.manager.entity.WbsInfo;
import org.springblade.manager.entity.WbsTree;
import org.springblade.manager.service.IWbsTreeService;
import org.springblade.manager.vo.WbsInfoVO;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.manager.wrapper.WbsInfoWrapper;
import org.springblade.manager.service.IWbsInfoService;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * wbs动态库信息 控制器
 *
 * @author liuyc
 * @since 2022-04-25
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wbsInfo")
@Api(value = "wbs动态库信息", tags = "wbs动态库信息接口")
public class WbsInfoController extends BladeController {

    private final IWbsInfoService wbsInfoService;
    private final IWbsTreeService wbsTreeService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入id")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public R<WbsInfoVO> detail(WbsInfoDTO wbsInfo) {
        WbsInfo detail = wbsInfoService.getOne(Condition.getQueryWrapper(wbsInfo));
        return R.data(WbsInfoWrapper.build().entityVO(detail));
    }

    /**
     * 分页 wbs动态库信息
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入wbsInfo")
    public R<IPage<WbsInfoVO>> list(WbsInfoDTO wbsInfo, Query query) {
        IPage<WbsInfo> pages = wbsInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(wbsInfo));
        return R.data(WbsInfoWrapper.build().pageVO(pages));
    }


    /**
     * 自定义分页 wbs动态库信息
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "自定义分页", notes = "传入wbsInfo")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "current", value = "当前页", required = true),
            @ApiImplicitParam(name = "size", value = "每页的数量", required = true)
    })
    public R<IPage<WbsInfo>> page(WbsInfo wbsInfo, Query query) {
        IPage<WbsInfo> pages = wbsInfoService.selectWbsInfoPage(Condition.getPage(query), wbsInfo);
        return R.data(pages);
    }

    /**
     * 新增 wbs动态库信息
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入wbsInfo")
    public R save(@Valid @RequestBody WbsInfoDTO wbsInfo) {
        return R.status(wbsInfoService.save(wbsInfo));
    }

    /**
     * 修改 wbs动态库信息
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入wbsInfo")
    @ApiImplicitParam(value = "id", name = "id", required = true)
    public R update(@Valid @RequestBody WbsInfoDTO wbsInfo) {
        return R.status(wbsInfoService.updateById(wbsInfo));
    }

    /**
     * 新增wbs动态库信息
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增", notes = "传入wbsInfo")
    @ApiImplicitParam(value = "id", name = "id", required = true)
    @Transactional(rollbackFor = Exception.class)
    public R submit(@Valid @RequestBody WbsInfoDTO wbsInfo) {
        boolean result1 = wbsInfoService.saveOrUpdate(wbsInfo);
        if (result1) {
            Long wbsId = wbsInfo.getId();
            String nodeName = wbsInfo.getWbsName();
            //初始化wbs动态库树的根节点
            WbsTree wbsTree = new WbsTree();
            wbsTree.setWbsId(String.valueOf(wbsId));
            wbsTree.setTenantId(AuthUtil.getTenantId());
            wbsTree.setParentId(0L);
            wbsTree.setAncestors("0");
            wbsTree.setDeptCategory(1);
            wbsTree.setDeptName(nodeName);
            wbsTree.setFullName(nodeName);
            wbsTree.setType(1);
            boolean result2 = wbsTreeService.save(wbsTree);
            if (result2) {
                return R.data(wbsInfo, "操作成功");
            }
        }
        return R.fail("操作失败");
    }


    /**
     * 删除 wbs动态库信息
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
        return R.status(wbsInfoService.deleteLogic(Func.toLongList(ids)));
    }

    /**
     * 根据类型查找
     */
    @GetMapping("/findByWbsType")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据类型查找", notes = "传入type,1=质检 2=实验 3=计量")
    public R<List<WbsInfo>> findByWbsType(String type) {
        List<WbsInfo> wbsInfos = wbsInfoService.findByWbsType(type);
        if (wbsInfos != null) {
            return R.data(wbsInfos);
        }
        return R.fail("未查询到信息");
    }


}
