/*
 * 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.CleaningRecordPo;
import com.java110.user.dao.property.ICleaningRecordV1ServiceDao;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.Assert;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * 保洁作业上报接口
 * 服务编码：property.cleaningReport
 * 请求路径：/app/property.cleaningReport
 */
@Java110Cmd(serviceCode = "property.cleaningReport")
public class CleaningReportCmd extends Cmd {

    private static Logger logger = LoggerFactory.getLogger(CleaningReportCmd.class);

    @Autowired
    private ICleaningRecordV1ServiceDao cleaningRecordV1ServiceDao;

    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) {
        Assert.hasKeyAndValue(reqJson, "userId", "未包含作业人ID");
        Assert.hasKeyAndValue(reqJson, "workArea", "未包含作业区域");
        Assert.hasKeyAndValue(reqJson, "longitude", "未包含经度");
        Assert.hasKeyAndValue(reqJson, "latitude", "未包含纬度");
        // 保洁图片：最少1张，最多3张
        Assert.hasKeyAndValue(reqJson, "images", "未包含作业图片，至少上传1张");
        JSONArray images = reqJson.getJSONArray("images");
        if (images == null || images.isEmpty()) {
            throw new CmdException("保洁作业图片至少上传1张，最多3张");
        }
        if (images.size() > 3) {
            throw new CmdException("保洁作业图片最多上传3张");
        }
    }

    @Override
    @Java110Transactional
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException {
        CleaningRecordPo po = new CleaningRecordPo();
        po.setId(GenerateCodeFactory.getGeneratorId("12"));
        po.setUserId(reqJson.getString("userId"));
        po.setUserName(reqJson.containsKey("userName") ? reqJson.getString("userName") : "");
        po.setWorkArea(reqJson.getString("workArea"));
        po.setWorkContent(reqJson.containsKey("workContent") ? reqJson.getString("workContent") : "");
        po.setWorkTime(new Date());
        po.setLongitude(reqJson.getBigDecimal("longitude"));
        po.setLatitude(reqJson.getBigDecimal("latitude"));
        JSONArray imagesArr = reqJson.getJSONArray("images");
        po.setImages(imagesArr != null ? imagesArr.toJSONString() : null);
        po.setRemark(reqJson.containsKey("remark") ? reqJson.getString("remark") : null);

        cleaningRecordV1ServiceDao.saveCleaningRecord(po);
        context.setResponseEntity(ResultVo.success());
    }
}
