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.EnvironmentalReviewDto;
import com.sptg.estate.api.dto.ProjectDto;
import com.sptg.estate.api.entity.EnvironmentalReview;
import com.sptg.estate.api.entity.InstallationReview;
import com.sptg.estate.api.entity.Project;
import com.sptg.estate.api.feigns.EnvironmentalReviewFeign;
import com.sptg.permissions.api.entity.Account;
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: EnvironmentalReviewController
 * package: com.sptg.estate.biz.service
 * description: 实施小程序-环境评审控制器
 *
 *               
 *                     
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/app/environmental/review")
public class EnvironmentalReviewController {

     private final EnvironmentalReviewFeign environmentalReviewFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    /**
     * 功能描述:检测定位是否已经创建环境评审记录
     * @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.environmentalReviewFeign.countByPositionId(Long.valueOf(positionId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_msg", "获取成功");
                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{
     *           positionId      :       定位id
     * }
     * @return
     *               
     *                      
     */
    @PostMapping("/findByPositionId")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> findByPositionId(@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<EnvironmentalReview> resultBean = this.environmentalReviewFeign.findByPositionId(Long.valueOf(positionId));
            if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                hashMap.put("result_msg", "获取成功");
                hashMap.put("result_code", 1);
                hashMap.put("environmentalReview",resultBean.getData());
            }
        }catch (Exception e){
            log.error("异常", e);
            hashMap.put("result_msg", "后台程序异常");
        }
        return hashMap;
    }

    /**
     * 功能描述:新增/修改
     * @param environmentalReview
     * @return
     *               
     *                     
     */
    @PostMapping("/insertOrUpdate")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> insertOrUpdate(@RequestBody EnvironmentalReview environmentalReview){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "保存失败");
        try {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            //新增
            if(environmentalReview.getId()==null){
                environmentalReview.setCheckState(OperationConstant.ZERO);
                environmentalReview.setCreateAccountId(onLineUser.getId());
                environmentalReview.setCreateName(onLineUser.getUserName());
                environmentalReview.setCreateDate(new Date());
                environmentalReview.setOrgId(onLineUser.getOrgId());
                environmentalReview.setRootOrgId(onLineUser.getRootOrgId());
                environmentalReview.setOrgPosition(onLineUser.getOrgPosition());
            }
            ResultBean<Boolean> resultBean = this.environmentalReviewFeign.insertOrUpdate(environmentalReview);
            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;
    }


    /**
     * 查询(分页)
     * @param page {
     *              current         :    当前页码,
     *              size            :    登录密码
     *
     *            }
     * @param dto{
     *            residentialName   :    项目名称
     *         }
     *               
     *                     
     */
    @PostMapping(value = "/showList")
    @Security(format = DataFormatEnum.JSON)
    public Map<String,Object> showList(@RequestBody EnvironmentalReviewDto dto, @RequestBody Page<EnvironmentalReview> page){
        Map<String,Object> hashMap = new HashMap<>(3);
        hashMap.put("result_code", 0);
        hashMap.put("result_msg", "获取数据失败");
        try {
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            dto.setOrgPosition(onLineUser.getOrgPosition());
            dto.setPage(page);
            ResultBean<Page<EnvironmentalReview>> resultBean =  this.environmentalReviewFeign.showList(dto);
            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 = "/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 onLineUser = this.securityServiceInterface.getOnLineUser();
            // 统计数量
            ResultBean<Integer> resultBean = this.environmentalReviewFeign.countByOrgPosition(onLineUser.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 dto
     * {
     *     id:环境评审id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *                
     *                      
     */
    @PostMapping(value = "/checkState")
    public Map<String,Object> checkState(@RequestBody EnvironmentalReviewDto 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.environmentalReviewFeign.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;
    }
}
