ServiceOrderController.java 4.02 KB
package vion.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.hutool.core.lang.Assert;
import org.dromara.hutool.core.data.id.IdUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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("/orders")
    @SaCheckPermission(value = "order:list", orRole = "admin")
    public Page<ServiceOrderVO> getServiceOrderList(ServiceOrderDTO data) {
        return serviceOrderService.getServiceOrderList(data);
    }

    @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")
    public Object save(@RequestBody ServiceOrderDTO serviceOrderDTO) {
        Long count = serviceOrderService.lambdaQuery().eq(ServiceOrder::getTaskId, serviceOrderDTO.getTaskId()).count();
        Assert.isFalse(count > 0, "该工单已存在服务单,不能重复创建!");
        ServiceOrder serviceOrder = converter.convert(serviceOrderDTO, ServiceOrder.class);
        serviceOrder.setUnid(IdUtil.nanoId());
        return serviceOrderService.save(serviceOrder) ? serviceOrder.getId() : "创建失败";
    }

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

    @DeleteMapping("/order/{id}")
    @SaCheckPermission(value = "order:remove", orRole = "admin")
    public 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, ServiceOrderVO.class);
        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, ServiceOrder.class);
        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);
    }
}