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.core.constant.SysLogConstant;
import com.sptg.common.core.util.WebUtils;
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.PositionDto;
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.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 com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * title: PositionController
 * package: com.sptg.gateway.controller.implement
 * description:设备定位
 *
 *                
 *                
 */
@RestController
@RequestMapping("/implement/position")
@RequiredArgsConstructor
public class PositionController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final PositionFeign positionFeign;
    private final OrgFeign orgFeign;

    /**
     * 功能描述:设备定位界面
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping("/index")
    @Security(token = EstateSecurityToken.IMPLEMENT_POSITION_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView index(){
        return new ModelAndView("implement/position-manage");
    }

    /**
     * 功能描述:分页查询设备定位列表
     * @param dto
     * @param page 分页
     * @return ResultBean<Page<Position>>
     *                
     *                      
     */
    @PostMapping(value = "/showList")
    @Security(token = EstateSecurityToken.IMPLEMENT_POSITION_MANAGE,format = DataFormatEnum.VIEW)
    public ResultBean<Page<Position>> showList(PositionDto dto,Page<Position> 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.positionFeign.showList(dto);
    }

    /**
     * 功能描述:设备定位详细
     * @param id
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping(value = "/detail/view/{id}")
    @Security(token = EstateSecurityToken.IMPLEMENT_POSITION_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable("id") Long id){
        ModelAndView modelAndView = new ModelAndView("implement/position-detail");
        ResultBean<Position> resultBean = this.positionFeign.findById(id);
        Position position=null;
        List<PositionRecord> positionRecordList=null;
        if(resultBean!=null){
            position=resultBean.getData();
            if(position!=null){
                //定位记录
                positionRecordList= this.positionFeign.getPositionRecordListById(id);
            }else{
                position=new Position();
            }
        }
        modelAndView.addObject("bean",position);
        modelAndView.addObject("positionRecordList",positionRecordList);
        modelAndView.addObject("fastDfsServer", this.confFeign.getFileServerHost());
        return  modelAndView;
    }

    /**
     * 功能描述:审核状态
     * @param dto
     * {
     *     id:定位id
     *     checkState:审核状态：0-待审核 1-审核通过 2-审核不通过
     *     checkOpinion:审核不通过原因，当checkState = 2有值
     *     checkDate:审核时间
     *     checkAccountId:审核人id
     *     CheckName:审核人
     *
     * }
     * @return
     *                
     *                      
     */
    @PostMapping(value = "/checkState")
    @Security(token = EstateSecurityToken.IMPLEMENT_POSITION_AUDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> checkState(PositionDto dto){
        if(dto.getCheckState()==null||!(dto.getCheckState()==OperationConstant.ONE||dto.getCheckState()==OperationConstant.TWO)){
            return new ResultBean<>(ResultEnum.SUCCESS,false);
        }
        //获取审核人信息
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setCheckName(onLineUser.getUserName());
        dto.setCheckAccountId(onLineUser.getId());
        ResultBean<Boolean> resultBean = this.positionFeign.checkState(dto);
        if(resultBean.getData()!=null&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.IMPLEMENT_POSITION_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"审核",dto,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
    
    /**
     * 功能描述:设备定位导出
     * @param response
     *             
     *                      
     */
    @RequestMapping(value = "/exportPosition")
    @Security(token = EstateSecurityToken.IMPLEMENT_POSITION_EXPORT,format = DataFormatEnum.VIEW)
    public void exportPosition(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/11");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/findByProjectId")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<Position> findByProjectId(Long projectId){
        ResultBean<Position> resultBean = this.positionFeign.findByProjectId(projectId);
        return resultBean;
    }
}
