package com.sptg.gateway.controller.implement;

import com.baomidou.mybatisplus.plugins.Page;
import com.sptg.common.core.beans.ResultBean;
import com.sptg.common.core.beans.ResultEnum;
import com.sptg.common.core.constant.OperationConstant;
import com.sptg.common.security.annotation.Security;
import com.sptg.common.security.enums.DataFormatEnum;
import com.sptg.common.security.service.SecurityServiceInterface;
import com.sptg.estate.api.dto.PositionDto;
import com.sptg.estate.api.entity.Position;
import com.sptg.estate.api.entity.PositionRecord;
import com.sptg.estate.api.feigns.PositionFeign;
import com.sptg.estate.api.feigns.PositionRecordFeign;
import com.sptg.permissions.api.entity.Account;
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.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * title: PositionController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-定位控制器
 *
 *               
 *                      
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/position")
public class PositionController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final PositionFeign positionFeign;
    private final PositionRecordFeign positionRecordFeign;
    private final ConfFeign confFeign;
    /**
     * 功能描述: 统计定位数量
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/count")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> count(){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取结果失败");
        try {
            //获取登录账号
            Account account = this.securityServiceInterface.getOnLineUser();
            // 统计项目数量
            ResultBean<Integer> resultBean = this.positionFeign.countByOrgPosition(account.getOrgPosition());
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "获取成功");
                hashMap.put("count", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 查询实施项目列表(分页)
     * @param page {
     *              current         :    当前页码,
     *              size            :    登录密码
     *
     *            }
     * @param positionDto{
     *            residentialName   :    项目名称
     *         }
     * @return
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> showList(@RequestBody PositionDto positionDto, @RequestBody Page<Position> page){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取数据失败");
        try {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            positionDto.setOrgPosition(onLineUser.getOrgPosition());
            positionDto.setPage(page);
            ResultBean<Page<Position>> resultBean = this.positionFeign.showList(positionDto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("list", resultBean.getData().getRecords());
                hashMap.put("result_code", 1);
                hashMap.put("result_msg", "成功");
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }

        return hashMap;
    }

    /**
     * 功能描述: 定位详情
     * @return ResultBean<hashMap> 返回结果
     *               
     *                      
     */
    @PostMapping(value = "/detail")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> detail(@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;
            }
            Position position  = this.positionFeign.findById(Long.valueOf(id)).getData();
            if(position!=null){
                hashMap.put("result_code", 1);
                hashMap.put("position", position);
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:检测项目是否已经创建定位记录
     * @param map{
     *           projectId      :       项目id
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/countByProjectId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> countByProjectId(@RequestBody Map<String,String> map){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "检测失败");
        try {
            String projectId = map.get("projectId");
            if(StringUtils.isBlank(projectId)){
                hashMap.put("result_msg", "projectId不能为空");
                return hashMap;
            }
            ResultBean<Boolean> resultBean = this.positionFeign.countByProjectId(Long.valueOf(projectId));
            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;
    }

    /**
     * 功能描述:添加
     * @param dto
     * @return
     *               
     *                      
     */
    @RequestMapping(value = "/insert")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insert(@RequestBody PositionDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "添加失败");
        try {
            if(dto.getProjectId()==null){
                hashMap.put("result_msg", "projectId不能为空");
                return hashMap;
            }
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setCreateAccountId(onLineUser.getId());
            dto.setCreateName(onLineUser.getUserName());
            dto.setCreateDate(new Date());
            dto.setOrgId(onLineUser.getOrgId());
            dto.setRootOrgId(onLineUser.getRootOrgId());
            dto.setOrgPosition(onLineUser.getOrgPosition());
            dto.setCheckState(OperationConstant.ZERO);
            ResultBean<Boolean> resultBean = this.positionFeign.insert(dto);
            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;
    }

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

    /**
     * 功能描述:根据定位id获取定位记录
     * @param map{
     *           positionId      :       定位id
     * }
     * @return
     *               
     *                     
     */
    @PostMapping("/findRecordByPositionId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findRecordByPositionId(@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<PositionRecord> resultBean = this.positionRecordFeign.getListByPositionId(Long.valueOf(positionId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_msg", "获取成功");
                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;
    }

    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:定位id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *                
     *                      
     */
    @PostMapping(value = "/checkState")
    public Map<String,Object> checkState(@RequestBody PositionDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "审核失败");
        try {
            //获取审核人信息
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setCheckName(onLineUser.getUserName());
            dto.setCheckAccountId(onLineUser.getId());
            dto.setCheckDate(new Date());
            ResultBean<Boolean> resultBean = this.positionFeign.checkState(dto);
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode()) &&  resultBean.getData()){
                hashMap.put("result_msg", "审核成功");
                hashMap.put("result_code", 1);
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:修改
     * @param dto
     * @return
     *               
     *                      
     */
    @RequestMapping(value = "/edit")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> edit(@RequestBody PositionDto dto){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "保存失败");
        try {
            if(dto.getId()==null){
                hashMap.put("result_msg", "id不能为空");
            }
            ResultBean<Boolean> resultBean = this.positionFeign.editEntity(dto);
            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{
     *           positionRecordId      :       记录id
     * }
     * @return
     *               
     *                     
     */
    @PostMapping("/findRecordByPositionRecordId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findRecordByPositionRecordId(@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<PositionRecord> resultBean = this.positionRecordFeign.findById(Long.valueOf(positionRecordId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_msg", "获取成功");
                hashMap.put("result_code", 1);
                hashMap.put("positionRecord", resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }
}
