package com.gxwebsoft.shop.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.shop.service.CountService;
import com.gxwebsoft.shop.entity.Count;
import com.gxwebsoft.shop.param.CountParam;
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 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.util.Date;
import java.util.List;

/**
 * 商城销售统计表控制器
 *
 * @author 科技小王子
 * @since 2024-07-31 22:25:10
 */
@Api(tags = "商城销售统计表管理")
@RestController
@RequestMapping("/api/shop/count")
public class CountController extends BaseController {
    @Resource
    private CountService countService;

    @ApiOperation("分页查询商城销售统计表")
    @GetMapping("/data")
    public ApiResult<?> data(CountParam param) {
      final DateTime dateTime = DateUtil.offsetDay(DateUtil.date(), 1);
      System.out.println("dateTime = " + dateTime);

      final DateTime dateTime2 = DateUtil.offsetDay(DateUtil.date(), -1);
      System.out.println("dateTime2 = " + dateTime2);
      final DateTime dateTime3 = DateUtil.offsetDay(DateUtil.date(), -2);
      System.out.println("dateTime3 = " + dateTime3);
      return success(countService.list(new LambdaQueryWrapper<Count>().ge(Count::getDateTime, dateTime3).last("limit 2")));
    }

    @ApiOperation("分页查询商城销售统计表")
    @GetMapping("/page")
    public ApiResult<PageResult<Count>> page(CountParam param) {
        // 使用关联查询
        return success(countService.pageRel(param));
    }

    @ApiOperation("查询全部商城销售统计表")
    @GetMapping()
    public ApiResult<List<Count>> list(CountParam param) {
        // 使用关联查询
        return success(countService.listRel(param));
    }

    @PreAuthorize("hasAuthority('shop:count:list')")
    @ApiOperation("根据id查询商城销售统计表")
    @GetMapping("/{id}")
    public ApiResult<Count> get(@PathVariable("id") Integer id) {
        // 使用关联查询
        return success(countService.getByIdRel(id));
    }

    @ApiOperation("添加商城销售统计表")
    @PostMapping()
    public ApiResult<?> save(@RequestBody Count count) {
        if (countService.save(count)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("修改商城销售统计表")
    @PutMapping()
    public ApiResult<?> update(@RequestBody Count count) {
        if (countService.updateById(count)) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("删除商城销售统计表")
    @DeleteMapping("/{id}")
    public ApiResult<?> remove(@PathVariable("id") Integer id) {
        if (countService.removeById(id)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

    @ApiOperation("批量添加商城销售统计表")
    @PostMapping("/batch")
    public ApiResult<?> saveBatch(@RequestBody List<Count> list) {
        if (countService.saveBatch(list)) {
            return success("添加成功");
        }
        return fail("添加失败");
    }

    @ApiOperation("批量修改商城销售统计表")
    @PutMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody BatchParam<Count> batchParam) {
        if (batchParam.update(countService, "id")) {
            return success("修改成功");
        }
        return fail("修改失败");
    }

    @ApiOperation("批量删除商城销售统计表")
    @DeleteMapping("/batch")
    public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
        if (countService.removeByIds(ids)) {
            return success("删除成功");
        }
        return fail("删除失败");
    }

}
