StoreController.java 3.23 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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.Contract;
import vion.model.RContractStore;
import vion.model.Store;
import vion.service.IContractService;
import vion.service.IRContractStoreService;
import vion.service.IStoreService;
import vion.vo.ContractVO;
import vion.vo.StoreVO;
import vion.vo.UserVO;

import java.util.List;
import java.util.stream.Collectors;

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

    private final IStoreService storeService;
    private final IContractService contractService;
    private final IRContractStoreService contractStoreService;
    private final Converter converter;


    @GetMapping("/stores")
    @SaCheckPermission(value = "store:list", orRole="admin")
    public Page<StoreVO> getStoreList(StoreDTO data) {
        return storeService.getStoreList(data);
    }

    @GetMapping("/store")
    @SaCheckPermission(value = "store:query", orRole="admin")
    public StoreVO getStoreById(@RequestParam Integer id) {
        Store store = storeService.getById(id);
        return converter.convert(store, StoreVO.class);
    }

    @GetMapping("/storeList")
    @SaCheckPermission(value = "store:list1", orRole="admin")
    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")
    @SaCheckPermission(value = "store:editAndSave", orRole="admin")
    public String saveOrUpdate(@RequestBody StoreDTO data, @RequestHeader String token) {
        UserVO 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")
    @SaCheckPermission(value = "store:editStatus", orRole="admin")
    public Object updateStoreStage(StatusDTO statusDTO, @RequestHeader String token) {
        return storeService.updateStoreStage(statusDTO, token);
    }

    @GetMapping("/store/contract/{id}")
    @SaCheckPermission(value = "store:contract", orRole="admin")
    // todo 权限未加库
    public List<ContractVO> listContractById(@PathVariable Long id) {
        List<RContractStore> contractStoreList = contractStoreService.lambdaQuery().eq(RContractStore::getStoreId, id).list();
        List<Long> contractIdList = contractStoreList.stream().map(RContractStore::getContractId).collect(Collectors.toList());
        List<Contract> contractList = contractService.lambdaQuery().in(Contract::getId, contractIdList).list();
        return converter.convert(contractList, ContractVO.class);
    }
}