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.DeviceTemplateDto;
import com.sptg.device.api.entity.DeviceTemplate;
import com.sptg.device.api.feigns.DeviceTemplateFeign;
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.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@RestController
@AllArgsConstructor
public class DeviceTemplateController {
    private final DeviceTemplateFeign deviceTemplateFeign;
    private final SecurityServiceInterface<Account> securityServiceInterface;
    private final SysOperationLogFeign sysOperationLogFeign;



    @GetMapping(value = "/deviceTemplate/manage")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE,format = DataFormatEnum.JSON)
    public ModelAndView deviceTemplateManage(){
        return new ModelAndView("device/device-template-manage");
    }

    @GetMapping(value = "/deviceTemplate/add")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_ADD,format = DataFormatEnum.JSON)
    public ModelAndView add(){
        return new ModelAndView("device/device-template-add");
    }

    @GetMapping(value = "/deviceTemplate/edit/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_UPDATE,format = DataFormatEnum.JSON)
    public ModelAndView edit(@PathVariable Long id){
        ModelAndView modelAndView = new ModelAndView("device/device-template-edit");
        DeviceTemplate deviceTemplate = this.deviceTemplateFeign.getOneById(id);
        Assert.notNull(deviceTemplate,"该记录不存在");
        modelAndView.addObject("deviceTemplate",deviceTemplate);
        return modelAndView;
    }

    @PostMapping(value = "/deviceTemplate/manage/pagingQuery")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE,format = DataFormatEnum.JSON)
    public ResultBean<Page<DeviceTemplate>> pagingQuery(DeviceTemplateDto deviceTemplateDto, Page<DeviceTemplate> page){
        deviceTemplateDto.setPage(page);
        return this.deviceTemplateFeign.pagingQuery(deviceTemplateDto);
    }

    @PostMapping(value = "/deviceTemplate/manage/addDeviceTemplate")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_ADD,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> addDeviceTemplate(DeviceTemplateDto deviceTemplateDto){
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean = this.deviceTemplateFeign.addDeviceTemplate(deviceTemplateDto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_TEMPLATE_MANAGE, SysLogConstant.ADD_EVENT,
                    WebUtils.getIp(),deviceTemplateDto,onLineUser,deviceTemplateDto);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    @PostMapping(value = "/deviceTemplate/manage/updateDeviceTemplate")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_UPDATE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> updateDeviceTemplate(DeviceTemplateDto deviceTemplateDto){
        DeviceTemplate oldObj = this.deviceTemplateFeign.getOneById(deviceTemplateDto.getId());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean = this.deviceTemplateFeign.updateDeviceTemplate(deviceTemplateDto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            DeviceTemplate newObj = this.deviceTemplateFeign.getOneById(deviceTemplateDto.getId());
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_TEMPLATE_MANAGE, SysLogConstant.ALTER_EVENT,
                    WebUtils.getIp(),deviceTemplateDto,onLineUser,oldObj,newObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    @PostMapping(value = "/deviceTemplate/manage/deleteDeviceTemplate")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_DELETE,format = DataFormatEnum.JSON)
    public ResultBean<Boolean> deleteDeviceTemplate(DeviceTemplateDto deviceTemplateDto){
        DeviceTemplate oldObj = this.deviceTemplateFeign.getOneById(deviceTemplateDto.getId());
        Account onLineUser = this.securityServiceInterface.getOnLineUser();
        ResultBean<Boolean> resultBean = this.deviceTemplateFeign.deleteDeviceTemplate(deviceTemplateDto);
        if(null!=resultBean.getData()&&resultBean.getData()){
            /*---------------------------------------------------记录日志--------------------------------------------------------*/
            SysOperationLog sysOperationLog=new SysOperationLog(SysLogMenuConstant.DEVICE_TEMPLATE_MANAGE, SysLogConstant.DELETE_EVENT,
                    WebUtils.getIp(),deviceTemplateDto,onLineUser,oldObj);
            sysOperationLogFeign.info(sysOperationLog);
            /*-------------------------------------------------------------------------------------------------------------------*/
        }
        return resultBean;
    }

    @GetMapping(value = "/deviceTemplate/manage/getOne")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE,format = DataFormatEnum.JSON)
    public DeviceTemplate getOne(DeviceTemplateDto deviceTemplateDto){
        return this.deviceTemplateFeign.getOne(deviceTemplateDto);
    }

    @GetMapping(value = "/deviceTemplate/manage/getOneById/{id}")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE,format = DataFormatEnum.JSON)
    public DeviceTemplate getOne(@PathVariable(value = "id") Long id){
        return this.deviceTemplateFeign.getOneById(id);
    }

    @GetMapping(value = "/deviceTemplate/manage/getListByCondition")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE,format = DataFormatEnum.JSON)
    public List<DeviceTemplate> getListByCondition(DeviceTemplateDto deviceTemplateDto){
        return this.deviceTemplateFeign.getListByCondition(deviceTemplateDto);
    }

    /**
     * 功能描述: 根据设备型号模糊查询
     * @param deviceTemplateDto
     * @return ResultBean<Organization>
     *
     */
    @GetMapping(value = "/deviceTemplate/manage/getListByLikeTypeCode")
    @Security(token = DeviceSecurityToken.DEVICE_TEMPLATE_MANAGE, format = DataFormatEnum.JSON)
    public ResultBean<DeviceTemplate> getListByLikeName(DeviceTemplateDto deviceTemplateDto){
        return new ResultBean<>(ResultEnum.SUCCESS,this.deviceTemplateFeign.getListByLikeTypeCode(deviceTemplateDto));
    }
}
