package com.sptg.gateway.controller.implement;

import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.estate.api.entity.InstallationReview;
import com.sptg.estate.api.feigns.InstallationReviewFeign;
import com.sptg.permissions.api.feigns.ConfFeign;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
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 java.util.HashMap;
import java.util.Map;

/**
 * title: InstallationReviewController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-安装环境控制器
 *
 *               
 *                     
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/installation/review")
public class InstallationReviewController {

    private final InstallationReviewFeign installationReviewFeign;
    private final ConfFeign confFeign;

    /**
     * 功能描述:检测定位是否已经创建环境评审记录
     * @param map{
     *           positionId      :       定位id
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/countByPositionId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> countByPositionId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "检测失败");
        try {
            String positionId = map.get("positionId");
            if(StringUtils.isBlank(positionId)){
                hashMap.put("result_msg", "positionId不能为空");
                return hashMap;
            }
            ResultBean<Boolean> resultBean = this.installationReviewFeign.countByPositionId(Long.valueOf(positionId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:根据定位id获取定位记录
     * @param map{
     *           positionRecordId      :       定位记录id
     * }
     * @return
     *               
     *                     
     */
    @PostMapping("/findByPositionRecordId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findByPositionRecordId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取失败");
        try {
            String positionRecordId = map.get("positionRecordId");
            if(StringUtils.isBlank(positionRecordId)){
                hashMap.put("result_msg", "positionRecordId不能为空");
                return hashMap;
            }
            ResultBean<InstallationReview> resultBean = this.installationReviewFeign.findByPositionRecordId(Long.valueOf(positionRecordId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("installationReview", resultBean.getData());
                hashMap.put("fastDfsServer", this.confFeign.getFileServerHost());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:新增/修改
     * @param installationReview
     * @return
     *               
     *                     
     */
    @PostMapping("/insertOrUpdate")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insertOrUpdate(@RequestBody InstallationReview installationReview){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "保存失败");
        try {
            ResultBean<Boolean> resultBean = this.installationReviewFeign.insertOrUpdate(installationReview);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "保存成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:根据环境评审id获安装环境评审记录
     * @param map{
     *           environmentalReviewId      :       环境评审id
     * }
     * @return
     *               
     *                     
     */
    @PostMapping("/findByEnvironmentalReviewId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findByEnvironmentalReviewId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取失败");
        try {
            String environmentalReviewId = map.get("environmentalReviewId");
            if(StringUtils.isBlank(environmentalReviewId)){
                hashMap.put("result_msg", "environmentalReviewId不能为空");
                return hashMap;
            }
            ResultBean<InstallationReview> resultBean = this.installationReviewFeign.findByEnvironmentalReviewId(Long.valueOf(environmentalReviewId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("list", resultBean.getList());
                hashMap.put("fastDfsServer", this.confFeign.getFileServerHost());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:根据id获取记录
     * @param map{
     *           id      :       id
     * }
     * @return
     *               
     *                     
     */
    @PostMapping("/findInstallationReviewById")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findInstallationReviewById(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取失败");
        try {
            String id = map.get("id");
            if(StringUtils.isBlank(id)){
                hashMap.put("result_msg", "id不能为空");
                return hashMap;
            }
            ResultBean<InstallationReview> resultBean = this.installationReviewFeign.findById(Long.valueOf(id));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("installationReview", resultBean.getData());
                hashMap.put("result_msg", "获取成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
