package com.shkj.wms.controller.header;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shkj.common.annotation.Log;
import com.shkj.common.core.controller.BaseController;
import com.shkj.common.core.domain.Result;
import com.shkj.common.core.page.TableDataInfo;
import com.shkj.common.enums.BusinessType;
import com.shkj.common.utils.poi.ExcelUtil;
import com.shkj.wms.common.SwitchShowEntity;
import com.shkj.wms.domain.TableHeader;
import com.shkj.wms.service.ITableHeaderService;
import com.shkj.wms.bo.SyncTableHeaderBo;
import com.shkj.wms.bo.TableHeaderAddBo;
import com.shkj.wms.bo.TableHeaderEditBo;
import com.shkj.wms.bo.TableHeaderQueryBo;
import com.shkj.wms.vo.TableHeaderVo;
import com.shkj.wms.vo.TableListVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 表格表头Controller
 *
 * @author lch
 * @menu 表格表头管理
 * @date 2022-03-19
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/wms/header")
public class TableHeaderController extends BaseController {

    private final ITableHeaderService iTableHeaderService;


    /**
     * 根据表名模糊查询匹配的表列表
     */
    @GetMapping("/getTableOption")
    public Result<List<TableListVo>> getTableOption(SyncTableHeaderBo bo) {
        return iTableHeaderService.getTableOption(bo);
    }

    /**
     * 根据表名同步表头
     */
    @PostMapping("/syncTableHeader")
    public Result<Boolean> syncTableHeader(@Valid @RequestBody SyncTableHeaderBo bo) {
        return iTableHeaderService.syncTableHeader(bo);
    }


    /**
     * 查询表格表头列表
     */
    @GetMapping("/getTableHeader/{tableName}")
    public Result<List<TableHeader>> list(@PathVariable("tableName") String tableName) {
        LambdaQueryWrapper<TableHeader> wrapper = new LambdaQueryWrapper<TableHeader>().eq(TableHeader::getTableName, tableName)
                .eq(TableHeader::getIstrue, true)
                .orderByAsc(TableHeader::getPropSort)
                .orderByAsc(TableHeader::getId);

        List<TableHeader> list = iTableHeaderService.list(wrapper);
        return Result.ok().data(list);
    }

    /**
     * 查询表格所有字段
     */
    @GetMapping("/getAllTableFiled/{tableName}")
    public Result<List<TableHeader>> getAllTableFiled(@PathVariable("tableName") String tableName) {
        LambdaQueryWrapper<TableHeader> wrapper = new LambdaQueryWrapper<TableHeader>().eq(TableHeader::getTableName, tableName)
                .orderByAsc(TableHeader::getPropSort)
                .orderByAsc(TableHeader::getId);

        List<TableHeader> list = iTableHeaderService.list(wrapper);
        return Result.ok().data(list);
    }

    /**
     * 查询表单列表
     */
    @GetMapping("/queryTableList")
    public TableDataInfo<TableHeaderVo> queryTableList(TableHeaderQueryBo bo) {
        startPage();
        List<TableHeaderVo> list = iTableHeaderService.queryTableList(bo);
        return getDataTable(list);
    }

    /**
     * 查询表格表头列表
     */
    @GetMapping("/list")
    public TableDataInfo<TableHeaderVo> list(TableHeaderQueryBo bo) {
        startPage();
        List<TableHeaderVo> list = iTableHeaderService.queryList(bo);
        return getDataTable(list);
    }

    /**
     * 导出表格表头列表
     */
    @PreAuthorize("@ss.hasPermi('wms:header:export')")
    @GetMapping("/export")
    public void export(HttpServletResponse response, TableHeaderQueryBo bo) {
        List<TableHeaderVo> list = iTableHeaderService.queryList(bo);
        ExcelUtil<TableHeaderVo> util = new ExcelUtil<TableHeaderVo>(TableHeaderVo.class);
        util.exportExcel(response, list, "表格表头数据");
    }

    /**
     * 获取表格表头详细信息
     */
    @GetMapping("/{id}")
    public Result<TableHeaderVo> getInfo(@PathVariable("id") Long id) {
        return iTableHeaderService.queryById(id);
    }

    /**
     * 新增表格表头
     */
    @Log(title = "表格表头", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<Boolean> add(@Valid @RequestBody TableHeaderAddBo bo) {
        return iTableHeaderService.insertByAddBo(bo);
    }

    /**
     * 修改表格表头
     */
    @Log(title = "表格表头", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public Result<Boolean> edit(@Valid @RequestBody TableHeaderEditBo bo) {
        return iTableHeaderService.updateByEditBo(bo);
    }

    /**
     * 删除表格表头
     */
    @Log(title = "表格表头", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{id}")
    public Result<Boolean> remove(@PathVariable Long id) {
        return iTableHeaderService.deleteWithValidById(id, true);
    }

    /**
     * 切换是否显示
     */
    @Log(title = "切换是否显示", businessType = BusinessType.UPDATE)
    @PostMapping("/switchStatus")
    public Result<Boolean> switchStatus(@Valid @RequestBody SwitchShowEntity switchStatusEntity) {
        return iTableHeaderService.switchStatus(switchStatusEntity);
    }
}
