package com.sptg.gateway.controller.device;

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.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.device.api.constant.DeviceSecurityToken;
import com.sptg.device.api.dto.DevicePathDto;
import com.sptg.device.api.entity.DevicePath;
import com.sptg.device.api.feigns.DevicePathFeign;
import com.sptg.permissions.api.entity.Account;
import com.sptg.statistics.api.constant.SysLogMenuConstant;
import com.sptg.statistics.api.entity.SysOperationLog;
import com.sptg.statistics.api.feigns.SysOperationLogFeign;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * title: DevicePathController
 * package: com.sptg.gateway.controller.device
 * description: 设备位置-控制器
 *
 *
 *
 */
@RestController
@AllArgsConstructor
public class DevicePathController {

    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final DevicePathFeign devicePathFeign;
    private final SysOperationLogFeign sysOperationLogFeign;

    /**
     * 功能描述:分页查询设备位置列表
     * @param dto 查询条件
     * @param page 分页条件
     * @return ResultBean<Page>
     *
     *
     */
    @GetMapping(value = "/devicePath/manage/pagingQuery")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page> pagingQuery(DevicePathDto dto, Page<DevicePath> page){
        //  获取当前用户
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setPage(page);
        dto.setOrgPosition(onLineUser.getOrgPosition());
        ResultBean<Page> pageResultBean = devicePathFeign.pagingQuery(dto);
        return pageResultBean;
    }

    /**
     * 功能描述:添加设备位置信息
     * @param dto
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/path/add")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addEntity(DevicePathDto dto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        dto.setRootOrgId(onLineUser.getRootOrgId());
        ResultBean<Boolean> resultBean = devicePathFeign.addEntity(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_PATH_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),dto,onLineUser,dto);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:根据id获取设备位置信息
     * @param id
     * @return ResultBean<DevicePath>
     *
     *
     */
    @GetMapping(value = "/device/path/get/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<DevicePath> get(@PathVariable(value = "id") Long id){
        DevicePathDto dto=new DevicePathDto();
        dto.setId(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        DevicePath devicePath = devicePathFeign.getEntityById(dto);
        return new ResultBean<>(ResultEnum.SUCCESS,devicePath);
    }

    /**
     * 功能描述:修改设备位置信息
     * @param dto
     * @return ResultBean<Boolean>
     *
     *
     */
    @PostMapping(value = "/device/path/editEntity")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> editEntity(DevicePathDto dto){
        DevicePath oldObj = this.devicePathFeign.getEntityById(dto.getId());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        ResultBean<Boolean> resultBean = devicePathFeign.editEntity(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            DevicePath newObj = this.devicePathFeign.getEntityById(dto.getId());
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_PATH_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:删除设备位置信息
     * @param id
     * @return ResultBean<Boolean>
     *
     *
     */
    @GetMapping(value = "/device/path/deleteEntity/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteEntity(@PathVariable(value = "id") Long id){
        DevicePath oldObj = this.devicePathFeign.getEntityById(id);
        DevicePathDto dto = new DevicePathDto();
        dto.setId(id);
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        ResultBean<Boolean> resultBean = devicePathFeign.deleteEntity(dto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_FLOW_CARD, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),dto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    /**
     * 功能描述:根据名称查询设备位置列表
     * @param dto
     * @return List<DevicePath>
     *
     *
     */
    @GetMapping(value = "/device/path/getDevicePathListByName")
    @Security(token = DeviceSecurityToken.DEVICE_PATH_MANAGE,format = DataFormatEnum.JSON)
    public  ResultBean<DevicePath> getDevicePathListByName(DevicePathDto dto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        dto.setOrgPosition(onLineUser.getOrgPosition());
        List<DevicePath> list = devicePathFeign.getList(dto);
        return new ResultBean<DevicePath>(ResultEnum.SUCCESS,list);
    }
}
