SubmitRepairOrderCmd.java 4.65 KB
/*
 * Copyright 2017-2020 吴学文 and java110 team.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.java110.user.cmd.property;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.annotation.Java110Transactional;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.factory.GenerateCodeFactory;
import com.java110.po.property.MessageNotifyPo;
import com.java110.po.property.RepairOrderPo;
import com.java110.user.dao.property.IMessageNotifyV1ServiceDao;
import com.java110.user.dao.property.IPropertyUserV1ServiceDao;
import com.java110.user.dao.property.IRepairOrderV1ServiceDao;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;

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

@Java110Cmd(serviceCode = "property.submitRepairOrder")
public class SubmitRepairOrderCmd extends Cmd {

    @Autowired
    private IRepairOrderV1ServiceDao repairOrderV1ServiceDao;
    @Autowired
    private IPropertyUserV1ServiceDao propertyUserV1ServiceDao;
    @Autowired
    private IMessageNotifyV1ServiceDao messageNotifyV1ServiceDao;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "submitterId", "未包含提交人ID");
        Assert.hasKeyAndValue(reqJson, "title", "未包含工单标题");
        Assert.hasKeyAndValue(reqJson, "repairLocation", "未包含报修位置");
        Assert.hasKeyAndValue(reqJson, "longitude", "未包含经度");
        Assert.hasKeyAndValue(reqJson, "latitude", "未包含纬度");
        Assert.hasKeyAndValue(reqJson, "images", "未包含报修图片,至少上传1张");
        JSONArray images = reqJson.getJSONArray("images");
        if (images == null || images.isEmpty()) {
            throw new CmdException("报修图片至少上传1张");
        }
    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        Map assignParams = new HashMap<>();
        assignParams.put("workType", "ENGINEERING");
        assignParams.put("status", "ON");
        List<Map> engineers = propertyUserV1ServiceDao.queryPropertyUsers(assignParams);

        RepairOrderPo po = new RepairOrderPo();
        po.setId(GenerateCodeFactory.getGeneratorId("20"));
        po.setSubmitterId(reqJson.getString("submitterId"));
        po.setSubmitterName(reqJson.containsKey("submitterName") ? reqJson.getString("submitterName") : "");
        po.setTitle(reqJson.getString("title"));
        po.setDescription(reqJson.containsKey("description") ? reqJson.getString("description") : "");
        po.setRepairLocation(reqJson.getString("repairLocation"));
        po.setLongitude(reqJson.getBigDecimal("longitude"));
        po.setLatitude(reqJson.getBigDecimal("latitude"));
        po.setStatus("PENDING");
        JSONArray imagesArr = reqJson.getJSONArray("images");
        po.setImages(imagesArr != null ? imagesArr.toJSONString() : null);

        if (engineers != null && !engineers.isEmpty()) {
            Map engineer = engineers.get(0);
            po.setRepairerId((String) engineer.get("user_id"));
            po.setRepairerName((String) engineer.get("name"));
        }

        repairOrderV1ServiceDao.saveRepairOrder(po);

        if (po.getRepairerId() != null) {
            MessageNotifyPo notify = new MessageNotifyPo();
            notify.setId(GenerateCodeFactory.getGeneratorId("30"));
            notify.setUserId(po.getRepairerId());
            notify.setTitle("新工单分配");
            notify.setContent("您有新的维修工单【" + po.getTitle() + "】待接单");
            notify.setMsgType("REPAIR_NEW");
            notify.setRelateId(po.getId());
            messageNotifyV1ServiceDao.saveMessageNotify(notify);
        }

        context.setResponseEntity(ResultVo.createResponseEntity(1, 1, po.getId()));
    }
}