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.DeviceInstallationDto;
import com.sptg.estate.api.entity.DeviceInstallation;
import com.sptg.estate.api.feigns.DeviceInstallationFeign;
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;

/**
 * title: DeviceInstallationController
 * package: com.sptg.gateway.controller.implement
 * description: 设备安装-控制器
 *
 *                
 *                
 */
@RestController
@RequestMapping("/implement/device/installation")
@RequiredArgsConstructor
public class DeviceInstallationController {

    private final ConfFeign confFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;
    private final OrgFeign orgFeign;
    private final DeviceInstallationFeign deviceInstallationFeign;

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

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

    /**
     * 功能描述:设备安装详细界面
     * @param id
     * @return ModelAndView
     *                
     *                      
     */
    @GetMapping(value = "/detail/view/{id}")
    @Security(token = EstateSecurityToken.IMPLEMENT_INSTALL_MANAGE,format = DataFormatEnum.VIEW)
    public ModelAndView detail(@PathVariable("id") Long id){
        ModelAndView modelAndView = new ModelAndView("implement/device-installation-detail");
        DeviceInstallation bean = null;
        ResultBean<DeviceInstallation> resultBean = this.deviceInstallationFeign.findById(id);
        if(resultBean.getData()!=null){
            bean=resultBean.getData();
        }else {
            bean=new DeviceInstallation();
        }
        modelAndView.addObject("bean",bean);
        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_INSTALL_AUDIT,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> checkState(DeviceInstallationDto 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.deviceInstallationFeign.checkState(dto);
        if(resultBean.getData()!=null&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.IMPLEMENT_INSTALL_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),"审核",dto,onLineUser);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }
    
    /**
     * 功能描述:设备安装导出
     * @param response
     *             
     *                      
     */
    @RequestMapping(value = "/exportDeviceInstall")
    @Security(token = EstateSecurityToken.IMPLEMENT_INSTALL_EXPORT,format = DataFormatEnum.VIEW)
    public void exportDeviceInstall(HttpServletResponse response) {
        try {
            response.sendRedirect("/common/excel/exportExcel/12");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能描述:根据小区id查询设备安装记录
     * @param deviceInstallation {
     *      residentialId : 小区id
     *      checkState    : 审核状态
     * }
     *               
     *                      
     */
    @RequestMapping(value = "/findByResidentialId")
    @Security(format = DataFormatEnum.JSON)
    public ResultBean<DeviceInstallation> findByResidentialId(DeviceInstallation deviceInstallation){
        ResultBean<DeviceInstallation> resultBean = this.deviceInstallationFeign.findByCondition(deviceInstallation);
        return resultBean;
    }

    /**
     * 功能描述:删除--软删除
     * @param id
     * @return
     *               
     *                      
     */
    @GetMapping(value = "/delete/{id}")
    public ResultBean<Boolean> delete(@PathVariable(value = "id")Long id){
        ResultBean<Boolean> resultBean=this.deviceInstallationFeign.delete(id);
        return resultBean;
    }
}
