package cn.ysk.cashier.controller.shop;

import cn.ysk.cashier.annotation.Log;
import cn.ysk.cashier.annotation.rest.AnonymousGetMapping;
import cn.ysk.cashier.annotation.rest.AnonymousPostMapping;
import cn.ysk.cashier.config.security.config.bean.SecurityProperties;
import cn.ysk.cashier.config.security.security.TokenProvider;
import cn.ysk.cashier.config.security.service.OnlineUserService;
import cn.ysk.cashier.dto.BindingDto;
import cn.ysk.cashier.dto.shop.TbShopInfoDto;
import cn.ysk.cashier.dto.shop.TbShopInfoQueryCriteria;
import cn.ysk.cashier.exception.BadRequestException;
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.HashMap;
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;
    private final SecurityProperties properties;

    @PostMapping("changChildShop")
    @ApiOperation("切换子店铺")
    public ResponseEntity<Object> changChildShop(@RequestBody TbShopInfoQueryCriteria criteria, HttpServletRequest request) {
        TbShopInfoDto shopInfo = tbShopInfoService.findById(criteria.getId());
        if (shopInfo != null) {
            //生成token
            String token = tokenProvider.createToken(shopInfo.getAccount(), criteria.getId().toString());
            Map<String, Object> authInfo = new HashMap<String, Object>(2) {{
                put("token", properties.getTokenStartWith() + token);
                if (shopInfo != null) {
                    put("shopId", shopInfo.getId());
                    put("mainId", shopInfo.getMainId());
                    put("loginType", "merchant");
                    put("shopName", shopInfo.getShopName());
                    put("logo", shopInfo.getLogo());
                }
            }};
            // 保存在线信息
            onlineUserService.save(shopInfo.getAccount(), shopInfo.getShopName(), token, request, shopInfo.getId());
            return ResponseEntity.ok(authInfo);
        } else {
            throw new BadRequestException("店铺信息不存在");
        }
    }

    @GetMapping("queryChildShop")
    @ApiOperation("查询子店铺")
    public ResponseEntity<Object> queryChildShop(TbShopInfoQueryCriteria criteria){
        return new ResponseEntity<>(tbShopInfoService.queryChildShop(criteria),HttpStatus.OK);
    }

    @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);
    }

}
