package cn.ysk.cashier.controller.shop;

import cn.hutool.core.util.StrUtil;
import cn.ysk.cashier.annotation.AnonymousAccess;
import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.dto.shop.TbShopRechargeListDto;
import cn.ysk.cashier.dto.shop.TbShopUserQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
import cn.ysk.cashier.mybatis.service.TbMShopUserService;
import cn.ysk.cashier.mybatis.service.TbShopUserFlowService;
import cn.ysk.cashier.pojo.shop.TbShopUser;
import cn.ysk.cashier.service.shop.TbShopUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @author lyf
 * @website https://eladmin.vip
 * @date 2024-03-01
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "/shop/user管理")
@RequestMapping("/api/tbShopUser")
public class TbShopUserController {

    private final TbShopUserService tbShopUserService;
    private final TbMShopUserService tbMShopUserService;

    private final TbShopUserFlowService tbShopUserFlowService;


    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    public void exportTbShopUser(HttpServletResponse response, TbShopUserQueryCriteria criteria) throws IOException {
        tbShopUserService.download(tbShopUserService.queryAll(criteria), response);
    }

    @GetMapping
    @ApiOperation("查询/shop/user")
    public ResponseEntity<Object> queryTbShopUser(TbShopUserQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(tbShopUserService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping("queryAllShopUser")
    @ApiOperation("查询商家用户")
    public ResponseEntity<Object> queryAllShopUser(TbShopUserQueryCriteria criteria) {
        return new ResponseEntity<>(tbShopUserService.queryShopUser(criteria), HttpStatus.OK);
    }

    @GetMapping("summary")
    @ApiOperation("查询会员概述")
    public ResponseEntity<Object> summary(TbShopUserQueryCriteria criteria) {
        if (StrUtil.isBlank(criteria.getShopId())) {
            throw new BadRequestException("店铺id不为空");
        }
        return new ResponseEntity<>(tbMShopUserService.summary(criteria), HttpStatus.OK);
    }


    @PostMapping
    @ApiOperation("新增/shop/user")
    public ResponseEntity<Object> createTbShopUser(@Validated @RequestBody TbShopUser resources) {
        return new ResponseEntity<>(tbShopUserService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @ApiOperation("修改/shop/user")
    public ResponseEntity<Object> updateTbShopUser(@Validated @RequestBody TbShopUser resources) {
        tbShopUserService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @ApiOperation("删除/shop/user")
    public ResponseEntity<Object> deleteTbShopUser(@RequestBody Integer[] ids) {
        tbShopUserService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/recharge")
    @ApiOperation("充值记录")
    public ResponseEntity<Object> rechargeList(TbShopRechargeListDto criteria, Pageable pageable) {
        return new ResponseEntity<>(tbShopUserService.rechargeList(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping("/recharge/download")
    @ApiOperation("导出充值记录")
    public void rechargeListDownload(HttpServletResponse response, @RequestBody TbShopRechargeListDto criteria) throws IOException {
        tbShopUserService.rechargeListDownload(response, criteria);
    }


    @PostMapping("midfiyAccount")
    @ApiOperation("增加扣减会员余额")
    public ResponseEntity<Object> midfiyAccount(@RequestBody Map<String,Object> map){
        tbShopUserService.modfiyAccount(map);
        return new ResponseEntity<>(HttpStatus.OK);
    }


    @GetMapping("queryShopUserFlow")
    public ResponseEntity<Object> queryShopUserFlow(@RequestParam("userId") Integer userId,@RequestParam("page") Integer page,@RequestParam("pageSize") Integer pageSize){
       return new ResponseEntity<>( tbShopUserFlowService.selectByUserId(userId, page, pageSize),HttpStatus.OK);
    }
}
