ContractController.java 4.16 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
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 com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.cron.ContractRunner;
import vion.dto.ContractDTO;
import vion.dto.RContractStoreDTO;
import vion.model.RContractStore;
import vion.service.IContractService;
import vion.service.IRContractStoreService;
import vion.vo.ContractVO;

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

/**
 * 合同管理
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ContractController {

    private final IContractService contractService;
    private final ContractRunner contractRunner;
    private final IRContractStoreService contractStoreService;
    private final Converter converter;

    @GetMapping("/contract")
    @SaCheckPermission(value = "contract:list", orRole = "admin")
    public Page<ContractVO> list(ContractDTO dto) {
        return contractService.list(dto);
    }

    @GetMapping("/contract/{id}")
    @SaCheckPermission(value = "contract:query", orRole = "admin")
    public ContractVO getById(@PathVariable Long id) {
        return converter.convert(contractService.getById(id), new ContractVO());
    }

    @PostMapping("/contract/{id}")
    @SaCheckPermission(value = "contract:edit", orRole = "admin")
    public String updateById(@PathVariable Long id, ContractDTO dto) {
        return contractService.updateById(id, dto);
    }

    @PostMapping("/contract/store")
    @SaCheckPermission(value = "contract:store:edit", orRole = "admin")
    public String saveOrUpdateRContractStore(@RequestBody RContractStoreDTO dto) {
        Assert.isFalse(ArrayUtil.isAllNotNull(dto.getStoreId(), dto.getContractId()), "合同id和项目id不能同时传值");
        if (ObjUtil.isNotNull(dto.getContractId())) {
            Opt.ofEmptyAble(dto.getStoreIds())
                    .ifPresent(storeIds -> {
                        List<RContractStore> contractStoreList = storeIds.stream()
                                .map(storeId -> {
                                    RContractStore contractStore = new RContractStore();
                                    contractStore.setContractId(dto.getContractId());
                                    contractStore.setStoreId(storeId);
                                    return contractStore;
                                }).collect(Collectors.toList());
                        contractStoreService.saveBatch(contractStoreList);
                    });
        }
        if (ObjUtil.isNotNull(dto.getStoreId())) {
            Opt.ofEmptyAble(dto.getContractIds())
                    .ifPresent(contractIds -> {
                        List<RContractStore> contractStoreList = contractIds.stream()
                                .map(contractId -> {
                                    RContractStore contractStore = new RContractStore();
                                    contractStore.setContractId(contractId);
                                    contractStore.setStoreId(dto.getStoreId());
                                    return contractStore;
                                }).collect(Collectors.toList());
                        contractStoreService.saveBatch(contractStoreList);
                    });
        }
        return "成功";
    }

    @GetMapping("/contract/store")
    @SaCheckPermission(value = "contract:store:query", orRole = "admin")
    public List<RContractStore> getRContractStore(Long contractId, Long storeId) {
        return contractStoreService.lambdaQuery()
                .eq(ObjUtil.isNotNull(contractId), RContractStore::getContractId, contractId)
                .eq(ObjUtil.isNotNull(storeId), RContractStore::getStoreId, storeId)
                .list();
    }

    @PostMapping("/contractSync")
    @SaCheckPermission(value = "contract:sync", orRole = "admin")
    public String manualSyncContract() {
        return contractRunner.contractSync() ? "成功" : "失败";
    }
}