package com.zjrs.ggfw.xcx.bpo.impl;

import org.apache.commons.lang.StringUtils;
import org.mohrss.leaf.core.framework.domain.bpo.impl.BPOImpl;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import com.zjrs.ggfw.xcx.blo.Gcj02BLO;
import com.zjrs.ggfw.xcx.bpo.Gcj02BPO;
import com.zjrs.ggfw.xcx.dto.Gcj02DTO;
import com.zjrs.ggfw.xcx.po.Gcj02Data;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
public class Gcj02BPOImpl extends BPOImpl implements Gcj02BPO {
	
	@Resource
    private Gcj02BLO gcj02BLO;
	
	@Override
	public AjaxResponse getRecord(Gcj02DTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();

		String msg = "";

		//目标的名称、地址校验
		if(StringUtils.isBlank(dto.getName())) {
			msg += "目标地名称不可为空！";
		}
		if(StringUtils.isBlank(dto.getAddress())) {
			msg += "目标地地址不可为空！";
		}

		if(!"".equals(msg)) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg(msg);
			return ajaxResponse;
		}

		Integer num = gcj02BLO.getRecord(dto);
		ajaxResponse.setData("count", num);

		return ajaxResponse;
	}
	
	@Override
	public AjaxResponse saveRecord(Gcj02DTO dto) {
		AjaxResponse ajaxResponse = new AjaxResponse();
		DateTimeFormatter formatters = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    	String dqsj = LocalDateTime.now().format(formatters);//当前时间

		//校验必录
		String msg = "";

		//目标地经度、纬度校验
		if(dto.getLongitude() == null) {
			msg += "目标地经度不可为空！";
		}
		if(dto.getLatitude() == null) {
			msg += "目标地纬度不可为空！";
		}
		
		//目标的名称、地址校验
		if(StringUtils.isBlank(dto.getName())) {
			msg += "目标地名称不可为空！";
		}
		if(StringUtils.isBlank(dto.getAddress())) {
			msg += "目标地地址不可为空！";
		}

		if(!"".equals(msg)) {
			ajaxResponse.setAppcode("1");
			ajaxResponse.setMsg(msg);
			return ajaxResponse;
		}
		
		//保存选中火星坐标数据对象
		Gcj02Data gcj02Data = new Gcj02Data();
		BeanUtils.copyProperties(dto, gcj02Data);
		//默认参数
		if(StringUtils.isBlank(gcj02Data.getGcj02DataLsh())) {
			gcj02Data.setGhsj(dqsj);//固化时间
		}
		gcj02Data = gcj02BLO.saveRecord(gcj02Data);

		ajaxResponse.setData("data", gcj02Data);
		
		return ajaxResponse;
	}
}