ServiceOrderController.java 3.69 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.IdUtil;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import vion.dto.ServiceOrderDTO;
import vion.model.Dictionary;
import vion.model.ServiceOrder;
import vion.service.IDictionaryService;
import vion.service.IServiceOrderService;
import vion.vo.ServiceOrderVO;

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

/**
 * 服务单
 *
 * @author HlQ
 * @date 2023/11/21
 */
@RestController
@RequestMapping("/api")
@RequiredArgsConstructor
public class ServiceOrderController {

    private final IServiceOrderService serviceOrderService;
    private final IDictionaryService dictionaryService;
    private final Converter converter;

    @GetMapping("/order/{id}")
    @SaCheckPermission(value = "order:query", orRole="admin")
    public ServiceOrder getOrderById(@PathVariable Long id) {
        return serviceOrderService.getById(id);
    }

    @PostMapping("/order")
    @SaCheckPermission(value = "order:save", orRole="admin")
    private Object save(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        Long count = serviceOrderService.lambdaQuery().eq(ServiceOrder::getTaskId, serviceOrderDTO.getTaskId()).count();
        Assert.isFalse(count > 0, "该工单已存在服务单,不能重复创建!");
        ServiceOrder serviceOrder = converter.convert(serviceOrderDTO, new ServiceOrder());
        serviceOrder.setUnid(IdUtil.nanoId());
        return serviceOrderService.save(serviceOrder) ? serviceOrder.getId() : "创建失败";
    }

    @PostMapping("/order/{id}")
    @SaCheckPermission(value = "order:edit", orRole="admin")
    private String update(@PathVariable Long id, @RequestBody ServiceOrderDTO serviceOrderDTO) {
        ServiceOrder serviceOrder = converter.convert(serviceOrderDTO, new ServiceOrder());
        serviceOrder.setId(id);
        return serviceOrderService.updateById(serviceOrder) ? "更新成功" : "更新失败";
    }

    @DeleteMapping("/order/{id}")
    @SaCheckPermission(value = "order:remove", orRole="admin")
    private String remove(@PathVariable Long id) {
        return serviceOrderService.removeById(id) ? "删除成功" : "删除失败";
    }

    @GetMapping("/order/sign/{unid}")
    public ServiceOrderVO getOrderByUnid(@PathVariable String unid) {
        // todo 字典加入缓存
        List<Dictionary> dictionaryList = dictionaryService.lambdaQuery().eq(Dictionary::getType, "service_type").list();
        Map<Integer, String> serviceTypeMap = dictionaryList.stream().collect(Collectors.toMap(Dictionary::getKey, Dictionary::getValue));
        ServiceOrder order = serviceOrderService.lambdaQuery().eq(ServiceOrder::getUnid, unid).one();
        ServiceOrderVO vo = converter.convert(order, new ServiceOrderVO());
        vo.setServiceType(serviceTypeMap.get(order.getServiceType()));
        return vo;
    }

    /**
     * 签字专用更新接口
     *
     * @param unid 服务单unid
     * @param serviceOrderDTO dto
     * @return java.lang.String
     */
    @PostMapping("/order/sign/{unid}")
    public String updateByUnid(@PathVariable String unid, @RequestBody ServiceOrderDTO serviceOrderDTO) {
        ServiceOrder serviceOrder = converter.convert(serviceOrderDTO, new ServiceOrder());
        return serviceOrderService.lambdaUpdate().eq(ServiceOrder::getUnid, unid).update(serviceOrder) ? "提交成功" : "提交失败";
    }

    @GetMapping("/order/pushWechat")
    @SaCheckPermission(value = "order:wechatPush", orRole="admin")
    public String sendOrder(Long id) {
        return serviceOrderService.sendOrder(id);
    }
}