StoreController.java 6.37 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
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.dto.StatusDTO;
import vion.dto.StoreDTO;
import vion.model.RStoreUser;
import vion.model.Store;
import vion.model.StoreLog;
import vion.service.IRStoreUserService;
import vion.service.IStoreLogService;
import vion.service.IStoreService;
import vion.vo.StoreVO;
import vion.vo.UserVO;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
@Slf4j
public class StoreController {

    private final IStoreService storeService;
    private final IRStoreUserService storeUserService;
    private final IStoreLogService storeLogService;
    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 Long id) {
        Store store = storeService.getById(id);
        return converter.convert(store, StoreVO.class);
    }

    @GetMapping("/storeList")
    @SaCheckPermission(value = "store:list1", orRole = "admin")
    public List<StoreVO> getStoreList(Long id, Long accountId, String name, Integer limit) {
        List<Store> storeList = storeService.lambdaQuery()
                .eq(ObjUtil.isNotNull(accountId), Store::getAccountId, accountId)
                .eq(ObjUtil.isNotNull(id), Store::getId, id)
                .like(StrUtil.isNotBlank(name), Store::getName, name)
                .last(limit != null, "limit " + limit)
                .list();
        return converter.convert(storeList, StoreVO.class);
    }

    @PostMapping("/stores")
    @SaCheckPermission(value = "store:editAndSave", orRole = "admin")
    public String saveOrUpdate(@RequestBody StoreDTO data) {
        UserVO user = (UserVO) StpUtil.getTokenSession().get("curLoginUser");

        // 创建项目时,默认维保状态为 --
        data.setMaintainStatus("--");
        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(@RequestBody StatusDTO statusDTO, @RequestHeader String token) {
        return storeService.updateStoreStage(statusDTO, token);
    }

    @PostMapping("/store/bindContract")
    @SaCheckPermission(value = "store:bindContract", orRole = "admin")
    public String setMasterContract(@RequestBody StoreDTO dto) {
        return storeService.lambdaUpdate().eq(Store::getId, dto.getId()).set(Store::getMasterContract, dto.getMasterContract()).update(new Store()) ? "绑定主合同成功" : "绑定主合同失败";
    }

    @GetMapping("/store/manualCal")
    @SaCheckPermission(value = "store:calMaintainStatus", orRole = "admin")
    // todo 权限未加,前端未使用到该接口
    public String manualCalMaintainStatus() {
        return storeService.calMaintainStatus(ListUtil.empty());
    }

    @GetMapping("/store/user/{storeId}")
    @SaCheckPermission(value = "store:user:list", orRole = "admin")
    public List<RStoreUser> getUserList(@PathVariable Long storeId) {
        return storeUserService.lambdaQuery().eq(RStoreUser::getStoreId, storeId).list();
    }

    @PostMapping("/store/assignUser")
    @SaCheckPermission(value = "store:assignUser", orRole = "admin")
    public String assignUser(@RequestBody RStoreUser storeUser) {
        storeUserService.lambdaUpdate().eq(RStoreUser::getStoreId, storeUser.getStoreId()).remove();
        Opt.ofNullable(storeUser.getStoreUsers())
                .ifPresent(su -> storeUserService.saveBatch(Arrays.asList(su)));
        return "成功";
    }

    @GetMapping("/store/log/{storeId}")
    @SaCheckPermission(value = "store:log:list", orRole = "admin")
    public Page<StoreLog> getLogList(@PathVariable Long storeId, StoreLog storeLog) {
        storeLog.setStoreId(storeId);
        return storeLogService.lambdaQuery(storeLog)
                .page(Page.of(storeLog.getPageNum(), storeLog.getPageSize()));
    }

    @PostMapping("/store/log")
    @SaCheckPermission(value = "store:log:save", orRole = "admin")
    public String saveLog(@RequestBody StoreLog storeLog) {
        return storeLogService.save(storeLog) ? "新增成功" : "新增失败";
    }

    @DeleteMapping("/store/log/{logId}")
    @SaCheckPermission(value = "store:log:delete", orRole = "admin")
    public String delLog(@PathVariable Long logId) {
        return storeLogService.removeById(logId) ? "删除成功" : "删除失败";
    }

    @PostMapping("/store/log/{logId}")
    @SaCheckPermission(value = "store:log:edit", orRole = "admin")
    public String updateLog(@PathVariable Long logId, @RequestBody StoreLog storeLog) {
        storeLog.setId(logId);
        return storeLogService.updateById(storeLog) ? "更新成功" : "更新失败";
    }

    @GetMapping("/store/screen")
    @SaCheckPermission(value = "store:screen", orRole = "admin")
    public List<StoreVO> storeScreen() {
        return storeService.storeScreen();
    }

    @PostMapping("/store/merge")
    @SaCheckPermission(value = "store:merge", orRole = "admin")
    public String mergeStore(@RequestBody StoreDTO dto) {
        Assert.isFalse(ArrayUtil.hasNull(dto.getSourceId(), dto.getTargetId()), "合并项目id不能为空");
        return storeService.mergeStore(dto);
    }

    @PostMapping("/store/tag/{id}")
    @SaCheckPermission(value = "store:tag", orRole = "admin")
    // 权限未加
    public String addTag(@PathVariable Long id, @RequestBody List<Long> tagIdList) {
        return storeService.addTag(id, tagIdList);
    }

}