/*
 *      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.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springblade.manager.dto.FormElementDTO;
import org.springblade.manager.dto.WbsFormElementDTO;
import org.springblade.manager.dto.WbsFormElementDTO2;
import org.springblade.manager.dto.WbsTreeDTO2;
import org.springblade.manager.entity.WbsFormElement;
import org.springblade.manager.entity.WbsTree;
import org.springblade.manager.excel.*;
import org.springblade.manager.vo.WbsFormElementVO;
import org.springblade.manager.vo.WbsTreeVO;
import org.springblade.manager.wrapper.WbsFormElementWrapper;
import org.springblade.manager.wrapper.WbsTreeWrapper;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import org.springblade.manager.service.IWbsFormElementService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * wbs表单元素 控制器
 *
 * @author liuyc
 * @since 2022-04-27
 */
@RestController
@AllArgsConstructor
@RequestMapping("/wbsFormElement")
@Api(value = "wbs表单元素", tags = "wbs表单元素接口")
public class WbsFormElementController extends BladeController {

    private final IWbsFormElementService wbsFormElementService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入元素id")
    @ApiImplicitParam(name = "id", value = "id", required = true)
    public R<WbsFormElementVO> detail(WbsFormElement wbsFormElement) {
        WbsFormElement detail = wbsFormElementService.getDetailById(wbsFormElement);
        if (detail != null) {
            return R.data(WbsFormElementWrapper.build().entityVO(detail));
        }
        return R.fail(200, "未查询到信息");
    }

    /**
     * 新增或同步 wbs表单元素
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新增", notes = "传入wbsFormElement")
    public R save(@Valid @RequestBody WbsFormElementDTO wbsFormElement) {
        //新增同步元素字段到实体表
        return wbsFormElementService.saveAndSyn(wbsFormElement, wbsFormElement.getInitTableName());
    }


    /**
     * 修改 wbs表单元素
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改", notes = "传入wbsFormElement")
    public R update(@Valid @RequestBody WbsFormElement wbsFormElement) {
        return R.status(wbsFormElementService.updateById(wbsFormElement));
    }


    /**
     * 新增或修改 wbs表单元素
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "新增或修改", notes = "传入wbsFormElement")
    public R submit(@Valid @RequestBody WbsFormElement wbsFormElement) {
        return R.status(wbsFormElementService.saveOrUpdate(wbsFormElement));
    }

    /**
     * 删除 wbs表单元素 同步删除实体表字段
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam("ids") String ids,
                    @RequestParam("eKey") String eKey, @RequestParam("initTableName") String initTableName) {
        //同步删除实体表元素字段
        boolean b = wbsFormElementService.deleteAndSyn(ids, eKey, initTableName);
        if (b) {
            return R.success("操作成功");
        }
        return R.fail("操作失败");
    }

    /**
     * 导出Wbs元素模板
     */
    @GetMapping("/export-template")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "导出WBS元素模板")
    public void exportWbsTree(HttpServletResponse response) {
        List<WbsFormElementExcel> list = new ArrayList<>();
        ExcelUtil.export(response, "WBS元素模板", "WBS元素模板", list, WbsFormElementExcel.class);
    }

    /**
     * 导入Wbs元素
     */
    @PostMapping("/import-wbsElement")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "导入WBS元素数据", notes = "传入excel")
    public R importRegion(MultipartFile file) {
        List<WbsFormElementExcel> list = ExcelUtil.read(file, WbsFormElementExcel.class);
        for (WbsFormElementExcel wbsFE : list) {
            if (("字符串").equals(wbsFE.getElementType()) || ("string").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("1");
            } else if (("整数").equals(wbsFE.getElementType()) || ("bigint").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("2");
            } else if (("小数").equals(wbsFE.getElementType()) || ("decimal").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("3");
            } else if (("日期").equals(wbsFE.getElementType()) || ("datetime").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("4");
                wbsFE.setElementLength("0");
            } else if (("数值").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("3");
            } else if (("签名").equals(wbsFE.getElementType())) {
                wbsFE.setElementType("1");
            }
        }
        return R.data(list, "操作成功");
    }

    /**
     * 查询当前节点下所有表单
     */
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "查询当前节点下所有表单", notes = "传入该节点id")
    @RequestMapping(value = "/findWbsTreeTableSameLevel", method = RequestMethod.GET)
    public R<List<WbsFormElementDTO2>> findWbsTreeTableSameLevel(@RequestParam("id") String id) {
        List<WbsFormElementDTO2> trees = wbsFormElementService.findWbsTreeTableSameLevel(id);
        return R.data(trees);
    }

    /**
     * wbs树节点手动排序
     */
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "wbs树节点手动排序", notes = "WbsFormElementDTO2集合,id=表单id")
    @RequestMapping(value = "/wbsTreeTableSort", method = RequestMethod.POST)
    public R wbsTreeTableSort(@RequestBody List<WbsFormElementDTO2> wbsTreeDTO) {
        boolean b = wbsFormElementService.wbsTreeTableSort(wbsTreeDTO);
        return R.status(b);
    }


}
