package cn.com.ty.lift.ud.motiontrail.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.com.ty.lift.ud.motiontrail.controller.query.MotionTrailQuery;
import cn.com.ty.lift.ud.motiontrail.mapper.MotionTrailMapper;
import cn.com.ty.lift.ud.motiontrail.mapper.entity.MotionTrail;
import cn.com.ty.lift.ud.motiontrail.service.IMotionTrailService;
import cn.com.ty.lift.ud.userInfo.mapper.UserInfoMapper;
import cn.com.ty.lift.ud.userInfo.mapper.entity.UserInfoEntity;
import cn.com.xwy.boot.web.dto.RestResponse;

/**
 * 人员轨迹-ServiceImpl
 *
 * @author Xson
 * @date 2020-07-15
 */
@Service
@Transactional
public class MotionTrailServiceImpl extends ServiceImpl<MotionTrailMapper, MotionTrail> implements IMotionTrailService {

    @Autowired
    private MotionTrailMapper motionTrailMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 根据条件查询人员轨迹
     *
     * @param request
     * @return
     */
    @Override
    public RestResponse queryTrail(MotionTrailQuery request) {
        if (null == request.getCreateTime()) {
            return RestResponse.fail("查询时间不能为空");
        }
        if (request.getUserId() == null) {
            return RestResponse.fail("维保工不能为空");
        }
        /*if (null == request.getMtCompanyId()) {
            RestResponse.fail("维保公司不能为空");
        }*/

        List<MotionTrail> list = list(request.condition());

        // 格式化返回数据
        /**
         * polyline: { path: [[114.462895,30.473531], [114.461607,30.468315] }, texts: [ {position:
         * [114.462895,30.473531],text: '1.2020-07-15 9：00pm'}, {position: [114.461607,30.468315],text: '2.2020-07-15
         * 7：00pm'} ]
         */
        Map<String, List> rst = new HashMap<>();
        List polylines = new ArrayList();
        List texts = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        list.forEach(item -> {
            String[] split = item.getPosition().split(",");
            List<Double> mark = Arrays.asList(Double.parseDouble(split[1]), Double.parseDouble(split[0]));
            polylines.add(mark);

            Map temp = new HashMap();
            temp.put("position", mark);
            temp.put("text", sdf.format(item.getCreateTime()));
            texts.add(temp);
        });
        rst.put("polylines", polylines);
        rst.put("texts", texts);

        return RestResponse.success(rst);
    }

    /**
     * 新增位置记录
     * 
     * @param motionTrail
     * @return
     */
    @Override
    public RestResponse addTrail(MotionTrail motionTrail) {
        motionTrail.setCreateTime(new Date());
        int num = motionTrailMapper.addTrail(motionTrail.getMobile(), motionTrail.getMtCompanyId(),
            motionTrail.getPosition(), motionTrail.getCreateTime(), motionTrail.getUserId());
        if (num > 0) {
            return RestResponse.success(null, "新增成功");
        } else {
            return RestResponse.fail("新增失败");
        }
    }

    @Override
    public RestResponse queryTrailByTeam(MotionTrailQuery request) {
        if (null == request.getMtCompanyId()) {
            RestResponse.fail("团队不能为空");
        }
        List<MotionTrail> queryTrailByTeam = motionTrailMapper.queryTrailByTeam(request.getMtCompanyId());
        for (MotionTrail mt : queryTrailByTeam) {
            UserInfoEntity byUserId = userInfoMapper.getByUserId(mt.getUserId());
            mt.setUserInfo(byUserId);
        }
        Map<String, List> rst = new HashMap<>();
        List polylines = new ArrayList();
        List texts = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        queryTrailByTeam.forEach(item -> {
            String[] split = item.getPosition().split(",");
            List<Double> mark = Arrays.asList(Double.parseDouble(split[1]), Double.parseDouble(split[0]));
            polylines.add(mark);

            Map temp = new HashMap();
            temp.put("position", mark);
            temp.put("text", sdf.format(item.getCreateTime()));
            texts.add(temp);
        });
        rst.put("polylines", polylines);
        rst.put("texts", texts);
        return RestResponse.success(rst);
    }
}
