ContractController.java 5.09 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.cron.ContractRunner;
import vion.dto.ContractDTO;
import vion.dto.RContractStoreDTO;
import vion.dto.RContractTeamDTO;
import vion.model.RContractStore;
import vion.model.RContractUser;
import vion.service.IContractService;
import vion.service.IRContractStoreService;
import vion.vo.ContractVO;

import java.util.List;
import java.util.Map;

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

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

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

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

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

    @GetMapping("/contractByNo/{no}")
    @SaCheckPermission(value = "contract:queryByNo", orRole = "admin")
    public ContractVO getByNo(@PathVariable String no) {
        return contractService.getByNo(no);
    }

    @PostMapping("/contract/{id}")
    @SaCheckPermission(value = "contract:edit", orRole = "admin")
    public String updateById(@PathVariable Long id, ContractDTO dto) {
        return contractService.updateById(id, null, dto);
    }
    @PostMapping("/contract/store")
    @SaCheckPermission(value = "contract:store:save", orRole = "admin")
    public String bindStore(@RequestBody RContractStoreDTO dto) {
        Assert.notNull(dto.getContractId(), "合同id不能为空");
        Assert.notNull(dto.getStoreId(), "项目id不能为空");
        RContractStore contractStore = new RContractStore();
        contractStore.setContractId(dto.getContractId());
        contractStore.setStoreId(dto.getStoreId());
        return contractStoreService.save(contractStore) ? "绑定成功" : "绑定失败";
    }

    @DeleteMapping("/contract/store")
    @SaCheckPermission(value = "contract:store:remove", orRole = "admin")
    public String unbindStore(@RequestBody RContractStoreDTO dto) {
        Assert.notNull(dto.getContractId(), "合同id不能为空");
        Assert.notNull(dto.getStoreId(), "项目id不能为空");
        return contractStoreService.lambdaUpdate()
                .eq(RContractStore::getContractId, dto.getContractId())
                .eq(RContractStore::getStoreId, dto.getStoreId())
                .remove() ? "解绑成功" : "解绑失败";
    }

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

    @GetMapping("/contract/calAmount")
    @SaCheckPermission(value = "contract:calAmount", orRole = "admin")
    public JSONObject calAmount(ContractDTO dto) {
        return contractService.calAmount(dto);
    }

    @GetMapping("/contract/analyze")
    @SaCheckPermission(value = "contract:analyze", orRole = "admin")
    public Map<String, Object> analyze(ContractDTO dto) {
        return contractService.analyze(dto);
    }

    @PostMapping("/contract/dispatch")
    @SaCheckPermission(value = "contract:dispatch", orRole = "admin")
    public String dispatch(@RequestBody RContractTeamDTO dto) {
        return contractService.dispatch(dto);
    }

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

    @PostMapping("/contract/assignSale")
    @SaCheckPermission(value = "contract:assignSale", orRole = "admin")
    public String assignSale(@RequestBody RContractUser dto) {
        return contractService.assignSale(dto);
    }

    @PostMapping("/contract/unbindSale")
    @SaCheckPermission(value = "contract:unbindSale", orRole = "admin")
    public String unbindSale(@RequestBody RContractUser dto) {
        return contractService.unbindSale(dto);
    }

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