package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.config.security.security.TokenProvider;
import cn.ysk.cashier.config.security.service.OnlineUserService;
import cn.ysk.cashier.dto.shop.TbShopInfoDto;
import cn.ysk.cashier.dto.shop.TbShopInfoQueryCriteria;
import cn.ysk.cashier.pojo.shop.TbShopInfo;
import cn.ysk.cashier.service.shop.TbShopInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
* @website https://eladmin.vip
* @author lyf
* @date 2023-11-07
**/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "/shop/list管理")
@RequestMapping("/api/tbShopInfo")
public class TbShopInfoController {

    private final TbShopInfoService tbShopInfoService;
    private final OnlineUserService onlineUserService;
    private final TokenProvider tokenProvider;

//    @Log("导出数据")
//    @ApiOperation("导出数据")
//    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('tbShopInfo:list')")
//    public void exportTbShopInfo(HttpServletResponse response, TbShopInfoQueryCriteria criteria) throws IOException {
//        tbShopInfoService.download(tbShopInfoService.queryAll(criteria), response);
//    }

    @GetMapping
    @ApiOperation("查询/shop/list")
    @PreAuthorize("@el.check('tbShopInfo:list')")
    public ResponseEntity<Object> queryTbShopInfo(TbShopInfoQueryCriteria criteria){
        return new ResponseEntity<>(tbShopInfoService.queryAll(criteria),HttpStatus.OK);
    }

    @GetMapping("/{shopId}")
    @ApiOperation("查询/shop/list")
    public Object queryInfo(@PathVariable("shopId") Integer shopId){
        return tbShopInfoService.findById(shopId);
    }

    @PostMapping
    @Log("新增商户:#resources.shopName")
    @ApiOperation("新增/shop/list")
    @PreAuthorize("@el.check('tbShopInfo:add')")
    public ResponseEntity<Object> createTbShopInfo(@Validated @RequestBody TbShopInfoDto resources){
        return new ResponseEntity<>(tbShopInfoService.create(resources),HttpStatus.CREATED);
    }

    @ApiOperation("修改商户密码")
    @PostMapping(value = "/shop/upPass")
    @AnonymousPostMapping
    public ResponseEntity<Object> upShopPass(HttpServletRequest request, @RequestBody Map<String, String> map) throws Exception {
        tbShopInfoService.upShopPass(map.get("username"),map.get("password"));
        //根据token踢出用户
        onlineUserService.logout(tokenProvider.getToken(request));
        log.info("修改商户密码成功。");
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PutMapping
    @Log("修改商户:#resources.shopName")
    @ApiOperation("修改/shop/list")
    public ResponseEntity<Object> updateTbShopInfo(@Validated @RequestBody TbShopInfo resources){
        tbShopInfoService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping("/shop")
    @Log("修改商户:#resources.shopName")
    public ResponseEntity<Object> updateShopInfoShopId(@Validated @RequestBody TbShopInfo resources){
        tbShopInfoService.updateShopId(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除商户:#ids")
    @ApiOperation("删除/shop/list")
    @PreAuthorize("@el.check('tbShopInfo:del')")
    public ResponseEntity<Object> deleteTbShopInfo(@RequestBody Integer[] ids) {
        tbShopInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}