package com.persagy.proxy.object.controller;

import cn.hutool.core.collection.CollUtil;
import com.persagy.proxy.adm.request.AdmCreateRequest;
import com.persagy.proxy.adm.request.AdmCreateResponse;
import com.persagy.proxy.adm.request.AdmQueryCriteria;
import com.persagy.proxy.adm.request.AdmResponse;
import com.persagy.proxy.adm.utils.AdmContextUtil;
import com.persagy.proxy.common.entity.DmpResult;
import com.persagy.proxy.common.entity.InstanceUrlParam;
import com.persagy.proxy.object.model.AdmLocationPoint;
import com.persagy.proxy.object.model.AdmPointImage;
import com.persagy.proxy.object.service.IAdmLocationPointService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.ws.rs.QueryParam;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName AdmLocationPointController
 * @Description: 点位标签
 * @Author linhuili
 * @Date 2021/8/31 09:44
 * @Version V1.0
 **/
@RestController
@RequestMapping("/object/location-point")
@Slf4j
public class AdmLocationPointController {

    @Autowired
    private IAdmLocationPointService service;

    /**
     * 统计点位标签
     * @param projectId
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/count")
    public AdmResponse count(@QueryParam("projectId") String projectId,
                             @RequestBody AdmQueryCriteria request) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        request.setName("point");
        request.setOnlyCount(true);
        return service.doQuery(context, request, AdmLocationPoint.class);
    }

    /**
     * 查询信息点标签
     * @param projectId
     * @param request
     * @return
     * @throws Exception
     */
    @PostMapping("/query")
    public AdmResponse query(@QueryParam("projectId") String projectId,
                             @RequestBody AdmQueryCriteria request) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        request.setName("point");
        // 查询信息点标签
        AdmResponse admResponse = service.doQuery(context, request, AdmLocationPoint.class);
        // 查询结果处理
        handleQueryLocationPointResult(admResponse);
        return  admResponse;
    }

    /**
     * 创建点位标签信息
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/create")
    public AdmCreateResponse create(@RequestBody AdmCreateRequest<AdmLocationPoint> createRequest, @QueryParam("projectId") String projectId) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<AdmLocationPoint> vos = CollUtil.newArrayList(createRequest.getContent());
        log.info("创建条件"+vos.toString());
        vos = service.doInsert(context, AdmLocationPoint.class, vos);
        return AdmCreateResponse.success(vos);
    }


    /**
     * 更新点位标签信息
     * @param createRequest
     * @return
     * @throws Exception
     */
    @PostMapping("/update")
    public AdmResponse update(@RequestBody AdmCreateRequest<AdmLocationPoint> createRequest, @QueryParam("projectId") String projectId) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        List<AdmLocationPoint> vos = CollUtil.newArrayList(createRequest.getContent());
        vos = service.doUpdate(context, AdmLocationPoint.class, vos);
        return AdmResponse.success(vos);
    }


    /**
     * 删除点位标签
     * @param vos
     * @return
     * @throws Exception
     */
    @PostMapping("/delete")
    public AdmResponse delete(@QueryParam("projectId") String projectId, @RequestBody List<AdmLocationPoint> vos) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        service.doDelete(context, vos);
        return AdmResponse.success();
    }

    /**
     * 删除点位图片
     * @param vos
     * @return
     * @throws Exception
     */
    @PostMapping("/delete-image")
    public AdmResponse deleteImage(@QueryParam("projectId") String projectId, @RequestBody List<AdmPointImage> vos) throws Exception {
        // 组装上下文条件
        InstanceUrlParam context = AdmContextUtil.toDmpContext();
        // 删除图片参数转换
        List<AdmLocationPoint> points = deleteImageParamRevert(vos);
        service.doUpdate(context,AdmLocationPoint.class, points);
        return AdmResponse.success();
    }

    /**
     * 删除图片参数转换
     * @param vos
     * @return
     */
    private List<AdmLocationPoint> deleteImageParamRevert(List<AdmPointImage> vos){
        if(CollUtil.isEmpty(vos)){
            return new ArrayList<>();
        }
        ArrayList<AdmLocationPoint> points = new ArrayList<>();
        for (AdmPointImage vo : vos) {
            AdmLocationPoint point = new AdmLocationPoint();
            point.setId(vo.getObjectId());
            point.setImageList(new ArrayList<>());
            points.add(point);
        }
        return points;
    }

    /**
     * 查询标签信息结果处理
     * @param admResponse
     */
    private void handleQueryLocationPointResult(AdmResponse admResponse){
        if(!DmpResult.SUCCESS.equals(admResponse.getResult())){
            return;
        }
        List<AdmLocationPoint> points = (List<AdmLocationPoint>) admResponse.getContent();
        if(CollUtil.isEmpty(points)){
            return;
        }
        for (AdmLocationPoint point : points) {
            ArrayList<AdmPointImage> imageList = point.getImageList();
            if(CollUtil.isEmpty(imageList)){
                continue;
            }
            imageList.forEach(item->{
                item.setObjectId(point.getId());
            });
        }
    }
}
