StoreController.java 1.94 KB
package vion.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import vion.Global;
import vion.dto.StatusDTO;
import vion.dto.StoreDTO;
import vion.model.Store;
import vion.model.User;
import vion.service.IStoreService;
import vion.vo.StoreVO;

import java.util.List;

@RestController
@RequestMapping(Global.BASE_URL)
@RequiredArgsConstructor
@Slf4j
public class StoreController {

    private final IStoreService storeService;
    private final Converter converter;


    @GetMapping("/stores")
    public Page<StoreVO> getStoreList(StoreDTO data) {
        return storeService.getStoreList(data);
    }

    @GetMapping("/store")
    public StoreVO getStoreByID(@RequestParam(name = "id") Integer id) {
        Store store = storeService.getById(id);
        return converter.convert(store, StoreVO.class);
    }

    @GetMapping("/storeList")
    public List<StoreVO> getStoreList(Integer accountId) {
        List<Store> storeList = storeService.lambdaQuery()
                .eq(accountId != null, Store::getAccountId, accountId)
                .list();
        return converter.convert(storeList, StoreVO.class);
    }

    @PostMapping("/stores")
    public String saveOrUpdate(@RequestBody StoreDTO data, @RequestHeader String token) {
        User user = Global.USERNAME_MAP.get(token);

        Store store = converter.convert(data, Store.class);
        if (data.getId() != null) {
            store.setModifyUser(user.getId());
        } else {
            store.setCreateUser(user.getId());
        }
        return storeService.saveOrUpdate(store) ? "成功" : "失败";
    }

    @PostMapping("/updateStoreStatus")
    public Object updateStoreStage(StatusDTO statusDTO, @RequestHeader String token) {
        return storeService.updateStoreStage(statusDTO, token);
    }
}