package com.gxwebsoft.apps.controller;

import com.alibaba.fastjson.JSONObject;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.apps.service.TestDataService;
import com.gxwebsoft.apps.entity.TestData;
import com.gxwebsoft.apps.param.TestDataParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.shop.entity.Order;
import com.gxwebsoft.shop.entity.OrderGoods;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 测试数据表控制器
 *
 * @author 科技小王子
 * @since 2023-02-01 12:13:46
 */
@Api(tags = "测试数据表管理")
@RestController
@RequestMapping("/api/apps/test-data")
public class TestDataController extends BaseController {
    @Resource
    private TestDataService testDataService;

    @PreAuthorize("hasAuthority('apps:testData:list')")
    @OperationLog
    @ApiOperation("分页查询测试数据表")
    @GetMapping("/page")
    public ApiResult<PageResult<TestData>> page(TestDataParam param) {
        PageParam<TestData, TestDataParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(testDataService.page(page, page.getWrapper()));
        // 使用关联查询
        //return success(testDataService.pageRel(param));
    }

    @PreAuthorize("hasAuthority('apps:testData:list')")
    @OperationLog
    @ApiOperation("查询全部测试数据表")
    @GetMapping()
    public ApiResult<List<TestData>> list(TestDataParam param) {
        PageParam<TestData, TestDataParam> page = new PageParam<>(param);
        page.setDefaultOrder("create_time desc");
        return success(testDataService.list(page.getOrderWrapper()));
        // 使用关联查询
        //return success(testDataService.listRel(param));
    }

    @PreAuthorize("hasAuthority('apps:testData:list')")
    @OperationLog
    @ApiOperation("根据id查询测试数据表")
    @GetMapping("/{id}")
    public ApiResult<TestData> get(@PathVariable("id") Integer id) {
        return success(testDataService.getById(id));
        // 使用关联查询
        //return success(testDataService.getByIdRel(id));
    }

    @PreAuthorize("hasAuthority('apps:testData:save')")
    @OperationLog
    @ApiOperation("添加测试数据表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody TestData testData) {
        if (testDataService.save(testData)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:testData:update')")
    @OperationLog
    @ApiOperation("修改测试数据表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody TestData testData) {
        if (testDataService.updateById(testData)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:testData:remove')")
    @OperationLog
    @ApiOperation("删除测试数据表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (testDataService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @PreAuthorize("hasAuthority('apps:testData:save')")
    @OperationLog
    @ApiOperation("批量添加测试数据表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<TestData> list) {
        if (testDataService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @PreAuthorize("hasAuthority('apps:testData:update')")
    @OperationLog
    @ApiOperation("批量修改测试数据表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<TestData> batchParam) {
        if (batchParam.update(testDataService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @PreAuthorize("hasAuthority('apps:testData:remove')")
    @OperationLog
    @ApiOperation("批量删除测试数据表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (testDataService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }



}
