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.constant.EstateSecurityToken;
import com.sptg.estate.api.dto.EnvironmentalReviewDto;
import com.sptg.estate.api.entity.EnvironmentalReview;
import com.sptg.estate.api.entity.InstallationReview;
import com.sptg.estate.api.entity.Position;
import com.sptg.estate.api.entity.PositionRecord;
import com.sptg.estate.api.feigns.EnvironmentalReviewFeign;
import com.sptg.estate.api.feigns.InstallationReviewFeign;
import com.sptg.estate.api.feigns.PositionFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.permissions.api.entity.Organization;
import com.sptg.permissions.api.feigns.ConfFeign;
import com.sptg.permissions.api.feigns.OrgFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * title: EnvironmentalReviewController
 * package: com.sptg.estate.biz.service
 * description: 环境评审-控制器
 *
 *               
 *                     
 */
@RestController
@RequestMapping("/implement/environmental/review")
@RequiredArgsConstructor
public class EnvironmentalReviewController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final OrgFeign orgFeign;
    private final EnvironmentalReviewFeign environmentalReviewFeign;
    private final InstallationReviewFeign installationReviewFeign;
    private final ConfFeign confFeign;
    private final PositionFeign positionFeign;

    /**
     * 功能描述:环境评审管理界面
     * @return ModelAndView
     *               
     *                     
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.IMPLEMENT_ENVIRONMENTAL_REVIEW_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("implement/environmental-review-manage");
    }

    /**
     * 功能描述:分页查询
     * @param dto
     * @param page
     * @return ResultBean<Page<DeviceInstallation>>
     *               
     *                      
     */
    @PostMapping(value = "/showList")
    @Security(token = EstateSecurityToken.IMPLEMENT_ENVIRONMENTAL_REVIEW_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<EnvironmentalReview>> showList(EnvironmentalReviewDto dto, Page<EnvironmentalReview> page){
        Long selectTreeId = dto.getSelectTreeId();
        String orgPosition="";
        if(selectTreeId==null){
            //获取当前登陆用户
            Account onLineUser = this.securityServiceInterface.getOnLineUser();
            orgPosition=onLineUser.getOrgPosition();
        }else{
            //选择树
            Organization organization = this.orgFeign.getOrgById(dto.getSelectTreeId());
            orgPosition=organization.getPosition();
        }
        dto.setOrgPosition(orgPosition);
        //分页
        dto.setPage(page);
        return this.environmentalReviewFeign.showList(dto);
    }

    /**
     * 功能描述:环境评审详细
     * @param id
     * @return ModelAndView
     *               
     *                      
     */
    @GetMapping(value = "/detail/view/{id}")
    @Security(format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable("id") Long id){
        ModelAndView modelAndView = new ModelAndView("implement/environmental-review--detail");
        ResultBean<EnvironmentalReview> resultBean = this.environmentalReviewFeign.findById(id);
        EnvironmentalReview environmentalReview = null;
        Position position = null;
        List<InstallationReview> installationReviewList = null;
        if(resultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
            environmentalReview=resultBean.getData();
            if(environmentalReview!=null){
                ResultBean<Position> positionResultBean  = this.positionFeign.findById(environmentalReview.getPositionId());
                if(positionResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                    position = positionResultBean.getData();
                }
                ResultBean<InstallationReview> reviewResultBean = this.installationReviewFeign.findByEnvironmentalReviewId(environmentalReview.getId());
                if(reviewResultBean.getCode().equals(ResultEnum.SUCCESS.getCode())){
                    installationReviewList = reviewResultBean.getList();
                }
            }else{
                environmentalReview=new EnvironmentalReview();
                position = new Position();
            }
        }
        modelAndView.addObject("environmentalReview",environmentalReview);
        modelAndView.addObject("position",position);
        modelAndView.addObject("installationReviewList",installationReviewList);
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return  modelAndView;
    }
    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:环境评审id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     * }
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/checkState")
    @Security(token = EstateSecurityToken.IMPLEMENT_ENVIRONMENTAL_REVIEW_AUDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> checkState(EnvironmentalReviewDto dto){
        //获取审核人信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setCheckName(onLineUser.getUserName());
        dto.setCheckAccountId(onLineUser.getId());
        dto.setCheckDate(new Date());
        return this.environmentalReviewFeign.checkState(dto);
    }

    /**
     * 功能描述:根据定位id查询
     * @param positionId
     * @return
     *               
     *                      
     */
    @PostMapping(value = "/findByPositionId")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<EnvironmentalReview> findByPositionId(Long positionId){
        ResultBean<EnvironmentalReview> resultBean = this.environmentalReviewFeign.findByPositionId(Long.valueOf(positionId));
        return resultBean;
    }
}
